当我使用golang调用合约方法时,如何确认这个trx状态?

我将令牌铸造到我的帐户并返回交易哈希,但我现在不知道这个哈希状态。就像 js 可以使用回调函数来等待这个 trx 完成


     var promise = await token.mint("my account",1)

     console.log(promise.transactionHash)

高朗


transaction, err := erc721.Mint(trx, common.HexToAddress("my account"), big.NewInt(int64(i)))

        if err != nil {

            fmt.Println(err)

        }

        fmt.Println(transaction.Hash().String())


蛊毒传说
浏览 61回答 1
1回答

德玛西亚99

如评论中所述,您必须收听事件日志(最好针对您的地址进行过滤),并在确认后致电收据。注意:示例只是为了演示必要的步骤。func waitForReceipt(c *ethclient.Client, hash, addr string) (*types.Receipt, error) {&nbsp; &nbsp; query := ethereum.FilterQuery{&nbsp; &nbsp; &nbsp; &nbsp; Addresses: []common.Address{addr},&nbsp; &nbsp; }&nbsp; &nbsp; var ch = make(chan types.Log)&nbsp; &nbsp; sub, err := c.SubscribeFilterLogs(ctx, query, ch) // subscribe to all logs for addr&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; for confirmed := false; !confirmed;&nbsp; { // wait for confirmation on blockchain&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case err := <-sub.Err():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; &nbsp; &nbsp; case vLog := <-ch:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if vLog.TxHash.Hex() == hash {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; confirmed = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return c.TransactionReceipt(ctx, hash) // call for receipt}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go