-
Notifications
You must be signed in to change notification settings - Fork 249
/
uniswap_tokenstore.go
54 lines (43 loc) · 1.29 KB
/
uniswap_tokenstore.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
package token
import (
"io/ioutil"
"net/http"
"time"
)
type uniswapStore struct {
client *http.Client
tokensFetched bool
}
const uniswapTokensURL = "https://gateway.ipfs.io/ipns/tokens.uniswap.org" // nolint:gosec
const tokenListSchemaURL = "https://uniswap.org/tokenlist.schema.json" // nolint:gosec
func newUniswapStore() *uniswapStore {
return &uniswapStore{client: &http.Client{Timeout: time.Minute}, tokensFetched: false}
}
func (ts *uniswapStore) doQuery(url string) (*http.Response, error) {
return ts.client.Get(url)
}
func (ts *uniswapStore) areTokensFetched() bool {
return ts.tokensFetched
}
func (ts *uniswapStore) GetTokens() ([]*Token, error) {
resp, err := ts.doQuery(uniswapTokensURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// In an unlikely case when token list is fetched fine,
// but fails to validate against the schema, we don't want
// to refetch the tokens on every GetTokens call as it will
// still fail but will be wasting CPU cycles until restart,
// so lets keep tokensFetched before validate() call
ts.tokensFetched = true
_, err = validateDocument(string(body), tokenListSchemaURL)
if err != nil {
return nil, err
}
return bytesToTokens(body)
}