-
Notifications
You must be signed in to change notification settings - Fork 1
/
providers.go
178 lines (146 loc) · 4.39 KB
/
providers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package tokendirectory
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
var _DefaultMetadataSource = "https://metadata.sequence.app"
// Provider is a source of tokens, organized by chainID and sourceName.
type Provider interface {
GetID() string
GetConfig(ctx context.Context) (chainIDs []uint64, sources []SourceType, err error)
FetchTokenList(ctx context.Context, chainID uint64, source SourceType) (*TokenList, error)
}
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
func NewSequenceProvider(baseURL string, client HTTPClient) (Provider, error) {
if client == nil {
client = http.DefaultClient
}
return sequenceProvider{
id: "sequence-token-directory",
client: client,
baseURL: baseURL,
}, nil
}
type sequenceProvider struct {
id string
client HTTPClient
baseURL string
}
func (p sequenceProvider) GetConfig(ctx context.Context) (chainIDs []uint64, sources []SourceType, err error) {
respBody := struct {
ChainIds []uint64 `json:"chainIds"`
Types []SourceType `json:"sources"`
}{}
resp, err := http.Get(p.baseURL + "/token-directory/")
if err != nil {
return nil, nil, fmt.Errorf("info: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("info: %s", resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
return nil, nil, fmt.Errorf("decode: %w", err)
}
return respBody.ChainIds, respBody.Types, nil
}
func (p sequenceProvider) GetID() string {
return p.id
}
func (p sequenceProvider) FetchTokenList(ctx context.Context, chainID uint64, source SourceType) (*TokenList, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/token-directory/%d/%s.json", p.baseURL, chainID, source), nil)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
res, err := p.client.Do(req.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("fetching: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetching: %s", res.Status)
}
buf, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("reading body: %w", err)
}
var list TokenList
if err := json.Unmarshal(buf, &list); err != nil {
// failed to decode, likely because it doesn't follow standard token-list format,
// and its just returning the ".tokens" part.
list = TokenList{Name: fmt.Sprintf("%d", chainID), ChainID: chainID}
tokens := list.Tokens
err = json.Unmarshal(buf, &tokens)
if err != nil {
return nil, fmt.Errorf("decoding json: %w", err)
}
list.Tokens = tokens
}
return &list, nil
}
func LegacyProvider() Provider {
return urlListProvider{
id: "legacy-token-directory",
client: http.DefaultClient,
sources: LegacySources,
}
}
type urlListProvider struct {
id string
client *http.Client
sources map[uint64]map[SourceType]string
}
func (p urlListProvider) GetID() string {
return p.id
}
func (p urlListProvider) GetConfig(ctx context.Context) ([]uint64, []SourceType, error) {
chainIDs := make([]uint64, 0, len(p.sources))
for chainID := range p.sources {
chainIDs = append(chainIDs, chainID)
}
return chainIDs, []SourceType{SourceTypeERC20, SourceTypeERC721, SourceTypeERC1155}, nil
}
func (p urlListProvider) FetchTokenList(ctx context.Context, chainID uint64, source SourceType) (*TokenList, error) {
m, ok := p.sources[chainID]
if !ok {
return nil, fmt.Errorf("no sources for chain %d", chainID)
}
url, ok := m[source]
if !ok {
return nil, nil
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
res, err := p.client.Do(req.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("fetching: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetching: %s", res.Status)
}
buf, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("reading body: %w", err)
}
var list TokenList
if err := json.Unmarshal(buf, &list); err != nil {
// failed to decode, likely because it doesn't follow standard token-list format,
// and its just returning the ".tokens" part.
list = TokenList{Name: fmt.Sprintf("%d", chainID), ChainID: chainID}
tokens := list.Tokens
err = json.Unmarshal(buf, &tokens)
if err != nil {
return nil, fmt.Errorf("decoding json: %w", err)
}
list.Tokens = tokens
}
return &list, nil
}