【问题标题】:How can I wait for transaction to confirm using tonweb?如何使用 tonweb 等待交易确认?
【发布时间】:2022-09-22 23:56:09
【问题描述】:

我正在写一个简单的 DAppTON 区块链并使用tonweb 与之交互。

我需要发送一些交易,在链上确认后,在 JS 中执行一些其他的事情。 例子:

await ton.send(\'ton_sendTransaction\', [{
        to: \'some address\',
        value: \'1000\'
    }]
)

// wait for tx to confirm on chain

console.log(\'Done!\')

但我不明白如何使用 tonweb(或其他一些库)来做到这一点

    标签: ton


    【解决方案1】:

    简单的方法是保存最后一个用户的交易并运行一个循环,直到用户的地址有新的交易。

    你可以用 tonweb 做这样的事情:

    // Get user's wallet address from TON wallet browser extension
    const address = (await ton.send('ton_requestAccounts'))[0]
    
    // Get user's last transaction hash using tonweb
    const lastTx = (await tonweb.getTransactions(address, 1))[0]
    const lastTxHash = lastTx.transaction_id.hash
    
    // Send your transaction
    await ton.send('ton_sendTransaction', [{
            to: 'some address',
            value: '1000'
        }]
    )
    
    // Run a loop until user's last tx hash changes
    var txHash = lastTxHash
    while (txHash == lastTxHash) {
        await sleep(1500) // some delay between API calls
        let tx = (await tonweb.getTransactions(address, 1))[0]
        txHash = tx.transaction_id.hash
    }
    
    console.log('Done!')
    

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      相关资源
      最近更新 更多