When I use golang call the contract method,how can I confirm this trx status?

1.2k Views Asked by At

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())
1

There are 1 best solutions below

4
On BEST ANSWER

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
}