/
handle_parts.go
74 lines (66 loc) · 1.94 KB
/
handle_parts.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
package tokensPkg
import (
"context"
"errors"
"fmt"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/names"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/ethereum/go-ethereum"
)
func (opts *TokensOptions) HandleParts() error {
chain := opts.Globals.Chain
testMode := opts.Globals.TestMode
ctx, cancel := context.WithCancel(context.Background())
fetchData := func(modelChan chan types.Modeler[types.RawToken], errorChan chan error) {
for _, address := range opts.Addrs {
addr := base.HexToAddress(address)
currentBn := uint64(0)
currentTs := base.Timestamp(0)
for _, br := range opts.BlockIds {
blockNums, err := br.ResolveBlocks(chain)
if err != nil {
errorChan <- err
if errors.Is(err, ethereum.NotFound) {
continue
}
cancel()
return
}
for _, bn := range blockNums {
if state, err := opts.Conn.GetTokenState(addr, fmt.Sprintf("0x%x", bn)); err != nil {
errorChan <- err
} else {
s := &types.SimpleToken{
Address: state.Address,
BlockNumber: bn,
TotalSupply: state.TotalSupply,
Decimals: uint64(state.Decimals),
}
if opts.Globals.Verbose {
if bn == 0 || bn != currentBn {
currentTs, _ = tslib.FromBnToTs(chain, bn)
}
s.Timestamp = currentTs
currentBn = bn
}
modelChan <- s
}
}
}
}
}
nameTypes := names.Custom | names.Prefund | names.Regular
namesMap, err := names.LoadNamesMap(chain, nameTypes, nil)
if err != nil {
return err
}
extra := map[string]interface{}{
"testMode": testMode,
"namesMap": namesMap,
"parts": opts.Parts,
}
return output.StreamMany(ctx, fetchData, opts.Globals.OutputOptsWithExtra(extra))
}