uniswap_core

package module
v0.0.0-...-6ed0faf Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: MIT Imports: 6 Imported by: 0

README

uniswap_core


Go package for simulation of swap, mint, burn and collect assets operations according to Uniswap V3 protocol.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var A [19]*big.Int
View Source
var B [19]*big.Int
View Source
var C [7]*big.Int
View Source
var INIT0 *big.Int
View Source
var INIT1 *big.Int
View Source
var LOWER_ERR_BOUND *big.Int
View Source
var MAX_SQRT_RATIO *big.Int

The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)

View Source
var MAX_TICK *big.Int

The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128

View Source
var MAX_UINT_160 *big.Int
View Source
var MAX_UINT_256 *big.Int
View Source
var MIN_SQRT_RATIO *big.Int

The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)

View Source
var MIN_TICK *big.Int

The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128

View Source
var ONE_UINT_256 *big.Int
View Source
var SQRT_10001 *big.Int
View Source
var UPPER_ERR_BOUND *big.Int
View Source
var ZERO_UINT_256 *big.Int

Functions

func AddLiquidityDelta

func AddLiquidityDelta(x *big.Int, y *big.Int) (z *big.Int)

func ComputeSwapStep

func ComputeSwapStep(
	sqrtRatioCurrentX96 *big.Int,
	sqrtRatioTargetX96 *big.Int,
	liquidity *big.Int,
	amountRemaining *big.Int,
	feePips *big.Int) (sqrtRatioNextX96 *big.Int, amountIn *big.Int, amountOut *big.Int, feeAmount *big.Int)

func DoSwap

func DoSwap(zeroForOne bool,
	amountSpecified *big.Int,
	sqrtPriceLimitX96 *big.Int,
	ticker TickReader,
	slotReader PoolStateReader) (amount0 *big.Int, amount1 *big.Int, feeTotal *big.Int)

Swap token0 for token1, or token1 for token0 zeroForOne bool The direction of the swap, true for token0 to token1, false for token1 to token0 amountSpecified big.Int The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative) sqrtPriceLimitX96 big.Int The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this ticker TickReader tick bitmap object slotReader PoolStateReader Pool's state retriever object amount0 big.Int The delta of the balance of token0 of the pool, exact when negative, minimum when positive amount1 big.Int The delta of the balance of token1 of the pool, exact when negative, minimum when positive

func GetAmount0Delta

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

func GetAmount0DeltaRoundingUp

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

Calculates liquidity / sqrt(lower) - liquidity / sqrt(upper), i.e. liquidity * (sqrt(upper) - sqrt(lower)) / (sqrt(upper) * sqrt(lower)) http://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

func GetAmount1Delta

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

func GetAmount1DeltaRoundingUp

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

Calculates liquidity * (sqrt(upper) - sqrt(lower)) http://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

func GetMaxValue

func GetMaxValue(bitsCount uint) *big.Int

func GetNextSqrtPriceFromInput

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

func GetNextSqrtPriceFromOutput

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

func GetSqrtRatioAtTick

func GetSqrtRatioAtTick(tick *big.Int) *big.Int

Ported function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) Source: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/TickMath.sol tick int24 return int96 sqrtPriceX96

func GetTickAtSqrtRatio

func GetTickAtSqrtRatio(sqrtPriceX96 *big.Int) *big.Int

Ported function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick) Source: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/TickMath.sol sqrtPriceX96 int96 return tick int24

func MulDiv

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

func MulDivChn deprecated

func MulDivChn(
	a *big.Int,
	b *big.Int,
	denominator *big.Int) (result *big.Int)

Deprecated: Obtain [a * b / denominator] Source: https://xn--2-umb.com/21/muldiv/index.html Origin: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/FullMath.sol

func MulDivRoundingUp

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

Origin: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/FullMath.sol

Types

type BigDecimal

type BigDecimal struct {
	Val *big.Float
}

func (*BigDecimal) UnmarshalJSON

