-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of how to decode input data in transaction #49
Comments
Lets say you have a ERC20 transfer tx. You need to define the tokens method first and then you can use it to decode the input data like this: funcTransfer := w3.MustNewFunc("transfer(address to, uint256 value)", "bool")
input := w3.B("0xa9059cbb00000000000000000000000005972A481E2DdB2d332fDd88A10665D8e9417c20000000000000000000000000000000000000000000000000000000000000ffff")
var (
to common.Address
value *big.Int
)
err := funcTransfer.DecodeArgs(input, &to, &value)
if err != nil {
// ...
}
fmt.Printf("to: %s, amount: %v\n", to, value) The input is just some random transfer calldata. You can also take it from a tx you fetched before. |
thanks sir) |
Can I ask you one more question? How do I take only transfer transactions? Because it can also decode approve transactions |
If you already have a list of transactions you can filter by the function selector (the first 4 byte of the input data). The function selector of the transfer method is here: Otherwise it might be better to look for Transfer events instead. evtTransfer := w3.MustNewEvent("Transfer(address indexed from, address indexed to, uint256 value)")
client := w3.MustDial("https://rpc.ankr.com/eth")
var logs []types.Log
err = client.Call(
eth.Logs(ethereum.FilterQuery{
Topics: [][]common.Hash{{evtTransfer.Topic0}},
FromBlock: big.NewInt(17_000_000),
ToBlock: big.NewInt(17_000_100),
}).Returns(&logs),
)
if err != nil {
// ...
}
for _, log := range logs {
var (
from common.Address
to common.Address
value *big.Int
)
err := evtTransfer.DecodeArgs(&log, &from, &to, &value)
if err != nil {
// ...
}
fmt.Printf("from: %s, to: %s, amount: %v\n", from, to, value)
} You can also filter the logs for a specific token only. |
how to decode input data in transaction?I just found it more convenient to take data from input data than from logs, because to get the receipt you need to send another additional request.
Forgive me in advance if this question seems simple. I am a beginner.
The text was updated successfully, but these errors were encountered: