Пример #1
0
def _call_and_transact(
    contract_function: ContractFunction, transaction_params: Optional[Dict] = None
) -> str:
    """ Executes contract_function.{call, transaction}(transaction_params) and returns txhash """
    # First 'call' might raise an exception
    contract_function.call(transaction_params)
    return contract_function.transact(transaction_params)
Пример #2
0
def call_and_transact(
    contract_function: ContractFunction,
    transaction_params: Optional[TxParams] = None,
) -> HexBytes:
    """ Executes contract_function.{call, transaction}(transaction_params) and returns txhash """
    # First 'call' might raise an exception
    contract_function.call(transaction_params)
    return contract_function.transact(transaction_params)
Пример #3
0
 def transact(self, contract_method: ContractFunction) -> TxReceipt:
     """A wrapper around to_be_called.transact() that waits until the transaction succeeds."""
     txhash = contract_method.transact(self.transaction)
     LOG.debug(f"Sending txHash={encode_hex(txhash)}")
     receipt, _ = check_successful_tx(web3=self.web3,
                                      txid=txhash,
                                      timeout=self.wait)
     return receipt
Пример #4
0
def submit_update(network: str, web3_client: Web3,
                  function_call: ContractFunction) -> None:
    tx_params = get_transaction_params(network, web3_client)
    estimated_gas = function_call.estimateGas(tx_params)

    # add 10% margin to the estimated gas
    tx_params["gas"] = int(estimated_gas * 0.1) + estimated_gas

    # execute transaction
    tx_hash = function_call.transact(tx_params)
    logger.info(f"[{network}] Submitted transaction: {Web3.toHex(tx_hash)}")
    wait_for_transaction(network, web3_client, tx_hash)
Пример #5
0
def checked_transact(
    web3: Web3,
    sender_address: Address,
    function_call: ContractFunction,
    task_name: str,
    wait_confirmation_interval: bool = True,
) -> TxReceipt:

    log.info(f"Starting: {task_name}")
    transaction_hash = function_call.transact({"from": sender_address})

    confirmation_msg = ""
    if wait_confirmation_interval:
        confirmation_msg = " and waiting for confirmation"
    click.secho(
        f"\nSending transaction{confirmation_msg}: {task_name}"
        f"\n\tSee {etherscan_url_for_txhash(web3.eth.chain_id, transaction_hash)}"
    )

    timeout = 60 * 10  # 10mins
    transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout, 1.0)
    if wait_confirmation_interval:
        while (
            "blockNumber" not in transaction_receipt
            or web3.eth.block_number
            < transaction_receipt["blockNumber"] + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS
        ):
            transaction_receipt = web3.eth.wait_for_transaction_receipt(
                transaction_hash, timeout, 1.0
            )
            gevent.sleep(1)

    was_successful = transaction_receipt["status"] == 1

    if not was_successful:
        log.error(
            f"Failed: {task_name}\nPlease check that the account "
            f"{to_checksum_address(sender_address)} has sufficient funds.",
            receipt=transaction_receipt,
        )
        sys.exit(1)

    log.info(
        f"Finished: {task_name}",
        successful=was_successful,
        transaction_hash=to_hex(transaction_hash),
    )

    return transaction_receipt
Пример #6
0
def checked_transact(
    web3: Web3, service_address: Address, function_call: ContractFunction, task_name: str
) -> None:
    log.info(f"Starting: {task_name}")
    transaction_hash = function_call.transact({"from": service_address})
    transaction_receipt = web3.eth.waitForTransactionReceipt(transaction_hash)
    was_successful = transaction_receipt["status"] == 1

    if not was_successful:
        log.error(
            f"Failed: {task_name}\nPlease check that the account "
            f"{to_checksum_address(service_address)} has sufficient funds.",
            receipt=transaction_receipt,
        )
        sys.exit(1)

    log.info(
        f"Finished: {task_name}",
        successful=was_successful,
        transaction_hash=to_hex(transaction_hash),
    )