我有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!");
});但在这个事务挖掘之后,我需要在contract内部进行转移和一些更改。
我可以在没有用户交互的情况下进行智能合约的延迟调用(1块延迟)吗?当然还有我这边的一些汽油。
发布于 2019-03-04 18:19:28
当挖掘事务时,您将获得收据id,这表示事务已执行。因此,您可以在获得收据id之后执行下一个函数。如果您想要在下一个块中执行它,一种方法可能是在dapp中创建一个延迟块的平均时间是14-15秒( Reference),并在14-15秒的延迟后执行另一个函数
发布于 2019-03-05 15:44:54
让我们从头开始,当您将事务发送到区块链时,您将立即收到transactionHash。txHash你可以用它来检查你的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-15秒来随机化您的等待:)
https://stackoverflow.com/questions/54973534
复制相似问题