智能合约包装器的交易哈希和超时异常

Transaction Hash and Timeout Exception with Smart Contract Wrappers

我们目前正在为 Android 开发一个使用 web3j 及其功能的应用程序。我们使用我们从智能合约创建的 Generated SmartContract Wrapper。

现在,合约创建可能需要一些时间(希望获得关于我们应该在 Ropsten 测试网上将 Gas Prices 设置多高的提示)。我们使用这个方法:

TestContract contract = TestContract.deploy(web3, credentials,
        GAS_PRICE, GAS_LIMIT, eth.toBigInteger(),
        stringToBytes32(conditions), eth.toBigInteger(), Addresses, Roles).send();  // constructor params

现在就是这种方法,可能需要很长时间。在 Android 中,这是一个问题。而且我们似乎只能在完成后才能访问合约地址。 (嗯,这是合乎逻辑的,合同必须先被挖掘)我们想知道,如果我们能以某种方式事先得到交易哈希,所以在发生错误的情况下(该方法似乎在合同 5 分钟后抛出异常,而不是已部署)我们至少有一点可以检查它的进度,并在部署后进一步检查它的地址。

生成的包装器 类 有意将客户端抽象化,使其不了解发送交易的一些复杂性,包括交易哈希、签署交易、编码数据等。如果您想访问它,你必须直接与 TransactionManager 互动。经理公开了一个 sendTransaction 方法 returns EthSendTransaction

public abstract EthSendTransaction sendTransaction(BigInteger gasPrice, BigInteger gasLimit, String toAddress, String data, BigInteger value) throws IOException;

从那里,您可以调用 EthSendTransaction.getTransactionHash() 来获取交易哈希。

如果您唯一关心的是增加超时,事务的 polling/timeout 是通过 TransactionManager:

中使用的 TransactionReceiptProcessor 控制的
  public static final int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
  public static final long DEFAULT_POLLING_FREQUENCY = 15000L;

您可以通过传入您自己的 PollingTransactionReceiptProcessor 实例来覆盖它,而不是使用在 TransactionManager 中创建的默认实例:

RawTransactionManager manager = new RawTransactionManager(web3, credentials, CHAIN_ID, 
        new PollingTransactionReceiptProcessor(web3, SLEEP_IN_MILLIS, MAX_POLL_ATTEMPTS));
TestContract contract = TestContract.deploy(web3, manager,
        GAS_PRICE, GAS_LIMIT, eth.toBigInteger(),
        stringToBytes32(conditions), eth.toBigInteger(), Addresses, Roles).send();

加快交易的挖掘时间将根据区块链上的负载而有所不同。大多数时候,在合理的等待时间(<5 分钟)内,汽油价格为 10-20 GWei。即便如此,有时您会支付 >20 GWei 并且需要 10 分钟以上,或者您将花费 5 GWei 并且它会在 30 秒内被开采。这完全取决于负载。您可以使用添加到区块链的新块的元数据来确定负载和平均 gas 价格(如果您需要在代码中动态执行此操作),或者您可以只查看 https://ethgasstation.info/.