func (bi *BigDecimal) UnmarshalJSON(data []byte) error

type BigInt

type BigInt struct {
	Val *big.Int
}

func (*BigInt) UnmarshalJSON

func (bi *BigInt) UnmarshalJSON(data []byte) error

type FieldId

type FieldId struct {
	Id string
}

type Pool

type Pool struct {
	Id                           string
	CreatedAtTimestamp           BigInt
	CreatedAtBlockNumber         BigInt
	Token0                       Token
	Token1                       Token
	FeeTier                      BigInt
	Liquidity                    BigInt
	SqrtPrice                    BigInt
	FeeGrowthGlobal0X128         BigInt
	FeeGrowthGlobal1X128         BigInt
	Token0Price                  BigDecimal
	Token1Price                  BigDecimal
	Tick                         BigInt
	ObservationIndex             BigInt
	VolumeToken0                 BigDecimal
	VolumeToken1                 BigDecimal
	VolumeUSD                    BigDecimal
	UntrackedVolumeUSD           BigDecimal
	FeesUSD                      BigDecimal
	TxCount                      BigInt
	CollectedFeesToken0          BigDecimal
	CollectedFeesToken1          BigDecimal
	CollectedFeesUSD             BigDecimal
	TotalValueLockedToken0       BigDecimal
	TotalValueLockedToken1       BigDecimal
	TotalValueLockedETH          BigDecimal
	TotalValueLockedUSD          BigDecimal
	TotalValueLockedUSDUntracked BigDecimal
	LiquidityProviderCount       BigInt
	Swaps                        []Swap
	Ticks                        []Tick
}

func GetPool

func GetPool(client *graphql.Client, poolId string) (*Pool, error)

func (Pool) CurrentState

func (p Pool) CurrentState() *Slot0

func (Pool) FeerTierToTickSpacing

func (p Pool) FeerTierToTickSpacing() *big.Int

type PoolStateReader

type PoolStateReader interface {
	CurrentState() *Slot0
}

type Slot0

type Slot0 struct {
	TickSpacing          *big.Int
	Fee                  *big.Int
	Liquidity            *big.Int
	FeeGrowthGlobal0X128 *big.Int
	FeeGrowthGlobal1X128 *big.Int
	// the current price
	SqrtPriceX96 *big.Int
	// the current tick
	TickCurrent *big.Int
	// the most-recently updated index of the observations array
	ObservationIndex *big.Int
	// the current maximum number of observations that are being stored
	ObservationCardinality *big.Int
	// the next maximum number of observations to store, triggered in observations.write
	ObservationCardinalityNext *big.Int
	// the current protocol fee as a percentage of the swap fee taken on withdrawal
	// represented as an integer denominator (1/x)%
	FeeProtocol *big.Int
}

func NewSlot0

func NewSlot0() *Slot0

type StepComputations

type StepComputations struct {
	// contains filtered or unexported fields
}

func NewStepComputations

func NewStepComputations() *StepComputations

func (*StepComputations) ApplyTickLimits

func (step *StepComputations) ApplyTickLimits()

func (*StepComputations) CalcSqrtPriceNextX96

func (step *StepComputations) CalcSqrtPriceNextX96()

func (*StepComputations) GetSqrtRatioTargetX96

func (step *StepComputations) GetSqrtRatioTargetX96(
	zeroForOne bool, sqrtPriceLimitX96 *big.Int) (sqrtRatioTargetX96 *big.Int)

func (*StepComputations) UpdateAmount

func (step *StepComputations) UpdateAmount(
	zeroForOne bool, sqrtPriceLimitX96 *big.Int, state *SwapState, slot0 *Slot0) (sqrtPriceX96 *big.Int)

func (*StepComputations) UpdateSqrtPriceStartX96

func (step *StepComputations) UpdateSqrtPriceStartX96(state *SwapState)

func (*StepComputations) UpdateTickNext

func (step *StepComputations) UpdateTickNext(zeroForOne bool, state *SwapState, ticker TickReader)

