Skip to content
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

Closed
vodves-vodves opened this issue May 4, 2023 · 4 comments
Closed

Example of how to decode input data in transaction #49

vodves-vodves opened this issue May 4, 2023 · 4 comments

Comments

@vodves-vodves
Copy link

vodves-vodves commented May 4, 2023

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.

@lmittmann
Copy link
Owner

lmittmann commented May 4, 2023

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.

@vodves-vodves
Copy link
Author

thanks sir)

@vodves-vodves
Copy link
Author

Can I ask you one more question? How do I take only transfer transactions? Because it can also decode approve transactions

@lmittmann
Copy link
Owner

lmittmann commented May 5, 2023

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: funcTransfer.Selector.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants