-
Notifications
You must be signed in to change notification settings - Fork 2
/
erc20_test.go
99 lines (84 loc) · 2.3 KB
/
erc20_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
package caip
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
func TestERC20AssetID(t *testing.T) {
for _, tc := range []struct {
id string
}{{
// Ethereum mainnet
id: "eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f",
}} {
a := ERC20AssetID{}
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 := NewERC20AssetID(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 = ERC20AssetID{}
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 := ERC20AssetID{}
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 TestInvalidERC20AssetID(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/erc721:0x6b175474e89094c44da98b954eedeac495271d0a",
err: fmt.Errorf("invalid asset namespace: %s", "erc721"),
}, {
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 := ERC20AssetID{}
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 = NewERC20AssetID(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)
}
}
}