0

I am using the ethClient to receive all transactions of a block.

    client, _ := ethclient.Dial(PROVIDER_URL)

    blockNumber := big.NewInt(15000000)
    block, err := client.BlockByNumber(context.Background(), blockNumber)

Then I want to extract the from field from a transaction but it doesn't exist.

    tx := block.Transactions()[0]
    tx.To().Hash() // => exists
    tx.From // => doesn't exist

Link to ethClient lib: https://github.com/ethereum/go-ethereum/blob/a41ea8a97cd0f9db7a87e2dd15b380d4f1fbc311/ethclient/ethclient.go

Link to transaction object: https://github.com/ethereum/go-ethereum/blob/a41ea8a97cd0f9db7a87e2dd15b380d4f1fbc311/core/types/transaction.go#L51

Does anybody know a work around?

2 Answers 2

1

The "from" property of transactions is derived from the hash and signature, rather than being a field on the transaction itself. You can derive it with:

import "github.com/ethereum/go-ethereum/core/types"

// ...

var signer types.Signer
switch {
case tx.Type() == types.AccessListTxType:
  signer = types.NewEIP2930Signer(tx.ChainId())
case tx.Type() == types.DynamicFeeTxType:
  signer = types.NewLondonSigner(tx.ChainId())
default:
  signer = types.NewEIP155Signer(tx.ChainId())
}
sender, _ := types.Sender(signer, tx)
2
  • Is it save to use always use types.LatestSignerForChainID(netID) instead as a signer? Commented Aug 10, 2022 at 7:10
  • It kind of depends on your use case. If you're trying to validate blocks, you need to make sure that the transactions included in that block were valid for that block, and that there aren't any transactions of a type that wasn't accepted at that point. If you're just trying to derive the sender of a transaction and don't care about whether the transaction is valid for a given block, LatestSignerForChainID should be fine.
    – AusIV
    Commented Aug 10, 2022 at 15:16
0

you can use the ethClient package to get the sender's address.

var client *ethclient.Client

// Initialize the client
client, _ = ethclient.Dial("rpc URL")


sender, err := client.TransactionSender(context.Background(), tx, receipt.BlockHash, receipt.TransactionIndex)

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.