当前交易后如何在下一个区块中启动转账?

问题描述 投票:0回答:2

我有Dapp,用户使用web3进行智能合约的应付账款。

contract.methods.bet(number).send({
  from: accounts[0],
  gas: 3000000,
  value: web3.utils.toWei(bet.toString(), 'ether')
}, (err, result) => {})

我在Dapp听智能合约中的事件,所以我知道交易开采时间:

contract.events.blockNumberEvent((error, event) => {
  console.log("transaction mined!");
});

但在这笔交易开采后,我需要在合同内部进行转移和一些变更。

我可以在没有用户互动的情况下延迟拨打智能合约(1个区块延迟)吗?肯定有一些来自我身边的气体。

ethereum solidity smartcontracts web3
2个回答
0
投票

当事务被挖掘时,您将获得收据ID,这表示事务已执行。因此,您可以在获得收据ID后执行下一个功能。如果你想在下一个块中执行它,可能就是在dapp中你创建一个延迟平均时间的块是14-15秒(Reference)和延迟14-15秒之后执行另一个函数


0
投票

当您将交易发送到区块链时,让我们从头开始,您将立即收到transactionHashtxHash你可以用它来检查你的tx何时被接受(包含在一个区块中)或被拒绝,

web3 official doc可以看出,您可以使用多种替代方案

其中一个可能是:

contract.methods.bet(number).send({
  from: accounts[0],
  gas: 3000000,
  value: web3.utils.toWei(bet.toString(), 'ether')
}, (error, transactionHash) => {
 if(error) // Handle the error
 else {
   txReceipt = null;
   while(true) {
      let txReceipt = web3.eth.getTransactionReceipt(txReceiptId);
      if (txReceipt != null && typeof txReceipt !== 'undefined') {
        break;
      }
    }
   if (txReceipt.status == "0x1") // Actions to take when tx success
   else // Actions to take when tx fails
 }
})

另一个较短的选择可能是:

contract.methods.bet(number).send({
  from: accounts[0],
  gas: 3000000,
  value: web3.utils.toWei(bet.toString(), 'ether')
}).on('receipt', (txReceipt) => {
   if (txReceipt.status == "0x1") // Actions to take when tx success
   else // Actions to take when tx fails
})

因此,没有必要随机使用14-15s等待你的等待:)

© www.soinside.com 2019 - 2024. All rights reserved.