This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasset.go
404 lines (359 loc) · 10.4 KB
/
asset.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package crosschain
import (
"fmt"
"strings"
)
// Asset is an asset on a blockchain. It can be a token or native asset.
type Asset string
// AssetType is the type of an asset, either native or token
type AssetType string
// List of supported AssetType
const (
AssetTypeNative = AssetType("native")
AssetTypeToken = AssetType("token")
AssetTypeTask = AssetType("task")
)
// AssetType returns the type of an Asset
func (asset Asset) AssetType() AssetType {
switch native := NativeAsset(asset); native {
case BCH, BTC, DOGE, LTC:
return AssetTypeNative
case ACA,
APTOS,
ArbETH,
ATOM,
AurETH,
AVAX,
BNB,
CELO,
CHZ,
CHZ2,
ETC,
ETH,
ETHW,
FTM,
INJ,
KAR,
KLAY,
LUNA,
LUNC,
MATIC,
XDC,
OAS,
OasisROSE,
OptETH,
ROSE,
SOL,
SUI,
XPLA:
return AssetTypeNative
default:
return AssetTypeToken
}
}
// ChainType is the type of a chain
type ChainType string
// List of supported ChainType
const (
ChainTypeUnknown = ChainType("unknown")
ChainTypeUTXO = ChainType("utxo")
ChainTypeAccount = ChainType("account")
)
type SignatureType string
const (
K256 = SignatureType("k256")
Ed255 = SignatureType("ed255")
Schnorr = SignatureType("schnorr")
)
// ChainType returns the type of a chain, represented as its NativeAsset
func (native NativeAsset) ChainType() ChainType {
switch native {
case BCH, BTC, DOGE, LTC:
return ChainTypeUTXO
case ACA,
APTOS,
ArbETH,
ATOM,
AurETH,
AVAX,
BNB,
CELO,
CHZ,
CHZ2,
ETC,
ETH,
ETHW,
FTM,
INJ,
KAR,
KLAY,
LUNA,
LUNC,
MATIC,
XDC,
OAS,
OasisROSE,
OptETH,
ROSE,
SOL,
SUI,
XPLA:
return ChainTypeAccount
default:
return ChainTypeUnknown
}
}
// NativeAsset is an asset on a blockchain used to pay gas fees.
// In Crosschain, for simplicity, a NativeAsset represents a chain.
type NativeAsset Asset
// List of supported NativeAsset
const (
// UTXO
BCH = NativeAsset("BCH") // Bitcoin Cash
BTC = NativeAsset("BTC") // Bitcoin
DOGE = NativeAsset("DOGE") // Dogecoin
LTC = NativeAsset("LTC") // Litecoin
// Account-based
ACA = NativeAsset("ACA") // Acala
APTOS = NativeAsset("APTOS") // APTOS
ArbETH = NativeAsset("ArbETH") // Arbitrum
ATOM = NativeAsset("ATOM") // Cosmos
AurETH = NativeAsset("AurETH") // Aurora
AVAX = NativeAsset("AVAX") // Avalanche
BNB = NativeAsset("BNB") // Binance Coin
CELO = NativeAsset("CELO") // Celo
CHZ = NativeAsset("CHZ") // Chiliz
CHZ2 = NativeAsset("CHZ2") // Chiliz 2.0
ETC = NativeAsset("ETC") // Ethereum Classic
ETH = NativeAsset("ETH") // Ethereum
ETHW = NativeAsset("ETHW") // Ethereum PoW
FTM = NativeAsset("FTM") // Fantom
INJ = NativeAsset("INJ") // Injective
LUNA = NativeAsset("LUNA") // Terra V2
LUNC = NativeAsset("LUNC") // Terra Classic
KAR = NativeAsset("KAR") // Karura
KLAY = NativeAsset("KLAY") // Klaytn
XDC = NativeAsset("XDC") // XinFin
MATIC = NativeAsset("MATIC") // Polygon
OAS = NativeAsset("OAS") // Oasys (not Oasis!)
OasisROSE = NativeAsset("OasisROSE") // Rose (Oasis = main chain)
OptETH = NativeAsset("OptETH") // Optimism
ROSE = NativeAsset("ROSE") // Rose (Oasis Emerald parachain)
SOL = NativeAsset("SOL") // Solana
SUI = NativeAsset("SUI") // SUI
XPLA = NativeAsset("XPLA") // XPLA
)
// Driver is the type of a chain
type Driver string
// List of supported Driver
const (
DriverAptos = Driver("aptos")
DriverSui = Driver("sui")
DriverBitcoin = Driver("bitcoin")
DriverCosmos = Driver("cosmos")
DriverCosmosEvmos = Driver("evmos")
DriverEVM = Driver("evm")
DriverEVMLegacy = Driver("evm-legacy")
DriverSolana = Driver("solana")
)
var SupportedDrivers = []Driver{
DriverBitcoin,
DriverCosmos,
DriverCosmosEvmos,
DriverEVM,
DriverEVMLegacy,
DriverSolana,
DriverSui,
DriverAptos,
}
func (native NativeAsset) Driver() Driver {
switch native {
case BCH, BTC, DOGE, LTC:
return DriverBitcoin
case AVAX, CELO, ETH, ETHW, MATIC, OasisROSE, OptETH:
return DriverEVM
case BNB, FTM, ETC, ROSE, AurETH, ACA, KAR, KLAY, OAS, CHZ, XDC, CHZ2:
return DriverEVMLegacy
case APTOS:
return DriverAptos
case ATOM, XPLA, INJ, LUNC, LUNA:
return DriverCosmos
case SUI:
return DriverSui
case SOL:
return DriverSolana
}
return ""
}
func (driver Driver) SignatureAlgorithm() SignatureType {
switch driver {
case DriverBitcoin, DriverEVM, DriverEVMLegacy, DriverCosmos, DriverCosmosEvmos:
return K256
case DriverAptos, DriverSolana, DriverSui:
return Ed255
}
return ""
}
// AssetID is an internal identifier for each asset
// Examples: ETH, USDC, USDC.SOL - see tests for details
type AssetID string
// AssetConfig is the model used to represent an asset read from config file or db
type AssetConfig struct {
// [[silochain.beta.chains]]
// asset = "eth"
// net = "mainnet"
// url = "http://7.125.36.22:8089"
//
// [[silochain.beta.chains]]
// asset = "usdc"
// chain = "eth"
// net = "mainnet"
// contract = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
// decimals = 6
Asset string `yaml:"asset"`
Driver string `yaml:"driver"`
Net string `yaml:"net"`
URL string `yaml:"url"`
FcdURL string `yaml:"fcd_url"`
Auth string `yaml:"auth"`
Provider string `yaml:"provider"`
ChainID int64 `yaml:"chain_id"`
ChainIDStr string `yaml:"chain_id_str"`
ChainName string `yaml:"chain_name"`
ChainPrefix string `yaml:"chain_prefix"`
ChainCoin string `yaml:"chain_coin"`
GasCoin string `yaml:"gas_coin"`
ChainCoinHDPath uint32 `yaml:"chain_coin_hd_path"`
ChainGasPriceDefault float64 `yaml:"chain_gas_price_default"`
ChainGasMultiplier float64 `yaml:"chain_gas_multiplier"`
ChainGasTip uint64 `yaml:"chain_gas_tip"`
ExplorerURL string `yaml:"explorer_url"`
Decimals int32 `yaml:"decimals"`
Name string `yaml:"name"`
IndexerUrl string `yaml:"indexer_url"`
IndexerType string `yaml:"indexer_type"`
NoGasFees bool `yaml:"no_gas_fees"`
// Tokens
Chain string `yaml:"chain"`
Contract string `yaml:"contract"`
// Internal
AuthSecret string `yaml:"-"`
Type AssetType `yaml:"-"`
NativeAsset NativeAsset `yaml:"-"`
Metadata AssetMetadataConfig `yaml:"-"`
}
type NativeAssetConfig = AssetConfig
type TokenAssetConfig struct {
Asset string `yaml:"asset"`
Chain string `yaml:"chain"`
Net string `yaml:"net"`
Decimals int32 `yaml:"decimals"`
Contract string `yaml:"contract"`
Type AssetType `yaml:"type"`
AssetConfig `yaml:"-"`
NativeAssetConfig *NativeAssetConfig `yaml:"-"`
Metadata AssetMetadataConfig `yaml:"-"`
}
type AssetMetadataConfig struct {
PriceUSD AmountHumanReadable `yaml:"-"`
}
var _ ITask = &NativeAssetConfig{}
var _ ITask = &TokenAssetConfig{}
// Config is the full config containing all Assets
type Config struct {
Chains []*NativeAssetConfig `yaml:"chains"`
Tokens []*TokenAssetConfig `yaml:"tokens"`
AllPipelines []*PipelineConfig `yaml:"pipelines"`
AllTasks []*TaskConfig `yaml:"tasks"`
AllAssets []ITask `yaml:"-"`
}
func (c NativeAssetConfig) String() string {
// do NOT print AuthSecret
return fmt.Sprintf(
"NativeAssetConfig(id=%s asset=%s chainId=%d driver=%s type=%s chainCoin=%s prefix=%s net=%s url=%s auth=%s provider=%s native_asset=%s)",
c.ID(), c.Asset, c.ChainID, c.Driver, c.Type, c.ChainCoin, c.ChainPrefix, c.Net, c.URL, c.Auth, c.Provider, c.NativeAsset,
)
}
func (asset *NativeAssetConfig) ID() AssetID {
return GetAssetIDFromAsset("", asset.Asset)
}
func (asset NativeAssetConfig) GetAssetConfig() *AssetConfig {
return &asset
}
func (asset NativeAssetConfig) GetDriver() string {
return asset.Driver
}
func (asset *NativeAssetConfig) GetNativeAsset() *NativeAssetConfig {
return asset
}
func (asset NativeAssetConfig) GetTask() *TaskConfig {
return nil
}
func (c TokenAssetConfig) String() string {
return fmt.Sprintf(
"TokenAssetConfig(id=%s asset=%s chain=%s net=%s decimals=%d contract=%s)",
c.ID(), c.Asset, c.Chain, c.Net, c.Decimals, c.Contract,
)
}
func (asset TokenAssetConfig) ID() AssetID {
return GetAssetIDFromAsset(asset.Asset, asset.Chain)
}
func (asset TokenAssetConfig) GetNativeAsset() *NativeAssetConfig {
return asset.NativeAssetConfig
}
func (asset TokenAssetConfig) GetDriver() string {
return asset.GetNativeAsset().Driver
}
func (asset TokenAssetConfig) GetAssetConfig() *AssetConfig {
return &asset.AssetConfig
}
func (asset TokenAssetConfig) GetTask() *TaskConfig {
return nil
}
func parseAssetAndNativeAsset(asset string, nativeAsset string) (string, string) {
if asset == "" && nativeAsset == "" {
return "", ""
}
if asset == "" && nativeAsset != "" {
asset = nativeAsset
}
assetSplit := strings.Split(asset, ".")
if len(assetSplit) == 2 && Asset(assetSplit[1]).AssetType() == AssetTypeNative {
asset = assetSplit[0]
if nativeAsset == "" {
nativeAsset = assetSplit[1]
}
}
validNative := Asset(asset).AssetType() == AssetTypeNative
if nativeAsset == "" {
if validNative {
nativeAsset = asset
} else {
nativeAsset = "ETH"
}
}
return asset, nativeAsset
}
// GetAssetIDFromAsset return the canonical AssetID given two input strings asset, nativeAsset.
// Input can come from user input.
// Examples:
// - GetAssetIDFromAsset("USDC", "") -> "USDC.ETH"
// - GetAssetIDFromAsset("USDC", "ETH") -> "USDC.ETH"
// - GetAssetIDFromAsset("USDC", "SOL") -> "USDC.SOL"
// - GetAssetIDFromAsset("USDC.SOL", "") -> "USDC.SOL"
// See tests for more examples.
func GetAssetIDFromAsset(asset string, nativeAsset string) AssetID {
// id is SYMBOL for ERC20 and SYMBOL.CHAIN for others
// e.g. BTC, ETH, USDC, SOL, USDC.SOL
asset, nativeAsset = parseAssetAndNativeAsset(asset, nativeAsset)
validNative := Asset(asset).AssetType() == AssetTypeNative
// native asset, e.g. BTC, ETH, SOL
if asset == nativeAsset {
return AssetID(asset)
}
if nativeAsset == "ETH" && !validNative {
return AssetID(asset + ".ETH")
}
// token, e.g. USDC, USDC.SOL
return AssetID(asset + "." + nativeAsset)
}