type Swap

type Swap struct {
	Id           string
	Transaction  Tx
	Timestamp    BigInt
	Pool         FieldId
	Token0       Token
	Token1       Token
	Sender       string
	Recipient    string
	Origin       string
	Amount0      BigDecimal
	Amount1      BigDecimal
	AmountUSD    BigDecimal
	SqrtPriceX96 BigInt
	Tick         BigInt
	LogIndex     BigInt
}

func GetSwap

func GetSwap(client *graphql.Client, swapId string) (*Swap, error)

type SwapCache

type SwapCache struct {
	// contains filtered or unexported fields
}

func NewSwapCache

func NewSwapCache(zeroForOne bool, slot0 *Slot0) *SwapCache

type SwapState

type SwapState struct {
	// contains filtered or unexported fields
}

the top level state of the swap, the results of which are recorded in storage at the end

func NewSwapState

func NewSwapState(amountSpecified *big.Int, slot0 *Slot0, cache *SwapCache) *SwapState

func (*SwapState) UpdateAmount

func (state *SwapState) UpdateAmount(exactInput bool, step *StepComputations)

func (*SwapState) UpdateTickLiquidity

func (state *SwapState) UpdateTickLiquidity(zeroForOne bool, step *StepComputations, ticker TickReader)

type Tick

type Tick struct {
	Id                     string
	PoolAddress            string
	TickIdx                BigInt
	Pool                   FieldId
	LiquidityGross         BigInt
	LiquidityNet           BigInt
	Price0                 BigDecimal
	Price1                 BigDecimal
	VolumeToken0           BigDecimal
	VolumeToken1           BigDecimal
	VolumeUSD              BigDecimal
	UntrackedVolumeUSD     BigDecimal
	FeesUSD                BigDecimal
	CollectedFeesToken0    BigDecimal
	CollectedFeesToken1    BigDecimal
	CollectedFeesUSD       BigDecimal
	CreatedAtTimestamp     BigInt
	CreatedAtBlockNumber   BigInt
	LiquidityProviderCount BigInt
	FeeGrowthOutside0X128  BigInt
	FeeGrowthOutside1X128  BigInt
}

func GetTicks

func GetTicks(client *graphql.Client, poolId string) ([]Tick, error)

func (Tick) IsInitialized

func (t Tick) IsInitialized() bool

type TickReader

type TickReader interface {
	NextInitializedTick(tick *big.Int, zeroForOne bool) (*big.Int, bool)
	GetLiquidityNet(tick *big.Int) *big.Int
}

type TickStorage

type TickStorage struct {
	Ticks       map[int64]*Tick
	TickSpacing *big.Int
}

func NewTickStorage

func NewTickStorage(ticks []Tick, tickSpacing *big.Int) *TickStorage

func (TickStorage) GetLiquidityNet

func (t TickStorage) GetLiquidityNet(tick *big.Int) *big.Int

func (TickStorage) IsInitialized

func (t TickStorage) IsInitialized(tickKey *big.Int) bool

func (TickStorage) NextInitializedTick

func (t TickStorage) NextInitializedTick(tick *big.Int, zeroForOne bool) (*big.Int, bool)

type Token

type Token struct {
	Id                           string
	Symbol                       string
	Name                         string
	Decimals                     BigInt
	TotalSupply                  BigInt
	Volume                       BigDecimal
	VolumeUSD                    BigDecimal
	UntrackedVolumeUSD           BigDecimal
	FeesUSD                      BigDecimal
	TxCount                      BigInt
	PoolCount                    BigInt
	TotalValueLocked             BigDecimal
	TotalValueLockedUSD          BigDecimal
	TotalValueLockedUSDUntracked BigDecimal
	DerivedETH                   BigDecimal
}

type Tx

type Tx struct {
	Id          string
	BlockNumber BigInt
	Timestamp   BigInt
	GasUsed     BigInt
	GasPrice    BigInt
}

Jump to

Keyboard shortcuts

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