entities

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2022 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
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)
)
View Source
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")
)
View Source
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")
)
View Source
var (
	ErrInvalidSlippageTolerance = fmt.Errorf("invalid slippage tolerance")
	ErrInvalidCurrency          = fmt.Errorf("diff currency")
	ZeroFraction                = core.NewFraction(Zero, One)
)
View Source
var (
	ErrInvalidOption    = fmt.Errorf("invalid maxHops")
	ErrInvalidRecursion = fmt.Errorf("invalid recursion")
)

Functions

func GetAddress

func GetAddress(tokenA, tokenB *entities.Token, factory common.Address, initHash []byte) (common.Address, error)

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

func TradeComparator(a, b *Trade) int

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) ChainID

func (p *Pair) ChainID() uint

ChainID Returns the chain ID of the tokens in the pair.

func (*Pair) GetAddress

func (p *Pair) GetAddress() common.Address

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

func (p *Pair) InvolvesToken(token *entities.Token) bool

InvolvesToken Returns true if the token is either token0 or token1 @param token to check

func (*Pair) PriceOf

func (p *Pair) PriceOf(token *entities.Token) (*entities.Price, error)

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) ReserveOf

func (p *Pair) ReserveOf(token *entities.Token) (*entities.CurrencyAmount, error)

ReserveOf returns the CurrencyAmount that equals to the token

func (*Pair) Token0

func (p *Pair) Token0() *entities.Token

Token0 returns the first token in the pair

func (*Pair) Token0Price

func (p *Pair) Token0Price() *entities.Price

Token0Price Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0

func (*Pair) Token1

func (p *Pair) Token1() *entities.Token

Token1 returns the last token in the pair

func (*Pair) Token1Price

func (p *Pair) Token1Price() *entities.Price

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 Route struct {
	Pairs  []*Pair
	Path   []*core.Token
	Input  core.Currency
	Output core.Currency
	// contains filtered or unexported fields
}

func NewRoute

func NewRoute(pairs []*Pair, input, output core.Currency) (*Route, error)

func (*Route) ChainID

func (r *Route) ChainID() uint

func (*Route) MidPrice

func (r *Route) MidPrice() (*core.Price, error)

type Tokens

type Tokens [2]*entities.Token

Tokens warps Token array

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 NewTrade

func NewTrade(route *Route, amount *core.CurrencyAmount, tradeType TradeType) (*Trade, error)

NewTrade creates a new trade nolint gocyclo

func (*Trade) InputAmount

func (t *Trade) InputAmount() *core.CurrencyAmount

func (*Trade) MaximumAmountIn

func (t *Trade) MaximumAmountIn(slippageTolerance *core.Percent) (*core.CurrencyAmount, 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 *core.Percent) (*core.CurrencyAmount, error)

*

  • 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

type TradeType

type TradeType int
const (
	ExactInput TradeType = iota
	ExactOutput
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL