-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.go
93 lines (78 loc) · 2.23 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
86
87
88
89
90
91
92
93
package entities
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/nikolalosic/pancakeswap-sdk-go/constants"
"github.com/nikolalosic/pancakeswap-sdk-go/utils"
)
var (
ErrDiffChainID = fmt.Errorf("diff chain id")
ErrDiffToken = fmt.Errorf("diff token")
ErrSameAddrss = fmt.Errorf("same address")
_WETHCurrency, _ = newCurrency(constants.Decimals18, "WBNB", "Wrapped BNB")
WETH = map[constants.ChainID]*Token{
constants.Mainnet: {
Currency: _WETHCurrency,
ChainID: constants.Mainnet,
Address: utils.ValidateAndParseAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"),
},
constants.Testnet: {
Currency: _WETHCurrency,
ChainID: constants.Testnet,
Address: utils.ValidateAndParseAddress("0xaE8E19eFB41e7b96815649A6a60785e1fbA84C1e"),
},
}
)
/**
* Represents an ERC20 token with a unique address and some metadata.
*/
type Token struct {
*Currency
constants.ChainID
common.Address
}
func NewToken(chainID constants.ChainID, address common.Address, decimals int, symbol, name string) (*Token, error) {
currency, err := newCurrency(decimals, symbol, name)
if err != nil {
return nil, err
}
return &Token{
Currency: currency,
ChainID: chainID,
Address: address,
}, nil
}
/**
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
* @param other other token to compare
*/
func (t *Token) Equals(other *Token) bool {
if t == other {
return true
}
return t.ChainID == other.ChainID && t.Address == other.Address
}
/**
* Returns true if the address of this token sorts before the address of the other token
* @param other other token to compare
* @throws if the tokens have the same address
* @throws if the tokens are on different chains
*/
func (t *Token) SortsBefore(other *Token) (bool, error) {
if t.ChainID != other.ChainID {
return false, ErrDiffChainID
}
if t.Address == other.Address {
return false, ErrSameAddrss
}
return strings.ToLower(t.Address.String()) < strings.ToLower(other.Address.String()), nil
}
// NewETHRToken creates a token that currency is ETH
func NewETHRToken(chainID constants.ChainID, address common.Address) *Token {
return &Token{
Currency: ETHER,
ChainID: chainID,
Address: address,
}
}