forked from miraclesu/uniswap-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_amount.go
47 lines (39 loc) · 1.33 KB
/
currency_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
42
43
44
45
46
47
package entities
import (
"errors"
"math/big"
"github.com/liweimin90/uniswap-sdk-golang/constants"
"github.com/liweimin90/uniswap-sdk-golang/utils"
)
var (
// ErrInsufficientReserves doesn't have insufficient reserves
ErrInsufficientReserves = errors.New("doesn't have insufficient reserves")
// ErrInsufficientInputAmount the input amount insufficient reserves
ErrInsufficientInputAmount = errors.New("the input amount insufficient reserves")
)
// CurrencyAmount warps Fraction and Currency
type CurrencyAmount struct {
*Fraction
*Currency
}
// NewCurrencyAmount creates a CurrencyAmount
// amount _must_ be raw, i.e. in the native representation
func NewCurrencyAmount(currency *Currency, amount *big.Int) (*CurrencyAmount, error) {
if err := utils.ValidateSolidityTypeInstance(amount, constants.Uint256); err != nil {
return nil, err
}
fraction := NewFraction(amount, big.NewInt(0).Exp(constants.Ten, big.NewInt(int64(currency.Decimals)), nil))
return &CurrencyAmount{
Fraction: fraction,
Currency: currency,
}, nil
}
// Raw returns Fraction's Numerator
func (c *CurrencyAmount) Raw() *big.Int {
return c.Numerator
}
// NewEther Helper that calls the constructor with the ETHER currency
// @param amount ether amount in wei
func NewEther(amount *big.Int) (*CurrencyAmount, error) {
return NewCurrencyAmount(ETHER, amount)
}