status-react/test/appium/support/api/web3_api.py

141 lines
9.5 KiB
Python
Raw Normal View History

from eth_utils import to_checksum_address, is_address
from web3.auto.infura.goerli import w3
2022-06-22 14:09:04 +00:00
from ens import ENS
token_data = {"STT": [{
"abi": '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_amount","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"creationBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newController","type":"address"}],"name":"changeController","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_blockNumber","type":"uint256"}],"name":"balanceOfAt","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"version","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_cloneTokenName","type":"string"},{"name":"_cloneDecimalUnits","type":"uint8"},{"name":"_cloneTokenSymbol","type":"string"},{"name":"_snapshotBlock","type":"uint256"},{"name":"_transfersEnabled","type":"bool"}],"name":"createCloneToken","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"parentToken","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_owner","type":"address"},{"name":"_amount","type":"uint256"}],"name":"generateTokens","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_blockNumber","type":"uint256"}],"name":"totalSupplyAt","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"transfersEnabled","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"parentSnapShotBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_owner","type":"address"},{"name":"_amount","type":"uint256"}],"name":"destroyTokens","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_token","type":"address"}],"name":"claimTokens","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenFactory","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_transfersEnabled","type":"bool"}],"name":"enableTransfers","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":
"address": "0x3D6AFAA395C31FCd391fE3D562E75fe9E8ec7E6a"}]}
ACCOUNT_PRIVATE_KEY = '0x5507f8c5c12707770c12fd0fae5d012b947d61f43b9203ae67916e703fd12ad7'
2022-06-22 14:09:04 +00:00
ACCOUNT_ADDRESS = '0xE2363E6e91d1a29d82C2c695fa8fa2e3Fa5d55eA'
class Account(object):
def __init__(self, account_private_key):
self.pk = account_private_key
@property
def account_address(self):
return w3.eth.account.from_key(self.pk).address
@property
def nonce(self):
return w3.eth.getTransactionCount(self.account_address)
@property
def balance(self):
return w3.eth.getBalance(self.account_address)
def send_eth(self, to_address, eth_value, gas_price_increment=0):
signed_txn = w3.eth.account.sign_transaction(dict(
nonce=self.nonce,
gasPrice=w3.eth.gasPrice + gas_price_increment * 1000000000,
gas=21000,
to=to_address,
value=int(eth_value * 10 ** 18),
data=b'',
),
self.pk,
)
w3.eth.sendRawTransaction(signed_txn.rawTransaction)
return w3.toHex(w3.sha3(signed_txn.rawTransaction))
class ContractInteractions(object):
def __init__(self, contract_address, abi):
self.contract = w3.eth.contract(address=contract_address, abi=abi)
@property
def decimals(self):
return self.contract.functions.decimals().call()
def balance_of(self, account_address):
return self.contract.functions.balanceOf(account_address).call()
def nonce(self, account_address):
return w3.eth.getTransactionCount(account_address)
def transfer_token_to(self, from_address, to_address, number_of_tokens, nonce, gas_price_increment=0):
gas_price = w3.eth.gasPrice + gas_price_increment * 1000000000
token_value = int(number_of_tokens * 10 ** self.decimals)
return self.contract.functions.transfer(to_address, token_value, ).buildTransaction({
'from': from_address,
'gasPrice': gas_price,
'gas': 600000,
'nonce': nonce}
)
def balance_of_address(address):
if not is_address(address):
return ("Invalid address provided")
else:
return w3.eth.getBalance(to_checksum_address(address))
2022-06-22 14:09:04 +00:00
def transaction_status(hash):
return w3.eth.getTransaction(hash)
2022-06-22 14:09:04 +00:00
def get_tx_param_by_hash(hash: str, param: str):
return getattr(w3.eth.getTransaction(hash), param)
def to_checksumed_address(address):
return to_checksum_address(address)
def current_gas_price():
return str(w3.eth.gasPrice / 1000000000)
def sign_transaction(tx_data, pk):
return w3.eth.account.signTransaction(tx_data, pk)
def broadcast_signed_tx(signed_txn):
w3.eth.sendRawTransaction(signed_txn.rawTransaction)
return w3.toHex(w3.sha3(signed_txn.rawTransaction))
2022-06-22 14:09:04 +00:00
def get_address_from_ens(ens_name, is_stateofus=True):
# Not working on Goerli
2022-06-22 14:09:04 +00:00
ns = ENS.fromWeb3(w3)
ENS.fromWeb3()
2022-06-22 14:09:04 +00:00
eth_address = ns.address('%s.stateofus.eth' % ens_name) if is_stateofus is True else ns.address(ens_name)
return eth_address if eth_address else None
account = Account(ACCOUNT_PRIVATE_KEY)
account_address = account.account_address
def donate_testnet_eth(address=str(), amount=float(), inscrease_default_gas_price=int()):
"""
address: address where to send ETH to
amount: amount in Ether form
inscrease_default_gas_price: specify GWEI value (int) if you want to speed up transaction pick up
"""
return account.send_eth(address, amount, inscrease_default_gas_price)
def donate_testnet_token(token_name=str(), address=str(), amount=float(), inscrease_default_gas_price=int()):
"""
token_name: token 'name' value you want to send taken from token_data
address: address where to send ETH to
amount: amount in Ether form
inscrease_default_gas_price: specify GWEI value (int) if you want to speed up transaction pick up
"""
token_contract = ContractInteractions(token_data[token_name][0]['address'], token_data[token_name][0]['abi'])
to_address_data = token_contract.transfer_token_to(
from_address=account_address,
to_address=address,
number_of_tokens=amount,
nonce=token_contract.nonce(account_address),
gas_price_increment=inscrease_default_gas_price)
signed_tx = sign_transaction(tx_data=to_address_data, pk=account.pk)
return broadcast_signed_tx(signed_tx)