Documentation
¶
Index ¶
- Variables
- func GetAddress(tokenA, tokenB *entities.Token, factory common.Address, initHash []byte) (common.Address, error)
- 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 CurrencyAmounts
- type InputOutput
- type Pair
- func (p *Pair) ChainID() uint
- func (p *Pair) GetAddress() common.Address
- func (p *Pair) GetInputAmount(outputAmount *entities.CurrencyAmount) (*entities.CurrencyAmount, *Pair, error)
- func (p *Pair) GetLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB *entities.CurrencyAmount) (*entities.CurrencyAmount, error)
- func (p *Pair) GetLiquidityValue(token *entities.Token, totalSupply, liquidity *entities.CurrencyAmount, ...) (*entities.CurrencyAmount, error)
- func (p *Pair) GetOutputAmount(inputAmount *entities.CurrencyAmount) (*entities.CurrencyAmount, *Pair, error)
- func (p *Pair) InvolvesToken(token *entities.Token) bool
- func (p *Pair) PriceOf(token *entities.Token) (*entities.Price, error)
- func (p *Pair) Reserve0() *entities.CurrencyAmount
- func (p *Pair) Reserve1() *entities.CurrencyAmount
- func (p *Pair) ReserveOf(token *entities.Token) (*entities.CurrencyAmount, error)
- func (p *Pair) Token0() *entities.Token
- func (p *Pair) Token0Price() *entities.Price
- func (p *Pair) Token1() *entities.Token
- func (p *Pair) Token1Price() *entities.Price
- type PairOptions
- type Route
- type Tokens
- type Trade
- func BestTradeExactIn(pairs []*Pair, currencyAmountIn *entities.CurrencyAmount, ...) (sortedItems []*Trade, err error)
- func BestTradeExactOut(pairs []*Pair, currencyIn entities.Currency, ...) (sortedItems []*Trade, err error)
- func ExactIn(route *Route, amountIn *core.CurrencyAmount) (*Trade, error)
- func ExactOut(route *Route, amountOut *core.CurrencyAmount) (*Trade, error)
- func NewTrade(route *Route, amount *core.CurrencyAmount, tradeType TradeType) (*Trade, error)
- type TradeType
Constants ¶
This section is empty.
Variables ¶
var ( FactoryAddress = common.HexToAddress("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f") InitCodeHash = common.FromHex("0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f") MinimumLiquidity = big.NewInt(1000) Zero = big.NewInt(0) One = big.NewInt(1) Five = big.NewInt(5) B997 = big.NewInt(997) B1000 = big.NewInt(1000) )
var ( // ErrInvalidLiquidity invalid liquidity ErrInvalidLiquidity = errors.New("invalid liquidity") // ErrInvalidKLast invalid kLast ErrInvalidKLast = errors.New("invalid kLast") ErrDiffToken = errors.New("diff token") ErrInsufficientReserves = errors.New("doesn't have insufficient reserves") ErrInsufficientInputAmount = errors.New("the input amount insufficient reserves") )
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 ( ErrInvalidSlippageTolerance = fmt.Errorf("invalid slippage tolerance") ErrInvalidCurrency = fmt.Errorf("diff currency") ZeroFraction = core.NewFraction(Zero, One) )
var ( ErrInvalidOption = fmt.Errorf("invalid maxHops") ErrInvalidRecursion = fmt.Errorf("invalid recursion") )
Functions ¶
func GetAddress ¶
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 CurrencyAmounts ¶
type CurrencyAmounts [2]*entities.CurrencyAmount
func NewCurrencyAmounts ¶
func NewCurrencyAmounts(amountA, amountB *entities.CurrencyAmount) (CurrencyAmounts, error)
NewCurrencyAmounts creates a CurrencyAmounts
type InputOutput ¶
type InputOutput interface { InputAmount() *entities.CurrencyAmount OutputAmount() *entities.CurrencyAmount }
minimal interface so the input output comparator may be shared across types
type Pair ¶
type Pair struct { LiquidityToken *entities.Token TokenAmounts CurrencyAmounts // sorted tokens Address common.Address // Pair address Options *PairOptions }
Pair warps uniswap pair
func NewPair ¶
func NewPair(amountA, amountB *entities.CurrencyAmount, options *PairOptions) (*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 *entities.CurrencyAmount) (*entities.CurrencyAmount, *Pair, error)
GetInputAmount returns InputAmout and a Pair for the OutputAmount
func (*Pair) GetLiquidityMinted ¶
func (p *Pair) GetLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB *entities.CurrencyAmount) (*entities.CurrencyAmount, error)
GetLiquidityMinted returns liquidity minted CurrencyAmount
func (*Pair) GetLiquidityValue ¶
func (p *Pair) GetLiquidityValue(token *entities.Token, totalSupply, liquidity *entities.CurrencyAmount, feeOn bool, kLast *big.Int) (*entities.CurrencyAmount, error)
GetLiquidityValue returns liquidity value CurrencyAmount
func (*Pair) GetOutputAmount ¶
func (p *Pair) GetOutputAmount(inputAmount *entities.CurrencyAmount) (*entities.CurrencyAmount, *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() *entities.CurrencyAmount
Reserve0 returns the first CurrencyAmount in the pair
func (*Pair) Reserve1 ¶
func (p *Pair) Reserve1() *entities.CurrencyAmount
Reserve1 returns the last CurrencyAmount in the pair
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 PairOptions ¶
type PairOptions struct { Factory common.Address // Uniswap factory address InitCodeHash []byte // Chain init code Address *common.Address // Pair address if already known. Leave empty if not }
PairOptions for generating pair address
type Route ¶
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 TradeType /** * The price expressed in terms of output amount/input amount. */ ExecutionPrice *core.Price /** * The mid price after the trade executes assuming no slippage. */ NextMidPrice *core.Price /** * The percent difference between the mid price before the trade and the trade execution price. */ PriceImpact *core.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 *entities.CurrencyAmount, currencyOut entities.Currency, options *BestTradeOptions, currentPairs []*Pair, nextAmountIn *entities.CurrencyAmount, 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 nextAmountIn 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 entities.Currency, currencyAmountOut *entities.CurrencyAmount, options *BestTradeOptions, currentPairs []*Pair, originalAmountOut *entities.CurrencyAmount, 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 *core.CurrencyAmount) (*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 *core.CurrencyAmount) (*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() *core.CurrencyAmount
func (*Trade) MaximumAmountIn ¶
*
- 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 ¶
*
- MinimumAmountOut - 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() *core.CurrencyAmount