-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathasset_test.go
More file actions
420 lines (381 loc) · 15.1 KB
/
asset_test.go
File metadata and controls
420 lines (381 loc) · 15.1 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package crosschain_test
import (
"encoding/json"
"errors"
"fmt"
"slices"
"testing"
. "github.com/cordialsys/crosschain"
"github.com/cordialsys/crosschain/address"
xcbuilder "github.com/cordialsys/crosschain/builder"
"github.com/cordialsys/crosschain/call"
"github.com/cordialsys/crosschain/chain/cosmos"
"github.com/cordialsys/crosschain/chain/egld"
"github.com/cordialsys/crosschain/chain/eos"
"github.com/cordialsys/crosschain/chain/evm"
"github.com/cordialsys/crosschain/chain/hedera"
"github.com/cordialsys/crosschain/chain/kaspa"
"github.com/cordialsys/crosschain/chain/near"
"github.com/cordialsys/crosschain/chain/substrate"
"github.com/cordialsys/crosschain/factory"
"github.com/cordialsys/crosschain/factory/drivers"
"github.com/cordialsys/crosschain/normalize"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/require"
)
// Chain specific validation of the chain configs
func TestChains(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
xcf2 := factory.NewNotMainnetsFactory(&factory.FactoryOptions{})
for _, xcf := range []*factory.Factory{xcf1, xcf2} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
switch chain.Driver {
case DriverEVM, DriverEVMLegacy, DriverTempo:
evm.Validate(t, chain)
case DriverSubstrate:
substrate.Validate(t, chain)
case DriverKaspa:
kaspa.Validate(t, chain)
case DriverEOS:
eos.Validate(t, chain)
case DriverEGLD:
egld.Validate(t, chain)
case DriverTron:
// pass
case DriverTon:
// pass
case DriverSui:
// pass
case DriverDusk:
// pass
case DriverAptos:
// pass
case DriverSolana:
// pass
case DriverCosmos, DriverCosmosEvmos:
cosmos.Validate(t, chain)
case DriverCardano, DriverFilecoin, DriverXlm, DriverXrp:
// pass
case DriverBitcoin, DriverBitcoinCash, DriverBitcoinLegacy, DriverZcash:
// pass
case DriverInternetComputerProtocol:
// pass
case DriverHyperliquid:
// pass
case DriverHedera:
hedera.Validate(t, chain)
case DriverNear:
near.Validate(t, chain)
case "":
require.Fail(t, "unknown driver", chain.Driver)
default:
require.Fail(t, fmt.Sprintf("missing .Validate() for %s driver", chain.Driver))
}
})
}
}
}
func (s *CrosschainTestSuite) TestTypesAssetVsNativeAsset() {
require := s.Require()
require.Equal(NativeAsset("SOL"), SOL)
require.NotEqual("SOL", SOL)
}
func (s *CrosschainTestSuite) TestAssetDriver() {
require := s.Require()
require.Equal(DriverBitcoin, NativeAsset(BTC).Driver())
require.Equal(DriverEVM, NativeAsset(ETH).Driver())
require.Equal(DriverEVMLegacy, NativeAsset(FTM).Driver())
require.Equal(DriverAptos, NativeAsset(APTOS).Driver())
require.Equal(DriverSolana, NativeAsset(SOL).Driver())
require.Equal(DriverCosmos, NativeAsset(ATOM).Driver())
require.Equal(DriverSubstrate, NativeAsset(DOT).Driver())
require.Equal(DriverTron, NativeAsset(TRX).Driver())
require.Equal(DriverTon, NativeAsset(TON).Driver())
drivers := map[Driver]bool{}
for _, driver := range SupportedDrivers {
if _, ok := drivers[driver]; ok {
require.Fail("duplicate driver %s", driver)
}
drivers[driver] = true
// test driver is valid
require.NotEmpty(driver.SignatureAlgorithms(), "driver is not valid")
}
}
func (s *CrosschainTestSuite) TestChainType() {
require := s.Require()
// check whole native asset list
for _, na := range NativeAssetList {
require.True(na.IsValid(), fmt.Sprintf("%s should have a driver", na))
}
require.True(NativeAsset(ETH).IsValid())
require.True(NativeAsset(BTC).IsValid())
require.True(NativeAsset(ArbETH).IsValid())
require.True(NativeAsset(OptETH).IsValid())
require.True(NativeAsset("ETH").IsValid())
require.True(NativeAsset("BTC").IsValid())
require.True(NativeAsset("OptETH").IsValid())
require.False(NativeAsset("xxx").IsValid())
require.False(NativeAsset("unknown").IsValid())
}
func TestFeeLimitConfigured(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
xcf2 := factory.NewNotMainnetsFactory(&factory.FactoryOptions{})
for _, xcf := range []*factory.Factory{xcf1, xcf2} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
require := require.New(t)
chain := chain.GetChain()
if chain.FeeLimit.Decimal().IsZero() {
if len(chain.NativeAssets) == 0 {
require.Fail(
"Max fee is required, or additional native assets must be configured (e.g. Noble chain)",
)
}
for _, na := range chain.NativeAssets {
_, err := decimal.NewFromString(na.FeeLimit.String())
require.NoError(err, fmt.Sprintf("%s additional asset %s (%s) max fee should be a valid decimal", chain.Chain, na.AssetId, xcf.Config.Network))
f, _ := na.FeeLimit.Decimal().Float64()
// paranoid non-zero check to account for floating point error
require.True(f > 0.000000001, fmt.Sprintf("%s additional asset %s (%s) max fee should be non-zero", chain.Chain, na.AssetId, xcf.Config.Network))
}
} else {
_, err := decimal.NewFromString(chain.FeeLimit.String())
require.NoError(err, "max fee is required and should be a valid decimal")
f, _ := chain.FeeLimit.Decimal().Float64()
// paranoid non-zero check to account for floating point error
require.True(f > 0.000000001, "max fee should be non-zero")
}
})
}
}
}
func TestNativeAssetConfigs(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
xcf2 := factory.NewNotMainnetsFactory(&factory.FactoryOptions{})
// It's technically valid for an asset to have 0 decimals
// If this comes up, let's add it manually here. Otherwise we'd have to make it a pointer or add another config option.
valid0DecimalAssets := []string{
// (so far none)
}
for _, xcf := range []*factory.Factory{xcf1, xcf2} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
require := require.New(t)
chain := chain.GetChain()
require.NotEmpty(chain.Chain, fmt.Sprintf("%s should have a chain", chain.Chain))
require.NotEmpty(chain.Chain, fmt.Sprintf("%s should have a chain", chain.Chain))
if chain.NoNativeAsset {
// no decimals if no native asset
require.Greater(len(chain.NativeAssets), 0, fmt.Sprintf("%s should have additional-native-assets (for paying fees) if no new native asset is configured", chain.Chain))
} else {
if slices.Contains(valid0DecimalAssets, string(chain.Chain)) {
// valid 0-decimal native asset
} else {
require.NotZero(chain.Decimals, fmt.Sprintf("%s should have decimals set", chain.Chain))
}
}
require.GreaterOrEqual(int(chain.Decimals), 0, fmt.Sprintf("%s should have positive decimals (%d)", chain.Chain, chain.Decimals))
for _, na := range chain.NativeAssets {
require.NotEmpty(na.AssetId, fmt.Sprintf("%s additional asset %s should have an asset id", chain.Chain, na.AssetId))
require.True(
na.ContractId != "" || na.BridgedAsset != "",
fmt.Sprintf("%s additional asset %s must have at least one of contract_id or bridged_asset set", chain.Chain, na.AssetId),
)
normalizedAssetId := normalize.NormalizeAddressString(string(na.AssetId), chain.Chain)
require.Equal(
normalizedAssetId, string(na.AssetId),
fmt.Sprintf("%s additional asset-id '%s' is not in a normalized format", chain.Chain, na.AssetId),
)
normalizedContractId := normalize.NormalizeAddressString(string(na.ContractId), chain.Chain)
require.Equal(
normalizedContractId, string(na.ContractId),
fmt.Sprintf("%s additional contract-id '%s' is not in a normalized format", chain.Chain, na.ContractId),
)
if slices.Contains(valid0DecimalAssets, string(na.AssetId)) {
// valid 0-decimal native asset
} else {
require.NotZero(na.Decimals, fmt.Sprintf("%s additional asset %s should have decimals set", chain.Chain, na.AssetId))
}
require.GreaterOrEqual(int(na.Decimals), 0, fmt.Sprintf("%s additional asset %s should have positive decimals", chain.Chain, na.AssetId))
require.NotEmpty(na.FeeLimit, fmt.Sprintf("%s additional asset %s should have a max fee", chain.Chain, na.AssetId))
}
// AssetId and ContractId should be unique
for i, na1 := range chain.NativeAssets {
for j, na2 := range chain.NativeAssets {
if i == j {
continue
}
require.NotEqual(na1.AssetId, na2.AssetId, fmt.Sprintf("%s additional asset %s and %s should have unique asset ids", chain.Chain, na1.AssetId, na2.AssetId))
require.NotEqual(na1.ContractId, na2.ContractId, fmt.Sprintf("%s additional asset %s and %s should have unique contract ids", chain.Chain, na1.AssetId, na2.AssetId))
}
}
})
}
}
}
func TestLegacyChainCoinConfig(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
xcf2 := factory.NewNotMainnetsFactory(&factory.FactoryOptions{})
for _, xcf := range []*factory.Factory{xcf1, xcf2} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
require := require.New(t)
if chain.ChainCoin != "" {
// there should be a native-asset-config with the corresponding chain_coin as the contract_id
m := map[ContractAddress]*AdditionalNativeAsset{}
for _, na := range chain.NativeAssets {
m[na.ContractId] = na
}
_, ok := m[ContractAddress(chain.ChainCoin)]
require.True(ok, fmt.Sprintf(
"%s should have a native-asset-config with the corresponding chain_coin '%s' as the contract_id",
chain.Chain,
chain.ChainCoin,
))
na := m[ContractAddress(chain.ChainCoin)]
if na.AssetId == string(chain.Chain) {
require.Equal(na.Decimals, chain.Decimals, fmt.Sprintf(
"chain %s decimals does not match native asset %s decimals",
chain.Chain, na.AssetId,
))
}
}
})
}
}
}
func TestSupportedAddressFormats(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
xcf2 := factory.NewNotMainnetsFactory(&factory.FactoryOptions{})
for _, xcf := range []*factory.Factory{xcf1, xcf2} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
require := require.New(t)
if len(chain.Address.Formats) > 1 {
for _, format := range chain.Address.Formats {
addressArgs := []address.AddressOption{}
addressArgs = append(addressArgs, address.OptionFormat(format))
builder, err := factory.NewDefaultFactory().NewAddressBuilder(
chain.ChainBaseConfig, addressArgs...,
)
require.NoError(err)
withFormats, ok := builder.(AddressBuilderWithFormats)
if !ok {
require.Fail(
"missing AddressBuilderWithFormats",
"address builder %T does not implement AddressBuilderWithFormats despite having multiple formats", builder,
)
}
alg := withFormats.GetSignatureAlgorithm()
require.NotEmpty(alg, "address builder %T should have a signature algorithm for format %s", builder, format)
}
} else if len(chain.Address.Formats) == 1 {
require.Fail(
"invalid address formats configuration",
"%s, unnecessary base format", chain.Chain,
)
}
})
}
}
}
func TestExternalIdsAreSet(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
for _, xcf := range []*factory.Factory{xcf1} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
validExternalConfig := false
if chain.External.CoinGecko.AssetId != "" {
validExternalConfig = true
}
if chain.External.CoinGecko.ChainId != "" {
validExternalConfig = true
}
if chain.External.CoinMarketCap.AssetId != "" {
validExternalConfig = true
}
if chain.External.CoinMarketCap.ChainId != "" {
validExternalConfig = true
}
if chain.External.IndexingCo.ChainId != "" {
validExternalConfig = true
}
if chain.External.StakingRewards.ChainId != "" {
validExternalConfig = true
}
require.True(t, validExternalConfig, "Please provive external ids")
})
}
}
}
func derefOrZero[T any](ptr *T) T {
if ptr == nil {
var zero T
return zero
}
return *ptr
}
func TestChainSupportAnnotations(t *testing.T) {
xcf1 := factory.NewDefaultFactory()
xcf2 := factory.NewNotMainnetsFactory(&factory.FactoryOptions{})
for _, xcf := range []*factory.Factory{xcf1, xcf2} {
for _, chain := range xcf.GetAllChains() {
t.Run(fmt.Sprintf("%s_%s", chain.Chain, xcf.Config.Network), func(t *testing.T) {
require := require.New(t)
xcf := factory.NewDefaultFactory()
builder, err := xcf.NewTxBuilder(chain.ChainBaseConfig)
require.NoError(err)
for _, stakingMethod := range chain.Support.Staking {
require.True(stakingMethod.Valid(), fmt.Sprintf("chain.support.staking method %s is not valid", stakingMethod))
}
if !derefOrZero(chain.Support.Memo).Valid() {
require.Fail(fmt.Sprintf("chain.support.memo be set to %s, %s, or %s", MemoSupportString, MemoSupportNumeric, MemoSupportNone))
}
memoSupport := builder.SupportsMemo()
if derefOrZero(chain.Support.Memo) != memoSupport {
if (derefOrZero(chain.Support.Memo) == "" && memoSupport == MemoSupportNone) ||
(derefOrZero(chain.Support.Memo) == MemoSupportNone && memoSupport == "") {
// ok
} else {
require.Fail(fmt.Sprintf("chain.support.memo should be set to %s, or the value returned by the %T builder is wrong", memoSupport, builder))
}
}
staker, ok := builder.(xcbuilder.Staking)
if ok {
methods := staker.MethodsUsed()
if len(methods) > 0 {
if len(chain.Support.Staking) == 0 {
require.Fail(fmt.Sprintf("chain.support.staking be set to an array containing a subset of %s, %s, %s to match the current builder's methods %v", StakingMethodStake, StakingMethodUnstake, StakingMethodWithdraw, methods))
}
}
} else {
if len(chain.Support.Staking) > 0 {
require.Fail(fmt.Sprintf("chain.support.staking be set to an empty array since it's not supported on the %T builder", builder))
}
}
input, err := drivers.NewTxInput(chain.Driver)
require.NoError(err)
require.Equal(
input.IsFeeLimitAccurate(),
derefOrZero(chain.Support.Fee.Accurate),
fmt.Sprintf("chain.support.fee.accurate should be set to %t, or the chain input %T implementation is wrong", input.IsFeeLimitAccurate(), input),
)
_, supportsFeePayer := builder.(xcbuilder.BuilderSupportsFeePayer)
require.Equal(
supportsFeePayer,
derefOrZero(chain.Support.Fee.Payer),
fmt.Sprintf("chain.support.fee.payer should be set to %t, or the chain input %T implementation is wrong", supportsFeePayer, input),
)
_, err = drivers.NewCall(chain.ChainBaseConfig, call.Method(""), json.RawMessage("{}"), "")
// hacky way to determine chain's call support...
callSupported := !errors.Is(err, drivers.ErrCallNotSupported)
require.Equal(
callSupported,
derefOrZero(chain.Support.Call),
fmt.Sprintf("chain.support.call should be set to %t, or the drivers.NewCall implementation is wrong", callSupported),
)
})
}
}
}