-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
49 lines (41 loc) · 1.24 KB
/
main.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
package main
import (
"context"
"fmt"
"math/big"
"github.com/defiweb/go-eth/abi"
"github.com/defiweb/go-eth/rpc"
"github.com/defiweb/go-eth/rpc/transport"
"github.com/defiweb/go-eth/types"
)
func main() {
// Create transport.
t, err := transport.NewHTTP(transport.HTTPOptions{URL: "https://ethereum.publicnode.com"})
if err != nil {
panic(err)
}
// Create a JSON-RPC client.
c, err := rpc.NewClient(rpc.WithTransport(t))
if err != nil {
panic(err)
}
transfer := abi.MustParseEvent("Transfer(address indexed src, address indexed dst, uint256 wad)")
// Create filter query.
query := types.NewFilterLogsQuery().
SetAddresses(types.MustAddressFromHex("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")).
SetFromBlock(types.BlockNumberFromUint64Ptr(16492400)).
SetToBlock(types.BlockNumberFromUint64Ptr(16492400)).
SetTopics([]types.Hash{transfer.Topic0()})
// Fetch logs for WETH transfer events.
logs, err := c.GetLogs(context.Background(), query)
if err != nil {
panic(err)
}
// Decode and print events.
for _, log := range logs {
var src, dst types.Address
var wad *big.Int
transfer.MustDecodeValues(log.Topics, log.Data, &src, &dst, &wad)
fmt.Printf("Transfer: %s -> %s: %s\n", src.String(), dst.String(), wad.String())
}
}