Help us shape the future of Go! Take the 2026 Go Developer Survey

liquidity

package
v1.23.21 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package liquidity implements AMM liquidity pools for the DEX VM.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInsufficientLiquidity = errors.New("insufficient liquidity")
	ErrPoolNotFound          = errors.New("pool not found")
	ErrInvalidAmount         = errors.New("invalid amount")
	ErrSlippageExceeded      = errors.New("slippage exceeded")
	ErrZeroLiquidity         = errors.New("zero liquidity not allowed")
	ErrPoolExists            = errors.New("pool already exists")
	ErrSameToken             = errors.New("cannot create pool with same token")
)

Functions

This section is empty.

Types

type LPPosition

type LPPosition struct {
	Owner      ids.ShortID `json:"owner"`
	PoolID     ids.ID      `json:"poolId"`
	Liquidity  *big.Int    `json:"liquidity"`           // LP tokens held
	Token0Owed *big.Int    `json:"token0Owed"`          // Unclaimed token0 fees
	Token1Owed *big.Int    `json:"token1Owed"`          // Unclaimed token1 fees
	TickLower  int32       `json:"tickLower,omitempty"` // For concentrated
	TickUpper  int32       `json:"tickUpper,omitempty"` // For concentrated
	CreatedAt  int64       `json:"createdAt"`
}

LPPosition represents a liquidity provider's position in a pool.

type Manager

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

Manager manages all liquidity pools.

func NewManager

func NewManager() *Manager

NewManager creates a new liquidity pool manager.

func (*Manager) AddLiquidity

func (m *Manager) AddLiquidity(
	poolID ids.ID,
	amount0 *big.Int,
	amount1 *big.Int,
	minLiquidity *big.Int,
) (*big.Int, error)

AddLiquidity adds liquidity to a pool.

func (*Manager) CreatePool

func (m *Manager) CreatePool(
	token0, token1 ids.ID,
	initialAmount0, initialAmount1 *big.Int,
	poolType PoolType,
	feeBps uint16,
) (*Pool, error)

CreatePool creates a new liquidity pool.

func (*Manager) GetAllPools

func (m *Manager) GetAllPools() []*Pool

GetAllPools returns all pools.

func (*Manager) GetPool

func (m *Manager) GetPool(poolID ids.ID) (*Pool, error)

GetPool returns a pool by ID.

func (*Manager) GetPoolByPair

func (m *Manager) GetPoolByPair(token0, token1 ids.ID) (*Pool, error)

GetPoolByPair returns a pool by token pair.

func (*Manager) GetPoolsByTokenPair

func (m *Manager) GetPoolsByTokenPair(token0, token1 ids.ID) []*Pool

GetPoolsByTokenPair returns all pools for a given token pair.

func (*Manager) GetQuote

func (m *Manager) GetQuote(poolID ids.ID, tokenIn ids.ID, amountIn *big.Int) (*big.Int, error)

GetQuote returns the expected output for a swap.

func (*Manager) RemoveLiquidity

func (m *Manager) RemoveLiquidity(
	poolID ids.ID,
	liquidity *big.Int,
	minAmount0 *big.Int,
	minAmount1 *big.Int,
) (*big.Int, *big.Int, error)

RemoveLiquidity removes liquidity from a pool.

func (*Manager) Swap

func (m *Manager) Swap(
	poolID ids.ID,
	tokenIn ids.ID,
	amountIn *big.Int,
	minAmountOut *big.Int,
) (*SwapResult, error)

Swap executes a swap on a pool.

type Pool

type Pool struct {
	ID          ids.ID   `json:"id"`
	Token0      ids.ID   `json:"token0"`   // First token in the pair
	Token1      ids.ID   `json:"token1"`   // Second token in the pair
	Reserve0    *big.Int `json:"reserve0"` // Reserve of token0
	Reserve1    *big.Int `json:"reserve1"` // Reserve of token1
	Type        PoolType `json:"type"`
	FeeBps      uint16   `json:"feeBps"`      // Trading fee in basis points
	TotalSupply *big.Int `json:"totalSupply"` // Total LP tokens

	// Concentrated liquidity parameters (for Concentrated type)
	TickLower    int32    `json:"tickLower,omitempty"`
	TickUpper    int32    `json:"tickUpper,omitempty"`
	SqrtPriceX96 *big.Int `json:"sqrtPriceX96,omitempty"`

	// Statistics
	Volume0 *big.Int `json:"volume0"` // Cumulative volume in token0
	Volume1 *big.Int `json:"volume1"` // Cumulative volume in token1
	Fees0   *big.Int `json:"fees0"`   // Cumulative fees in token0
	Fees1   *big.Int `json:"fees1"`   // Cumulative fees in token1
	TxCount uint64   `json:"txCount"` // Total transaction count

	CreatedAt int64 `json:"createdAt"`
	UpdatedAt int64 `json:"updatedAt"`
}

Pool represents an AMM liquidity pool.

type PoolType

type PoolType uint8

PoolType represents the type of AMM pool.

const (
	ConstantProduct PoolType = iota // x * y = k (Uniswap V2 style)
	StableSwap                      // Low slippage for stable pairs
	Concentrated                    // Concentrated liquidity (Uniswap V3 style)
)

func (PoolType) String

func (t PoolType) String() string

type SwapResult

type SwapResult struct {
	AmountIn    *big.Int `json:"amountIn"`
	AmountOut   *big.Int `json:"amountOut"`
	Fee         *big.Int `json:"fee"`
	PriceImpact uint64   `json:"priceImpact"` // In basis points
	NewReserve0 *big.Int `json:"newReserve0"`
	NewReserve1 *big.Int `json:"newReserve1"`
}

SwapResult contains the result of a swap operation.

Jump to

Keyboard shortcuts

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