赞
踩
- package main
-
- import (
- "context"
- "github.com/ethereum/go-ethereum"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/ethclient"
- "github.com/wonderivan/logger"
- "math/big"
- "XXXX/abi" //自己使用abigen生成的那个go文件夹地址,我的pkg设置了为abi,所以这里就叫abi
- )
- // abigen --abi erc20.json -pkg abi -type erc20 --out erc20.go
- func main() {
- //部署后在remix上面可以看到刚刚部署的合约地址
- contractAddress := common.HexToAddress("0x12345...")
- //rpcURL可以参考 https://docs.bnbchain.org/docs/rpc
- client, err := ethclient.Dial("https://data-seed-prebsc-1-s2.binance.org:8545/") //如果实在拿不到可注册https://getblock.io/nodes/bsc,有免费调用次数
- if err != nil {
- panic(err)
- }
-
- query := ethereum.FilterQuery{
- FromBlock: big.NewInt(36660000), //生产环境中,查找当前区块高度,查询后修改区块记录,下一次就从后一个有记录的区块数开始
- ToBlock: big.NewInt(36660020),
- Addresses: []common.Address{
- contractAddress,
- },
- }
- logs, err := client.FilterLogs(context.Background(), query)
- if err != nil {
- panic(err)
- }
- erc20, _ := abi.NewAbi(contractAddress, client)
- for _, log := range logs {
- if len(log.Topics) == 0 {
- continue
- }
- switch log.Topics[0].Hex() {
- case TransferEvent():
- logger.Info("TransferEvent:%v\n", log.Data)
- data, err := erc20.ParseTransfer(log)
- logger.Debug(data.From.Hex(), data.To.Hex(), data.Value.String(), err)
- case MintEvent():
- logger.Info("MintEvent:%v\n", log)
- data, err := erc20.ParseMint(log)
- logger.Debug(data.To.Hex(), data.Value.String(), err)
- default:
- logger.Info("default:%v\n", log)
- }
- }
- }
-
- func TransferEvent() string {
- event := crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)")).Hex()
- return event
- }
-
- func MintEvent() string {
- event := crypto.Keccak256Hash([]byte("Mint(address,uint256)")).Hex()
- return event
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。