Go-Ethereum

Geth 客戶端:事務對像沒有來自欄位

  • August 9, 2022

我正在使用 ethClient 接收一個塊的所有事務。

   client, _ := ethclient.Dial(PROVIDER_URL)

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

然後我想從事務中提取 from 欄位,但它不存在。

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

連結到 ethClient 庫: https ://github.com/ethereum/go-ethereum/blob/a41ea8a97cd0f9db7a87e2dd15b380d4f1fbc311/ethclient/ethclient.go

交易對象連結: https ://github.com/ethereum/go-ethereum/blob/a41ea8a97cd0f9db7a87e2dd15b380d4f1fbc311/core/types/transaction.go#L51

有人知道解決方法嗎?

交易的“來自”屬性來源於散列和簽名,而不是交易本身的一個欄位。您可以通過以下方式得出它:

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)

引用自:https://ethereum.stackexchange.com/questions/133312