-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read_erc20_event.go
82 lines (68 loc) · 1.8 KB
/
Read_erc20_event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
ERC "goweb3/Contract/ERC20"
"goweb3/config"
"goweb3/connect"
"log"
"math/big"
"strings"
)
/*
* solc --abi -o ./ ERC20.sol
* abigen --abi=ERC20.abi --pkg=ERC --out=EToken.go
*/
type LogTransfer struct {
From common.Address
To common.Address
Tokens *big.Int
}
type LogApproval struct {
TokenOwner common.Address
Spender common.Address
Tokens *big.Int
}
func main() {
client := connect.Wss_proxy_eth()
contractAddress := config.Get_contractaddr()
query := ethereum.FilterQuery{
FromBlock: big.NewInt(5335710),
ToBlock: big.NewInt(5436266),
Addresses: []common.Address{
contractAddress,
},
}
logs, err := client.FilterLogs(context.Background(), query)
if err != nil {
log.Fatal(err)
}
contractAbi, err := abi.JSON(strings.NewReader(string(ERC.ERCMetaData.ABI)))
if err != nil {
log.Fatal(err)
}
logTransferSig := []byte("Transfer(address,address,uint256)")
logTransferSigHash := crypto.Keccak256Hash(logTransferSig)
for _, vLog := range logs {
fmt.Printf("Log Block Number: %d\n", vLog.BlockNumber)
fmt.Printf("Log Index: %d\n", vLog.Index)
switch vLog.Topics[0].Hex() {
case logTransferSigHash.Hex():
fmt.Printf("Log Name: Transfer\n")
var transferEvent LogTransfer
amount, err := contractAbi.Unpack("Transfer", vLog.Data)
if err != nil {
log.Fatal(err)
}
transferEvent.From = common.HexToAddress(vLog.Topics[1].Hex())
transferEvent.To = common.HexToAddress(vLog.Topics[2].Hex())
fmt.Printf("From: %s\n", transferEvent.From.Hex())
fmt.Printf("To: %s\n", transferEvent.To.Hex())
fmt.Printf("Amount: %s\n", amount[0])
}
}
}