/
polygonscan.go
61 lines (53 loc) · 1.37 KB
/
polygonscan.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
package eth
import (
"github.com/0xDeSchool/gap/app"
"strconv"
"github.com/0xDeSchool/gap/errx"
"github.com/0xDeSchool/gap/ginx"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
const BaseURL = "https://api.polygonscan.com/api"
type Filter struct {
From uint64
To *uint64
Address *common.Address
Topic0 *common.Hash
}
type dataResult[T any] struct {
Status string `json:"status"`
Message string `json:"message"`
Result T `json:"result"`
}
type PolygonscanClient struct {
apiKey string
client *ginx.RequestClient
}
func NewPolygonscanClient(apiKey string) *PolygonscanClient {
return &PolygonscanClient{
apiKey: apiKey,
client: app.Get[ginx.RequestClient](),
}
}
func (pc *PolygonscanClient) GetLogs(filter *Filter) []types.Log {
if filter == nil {
panic("filter must be not nil")
}
if filter.Address == nil {
panic("filter.Address must be not nil")
}
url := BaseURL + "?module=logs&action=getLogs&address=" + filter.Address.Hex()
if filter.Topic0 != nil {
url = url + "&topic0=" + filter.Topic0.Hex()
}
url = url + "&fromBlock=" + strconv.Itoa(int(filter.From))
if filter.To != nil {
url = url + "&toBlock=" + strconv.Itoa(int(*filter.To))
} else {
url = url + "&toBlock=latest"
}
var data dataResult[[]types.Log]
err := pc.client.GetObj(url, &data)
errx.CheckError(err)
return data.Result
}