/
token.go
48 lines (41 loc) · 1.28 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
package dexsdk
import (
"github.com/ethereum/go-ethereum/common"
"strings"
)
// Desc Blockchain Object Description
type Desc struct {
Name string `json:"name" yaml:"name"` // obj name
Symbol string `json:"symbol" yaml:"symbol"` // obj symbol
}
// Token Represents an ERC20 token with a unique address and some metadata.
type Token struct {
Desc
Decimals int8 `json:"decimals" yaml:"decimals"` // token decimals
Address string `json:"address" yaml:"address"` // token address
ChainId ChainId `json:"chainId" yaml:"chainId"` // token chain id enum
}
// NewToken Token Constructor
func NewToken(address string, decimals int8, chainId ChainId, desc ...string) Token {
var token Token
token.Address = address
token.Decimals = decimals
token.ChainId = chainId
if len(desc) >= 2 {
token.Name = desc[0]
token.Symbol = desc[1]
}
return token
}
// ToAddress convert to ethereum common.Address
func (t Token) ToAddress() common.Address {
return common.HexToAddress(t.Address)
}
// Equals judgment condition is unique address
func (t Token) Equals(other Token) bool {
return t.ToAddress() == other.ToAddress()
}
// SortsBefore check is sort before new object
func (t Token) SortsBefore(other Token) bool {
return strings.ToLower(t.Address) < strings.ToLower(other.Address)
}