Comments (4)
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.
from w3.
thanks sir)
from w3.
Can I ask you one more question? How do I take only transfer transactions? Because it can also decode approve transactions
from w3.
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.
from w3.
Related Issues (20)
- balanceOf error HOT 3
- Unable to EstimateGas for transfer func HOT 3
- Semver HOT 2
- abi: cannot use []hexutil.Bytes as type [0]array as argument HOT 1
- Failed to estimate gas HOT 3
- Arrays of tuples aren't supported? HOT 2
- internal/abi: Improve ABI Parser
- debug.CallTrace example HOT 5
- Support Subscription HOT 4
- Adding proxies to client requests HOT 11
- Support Rate Limiting by CUs
- Support the `txpool` namespace
- How does MustNewFunc handle functions that return multiple values? HOT 1
- Would MustNewEvent be used for listening for transaction events from a contract? HOT 1
- Accomodate for Ganache negative transaction cost during debug_traceTransaction HOT 2
- NewFunc fails when signature contains tuples without names HOT 13
- Add support for L2 networks like Arbitrum/Optimism HOT 2
- How to get errors back with w3 api? HOT 1
- How to get Transaction from address? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from w3.