forked from miraclesu/uniswap-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_amount.go
41 lines (32 loc) · 989 Bytes
/
token_amount.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
package entities
import "math/big"
type TokenAmount struct {
*CurrencyAmount
Token *Token
}
// amount _must_ be raw, i.e. in the native representation
func NewTokenAmount(token *Token, amount *big.Int) (*TokenAmount, error) {
currencyAmount, err := NewCurrencyAmount(token.Currency, amount)
if err != nil {
return nil, err
}
return &TokenAmount{
Token: token,
CurrencyAmount: currencyAmount,
}, nil
}
func (t *TokenAmount) Add(other *TokenAmount) (*TokenAmount, error) {
if !t.Token.Equals(other.Token) {
return nil, ErrDiffToken
}
return NewTokenAmount(t.Token, big.NewInt(0).Add(t.Raw(), other.Raw()))
}
func (t *TokenAmount) Subtract(other *TokenAmount) (*TokenAmount, error) {
if !t.Token.Equals(other.Token) {
return nil, ErrDiffToken
}
return NewTokenAmount(t.Token, big.NewInt(0).Sub(t.Raw(), other.Raw()))
}
func (t *TokenAmount) Equals(other *TokenAmount) bool {
return t.Token.Equals(other.Token) && t.Fraction.EqualTo(other.Fraction)
}