-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
executable file
·85 lines (68 loc) · 1.91 KB
/
token.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
package defi
import (
"errors"
"github.com/ethereum/go-ethereum/common"
"strings"
)
type Token struct {
chainID ChainID
address common.Address
decimals int64
symbol string
name string
// bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
bypassChecksum bool
}
// ensure Token conforms to BaseCurrency interface
//var _ Currency = &Token{}
func NewToken(chainID ChainID, address string, decimals int64, symbol string, name string, bypassChecksum bool) *Token {
var hexAddress common.Address
if address != "" {
hexAddress = common.HexToAddress(address)
}
return &Token{
chainID: chainID,
address: hexAddress,
decimals: decimals,
symbol: symbol,
name: name,
bypassChecksum: bypassChecksum,
}
}
func (currency *Token) IsNative() bool {
return false
}
func (currency *Token) IsToken() bool {
return true
}
func (currency *Token) ChainID() ChainID {
return currency.chainID
}
func (currency *Token) Address() common.Address {
return currency.address
}
func (currency *Token) Decimals() int64 {
return currency.decimals
}
func (currency *Token) Symbol() string {
return currency.symbol
}
func (currency *Token) Name() string {
return currency.name
}
func (currency *Token) Equals(other *Token) bool {
// Implement the comparison logic here
return other.IsToken() && currency.chainID == other.ChainID() && strings.ToLower(currency.address.Hex()) == strings.ToLower(other.Address().Hex())
}
func (currency *Token) GetWrapped() *Token {
return currency
}
func (currency *Token) SortsBefore(other *Token) (bool, error) {
if currency.ChainID() != other.ChainID() {
return false, errors.New("chainId mismatch")
}
if currency.Address() == other.Address() {
return false, errors.New("identical addresses")
}
return strings.ToLower(currency.Address().String()) < strings.ToLower(other.Address().String()), nil
}