Giter Site home page Giter Site logo

Comments (4)

lmittmann avatar lmittmann commented on May 21, 2024

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.

vodves-vodves avatar vodves-vodves commented on May 21, 2024

thanks sir)

from w3.

vodves-vodves avatar vodves-vodves commented on May 21, 2024

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

from w3.

lmittmann avatar lmittmann commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.