2

How I can get transaction's field "From"?

client, err := ethclient.Dial("https://mainnet.infura.io")
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

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

This code don't show it.

For instance, for transaction https://etherscan.io/tx/0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2 it shows nothing.

For transaction https://etherscan.io/tx/0x36368eb4665367100bcb46427e8ac39b7873abfca2015116c478f84642a8812d it shows 0x0000000000000000000000000000000000000000

  • what if we use types.EIP155Signer instead of types.HomesteadSigner ? – Ha ĐANG Aug 27 '18 at 15:28
  • @HaĐANG didn't help. Moreover, I trying to detect signer by transaction's block number: signer := types.MakeSigner(&s.ChainConfig, big.NewInt(blockNumber)) and it it don't work too. – user7851819 Aug 28 '18 at 7:42
  • Can you post the full source code then I can run on my local? Thanks! – Ha ĐANG Aug 28 '18 at 8:16
  • @HaĐANG github.com/miguelmota/ethereum-development-with-go-book/blob/… What I have as result for first transaction: 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2 10000000000000000 105000 102000000000 110644 [] 0x55fE59D8Ad77035154dDd0AD0388D09Dd4047A8e 1 What I expect: 0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2 10000000000000000 105000 102000000000 110644 [] 0x55fE59D8Ad77035154dDd0AD0388D09Dd4047A8e 0x0fd081e3bb178dc45c0cb23202069dda57064258 <- field "from" 1 – user7851819 Aug 28 '18 at 9:31
  • okay, one more thing I want to confirm that you dont put the api key in the code for security purpose right? client, err := ethclient.Dial("https://mainnet.infura.io"). Because infura provide us an API key to access the end-point and i'm sure that you put it into the code when you running your program. – Ha ĐANG Aug 28 '18 at 9:35
1

There's one mistake in your code then you cannot printout the from

just replace this code

if msg, err := tx.AsMessage(types.HomesteadSigner{}); err != nil {

to

if msg, err := tx.AsMessage(types.HomesteadSigner{}); err == nil {

And we may need to use tx.AsMessage(types.NewEIP155Signer(big.NewInt(1))) for the mainnet instead of using tx.AsMessage(types.HomesteadSigner{})

Hope this help!

  • oh, yes, thanks. It helped for first two transactions. But for third transaction 0x36368eb4665367100bcb46427e8ac39b7873abfca2015116c478f84642a8812d from block 5671744 still no "from" field – user7851819 Aug 28 '18 at 10:11
  • yep, it report error invalid transaction v, r, s values; it seems that we need to make correct Signer for each tnx – Ha ĐANG Aug 28 '18 at 10:14
  • if I use tx.AsMessage(types.NewEIP155Signer(big.NewInt(1))) then we can get correct From for tnx 3 – Ha ĐANG Aug 28 '18 at 10:18
  • Great, it really helped! Thank you! P.S. By the way I try to make a signer with chainConfig := params.ChainConfig{ChainID: big.NewInt(1)} signer := types.MakeSigner(&chainConfig, blockNumber) and then tx.AsMessage(signer) but it didn't help. May be it can be more correct way. – user7851819 Aug 28 '18 at 10:31
  • 1
    you need to add EIP155Block into the params.ChainConfig{} also to make it work properly – Ha ĐANG Aug 29 '18 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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