core

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxPoolPathLength = 3
)
View Source
const (
	PRECISION uint32 = 32
)

Variables

View Source
var (
	ERR_INVALID_POOL       = errors.New("err_invalid_pool")
	ERR_NO_POOL            = errors.New("err_no_pool")
	ERR_NO_LP              = errors.New("err_no_lp")
	ERR_NO_PATH            = errors.New("err_no_path")
	ERR_INVALID_AMOUNT     = errors.New("err_invalid_amount")
	ERR_OUT_OF_RANGE       = errors.New("err_out_of_range")
	ERR_INVALID_TOKEN      = errors.New("err_invalid_token")
	ERR_INVALID_NUMBER     = errors.New("err_invalid_number")
	ERR_NO_IMPLEMENT       = errors.New("err_no_implement")
	ERR_INVALID_PATH       = errors.New("err_invalid_path")
	ERR_INVALID_SWAPOUTS   = errors.New("err_invalid_swapouts")
	ERR_INVALID_POOL_PATHS = errors.New("err_invalid_pool_paths")
	ERR_INVALID_SWAP_USER  = errors.New("err_invalid_swap_user")
	ERR_INVALID_PATH_FEE   = errors.New("err_invalid_path_fee")
	ERR_FEE_TOO_SMALL      = errors.New("err_fee_too_small")
	// error for lp
	ERR_INVALID_PRICE           = errors.New("err_invalid_price")
	ERR_INVALID_LIQUIDITY       = errors.New("err_invalid_liquidity")
	ERR_INVALID_PRICE_DIRECTION = errors.New("err_invalid_price_direction")
	ERR_INVALID_FEE             = errors.New("err_invalid_fee")
)

Functions

func AmountXFromAmountY

func AmountXFromAmountY(lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal, amountY string) (amountX string, err error)

func AmountYFromAmountX

func AmountYFromAmountX(lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal, amountX string) (amountY string, err error)

func BigDotIntToDecimal

func BigDotIntToDecimal(b *big.Int) (*apd.Decimal, error)

func DecimalToBigDotInt

func DecimalToBigDotInt(d *apd.Decimal, isRoundUp bool) (*big.Int, error)

func GetLpID

func GetLpID(poolID, address string, lowSqrtPrice, highSqrtPrice *apd.Decimal, priceDirection string) string

func GetPoolCurrentPrice

func GetPoolCurrentPrice(pool *schema.Pool, priceDirection string) (string, error)

func GetPoolCurrentPrice2

func GetPoolCurrentPrice2(pool *schema.Pool, priceDirection string) (string, error)

func GetPoolID

func GetPoolID(tokenXTag, tokenYTag string, feeRatio *apd.Decimal) string

func GetPoolLp

func GetPoolLp(pool *schema.Pool, lpID string) (*schema.Lp, bool)

func GetPoolLps

func GetPoolLps(pool *schema.Pool, excludedLpIDs []string) []*schema.Lp

func GetPoolLps2

func GetPoolLps2(pool *schema.Pool) []schema.Lp

func LiquidityFromAmount

func LiquidityFromAmount(amountX, amountY string) (string, error)

func LiquidityFromAmountX

func LiquidityFromAmountX(lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal, amountX string) (liquidity string, err error)

func LiquidityFromAmountY

func LiquidityFromAmountY(lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal, amountY string) (liquidity string, err error)

func LiquidityToAmount

func LiquidityToAmount(liquidity string, lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal, priceDirection string) (amountX, amountY string, err error)

func LiquidityToAmount2

func LiquidityToAmount2(liquidity string, lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal) (amountX, amountY string, err error)

deprecated

func LpCurrentPrice

func LpCurrentPrice(lp schema.Lp) string

func LpHighPrice

func LpHighPrice(lp schema.Lp) string

func LpLowPrice

func LpLowPrice(lp schema.Lp) string

func LpSwap

func LpSwap(lp *schema.Lp, tokenIn, tokenOut string, amountIn *big.Int, isDryRun bool) (*schema.SwapOutput, error)

func LpVerifySwap

func LpVerifySwap(lp *schema.Lp, tokenIn, tokenOut string, amountIn, amountOut *big.Int) (bool, error)

func NewLp

func NewLp(poolID, tokenXTag, tokenYTag, address string,
	feeRatio, lowSqrtPrice, currentSqrtPrice, highSqrtPrice *apd.Decimal,
	liquidity, priceDirection string) (*schema.Lp, error)

func NewPool

func NewPool(tokenXTag, tokenYTag, feeRatio string) (*schema.Pool, error)

func PathsToSwapInputs

func PathsToSwapInputs(user string, paths []schema.Path) (map[string]*schema.SwapInput, error)

func PoolAddLiquidity

func PoolAddLiquidity(pool *schema.Pool, lp *schema.Lp) error

func PoolRemoveLiquidity

