-
Notifications
You must be signed in to change notification settings - Fork 2
/
asset_test.go
176 lines (153 loc) · 4.16 KB
/
asset_test.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
package caip
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
// See: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-19.md#test-cases
func TestAssetID(t *testing.T) {
for _, tc := range []struct {
id string
}{{
// Ether Token
id: "eip155:1/slip44:60",
}, {
// Bitcoin Token
id: "bip122:000000000019d6689c085ae165831e93/slip44:0",
}, {
// ATOM Token
id: "cosmos:cosmoshub-3/slip44:118",
}, {
// Litecoin Token
id: "bip122:12a765e31ffd4059bada1e25190f6e98/slip44:2",
}, {
// Binance Token
id: "cosmos:Binance-Chain-Tigris/slip44:714",
}, {
// IOV Token
id: "cosmos:iov-mainnet/slip44:234",
}, {
// Lisk Token
id: "lip9:9ee11e9df416b18b/slip44:134",
}, {
// DAI Token
id: "eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f",
}, {
// CryptoKitties Collectible
id: "eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d",
}, {
// CryptoKitties Collectible ID
id: "eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/771769",
}} {
a := AssetID{}
if err := a.Parse(tc.id); err != nil {
t.Fatalf("Failed to parse asset id: %v", err)
}
if a.String() != tc.id {
t.Fatalf("Failed to serialize asset id to string")
}
if _, err := NewAssetID(a.ChainID, a.Namespace, a.Reference); err != nil {
t.Fatalf("Failed to create asset id from namespace and reference")
}
b, err := json.Marshal(a)
if err != nil {
t.Fatalf("Failed to marshal to json")
}
a = AssetID{}
if err := json.Unmarshal(b, &a); err != nil {
t.Fatalf("Failed to unmarshal to json")
}
if a.String() != tc.id {
t.Fatalf("Unmarshalled asset id invalid")
}
a2 := AssetID{}
if err := a2.Scan(a.String()); err != nil {
t.Errorf("Scanning value from sql.NullString")
}
if a2.String() != a.String() {
t.Errorf("Scanned value not valid")
}
}
}
func TestEVMAssetID(t *testing.T) {
for _, tc := range []struct {
id string
}{{
id: "eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f",
}, {
id: "eip155:1/erc720:0x6b175474e89094c44da98b954eedeac495271d0f",
}, {
id: "eip155:1/erc720:0x6b175474e89094c44da98b954eedeac495271d0f/1",
}, {
id: "eip155:1/erc1155:0x6b175474e89094c44da98b954eedeac495271d0f",
}, {
id: "eip155:1/erc1155:0x6b175474e89094c44da98b954eedeac495271d0f/1",
}} {
a := EVMAssetID{}
if err := a.Parse(tc.id); err != nil {
t.Errorf("Failed to parse asset id")
}
if a.String() != tc.id {
t.Errorf("Failed to serialize asset id to string")
}
if _, err := NewEVMAssetID(a.ChainID, a.AssetID.Namespace, a.AssetID.Reference); err != nil {
t.Errorf("Failed to create asset id from address")
}
b, err := json.Marshal(a)
if err != nil {
t.Errorf("Failed to marshal to json")
}
a = EVMAssetID{}
if err := json.Unmarshal(b, &a); err != nil {
t.Errorf("Failed to unmarshal to json")
}
if a.String() != tc.id {
t.Errorf("Unmarshalled asset id invalid")
}
a2 := EVMAssetID{}
if err := a2.Scan(a.String()); err != nil {
t.Errorf("Scanning value from sql.NullString")
}
if a2.String() != a.String() {
t.Errorf("Scanned value not valid")
}
}
}
func TestInvalidEVMAssetID(t *testing.T) {
for _, tc := range []struct {
id string
err error
}{{
id: "eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0x",
err: fmt.Errorf("invalid eth address: %s", "0x6b175474e89094c44da98b954eedeac495271d0x"),
}, {
id: "eip155:1/erc20:0x6b175474e",
err: fmt.Errorf("invalid eth address: %s", "0x6b175474e"),
}, {
id: "cosmos:1/erc20:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdd",
err: fmt.Errorf("invalid chain namespace: %s", "cosmos"),
}} {
a := EVMAssetID{}
if err := a.Parse(tc.id); err != nil {
t.Errorf("Failed to parse asset id")
}
if a.String() != tc.id {
t.Errorf("Failed to serialize asset id to string")
}
err := a.Validate()
if err == nil {
t.Errorf("Validate asset id should error")
}
if errors.Is(err, tc.err) {
t.Errorf("expected error: %s", tc.err)
}
_, err = NewEVMAssetID(a.ChainID, a.AssetID.Namespace, a.AssetID.Reference)
if err == nil {
t.Errorf("Create asset id should error")
}
if err.Error() != tc.err.Error() {
t.Errorf("expected error: %s, got: %s", tc.err, err)
}
}
}