Documentation
¶
Index ¶
- Variables
- func GetPairAddress(token1Addr, token2Addr common.Address) common.Address
- func InputOutputComparator(a, b InputOutput) int
- func SortedInsert(items []*Trade, add *Trade, maxSize int, comparator func(a, b *Trade) int) (sortedItems []*Trade, pop *Trade, err error)
- func TradeComparator(a, b *Trade) int
- type BestTradeOptions
- type Currency
- type CurrencyAmount
- 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 uint, opt ...number.Option) string
- func (f *Fraction) ToSignificant(significantDigits uint, opt ...number.Option) string
- type InputOutput
- type Pair
- func (p *Pair) ChainID() constants.ChainID
- func (p *Pair) GetAddress() common.Address
- func (p *Pair) GetInputAmount(outputAmount *TokenAmount) (*TokenAmount, *Pair, error)
- func (p *Pair) GetLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB *TokenAmount) (*TokenAmount, error)
- func (p *Pair) GetLiquidityValue(token *Token, totalSupply, liquidity *TokenAmount, feeOn bool, kLast *big.Int) (*TokenAmount, error)
- func (p *Pair) GetOutputAmount(inputAmount *TokenAmount) (*TokenAmount, *Pair, error)
- func (p *Pair) InvolvesToken(token *Token) bool
- func (p *Pair) PriceOf(token *Token) (*Price, error)
- func (p *Pair) Reserve0() *TokenAmount
- func (p *Pair) Reserve1() *TokenAmount
- func (p *Pair) ReserveOf(token *Token) (*TokenAmount, error)
- func (p *Pair) Token0() *Token
- func (p *Pair) Token0Price() *Price
- func (p *Pair) Token1() *Token
- func (p *Pair) Token1Price() *Price
- type PairAddressCache
- type Percent
- type Price
- func (p *Price) Adjusted() *Fraction
- func (p *Price) Invert() *Price
- func (p *Price) Multiply(other *Price) (*Price, error)
- func (p *Price) Quote(currencyAmount *CurrencyAmount) (*CurrencyAmount, error)
- func (p *Price) Raw() *Fraction
- func (p *Price) ToFixed(decimalPlaces uint, opt ...number.Option) string
- func (p *Price) ToSignificant(significantDigits uint, opt ...number.Option) string
- type Route
- type Token
- type TokenAmount
- type TokenAmounts
- type Tokens
- type Trade
- func BestTradeExactIn(pairs []*Pair, currencyAmountIn *TokenAmount, currencyOut *Token, ...) (sortedItems []*Trade, err error)
- func BestTradeExactOut(pairs []*Pair, currencyIn *Token, currencyAmountOut *TokenAmount, ...) (sortedItems []*Trade, err error)
- func ExactIn(route *Route, amountIn *TokenAmount) (*Trade, error)
- func ExactOut(route *Route, amountOut *TokenAmount) (*Trade, error)
- func NewTrade(route *Route, amount *TokenAmount, tradeType constants.TradeType) (*Trade, error)
Constants ¶
This section is empty.
Variables ¶
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") )
var ( // ErrInvalidLiquidity invalid liquidity ErrInvalidLiquidity = fmt.Errorf("invalid liquidity") // ErrInvalidKLast invalid kLast ErrInvalidKLast = fmt.Errorf("invalid kLast") )
var ( ErrInvalidPairs = fmt.Errorf("invalid pairs") ErrInvalidPairsChainIDs = fmt.Errorf("invalid pairs chainIDs") ErrInvalidInput = fmt.Errorf("invalid token input") ErrInvalidOutput = fmt.Errorf("invalid token output") ErrInvalidPath = fmt.Errorf("invalid pairs for path") )
var ( ErrDiffChainID = fmt.Errorf("diff chain id") ErrDiffToken = fmt.Errorf("diff token") ErrSameAddrss = fmt.Errorf("same address") WCFX = map[constants.ChainID]*Token{ constants.Mainnet: { Currency: _WCFXCurrency, ChainID: constants.Mainnet, Address: utils.ValidateAndParseMainnetAddress("cfx:acg158kvr8zanb1bs048ryb6rtrhr283ma70vz70tx"), }, } )
var ( ErrInvalidOption = fmt.Errorf("invalid maxHops") ErrInvalidRecursion = fmt.Errorf("invalid recursion") )
var (
CONFLUX, _ = newCurrency(constants.Decimals18, "CFX", "Conflux")
)
*
- The only instance of the base class `Currency`.
var ( // ErrInvalidCurrency diff currency error ErrInvalidCurrency = fmt.Errorf("diff currency") )
var (
ErrInvalidSlippageTolerance = fmt.Errorf("invalid slippage tolerance")
)
var ( // Percent100 percent 100 Percent100 = NewFraction(constants.B100, constants.One) )
var ( // ZeroFraction zero fraction instance ZeroFraction = NewFraction(constants.Zero, nil) )
Functions ¶
func InputOutputComparator ¶
func InputOutputComparator(a, b InputOutput) int
comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
func SortedInsert ¶
func SortedInsert(items []*Trade, add *Trade, maxSize int, comparator func(a, b *Trade) int) (sortedItems []*Trade, pop *Trade, err error)
given an array of items sorted by `comparator`, insert an item into its sort index and constrain the size to `maxSize` by removing the last item
func TradeComparator ¶
extension of the input output comparator that also considers other dimensions of the trade in ranking them
Types ¶
type BestTradeOptions ¶
type BestTradeOptions struct { // how many results to return MaxNumResults int // the maximum number of hops a trade should contain MaxHops int }
func NewDefaultBestTradeOptions ¶
func NewDefaultBestTradeOptions() *BestTradeOptions
func (*BestTradeOptions) ReduceHops ¶
func (o *BestTradeOptions) ReduceHops() *BestTradeOptions
type Currency ¶
Currency is any fungible financial instrument on Ethereum, including Ether and all ERC20 tokens.
type CurrencyAmount ¶
CurrencyAmount warps Fraction and Currency
func NewCFX ¶
func NewCFX(amount *big.Int) (*CurrencyAmount, error)
NewCFX Helper that calls the constructor with the CONFLUX currency @param amount ether amount in wei
func NewCurrencyAmount ¶
func NewCurrencyAmount(currency *Currency, amount *big.Int) (*CurrencyAmount, error)
NewCurrencyAmount creates a CurrencyAmount amount _must_ be raw, i.e. in the native representation
func (*CurrencyAmount) Raw ¶
func (c *CurrencyAmount) Raw() *big.Int
Raw returns Fraction's Numerator
type Fraction ¶
type Fraction struct { Numerator *big.Int Denominator *big.Int // contains filtered or unexported fields }
Fraction warps math franction
func (*Fraction) GreaterThan ¶
GreaterThan identifies whether the caller is greater than the other
type InputOutput ¶
type InputOutput interface { InputAmount() *TokenAmount OutputAmount() *TokenAmount }
minimal interface so the input output comparator may be shared across types
type Pair ¶
type Pair struct { LiquidityToken *Token // sorted tokens TokenAmounts }
Pair warps uniswap pair
func NewPair ¶
func NewPair(tokenAmountA, tokenAmountB *TokenAmount) (*Pair, error)
NewPair creates Pair
func (*Pair) GetAddress ¶
GetAddress returns a contract's address for a pair
func (*Pair) GetInputAmount ¶
func (p *Pair) GetInputAmount(outputAmount *TokenAmount) (*TokenAmount, *Pair, error)
GetInputAmount returns InputAmout and a Pair for the OutputAmount
func (*Pair) GetLiquidityMinted ¶
func (p *Pair) GetLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB *TokenAmount) (*TokenAmount, error)
GetLiquidityMinted returns liquidity minted TokenAmount
func (*Pair) GetLiquidityValue ¶
func (p *Pair) GetLiquidityValue(token *Token, totalSupply, liquidity *TokenAmount, feeOn bool, kLast *big.Int) (*TokenAmount, error)
GetLiquidityValue returns liquidity value TokenAmount
func (*Pair) GetOutputAmount ¶
func (p *Pair) GetOutputAmount(inputAmount *TokenAmount) (*TokenAmount, *Pair, error)
GetOutputAmount returns OutputAmount and a Pair for the InputAmout
func (*Pair) InvolvesToken ¶
InvolvesToken Returns true if the token is either token0 or token1 @param token to check
func (*Pair) PriceOf ¶
PriceOf Returns the price of the given token in terms of the other token in the pair. @param token token to return price of
func (*Pair) Reserve0 ¶
func (p *Pair) Reserve0() *TokenAmount
Reserve0 returns the first TokenAmount in the pair
func (*Pair) Reserve1 ¶
func (p *Pair) Reserve1() *TokenAmount
Reserve1 returns the last TokenAmount in the pair
func (*Pair) ReserveOf ¶
func (p *Pair) ReserveOf(token *Token) (*TokenAmount, error)
ReserveOf returns the TokenAmount that equals to the token
func (*Pair) Token0Price ¶
Token0Price Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
func (*Pair) Token1Price ¶
Token1Price Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
type PairAddressCache ¶
type PairAddressCache struct {
// contains filtered or unexported fields
}
PairAddressCache warps pair address cache
func (*PairAddressCache) GetAddress ¶
func (p *PairAddressCache) GetAddress(addressA, addressB common.Address) common.Address
GetAddress returns contract address addressA < addressB
type Percent ¶
type Percent struct {
*Fraction
}
Percent warps Fraction
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 NewPriceFromRoute ¶
func (*Price) Quote ¶
func (p *Price) Quote(currencyAmount *CurrencyAmount) (*CurrencyAmount, error)
performs floor division on overflow
type Route ¶
func (*Route) PathToAddress ¶
type Token ¶
*
- Represents an ERC20 token with a unique address and some metadata.
func NewCFXToken ¶
NewCFXToken creates a token that currency is CFX
type TokenAmount ¶
type TokenAmount struct { *CurrencyAmount Token *Token }
func NewTokenAmount ¶
func NewTokenAmount(token *Token, amount *big.Int) (*TokenAmount, error)
amount _must_ be raw, i.e. in the native representation
func (*TokenAmount) Add ¶
func (t *TokenAmount) Add(other *TokenAmount) (*TokenAmount, error)
func (*TokenAmount) Equals ¶
func (t *TokenAmount) Equals(other *TokenAmount) bool
func (*TokenAmount) Subtract ¶
func (t *TokenAmount) Subtract(other *TokenAmount) (*TokenAmount, error)
type TokenAmounts ¶
type TokenAmounts [2]*TokenAmount
TokenAmounts warps TokenAmount array
func NewTokenAmounts ¶
func NewTokenAmounts(tokenAmountA, tokenAmountB *TokenAmount) (TokenAmounts, error)
NewTokenAmounts creates a TokenAmount
type Trade ¶
type Trade struct { /** * The route of the trade, i.e. which pairs the trade goes through. */ Route *Route /** * The type of the trade, either exact in or exact out. */ TradeType constants.TradeType /** * The price expressed in terms of output amount/input amount. */ ExecutionPrice *Price /** * The mid price after the trade executes assuming no slippage. */ NextMidPrice *Price /** * The percent difference between the mid price before the trade and the trade execution price. */ PriceImpact *Percent // contains filtered or unexported fields }
Trade Represents a trade executed against a list of pairs. Does not account for slippage, i.e. trades that front run this trade and move the price.
func BestTradeExactIn ¶
func BestTradeExactIn( pairs []*Pair, currencyAmountIn *TokenAmount, currencyOut *Token, options *BestTradeOptions, currentPairs []*Pair, originalAmountIn *TokenAmount, bestTrades []*Trade, ) (sortedItems []*Trade, err error)
*
- Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
- amount to an output token, making at most `maxHops` hops.
- Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
- the amount in among multiple routes.
- @param pairs the pairs to consider in finding the best trade
- @param currencyAmountIn exact amount of input currency to spend
- @param currencyOut the desired currency out
- @param maxNumResults maximum number of results to return
- @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
- @param currentPairs used in recursion; the current list of pairs
- @param originalAmountIn used in recursion; the original value of the currencyAmountIn parameter
- @param bestTrades used in recursion; the current list of best trades
func BestTradeExactOut ¶
func BestTradeExactOut( pairs []*Pair, currencyIn *Token, currencyAmountOut *TokenAmount, options *BestTradeOptions, currentPairs []*Pair, originalAmountOut *TokenAmount, bestTrades []*Trade, ) (sortedItems []*Trade, err error)
*
- similar to the above method but instead targets a fixed output amount
- given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
- to an output token amount, making at most `maxHops` hops
- note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
- the amount in among multiple routes.
- @param pairs the pairs to consider in finding the best trade
- @param currencyIn the currency to spend
- @param currencyAmountOut the exact amount of currency out
- @param maxNumResults maximum number of results to return
- @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
- @param currentPairs used in recursion; the current list of pairs
- @param originalAmountOut used in recursion; the original value of the currencyAmountOut parameter
- @param bestTrades used in recursion; the current list of best trades
func ExactIn ¶
func ExactIn(route *Route, amountIn *TokenAmount) (*Trade, error)
*
- Constructs an exact in trade with the given amount in and route
- @param route route of the exact in trade
- @param amountIn the amount being passed in
func ExactOut ¶
func ExactOut(route *Route, amountOut *TokenAmount) (*Trade, error)
*
- Constructs an exact out trade with the given amount out and route
- @param route route of the exact out trade
- @param amountOut the amount returned by the trade
func (*Trade) InputAmount ¶
func (t *Trade) InputAmount() *TokenAmount
func (*Trade) MaximumAmountIn ¶
func (t *Trade) MaximumAmountIn(slippageTolerance *Percent) (*TokenAmount, error)
*
- Get the maximum amount in that can be spent via this trade for the given slippage tolerance
- @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
func (*Trade) MinimumAmountOut ¶
func (t *Trade) MinimumAmountOut(slippageTolerance *Percent) (*TokenAmount, error)
*
- Get the minimum amount that must be received from this trade for the given slippage tolerance
- @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
func (*Trade) OutputAmount ¶
func (t *Trade) OutputAmount() *TokenAmount