def cancel_or_replace_transaction(from_wallet, nonce_value, gas_price=None, gas_limit=None): w3 = Web3Provider.get_web3() tx = { "from": from_wallet.address, "to": from_wallet.address, "value": 0 } gas = gas_limit if gas_limit is not None else w3.eth.estimateGas(tx) tx = { "from": from_wallet.address, "to": from_wallet.address, "value": 0, "gas": gas + 1, } wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address) raw_tx = wallet.sign_tx(tx, fixed_nonce=nonce_value, gas_price=gas_price) tx_hash = w3.eth.sendRawTransaction(raw_tx) receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30) return receipt
def deploy(cls, web3, deployer_wallet: Wallet, abi_path: str = '', *args): """ Deploy the DataTokenTemplate and DTFactory contracts to the current network. :param web3: :param abi_path: :param deployer_wallet: Wallet instance :return: smartcontract address of this contract """ if not abi_path: abi_path = ContractHandler.artifacts_path assert abi_path, f'abi_path is required, got {abi_path}' w3 = web3 _json = ContractHandler.read_abi_from_file(cls.CONTRACT_NAME, abi_path) _contract = w3.eth.contract(abi=_json['abi'], bytecode=_json['bytecode']) built_tx = _contract.constructor(*args)\ .buildTransaction({'from': deployer_wallet.address}) if 'gas' not in built_tx: built_tx['gas'] = web3.eth.estimateGas(built_tx) raw_tx = deployer_wallet.sign_tx(built_tx) logging.debug( f'Sending raw tx to deploy contract {cls.CONTRACT_NAME}, signed tx hash: {raw_tx.hex()}' ) tx_hash = web3.eth.sendRawTransaction(raw_tx) return cls.get_tx_receipt(tx_hash, timeout=60).contractAddress
def deploy(cls, web3: Web3, deployer_wallet: Wallet, *args) -> str: """ Deploy the DataTokenTemplate and DTFactory contracts to the current network. :param web3: :param deployer_wallet: Wallet instance :return: smartcontract address of this contract """ _json = get_contract_definition(cls.CONTRACT_NAME) _contract = web3.eth.contract(abi=_json["abi"], bytecode=_json["bytecode"]) built_tx = _contract.constructor(*args).buildTransaction({ "from": deployer_wallet.address, "gasPrice": cls.get_gas_price(web3) }) if "chainId" not in built_tx: built_tx["chainId"] = web3.eth.chain_id if "gas" not in built_tx: built_tx["gas"] = web3.eth.estimate_gas(built_tx) raw_tx = deployer_wallet.sign_tx(built_tx) logging.debug( f"Sending raw tx to deploy contract {cls.CONTRACT_NAME}, signed tx hash: {raw_tx.hex()}" ) tx_hash = web3.eth.send_raw_transaction(raw_tx) return cls.get_tx_receipt(web3, tx_hash, timeout=60).contractAddress
def cancel_or_replace_transaction( from_wallet: Wallet, nonce_value: Optional[Union[str, int]] = None, gas_price: Optional[int] = None, gas_limit: Optional[int] = None, ) -> AttributeDict: web3 = from_wallet.web3 chain_id = web3.eth.chain_id tx = { "from": from_wallet.address, "to": from_wallet.address, "value": 0, "chainId": chain_id, "gasPrice": get_gas_price(web3), } gas = gas_limit if gas_limit is not None else web3.eth.estimate_gas(tx) tx["gas"] = gas + 1 raw_tx = from_wallet.sign_tx(tx, fixed_nonce=nonce_value, gas_price=gas_price) tx_hash = web3.eth.send_raw_transaction(raw_tx) block_confirmations = from_wallet.block_confirmations.value block_number_poll_interval = BLOCK_NUMBER_POLL_INTERVAL[chain_id] transaction_timeout = from_wallet.transaction_timeout.value wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations, block_number_poll_interval, transaction_timeout, ) return web3.eth.get_transaction_receipt(tx_hash)
def send_ether(from_wallet: Wallet, to_address: str, amount: int) -> AttributeDict: if not Web3.isChecksumAddress(to_address): to_address = Web3.toChecksumAddress(to_address) web3 = from_wallet.web3 chain_id = web3.eth.chain_id tx = { "from": from_wallet.address, "to": to_address, "value": amount, "chainId": chain_id, "gasPrice": get_gas_price(web3), } tx["gas"] = web3.eth.estimate_gas(tx) raw_tx = from_wallet.sign_tx(tx) tx_hash = web3.eth.send_raw_transaction(raw_tx) block_confirmations = from_wallet.block_confirmations.value block_number_poll_interval = BLOCK_NUMBER_POLL_INTERVAL[chain_id] transaction_timeout = from_wallet.transaction_timeout.value wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations, block_number_poll_interval, transaction_timeout, ) return web3.eth.get_transaction_receipt(tx_hash)
def send_ether(from_wallet: Wallet, to_address: str, ether_amount: int): w3 = Web3Provider.get_web3() if not w3.isChecksumAddress(to_address): to_address = w3.toChecksumAddress(to_address) tx = { "from": from_wallet.address, "to": to_address, "value": w3.toWei(ether_amount, "ether"), } _ = w3.eth.estimateGas(tx) tx = { "from": from_wallet.address, "to": to_address, "value": w3.toWei(ether_amount, "ether"), "gas": 500000, } wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address) raw_tx = wallet.sign_tx(tx) tx_hash = w3.eth.sendRawTransaction(raw_tx) receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30) return receipt
def send_ether(from_wallet, to_address, ether_amount): w3 = Web3Provider.get_web3() if not w3.isChecksumAddress(to_address): to_address = w3.toChecksumAddress(to_address) tx = { 'from': from_wallet.address, 'to': to_address, 'value': w3.toWei(ether_amount, 'ether') } gas = w3.eth.estimateGas(tx) tx = { 'from': from_wallet.address, 'to': to_address, 'value': w3.toWei(ether_amount, 'ether'), 'gas': 500000 } wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address) raw_tx = wallet.sign_tx(tx) tx_hash = w3.eth.sendRawTransaction(raw_tx) receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30) return receipt