【问题标题】:Transaction Hash and Timeout Exception with Smart Contract Wrappers智能合约包装器的事务哈希和超时异常
【发布时间】:2018-09-12 23:16:37
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android blockchain ethereum smartcontracts


    【解决方案1】:

    生成的包装类有意将客户端抽象出来,使其不知道发送交易的一些复杂性,包括交易哈希、签署交易、编码数据等。如果你想访问它,你必须进行交互直接使用TransactionManager。管理器公开了一个sendTransaction 方法,该方法返回EthSendTransaction

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

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

    如果您唯一关心的是增加超时,事务的轮询/超时是通过TransactionReceiptProcessor 内部使用的TransactionManager 控制的:

      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();
    

    加快交易的挖掘时间取决于区块链上的负载。大多数情况下,您会在合理的等待时间(https://ethgasstation.info/。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2022-06-23
      • 1970-01-01
      相关资源
      最近更新 更多