utils

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 9 Imported by: 3

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.
)
View Source
const MaxFeeInt = 1000000

Variables

View Source
var (
	ErrMulDivOverflow = errors.New("muldiv overflow")
	One               = big.NewInt(1)
)
View Source
var (
	ErrExceedMaxInt256 = errors.New("exceed max int256")
	ErrOverflowUint128 = errors.New("overflow uint128")
	ErrOverflowUint160 = errors.New("overflow uint160")

	Uint128Max = uint256.MustFromHex("0xffffffffffffffffffffffffffffffff")
	Uint160Max = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffff")
)
View Source
var (
	ErrSqrtPriceLessThanZero = errors.New("sqrt price less than zero")
	ErrLiquidityLessThanZero = errors.New("liquidity less than zero")
	ErrInvariant             = errors.New("invariant violation")
	ErrAddOverflow           = errors.New("add overflow")
)
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.

	Q32U256          = uint256.NewInt(1 << 32)
	MinSqrtRatioU256 = uint256.NewInt(4295128739)                                                   // The sqrt ratio corresponding to the minimum tick that could be used on any pool.
	MaxSqrtRatioU256 = uint256.MustFromDecimal("1461446703485210103287273052203988822378723970342") // 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 MaxFeeUint256 = uint256.NewInt(MaxFeeInt)
View Source
var MaxUint160 = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffff")
View Source
var (
	MaxUint256 = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
)

Functions

func AddDeltaInPlace

func AddDeltaInPlace(x *Uint128, y *Int128) error

x = x + y

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 *Uint160,
	liquidity *Uint128,
	amountRemaining *Int256,
	feePips constants.FeeAmount,

	sqrtRatioNextX96 *Uint160, amountIn, amountOut, feeAmount *Uint256,
) error

func DivRoundingUp

func DivRoundingUp(a, denominator, result *uint256.Int)

Returns ceil(x / y)

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

deprecated

func GetAmount0DeltaV2

func GetAmount0DeltaV2(sqrtRatioAX96, sqrtRatioBX96 *Uint160, liquidity *Uint128, roundUp bool, result *Uint256) error

func GetAmount1Delta

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

deprecated

func GetAmount1DeltaV2

func GetAmount1DeltaV2(sqrtRatioAX96, sqrtRatioBX96 *Uint160, liquidity *Uint128, roundUp bool, result *Uint256) error

func GetNextSqrtPriceFromInput

func GetNextSqrtPriceFromInput(sqrtPX96 *Uint160, liquidity *Uint128, amountIn *uint256.Int, zeroForOne bool, result *Uint160) error

func GetNextSqrtPriceFromOutput

func GetNextSqrtPriceFromOutput(sqrtPX96 *Uint160, liquidity *Uint128, amountOut *uint256.Int, zeroForOne bool, result *Uint160) error

func GetSqrtRatioAtTick

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

deprecated

func GetSqrtRatioAtTickV2

func GetSqrtRatioAtTickV2(tick int, result *Uint160) 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)

deprecated

func GetTickAtSqrtRatioV2

func GetTickAtSqrtRatioV2(sqrtRatioX96 *Uint160) (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 *uint256.Int) (uint, error)

func MulDiv

func MulDiv(a, b, denominator *uint256.Int) (*uint256.Int, error)

Calculates floor(a×b÷denominator) with full precision

func MulDivRoundingUp

func MulDivRoundingUp(a, b, denominator *uint256.Int) (*uint256.Int, error)

Calculates ceil(a×b÷denominator) with full precision

func MulDivRoundingUpV2

func MulDivRoundingUpV2(a, b, denominator, result *uint256.Int) error

func MulDivV2

func MulDivV2(a, b, denominator, result, remainder *uint256.Int) error

result=floor(a×b÷denominator), remainder=a×b%denominator (pass remainder=nil if not required) (the main usage for `remainder` is to be used in `MulDivRoundingUpV2` to determine if we need to round up, so it won't have to call MulMod again)

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

func ToUInt256

func ToUInt256(value *Int256, result *Uint256) error

Types

type Int128

type Int128 = int256.Int

type Int256

type Int256 = int256.Int

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
}

type Uint128

type Uint128 = uint256.Int

type Uint160

type Uint160 = uint256.Int

type Uint256

type Uint256 = uint256.Int

Jump to

Keyboard shortcuts

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