utils

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinTick = -887272  // The minimum tick that can be used on any pool.
	MaxTick = -MinTick // The maximum tick that can be used on any pool.
)

Variables

View Source
var (
	ErrSqrtPriceLessThanZero = errors.New("sqrt price less than zero")
	ErrLiquidityLessThanZero = errors.New("liquidity less than zero")
	ErrInvariant             = errors.New("invariant violation")
)
View Source
var (
	Q32             = big.NewInt(1 << 32)
	MinSqrtRatio    = big.NewInt(4295128739)                                                          // The sqrt ratio corresponding to the minimum tick that could be used on any pool.
	MaxSqrtRatio, _ = new(big.Int).SetString("1461446703485210103287273052203988822378723970342", 10) // The sqrt ratio corresponding to the maximum tick that could be used on any pool.
)
View Source
var (
	ErrInvalidTick      = errors.New("invalid tick")
	ErrInvalidSqrtRatio = errors.New("invalid sqrt ratio")
)
View Source
var ErrInvalidInput = errors.New("invalid input")
View Source
var FeeUnits = new(big.Int).Exp(big.NewInt(10), big.NewInt(5), nil)
View Source
var MaxUint160 = new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(160), nil), constants.One)
View Source
var TwoFeeUnits = new(big.Int).Mul(FeeUnits, big.NewInt(2))

Functions

func AddDelta

func AddDelta(x, y *big.Int) *big.Int

func ComputePoolAddress

func ComputePoolAddress(factoryAddress common.Address, tokenA *entities.Token, tokenB *entities.Token, fee constants.FeeAmount, initCodeHashManualOverride string) (common.Address, error)

*

  • Computes a pool address
  • @param factoryAddress The Uniswap V3 factory address
  • @param tokenA The first token of the pair, irrespective of sort order
  • @param tokenB The second token of the pair, irrespective of sort order
  • @param fee The fee tier of the pool
  • @returns The pool address

func ComputeSwapStep

func ComputeSwapStep(
	sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, amountRemaining *big.Int, feeInUnits constants.FeeAmount,
	exactIn, isToken0 bool,
) (sqrtRatioNextX96, amountIn, amountOut, deltaL *big.Int, err error)

ComputeSwapStep computes the actual swap input / output amounts to be deducted or added, the swap fee to be collected and the resulting sqrtP

func EncodeSqrtRatioX96

func EncodeSqrtRatioX96(amount1 *big.Int, amount0 *big.Int) *big.Int

*

  • Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0
  • @param amount1 The numerator amount i.e., the amount of token1
  • @param amount0 The denominator amount i.e., the amount of token0
  • @returns The sqrt ratio

func GetAmount0Delta

func GetAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity *big.Int, roundUp bool) *big.Int

func GetAmount1Delta

func GetAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity *big.Int, roundUp bool) *big.Int

func GetNextSqrtPriceFromInput

func GetNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn *big.Int, zeroForOne bool) (*big.Int, error)

func GetNextSqrtPriceFromOutput

func GetNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut *big.Int, zeroForOne bool) (*big.Int, error)

func GetSmallerRootOfQuadEqn

func GetSmallerRootOfQuadEqn(a, b, c *big.Int) *big.Int

func GetSqrtRatioAtTick

func GetSqrtRatioAtTick(tick int) (*big.Int, error)

*

  • Returns the sqrt ratio as a Q64.96 for the given tick. The sqrt ratio is computed as sqrt(1.0001)^tick
  • @param tick the tick for which to compute the sqrt ratio

func GetTickAtSqrtRatio

func GetTickAtSqrtRatio(sqrtRatioX96 *big.Int) (int, error)

*

  • Returns the tick corresponding to a given sqrt ratio, s.t. #getSqrtRatioAtTick(tick) <= sqrtRatioX96
  • and #getSqrtRatioAtTick(tick + 1) > sqrtRatioX96
  • @param sqrtRatioX96 the sqrt ratio as a Q64.96 for which to compute the tick

func MaxLiquidityForAmounts

func MaxLiquidityForAmounts(sqrtRatioCurrentX96 *big.Int, sqrtRatioAX96, sqrtRatioBX96 *big.Int, amount0, amount1 *big.Int, useFullPrecision bool) *big.Int

*

  • Computes the maximum amount of liquidity received for a given amount of token0, token1,
  • and the prices at the tick boundaries.
  • @param sqrtRatioCurrentX96 the current price
  • @param sqrtRatioAX96 price at lower boundary
  • @param sqrtRatioBX96 price at upper boundary
  • @param amount0 token0 amount
  • @param amount1 token1 amount
  • @param useFullPrecision if false, liquidity will be maximized according to what the router can calculate,
  • not what core can theoretically support

func MostSignificantBit

func MostSignificantBit(x *big.Int) (int64, error)

func MulDiv

func MulDiv(a, b, denominator *big.Int) *big.Int

func MulDivRoundingDown added in v0.1.5

func MulDivRoundingDown(a, b, denominator *big.Int) *big.Int

func MulDivRoundingUp

func MulDivRoundingUp(a, b, denominator *big.Int) *big.Int

func PriceToClosestTick

func PriceToClosestTick(price *entities.Price, baseToken, quoteToken *entities.Token) (int, error)

*

  • Returns the first tick for which the given price is greater than or equal to the tick price
  • @param price for which to return the closest tick that represents a price less than or equal to the input price,
  • i.e. the price of the returned tick is less than or equal to the input price

func TickToPrice

func TickToPrice(baseToken *entities.Token, quoteToken *entities.Token, tick int) (*entities.Price, error)

*

  • Returns a price object corresponding to the input tick and the base/quote token
  • Inputs must be tokens because the address order is used to interpret the price represented by the tick
  • @param baseToken the base token of the price
  • @param quoteToken the quote token of the price
  • @param tick the tick for which to return the price

func ToHex

func ToHex(i *big.Int) string

*

  • Converts a big int to a hex string
  • @param bigintIsh
  • @returns The hex encoded calldata

Types

type MethodParameters

type MethodParameters struct {
	Calldata []byte   // The hex encoded calldata to perform the given operation
	Value    *big.Int // The amount of ether (wei) to send in hex
}

Jump to

Keyboard shortcuts

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