Documentation ¶
Index ¶
- Variables
- type BaseCurrency
- func (c *BaseCurrency) ChainId() uint
- func (c *BaseCurrency) Decimals() uint
- func (c *BaseCurrency) Equal(other Currency) bool
- func (c *BaseCurrency) IsNative() bool
- func (c *BaseCurrency) IsToken() bool
- func (c *BaseCurrency) MarshalJSON() ([]byte, error)
- func (c *BaseCurrency) Name() string
- func (c *BaseCurrency) Symbol() string
- func (c *BaseCurrency) UnmarshalJSON(data []byte) error
- func (c *BaseCurrency) Wrapped() *Token
- type Currency
- type CurrencyAmount
- func (ca *CurrencyAmount) Add(other *CurrencyAmount) *CurrencyAmount
- func (ca *CurrencyAmount) Divide(other *Fraction) *CurrencyAmount
- func (ca *CurrencyAmount) Multiply(other *Fraction) *CurrencyAmount
- func (ca *CurrencyAmount) Subtract(other *CurrencyAmount) *CurrencyAmount
- func (ca *CurrencyAmount) ToExact() string
- func (ca *CurrencyAmount) ToExactRaw() decimal.Decimal
- func (ca *CurrencyAmount) ToFixed(decimalPlaces int32) string
- func (ca *CurrencyAmount) ToSignificant(significantDigits int32) string
- func (c *CurrencyAmount) UnmarshalJSON(data []byte) error
- func (ca *CurrencyAmount) Wrapped() *CurrencyAmount
- type Ether
- type Fraction
- func (f *Fraction) Add(other *Fraction) *Fraction
- func (f *Fraction) Divide(other *Fraction) *Fraction
- func (f *Fraction) EqualTo(other *Fraction) bool
- func (f *Fraction) GreaterThan(other *Fraction) bool
- func (f *Fraction) Invert() *Fraction
- func (f *Fraction) LessThan(other *Fraction) bool
- func (f *Fraction) Multiply(other *Fraction) *Fraction
- func (f *Fraction) Quotient() *big.Int
- func (f *Fraction) Remainder() *Fraction
- func (f *Fraction) Subtract(other *Fraction) *Fraction
- func (f *Fraction) ToFixed(decimalPlaces int32) string
- func (f *Fraction) ToSignificant(significantDigits int32) string
- type Native
- type Percent
- func (p *Percent) Add(other *Percent) *Percent
- func (p *Percent) Divide(other *Percent) *Percent
- func (p *Percent) Multiply(other *Percent) *Percent
- func (p *Percent) Subtract(other *Percent) *Percent
- func (p *Percent) ToFixed(decimalPlaces int32) string
- func (p *Percent) ToSignificant(significantDigits int32) string
- type Price
- type Rounding
- type Token
- type TradeType
Constants ¶
This section is empty.
Variables ¶
var ( ErrDifferentChain = errors.New("different chain") ErrSameAddress = errors.New("same address") )
var (
ErrDifferentCurrencies = errors.New("different currencies")
)
var MaxUint256, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
var OneHundred = NewFraction(big.NewInt(100), big.NewInt(1))
var USDC = map[uint]*Token{ 1: NewToken(1, common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), 6, "USDC", "USDC Coin"), }
Known USDC implementation addresses
var USDT = map[uint]*Token{ 1: NewToken(1, common.HexToAddress("0xdac17f958d2ee523a2206206994597c13d831ec7"), 6, "USDT", "USDT Coin"), }
Known USDT implementation addresses
var WETH9 = map[uint]*Token{ 1: NewToken(1, common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), 18, "WETH", "Wrapped Ether"), 3: NewToken(3, common.HexToAddress("0xc778417E063141139Fce010982780140Aa0cD5Ab"), 18, "WETH", "Wrapped Ether"), 4: NewToken(4, common.HexToAddress("0xc778417E063141139Fce010982780140Aa0cD5Ab"), 18, "WETH", "Wrapped Ether"), 5: NewToken(5, common.HexToAddress("0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"), 18, "WETH", "Wrapped Ether"), 42: NewToken(42, common.HexToAddress("0xd0A1E359811322d97991E03f863a0C30C2cF029C"), 18, "WETH", "Wrapped Ether"), 10: NewToken(10, common.HexToAddress("0x4200000000000000000000000000000000000006"), 18, "WETH", "Wrapped Ether"), 69: NewToken(69, common.HexToAddress("0x4200000000000000000000000000000000000006"), 18, "WETH", "Wrapped Ether"), 42161: NewToken(42161, common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"), 18, "WETH", "Wrapped Ether"), 421611: NewToken(421611, common.HexToAddress("0xB47e6A5f8b33b3F17603C83a0535A9dcD7E32681"), 18, "WETH", "Wrapped Ether"), }
Known WETH9 implementation addresses, used in our implementation of Ether#wrapped
Functions ¶
This section is empty.
Types ¶
type BaseCurrency ¶
type BaseCurrency struct {
// contains filtered or unexported fields
}
BaseCurrency is an abstract struct, do not use it directly
func (*BaseCurrency) ChainId ¶
func (c *BaseCurrency) ChainId() uint
func (*BaseCurrency) Decimals ¶
func (c *BaseCurrency) Decimals() uint
func (*BaseCurrency) Equal ¶
func (c *BaseCurrency) Equal(other Currency) bool
Equal returns whether the currency is equal to the other currency
func (*BaseCurrency) IsNative ¶
func (c *BaseCurrency) IsNative() bool
func (*BaseCurrency) IsToken ¶
func (c *BaseCurrency) IsToken() bool
func (*BaseCurrency) MarshalJSON ¶
func (c *BaseCurrency) MarshalJSON() ([]byte, error)
MarshalJSON custom method to marshal BaseCurrency to JSON.
func (*BaseCurrency) Name ¶
func (c *BaseCurrency) Name() string
func (*BaseCurrency) Symbol ¶
func (c *BaseCurrency) Symbol() string
func (*BaseCurrency) UnmarshalJSON ¶
func (c *BaseCurrency) UnmarshalJSON(data []byte) error
func (*BaseCurrency) Wrapped ¶
func (c *BaseCurrency) Wrapped() *Token
type Currency ¶
type Currency interface { IsNative() bool IsToken() bool ChainId() uint Decimals() uint Symbol() string Name() string Equal(other Currency) bool Wrapped() *Token }
Currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
type CurrencyAmount ¶
type CurrencyAmount struct { *Fraction `json:"fraction"` Currency Currency `json:"currency"` DecimalScale *big.Int `json:"decimalScale"` }
func FromFractionalAmount ¶
func FromFractionalAmount(currency Currency, numerator *big.Int, denominator *big.Int) *CurrencyAmount
*
- Construct a currency amount with a denominator that is not equal to 1
- @param currency the currency
- @param numerator the numerator of the fractional token amount
- @param denominator the denominator of the fractional token amount
func FromRawAmount ¶
func FromRawAmount(currency Currency, rawAmount *big.Int) *CurrencyAmount
*
- Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
- @param currency the currency in the amount
- @param rawAmount the raw token or ether amount
func (*CurrencyAmount) Add ¶
func (ca *CurrencyAmount) Add(other *CurrencyAmount) *CurrencyAmount
Add adds two currency amounts together
func (*CurrencyAmount) Divide ¶
func (ca *CurrencyAmount) Divide(other *Fraction) *CurrencyAmount
Divide divides one currency amount by another
func (*CurrencyAmount) Multiply ¶
func (ca *CurrencyAmount) Multiply(other *Fraction) *CurrencyAmount
Multiply multiplies two currency amounts
func (*CurrencyAmount) Subtract ¶
func (ca *CurrencyAmount) Subtract(other *CurrencyAmount) *CurrencyAmount
Subtract subtracts one currency amount from another
func (*CurrencyAmount) ToExact ¶
func (ca *CurrencyAmount) ToExact() string
ToExact returns the currency amount as a string with the specified number of digits after the decimal
func (*CurrencyAmount) ToExactRaw ¶
func (ca *CurrencyAmount) ToExactRaw() decimal.Decimal
ToExact returns the currency amount as a string with the specified number of digits after the decimal
func (*CurrencyAmount) ToFixed ¶
func (ca *CurrencyAmount) ToFixed(decimalPlaces int32) string
ToFixed returns the currency amount as a string with the specified number of digits after the decimal
func (*CurrencyAmount) ToSignificant ¶
func (ca *CurrencyAmount) ToSignificant(significantDigits int32) string
ToSignificant returns the currency amount as a string with the most significant digits
func (*CurrencyAmount) UnmarshalJSON ¶
func (c *CurrencyAmount) UnmarshalJSON(data []byte) error
func (*CurrencyAmount) Wrapped ¶
func (ca *CurrencyAmount) Wrapped() *CurrencyAmount
type Ether ¶
type Ether struct {
*BaseCurrency
}
Ether is the main usage of a 'native' currency, i.e. for Ethereum mainnet and all testnets
func EtherOnChain ¶
type Fraction ¶
type Fraction struct { Numerator *big.Int `json:"numerator"` Denominator *big.Int `json:"denominator"` }
func NewFraction ¶
NewFraction creates a new fraction
func (*Fraction) GreaterThan ¶
GreaterThan returns true if the fraction is greater than the other fraction
func (*Fraction) ToSignificant ¶
ToSignificant returns a significant string representation of the fraction Example: NewFraction(big.NewInt(125), big.NewInt(1)).ToSignificant(2) // output: "130"
type Native ¶
type Native struct { *BaseCurrency // contains filtered or unexported fields }
type Percent ¶
type Percent struct {
*Fraction
}
func NewPercent ¶
NewPercent creates a new Percent
func (*Percent) ToFixed ¶
ToFixedFigures converts a Percent to a string with a given number of fixed figures
func (*Percent) ToSignificant ¶
ToSignificant converts a Percent to a string with a given number of significant figures
type Price ¶
type Price struct { *Fraction BaseCurrency Currency // input i.e. denominator QuoteCurrency Currency // output i.e. numerator Scalar *Fraction // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token }
func (*Price) Multiply ¶
Multiply Multiplies the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency
func (*Price) Quote ¶
func (p *Price) Quote(currencyAmount *CurrencyAmount) (*CurrencyAmount, error)
Quote returns the amount of quote currency corresponding to a given amount of the base currency
func (*Price) ToSignificant ¶
type Token ¶
type Token struct { *BaseCurrency Address common.Address `json:"address"` // The contract address on the chain on which this token lives }
Token represents an ERC20 token with a unique address and some metadata.
func NewToken ¶
func NewToken(chainID uint, address common.Address, decimals uint, symbol string, name string) *Token
NewToken creates a new token with the given currency and address.
func (*Token) Equal ¶
Equal *
- Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
- @param other token to compare
func (*Token) SortsBefore ¶
SortsBefore *
- 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