1

I mint the token to my account and it return transaction hash ,but I wnat to now this hash status .Like js can use callback function to wait this trx finished

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

golang

transaction, err := erc721.Mint(trx, common.HexToAddress("my account"), big.NewInt(int64(i)))
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(transaction.Hash().String())
Mr.c
  • 23
  • 5
  • You have to listen for all event logs and wait for confirmation on blockchain. Then you can call `client.TransactionReceipt()`. – medasx Jun 13 '22 at 13:22
  • I was trying to use this method at first, but I found that it does not support pending transactions – Mr.c Jun 17 '22 at 03:55

1 Answers1

0

As mentioned in comment, you have to listen for event logs (preferably filtered for your address), and call for receipt after confirmation.

NOTE: Example is just to demonstrate necessary steps.

func waitForReceipt(c *ethclient.Client, hash, addr string) (*types.Receipt, error) {
    query := ethereum.FilterQuery{
        Addresses: []common.Address{addr},
    }
    var ch = make(chan types.Log)
    sub, err := c.SubscribeFilterLogs(ctx, query, ch) // subscribe to all logs for addr
    if err != nil {
        return nil, err
    }

    for confirmed := false; !confirmed;  { // wait for confirmation on blockchain
        select {
        case err := <-sub.Err():
            return nil, err
        case vLog := <-ch:
            if vLog.TxHash.Hex() == hash {
                confirmed = true
            }
        }

    }
    return c.TransactionReceipt(ctx, hash) // call for receipt
}
medasx
  • 634
  • 4
  • 7
  • Only this function?I think it's too cumbersome to write like this。BTW thanks Bro!!! – Mr.c Jun 17 '22 at 03:52
  • It depends, you can easily create callback function and have more control over whole flow. – medasx Jun 18 '22 at 06:03
  • Can I compare the nonce of the current transaction with the nonce of the next transaction? As long as the nonce value is greater than the nonce of the previous trx, you can determine that the last transaction has been completed. – Mr.c Jun 20 '22 at 03:38
  • [Client.PendingNonceAt](https://pkg.go.dev/github.com/getamis/go-ethereum/ethclient#Client.PendingNonceAt), comparing not complete and future transactions has to be done locally by storing value. – medasx Jun 21 '22 at 08:09