pool

package
v0.0.0-...-581a61f Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package pool manages pool configuration and on-chain state queries.

Index

Constants

View Source
const LPPremint uint64 = 2_000_000_000_000_000

LPPremint is the total pre-minted LP token supply per pool. Elements consensus sums ALL explicit output values (cross-asset) and rejects transactions where the total exceeds MAX_MONEY (2.1e15). We reserve 1e14 (~$10M headroom) for pool deposits, change, and fees in the create-pool transaction where the full premint appears.

Variables

This section is empty.

Functions

func IntSqrt

func IntSqrt(n uint64) uint64

IntSqrt computes floor(sqrt(n)) using integer arithmetic.

func LPMintedForDeposit

func LPMintedForDeposit(deposit0, _, reserve0, _, totalSupply uint64) uint64

LPMintedForDeposit computes LP tokens to mint for add-liquidity. The lp_reserve_add contract validates: lp_minted == floor(deposit0 * supply / reserve0). deposit1 is validated separately by the contract's proportionality check.

func RemovePayouts

func RemovePayouts(lpBurned, reserve0, reserve1, totalSupply uint64) (payout0, payout1 uint64)

RemovePayouts computes proportional payouts for remove-liquidity.

func SwapOutput

func SwapOutput(amountIn, reserveIn, reserveOut, feeNum, feeDen uint64) uint64

SwapOutput computes the maximum output amount for a fee-adjusted swap. Formula: out = reserveOut * amountIn * feeNum / (reserveIn * feeDen + amountIn * feeNum) This is the fee-adjusted constant-product invariant used by the Simplicity contracts. If feeNum or feeDen is zero (e.g. legacy pool.json without fee fields), falls back to the no-fee formula (feeNum = feeDen = 1), which is the standard constant-product k formula. Uses big.Int to avoid overflow for mainnet-scale reserves.

Types

type Config

type Config struct {
	PoolCreation ContractInfo `json:"pool_creation"`

	// Pool A: dual-leaf taproot (swap + remove leaves share one address)
	PoolA       ContractInfo `json:"pool_a"`
	PoolASwap   PoolVariant  `json:"pool_a_swap"`
	PoolARemove PoolVariant  `json:"pool_a_remove"`

	// Pool B: dual-leaf taproot
	PoolB       ContractInfo `json:"pool_b"`
	PoolBSwap   PoolVariant  `json:"pool_b_swap"`
	PoolBRemove PoolVariant  `json:"pool_b_remove"`

	// LP Reserve: dual-leaf taproot (add + remove leaves share one address)
	LpReserve       ContractInfo `json:"lp_reserve"`
	LpReserveAdd    PoolVariant  `json:"lp_reserve_add"`
	LpReserveRemove PoolVariant  `json:"lp_reserve_remove"`

	// Asset IDs and fee parameters stored after compile/create-pool.
	// FeeNum/FeeDen are baked into the compiled contracts (.args at compile time)
	// and must be stored here so swap/add/remove operations use the correct formula.
	Asset0    string `json:"asset0,omitempty"`
	Asset1    string `json:"asset1,omitempty"`
	LPAssetID string `json:"lp_asset_id,omitempty"`
	FeeNum    uint64 `json:"fee_num,omitempty"`
	FeeDen    uint64 `json:"fee_den,omitempty"`
}

Config holds all pool contract addresses, binaries, and asset IDs.

func Load

func Load(path string) (*Config, error)

Load reads pool.json from path.

func (*Config) Save

func (c *Config) Save(path string) error

Save writes pool.json to path (pretty-printed).

type ContractInfo

type ContractInfo struct {
	Address   string `json:"address"`
	CMR       string `json:"cmr"`        // 32-byte Simplicity CMR hex (taproot leaf content)
	BinaryHex string `json:"binary_hex"` // hex-encoded Simplicity program
}

ContractInfo holds compiled contract data for one pool script.

type PoolVariant

type PoolVariant struct {
	CMR          string `json:"cmr"`
	BinaryHex    string `json:"binary_hex"`
	ControlBlock string `json:"control_block"` // hex-encoded taproot control block
}

PoolVariant holds the CMR, binary, and control block for one mode-specific pool script (e.g., swap-only or remove-only). The address is shared with the parent ContractInfo (dual-leaf taproot).

type State

type State struct {
	Reserve0      uint64 // pool_a asset0 satoshis
	Reserve1      uint64 // pool_b asset1 satoshis
	LPReserve     uint64 // lp_reserve LP token amount
	PoolATxID     string
	PoolAVout     uint32
	PoolBTxID     string
	PoolBVout     uint32
	LpReserveTxID string
	LpReserveVout uint32
}

State holds live on-chain pool reserves.

func Query

func Query(cfg *Config, client *rpc.Client) (*State, error)

Query fetches live pool state from the chain.

func (*State) TotalSupply

func (s *State) TotalSupply() uint64

TotalSupply derives the circulating LP token supply from the reserve.

Jump to

Keyboard shortcuts

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