-
Notifications
You must be signed in to change notification settings - Fork 23
/
cache.go
289 lines (246 loc) · 10.3 KB
/
cache.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package tokendata
import (
"context"
"fmt"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/synapsecns/sanguine/core/retry"
"github.com/synapsecns/sanguine/services/explorer/consumer/fetcher"
"golang.org/x/sync/errgroup"
)
// Service provides data about tokens using either a cache or bridgeconfig
// cache keys sare always ${KEY_NAME}_CHAIN_ID_ADDRESS so unless a token changes tokenID's
// (not the other way around), data is guaranteed to be accurate.
type Service interface {
// GetTokenData attempts to get token data from the cache otherwise its fetched from the bridge config
GetTokenData(ctx context.Context, chainID uint32, token common.Address) (ImmutableTokenData, error)
// GetPoolTokenData attempts to get pool token data from the cache otherwise its fetched from the erc20 interface
GetPoolTokenData(ctx context.Context, chainID uint32, token common.Address, swapService fetcher.SwapService) (ImmutableTokenData, error)
// GetCCTPTokenData attempts to get the token symbol from the cctp contract
GetCCTPTokenData(ctx context.Context, chainID uint32, token common.Address, cctpService fetcher.CCTPService) (ImmutableTokenData, error)
}
// TokenData is the data for a token -- specifically used for searching the token data map for Synapse X tokens not in the contract config.
type TokenData struct {
TokenID string
Decimals uint8
TokenAddress string
}
const cacheSize = 3000
// maxAttemptTime is how many times we will attempt to get the token data.
const maxAttemptTime = time.Minute * 5
const maxAttempt = 10
type tokenDataServiceImpl struct {
// tokenCache is the tokenCache of the tokenDataServices
tokenCache *lru.TwoQueueCache[string, ImmutableTokenData]
// fetcher is the fetcher used to fetch data from the bridge config contract
service fetcher.Service
// tokenSymbolToIDs is a mapping of token symbols to token IDs.
tokenSymbolToIDs map[string]string
}
// NewTokenDataService creates a new token data service.
func NewTokenDataService(service fetcher.Service, tokenSymbolToIDs map[string]string) (Service, error) {
cache, err := lru.New2Q[string, ImmutableTokenData](cacheSize)
if err != nil {
return nil, fmt.Errorf("could not create token data cache: %w", err)
}
return &tokenDataServiceImpl{
tokenCache: cache,
service: service,
tokenSymbolToIDs: tokenSymbolToIDs,
}, nil
}
// GetTokenData attempts to get token data from the cache otherwise it is fetched from the bridge config.
func (t *tokenDataServiceImpl) GetTokenData(ctx context.Context, chainID uint32, token common.Address) (ImmutableTokenData, error) {
// Fix for tokens not in the config or cache (typically SynapseX)
mapKey := fmt.Sprintf("%d_%s", chainID, token.String())
if tokenData, ok := tokenDataMap[mapKey]; ok {
immutableData := immutableTokenImpl{
tokenID: tokenData.TokenID,
decimals: tokenData.Decimals,
tokenAddress: tokenData.TokenAddress,
}
t.tokenCache.Add(mapKey, immutableData)
return immutableData, nil
}
key := fmt.Sprintf("token_%d_%s", chainID, token.Hex())
if data, ok := t.tokenCache.Get(key); ok {
return data, nil
}
tokenData, err := t.retrieveTokenData(ctx, chainID, token)
if err != nil {
return nil, fmt.Errorf("could not get token data: %w", err)
}
t.tokenCache.Add(key, tokenData)
return tokenData, nil
}
// GetPoolTokenData attempts to get pool token data from the cache otherwise it is fetched from the erc20 interface for that token.
func (t *tokenDataServiceImpl) GetPoolTokenData(ctx context.Context, chainID uint32, token common.Address, swapService fetcher.SwapService) (ImmutableTokenData, error) {
// Fix for tokens not in the config or cache (typically SynapseX)
mapKey := fmt.Sprintf("%d_%s", chainID, token.String())
if tokenData, ok := tokenDataMap[mapKey]; ok {
immutableData := immutableTokenImpl{
tokenID: tokenData.TokenID,
decimals: tokenData.Decimals,
tokenAddress: tokenData.TokenAddress,
}
t.tokenCache.Add(mapKey, immutableData)
return immutableData, nil
}
key := fmt.Sprintf("token_%d_%s", chainID, token.Hex())
if data, ok := t.tokenCache.Get(key); ok {
return data, nil
}
tokenData, err := t.retrievePoolTokenData(ctx, token, swapService)
if err != nil {
return nil, fmt.Errorf("could not get token data: %w", err)
}
t.tokenCache.Add(key, tokenData)
return tokenData, nil
}
// GetCCTPTokenData attempts to get cctp token data from the cache otherwise it is fetched using the cctp ref.
func (t *tokenDataServiceImpl) GetCCTPTokenData(ctx context.Context, chainID uint32, token common.Address, cctpService fetcher.CCTPService) (ImmutableTokenData, error) {
// Fix for tokens not in the config or cache (typically SynapseX)
mapKey := fmt.Sprintf("%d_%s", chainID, token.String())
if tokenData, ok := tokenDataMap[mapKey]; ok {
immutableData := immutableTokenImpl{
tokenID: tokenData.TokenID,
decimals: tokenData.Decimals,
tokenAddress: tokenData.TokenAddress,
}
t.tokenCache.Add(mapKey, immutableData)
return immutableData, nil
}
key := fmt.Sprintf("token_%d_%s", chainID, token.Hex())
if data, ok := t.tokenCache.Get(key); ok {
return data, nil
}
tokenData, err := t.retrieveCCTPTokenData(ctx, token, cctpService)
if err != nil {
return nil, fmt.Errorf("could not get token data: %w", err)
}
t.tokenCache.Add(key, tokenData)
return tokenData, nil
}
// retrieveTokenData retrieves the token data from the bridge config contract
// this will retry for maxAttemptTime.
func (t *tokenDataServiceImpl) retrieveTokenData(parentCtx context.Context, chainID uint32, token common.Address) (ImmutableTokenData, error) {
res := immutableTokenImpl{}
ctx, cancel := context.WithTimeout(parentCtx, maxAttemptTime)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
//nolint: wrapcheck
return retry.WithBackoff(ctx, func(ctx context.Context) error {
tokenData, err := t.service.GetToken(ctx, chainID, token)
if err != nil {
return fmt.Errorf("could not get token data: %w", err)
}
res.decimals = tokenData.TokenDecimals
return nil
}, retry.WithMaxAttemptTime(maxAttemptTime), retry.WithMaxAttempts(maxAttempt))
})
g.Go(func() error {
//nolint: wrapcheck
return retry.WithBackoff(ctx, func(ctx context.Context) error {
nullableTokenID, err := t.service.GetTokenID(ctx, big.NewInt(int64(chainID)), token)
if err != nil {
return fmt.Errorf("could not get token data: %w", err)
}
res.tokenID = *nullableTokenID
return nil
}, retry.WithMaxAttemptTime(maxAttemptTime), retry.WithMaxAttempts(maxAttempt))
})
err := g.Wait()
if err != nil {
return nil, fmt.Errorf("could not get token data: %w", err)
}
res.tokenAddress = token.String()
return res, nil
}
// retrieveTokenData retrieves the token data from the bridge config contract
// this will retry for maxAttemptTime.
//
// nolint:cyclop
func (t *tokenDataServiceImpl) retrievePoolTokenData(parentCtx context.Context, token common.Address, swapService fetcher.SwapService) (ImmutableTokenData, error) {
res := immutableTokenImpl{}
ctx, cancel := context.WithTimeout(parentCtx, maxAttemptTime)
defer cancel()
err := retry.WithBackoff(ctx, func(ctx context.Context) error {
symbol, decimals, err := swapService.GetTokenMetaData(ctx, token)
if err != nil {
return fmt.Errorf("could not get token data: %w", err)
}
if strings.Contains(strings.ToLower(*symbol), "dai") {
*symbol = "dai"
}
if strings.Contains(strings.ToLower(*symbol), "usdc") {
*symbol = "usdc"
}
if strings.Contains(strings.ToLower(*symbol), "nusd") {
*symbol = "nusd"
}
if strings.Contains(strings.ToLower(*symbol), "usdt") {
*symbol = "usdt"
}
if strings.Contains(strings.ToLower(*symbol), "eth") {
*symbol = "eth"
}
if strings.Contains(strings.ToLower(*symbol), "avax") {
*symbol = "avax"
}
if strings.Contains(strings.ToLower(*symbol), "movr") {
*symbol = "movr"
}
if strings.Contains(strings.ToLower(*symbol), "frax") {
*symbol = "frax"
}
if strings.Contains(strings.ToLower(*symbol), "jewel") {
*symbol = "jewel"
}
res.tokenID = t.tokenSymbolToIDs[strings.ToLower(*symbol)]
res.decimals = *decimals
res.tokenAddress = token.String()
return nil
})
if err != nil {
return nil, fmt.Errorf("could not get pool token data: %w", err)
}
return res, nil
}
func (t *tokenDataServiceImpl) retrieveCCTPTokenData(parentCtx context.Context, tokenAddress common.Address, cctpService fetcher.CCTPService) (ImmutableTokenData, error) {
res := immutableTokenImpl{}
ctx, cancel := context.WithTimeout(parentCtx, maxAttemptTime)
defer cancel()
err := retry.WithBackoff(ctx, func(ctx context.Context) error {
symbol, err := cctpService.GetTokenSymbol(ctx, tokenAddress)
if err != nil {
return fmt.Errorf("could not get cctp token: %w", err)
}
if strings.Contains(strings.ToLower(*symbol), "usdc") {
*symbol = "usdc"
}
res.tokenID = t.tokenSymbolToIDs[strings.ToLower(*symbol)]
res.decimals = 6 // TODO, as cctp bridging matures, retrieve this data from on chain somehow.
return nil
}, retry.WithMaxAttemptTime(maxAttemptTime), retry.WithMaxAttempts(maxAttempt))
if err != nil {
return nil, fmt.Errorf("could not get token data: %w", err)
}
res.tokenAddress = tokenAddress.String()
return res, nil
}
var tokenDataMap = map[string]TokenData{
"1_0x5f98805A4E8be255a32880FDeC7F6728C6568bA0": {"LUSD", 18, "0x5f98805A4E8be255a32880FDeC7F6728C6568bA0"},
"1_0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E": {"crvUSD", 18, "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E"},
"8453_0x417Ac0e078398C154EdFadD9Ef675d30Be60Af93": {"crvUSD", 18, "0x417Ac0e078398C154EdFadD9Ef675d30Be60Af93"},
"8453_0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA": {"USDbC", 6, "0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA"},
"1_0x853d955acef822db058eb8505911ed77f175b99e": {"FRAX", 18, "0x853d955acef822db058eb8505911ed77f175b99e"},
"42161_0x17FC002b466eEc40DaE837Fc4bE5c67993ddBd6F": {"FRAX", 18, "0x17FC002b466eEc40DaE837Fc4bE5c67993ddBd6F"},
"137_0x45c32fA6DF82ead1e2EF74d17b76547EDdFaFF89": {"FRAX", 18, "0x45c32fA6DF82ead1e2EF74d17b76547EDdFaFF89"},
"10_0x8c6f28f2F1A3C87F0f938b96d27520d9751ec8d9": {"sUSD", 18, "0x8c6f28f2F1A3C87F0f938b96d27520d9751ec8d9"},
"8453_0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb": {"DAI", 18, "0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb"},
// Add additional tokens that are not part of the cache yet (and not by nature in bridge config) here
}