Repository navigation
Expand file tree
/
Copy pathlog_fetcher.go
More file actions
84 lines (70 loc) · 2.25 KB
/
Copy pathlog_fetcher.go
File metadata and controls
84 lines (70 loc) · 2.25 KB
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
83
84
package chainalysis
import (
"context"
"fmt"
"math/big"
"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/go-sequence/lib/prototyp"
)
func FetchAndUpdateSanctionedAddresses(ctx context.Context, provider *ethrpc.Provider, source IndexSource, startingBlock uint64, endingBlock uint64) error {
preFetchedEvents, err := source.FetchSanctionedAddressEvents()
if err != nil {
return err
}
if len(preFetchedEvents) > 0 {
lastFetchedEvent := preFetchedEvents[len(preFetchedEvents)-1]
if lastFetchedEvent.BlockNum >= startingBlock {
startingBlock = lastFetchedEvent.BlockNum + 1
} else if lastFetchedEvent.BlockNum >= endingBlock {
return nil
}
}
newEvents, err := fetchSanctionedAddressEvents(ctx, provider, startingBlock, endingBlock)
if err != nil {
return err
}
newEvents = append(preFetchedEvents, newEvents...)
err = source.SetIndex(newEvents)
return err
}
func fetchSanctionedAddressEvents(ctx context.Context, provider *ethrpc.Provider, startingBlock uint64, endingBlock uint64) ([]SanctionedAddressEvent, error) {
result := []SanctionedAddressEvent{}
topicHash, _, err := ethcoder.EventTopicHash("SanctionedAddressesAdded(address[])")
if err != nil {
return nil, err
}
chainID, err := provider.ChainID(ctx)
if err != nil {
return nil, err
}
if chainID.Cmp(big.NewInt(OracleChainID)) != 0 {
return nil, fmt.Errorf("invalid chainID, expecting %d, got %d", OracleChainID, chainID)
}
contract := common.HexToAddress(OracleAddress)
logs, _, err := fetchEthereumLogs(ctx, provider, 10000, 8000, startingBlock, endingBlock, &contract, topicHash.String())
if err != nil {
return nil, err
}
for _, log := range logs {
logData, err := ethcoder.AbiDecoderWithReturnedValues([]string{"address[]"}, log.Data)
if err != nil {
return nil, err
}
addrs, ok := logData[0].([]common.Address)
if !ok {
return nil, err
}
event := SanctionedAddressEvent{
BlockNum: log.BlockNumber,
BlockHash: log.BlockHash.Hex(),
Addrs: []prototyp.Hash{},
}
for _, address := range addrs {
event.Addrs = append(event.Addrs, prototyp.HashFromString(address.Hex()))
}
result = append(result, event)
}
return result, nil
}