2

I am following

https://github.com/miguelmota/ethereum-development-with-go-book/tree/master/en/transaction-query

Complete code

package main

import (
    "context"
    "fmt"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
    "log"
    "math/big"

    "github.com/ethereum/go-ethereum/common"
)

func transactions() {
    client, err := ethclient.Dial("https://cloudflare-eth.com")
    if err != nil {
        log.Fatal(err)
    }
    blockNumber := big.NewInt(5671744)
    block, err := client.BlockByNumber(context.Background(), blockNumber)
    if err != nil {
        log.Fatal(err)
    }

    for _, tx := range block.Transactions() {
        fmt.Println(tx.Hash().Hex())        // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
        fmt.Println(tx.Value().String())    // 10000000000000000
        fmt.Println(tx.Gas())               // 105000
        fmt.Println(tx.GasPrice().Uint64()) // 102000000000
        fmt.Println(tx.Nonce())             // 110644
        fmt.Println(tx.Data())              // []
        fmt.Println(tx.To().Hex())          // 0x55fE59D8Ad77035154dDd0AD0388D09Dd4047A8e

        chainID, err := client.NetworkID(context.Background())
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(chainID)

        if msg, err := tx.AsMessage(types.NewEIP155Signer(chainID)); err == nil {
            fmt.Println(msg.From().Hex()) // 0x0fD081e3Bb178dc45c0cb23202069ddA57064258
        }

        receipt, err := client.TransactionReceipt(context.Background(), tx.Hash())
        if err != nil {
            log.Fatal(err)
        }

        fmt.Println(receipt.Status) // 1
    }

    blockHash := common.HexToHash("0x9e8751ebb5069389b855bba72d94902cc385042661498a415979b7b6ee9ba4b9")
    count, err := client.TransactionCount(context.Background(), blockHash)
    if err != nil {
        log.Fatal(err)
    }

    for idx := uint(0); idx < count; idx++ {
        tx, err := client.TransactionInBlock(context.Background(), blockHash, idx)
        if err != nil {
            log.Fatal(err)
        }

        fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
    }

    txHash := common.HexToHash("0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2")
    tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(tx.Hash().Hex()) // 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2
    fmt.Println(isPending)       // false
}

I get this error:

./transactions.go:40:21: tx.AsMessage undefined (type *types.Transaction has no field or method AsMessage) failed to build, error: exit status 1

Any advice how to get around this?

2 Answers 2

2

UPDATE: The good package is: import "github.com/ethereum/go-ethereum/types"

and the supported transaction type is: NewLondonSigner()

Overall, the code will be:

import "github.com/ethereum/go-ethereum/types"
// your code here
if from, err := types.Sender(types.NewLondonSigner(chainID), tx); err == nil {
    fmt.Println(from.Hex()) // 0x0fD081e3Bb178dc45c0cb23202069ddA57064258
}
1
  • It is probably better for most cases to use types.LatestSignerForChainID instead of types.NewLondonSigner. Also, there is no "github.com/ethereum/go-ethereum/types" only "github.com/ethereum/go-ethereum/core/types" (maybe got changed since u answered the question) which works perfectly 2 hours ago
1

AsMessage method has been removed from types.Transaction in newer versions of the go-ethereum, use the types.Sender instead.

first be sure to import the good package

import "github.com/ethereum/go-ethereum/crypto"

then replace this line

if msg, err := tx.AsMessage(types.NewEIP155Signer(chainID)); err == nil {
    fmt.Println(msg.From().Hex()) // 0x0fD081e3Bb178dc45c0cb23202069ddA57064258

}

with

if from, err := types.Sender(types.NewEIP155Signer(chainID), tx); err == nil {
    fmt.Println(from.Hex()) // 0x0fD081e3Bb178dc45c0cb23202069ddA57064258
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.