func PoolRemoveLiquidity(pool *schema.Pool, lpID string)

func PoolSwap

func PoolSwap(pool *schema.Pool, tokenIn, tokenOut string, amountIn *big.Int, excludedLpIDs []string, isDryRun bool) ([]schema.SwapOutput, error)

func PoolsSwap

func PoolsSwap(poolPaths []*schema.Pool, tokenIn, tokenOut string, amountIn *big.Int, excludedLpIDs []string) ([]schema.SwapOutput, *big.Int, error)

PoolsSwap is not for update, use it for query

func QuotientGreaterThan

func QuotientGreaterThan(dividend, divisor *apd.Decimal, minimum string) bool

func SqrtPrice

func SqrtPrice(price string) (*apd.Decimal, error)

func SqrtPriceToPrice

func SqrtPriceToPrice(sqrtPrice apd.Decimal) (string, error)

func SqrtPriceToPriceWithFee

func SqrtPriceToPriceWithFee(sqrtPrice, feeRatio apd.Decimal, priceDirection string) (string, error)

func StringToDecimal

func StringToDecimal(s string) (*apd.Decimal, error)

func SwapAmount

func SwapAmount(startSqrtPrice, endSqrtPrice *apd.Decimal, liquidity *big.Int) (amountIn, amountOut *big.Int, amountInDecimal, amountOutDecimal *apd.Decimal, err error)

func SwapOut

func SwapOut(startSqrtPrice *apd.Decimal, liquidity, amountIn *big.Int, tokenInIsX bool) (amountOut *big.Int, endSqrtPrice *apd.Decimal, err error)

func SwapOutputsToPaths

func SwapOutputsToPaths(user string, core *Core, swapOutputs []schema.SwapOutput) ([]schema.Path, error)

func TokenXYForPool

func TokenXYForPool(tokenA, tokenB string) (string, string)

Types

type Core

type Core struct {
	FeeRecepient      string                  // router fee recepient address
	FeeRatio          *apd.Decimal            // router fee ratio
	Pools             map[string]*schema.Pool // pool id -> Pool
	Lps               map[string]*schema.Lp   // lp id -> Lp
	AddressToLpIDs    map[string][]string     // address -> []lpID
	MaxPoolPathLength int
	TokenTagToPoolIDs map[string][]string // tokentag -> []lpID
}

func New

func New(pools map[string]*schema.Pool, routerFeeRecepient, routerFeeRatio string) *Core

func (*Core) AddLiquidity

func (c *Core) AddLiquidity(address string, msg routerSchema.LpMsgAdd) error

func (*Core) AddLiquidityByLp

func (c *Core) AddLiquidityByLp(lp *schema.Lp) error

func (*Core) FindPool

func (c *Core) FindPool(tokenX, tokenY string, feeRatio *apd.Decimal) (*schema.Pool, error)

func (*Core) FindPoolPaths

func (c *Core) FindPoolPaths(tokenIn, tokenOut string) ([][]*schema.Pool, error)

func (*Core) GetAllLps

func (c *Core) GetAllLps() (lps []schema.Lp)

func (*Core) GetLps

func (c *Core) GetLps(address string) (lps []schema.Lp)

func (*Core) GetPoolCurrentPrice

func (c *Core) GetPoolCurrentPrice(poolID, priceDirection string) (string, error)

func (*Core) GetPoolCurrentPrice2

func (c *Core) GetPoolCurrentPrice2(poolID, priceDirection string) (string, error)

func (*Core) Query

func (c *Core) Query(msg routerSchema.UserMsgQuery) ([]schema.Path, error)

func (*Core) RemoveLiquidity

func (c *Core) RemoveLiquidity(address string, msg routerSchema.LpMsgRemove) error

func (*Core) RemoveLiquidityByAddress

func (c *Core) RemoveLiquidityByAddress(lpAddr string) (err error)

func (*Core) RemoveLiquidityByID

func (c *Core) RemoveLiquidityByID(lpID string) (lp *schema.Lp, err error)

func (*Core) Update

func (c *Core) Update(userAddr string, paths []schema.Path) error

Update is only required for all transactions of paths (if they exist)

func (*Core) Verify

func (c *Core) Verify(userAddr string, paths []schema.Path) error

Verify requires strict verification and should return an error if it is not the LP's own transaction

type DecimalSlice

type DecimalSlice []*apd.Decimal

func (DecimalSlice) Len

func (p DecimalSlice) Len() int

func (DecimalSlice) Less

func (p DecimalSlice) Less(i, j int) bool

func (DecimalSlice) Swap

func (p DecimalSlice) Swap(i, j int)

type Tick

type Tick struct {
	SqrtPrice *apd.Decimal
	Liquidity *big.Int // net liquidity
}

func GetPoolTicks

func GetPoolTicks(pool *schema.Pool, priceDirection string, excludedLpIDs []string) ([]Tick, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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