/
contract.go
100 lines (86 loc) · 2.5 KB
/
contract.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package tokenmeta
import (
"context"
"encoding/json"
"fmt"
pbstatedb "github.com/zhongshuwen/historyexp/pb/dfuse/eosio/statedb/v1"
pbtokenmeta "github.com/zhongshuwen/historyexp/pb/dfuse/eosio/tokenmeta/v1"
zsw "github.com/zhongshuwen/zswchain-go"
"go.uber.org/zap"
)
type contractStats struct {
hasAccountsTable bool
hasStatsTable bool
isTokenContract bool
}
func getTokenContractStats(account string, rawABI []byte, isJsonEncoded bool) (*contractStats, error) {
var abi *zsw.ABI
var err error
if isJsonEncoded {
err = json.Unmarshal(rawABI, &abi)
} else {
err = zsw.UnmarshalBinary(rawABI, &abi)
}
if err != nil {
return nil, fmt.Errorf("failed decoding ABI in account %q: %w", account, err)
}
var hasStat, hasAccounts bool
var statStruct, accountStruct string
for _, tbl := range abi.Tables {
if tbl.Name == "stat" {
statStruct = tbl.Type
hasStat = true
} else if tbl.Name == "accounts" {
accountStruct = tbl.Type
hasAccounts = true
}
}
if !hasStat || !hasAccounts {
zlog.Debug("contract does not have either stats or accounts table not a token contract",
zap.String("account", account),
)
return &contractStats{
hasAccountsTable: hasAccounts,
hasStatsTable: hasStat,
}, nil
}
var hasStatFields, hasAccountFields bool
for _, s := range abi.Structs {
if s.Name == accountStruct {
if len(s.Fields) != 0 && s.Fields[0].Type == "asset" {
hasAccountFields = true
}
}
if s.Name == statStruct && len(s.Fields) > 2 {
if s.Fields[0].Name == "supply" &&
s.Fields[1].Name == "max_supply" &&
s.Fields[2].Name == "issuer" {
hasStatFields = true
} else {
zlog.Debug("stat failed for", zap.String("account", account))
}
}
}
return &contractStats{
hasAccountsTable: hasAccounts,
hasStatsTable: hasStat,
isTokenContract: (hasStatFields && hasAccountFields),
}, nil
}
func processContract(ctx context.Context, tokenContract zsw.AccountName, startBlockNum uint32, stateClient pbstatedb.StateClient) (tokens []*pbtokenmeta.Token, balances []*pbtokenmeta.AccountBalance, err error) {
var symcodes []zsw.SymbolCode
symcodes, err = getSymbolFromStateDB(ctx, stateClient, tokenContract, startBlockNum)
if err != nil {
return
}
if len(symcodes) == 0 {
// skip this contract no symbol was found
return
}
tokens, err = getTokensFromStateDB(ctx, stateClient, tokenContract, symcodes, startBlockNum)
if err != nil {
return
}
balances, err = getTokenBalancesFromStateDB(ctx, stateClient, tokenContract, startBlockNum)
return
}