def sign_tx(self, tx): _web3 = web3util.get_web3() account = _web3.eth.account.from_key(self._private_key) nonce = Web3Wallet._get_nonce(account.address) gas_price = int(_web3.eth.gasPrice / 100) gas_price = max(gas_price, self.MIN_GAS_PRICE) tx['nonce'] = nonce tx['gasPrice'] = gas_price signed_tx = _web3.eth.account.sign_transaction(tx, private_key) return signed_tx.rawTransaction
def _get_nonce(address): # We cannot rely on `web3.eth.getTransactionCount` because when sending multiple # transactions in a row without wait in between the network may not get the chance to # update the transaction count for the account address in time. # So we have to manage this internally per account address. _web3 = web3util.get_web3() if address not in Web3Wallet._last_tx_count: Web3Wallet._last_tx_count[address] = _web3.eth.getTransactionCount( address) else: Web3Wallet._last_tx_count[address] += 1 return Web3Wallet._last_tx_count[address]
def buildAndSendTx(function, from_wallet: Web3Wallet, gaslimit: int = constants.GASLIMIT_DEFAULT, num_wei: int = 0, to_address=None): assert isinstance(from_wallet.address, str) #assert isinstance(from_wallet.private_key, str) _web3 = web3util.get_web3() nonce = _web3.eth.getTransactionCount(from_wallet.address) network = web3util.get_network() gas_price = int(web3util.confFileValue(network, 'GAS_PRICE')) tx_params = { "from": from_wallet.address, "value": num_wei, "nonce": nonce, "gas": gaslimit, "gasPrice": gas_price, } if function is None: #just send ETH, versus smart contract call? assert to_address is not None assert isinstance(to_address, str) tx = tx_params tx["to"] = to_address else: assert to_address is None tx = function.buildTransaction(tx_params) signed_tx = _web3.eth.account.sign_transaction( tx, private_key=from_wallet.private_key) tx_hash = _web3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_receipt = _web3.eth.waitForTransactionReceipt(tx_hash) if tx_receipt['status'] == 0: # did tx fail? raise Exception("The tx failed. tx_receipt: {tx_receipt}") return (tx_hash, tx_receipt)
def __init__(self, contract_address): abi = web3util.abi('DataTokenTemplate') web3 = web3util.get_web3() self.contract = web3.eth.contract(contract_address, abi=abi)
def ETH_base(self) -> int: #returns ETH, in base 18 (i.e. num wei) _web3 = web3util.get_web3() return _web3.eth.getBalance(self._address)
def validate(self): _web3 = web3util.get_web3() key = self.__get_key() account = _web3.eth.account.from_key(key) return account.address == self._address