tx

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: 10 Imported by: 0

Documentation

Overview

Package tx builds Elements transactions for Anchor AMM operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeIssuanceEntropy

func ComputeIssuanceEntropy(txidHex string, vout uint32) ([]byte, error)

ComputeIssuanceEntropy computes the issuance entropy for a UTXO outpoint.

func ComputeLPAssetID

func ComputeLPAssetID(txidHex string, vout uint32) ([32]byte, error)

ComputeLPAssetID computes the LP asset ID from the creation UTXO outpoint. Elements deterministic issuance with 32-zero contract hash.

Types

type AddLiquidityParams

type AddLiquidityParams struct {
	State    *pool.State
	Deposit0 uint64 // satoshis of Asset0 to add
	Deposit1 uint64 // satoshis of Asset1 to add
	// Pool output addresses (from pool.json)
	PoolAAddr     string
	PoolBAddr     string
	LpReserveAddr string
	// User inputs: one or more UTXOs per asset. Change is returned if total > needed.
	Asset0Inputs []UserInput
	Asset1Inputs []UserInput
	LBTCInputs   []UserInput
	ChangeAddr   string // address to receive any surplus from user inputs
	UserLPAddr   string // address to receive LP tokens
	// Asset IDs
	LPAssetID string
	Asset0    string
	Asset1    string
	LBTCAsset string
	Fee       uint64
	// Pool swap/add-variant binaries, CMRs, and control blocks
	PoolABinaryHex        string
	PoolBBinaryHex        string
	LpReserveBinaryHex    string
	PoolACMRHex           string
	PoolBCMRHex           string
	LpReserveCMRHex       string
	PoolAControlBlock     string
	PoolBControlBlock     string
	LpReserveControlBlock string
}

AddLiquidityParams holds inputs for an add-liquidity transaction.

type AddLiquidityResult

type AddLiquidityResult struct {
	TxHex            string
	LPMinted         uint64
	PoolAWitness     [][]byte // attach to input[0] after wallet signing
	PoolBWitness     [][]byte // attach to input[1] after wallet signing
	LpReserveWitness [][]byte // attach to input[2] after wallet signing
}

AddLiquidityResult holds the completed add-liquidity transaction.

func BuildAddLiquidity

func BuildAddLiquidity(params *AddLiquidityParams) (*AddLiquidityResult, error)

BuildAddLiquidity builds an add-liquidity transaction.

Input layout:

[0] pool_a   [1] pool_b   [2] lp_reserve
[3] user_asset0   [4] user_asset1   [5] user_lbtc (fee)

Output layout:

[0] new_pool_a   [1] new_pool_b   [2] new_lp_reserve
[3] LP tokens → UserLPAddr
[4..] change outputs   [last] fee

Pool witnesses are returned in AddLiquidityResult and must be attached to inputs[0,1,2] AFTER signing with the wallet.

type CreatePoolParams

type CreatePoolParams struct {
	// pool_creation UTXO — provides LP issuance anchor + fee
	CreationTxID   string
	CreationVout   uint32
	CreationAmount uint64 // L-BTC sats in the creation UTXO

	// Deposit UTXOs — user wallet UTXOs providing the initial reserves
	Asset0TxID   string
	Asset0Vout   uint32
	Asset0Amount uint64 // total amount in this UTXO (deposit0 + any change)

	Asset1TxID   string
	Asset1Vout   uint32
	Asset1Amount uint64 // total amount in this UTXO (deposit1 + any change)

	BuildDir  string // path to directory containing .shl/.args files
	Deposit0  uint64 // satoshis of Asset0 to lock in pool_a
	Deposit1  uint64 // satoshis of Asset1 to lock in pool_b
	Asset0    string // hex asset ID (display format)
	Asset1    string
	LBTCAsset string // L-BTC asset ID
	Fee       uint64 // satoshis fee (paid from creation UTXO)
	FeeNum    uint64 // protocol fee numerator (for OP_RETURN announcement)
	FeeDen    uint64 // protocol fee denominator (for OP_RETURN announcement)
	Announce  bool   // include OP_RETURN pool announcement output

	ChangeAddr string // address to receive Asset0/Asset1/L-BTC change
	Network    *network.Network
}

CreatePoolParams holds inputs for the create-pool transaction.

type CreatePoolResult

type CreatePoolResult struct {
	TxHex             string
	LPAssetID         string // 32-byte hex
	LPMinted          uint64
	PoolConfig        *pool.Config
	SimplicityWitness [][]byte // witness for input[0]; attach after wallet signing
}

CreatePoolResult holds the completed create-pool outputs.

func BuildCreatePool

func BuildCreatePool(params *CreatePoolParams, cfg *pool.Config) (*CreatePoolResult, error)

BuildCreatePool builds the create-pool transaction.

Transaction structure:

Inputs:  [0] pool_creation UTXO (Simplicity spend, LP issuance attached)
         [1] Asset0 UTXO (wallet-signed)
         [2] Asset1 UTXO (wallet-signed)
Outputs: [0] pool_a              ← deposit0 of Asset0
         [1] pool_b              ← deposit1 of Asset1
         [2] lp_reserve          ← (LP_PREMINT - lp_minted) LP tokens
         [3] LP tokens           → change address (lp_minted, from issuance)
         [4] Asset0 change (if any)
         [5] Asset1 change (if any)
         [6] L-BTC change (if any)
         [7] OP_RETURN announcement (if Announce)
         [last] fee (explicit, empty script)

type RemoveLiquidityParams

type RemoveLiquidityParams struct {
	State        *pool.State
	LPBurned     uint64 // LP tokens to return to reserve
	UserLPAmount uint64 // full LP UTXO balance (input[3] total); change returned to user
	// Pool output addresses
	PoolAAddr     string
	PoolBAddr     string
	LpReserveAddr string
	// User's LP token UTXO (input[3] = LP_RETURN_INPUT in contracts)
	UserLPTxID string
	UserLPVout uint32
	// User's L-BTC UTXO for fee (input[4])
	UserLBTCTxID   string
	UserLBTCVout   uint32
	UserLBTCAmount uint64 // actual UTXO amount; change returned if > Fee
	// Where to send payouts
	UserAsset0Addr string // receives payout0 of Asset0
	UserAsset1Addr string // receives payout1 of Asset1
	ChangeAddr     string // receives LP change and L-BTC change
	// Asset IDs
	Asset0    string
	Asset1    string
	LPAssetID string
	LBTCAsset string
	Fee       uint64
	// Pool remove-variant binaries, CMRs, and control blocks
	PoolABinaryHex        string
	PoolBBinaryHex        string
	LpReserveBinaryHex    string
	PoolACMRHex           string
	PoolBCMRHex           string
	LpReserveCMRHex       string
	PoolAControlBlock     string
	PoolBControlBlock     string
	LpReserveControlBlock string
}

RemoveLiquidityParams holds inputs for a remove-liquidity transaction.

type RemoveLiquidityResult

type RemoveLiquidityResult struct {
	TxHex            string
	Payout0          uint64   // Asset0 paid to user
	Payout1          uint64   // Asset1 paid to user
	PoolAWitness     [][]byte // attach to input[0] after wallet signing
	PoolBWitness     [][]byte // attach to input[1] after wallet signing
	LpReserveWitness [][]byte // attach to input[2] after wallet signing
}

RemoveLiquidityResult holds the completed remove-liquidity transaction.

func BuildRemoveLiquidity

func BuildRemoveLiquidity(params *RemoveLiquidityParams) (*RemoveLiquidityResult, error)

BuildRemoveLiquidity builds a remove-liquidity transaction.

Input layout: [pool_a(0), pool_b(1), lp_reserve(2), user_lp(3), user_lbtc(4)] Output layout: [new_pool_a(0), new_pool_b(1), new_lp_reserve(2),

payout0(3), payout1(4),
lp_change→user(5, if any), lbtc_change→user(6, if any),
fee(last)]

LP tokens are returned to the LP Reserve (not destroyed). The reserve increases by LPBurned. If UserLPAmount > LPBurned, the remainder is returned as LP change.

The user must provide an L-BTC UTXO (input[4]) to cover the on-chain fee. Any surplus L-BTC is returned as change.

Pool witnesses are returned separately and must be attached AFTER wallet signing.

type SwapParams

type SwapParams struct {
	// Pool state
	State *pool.State
	// Which direction: true = Asset0→Asset1, false = Asset1→Asset0
	SwapAsset0In bool
	// Amount in (satoshis)
	AmountIn uint64
	// Minimum acceptable output (slippage guard, checked off-chain only)
	MinAmountOut uint64
	// User's input UTXO (provides AmountIn of the input asset)
	UserTxID        string
	UserVout        uint32
	UserAsset       string
	UserInputAmount uint64 // actual UTXO amount; change returned if > AmountIn (or > AmountIn+Fee when input is L-BTC)
	// Where to send output asset and change
	UserOutputAddr string
	ChangeAddr     string // receives input asset change and L-BTC change (if any)
	// Pool output addresses (from pool.json)
	PoolAAddr string
	PoolBAddr string
	// Asset IDs
	Asset0    string
	Asset1    string
	LBTCAsset string
	Fee       uint64
	// Fee parameters — must match what was compiled into the contracts.
	// Read from pool.json (Config.FeeNum / Config.FeeDen).
	FeeNum uint64
	FeeDen uint64
	// Optional: separate L-BTC UTXO for fee (when input asset != L-BTC)
	// If not set, fee is deducted from the user's input UTXO (input asset must be L-BTC).
	FeeTxID   string
	FeeVout   uint32
	FeeAmount uint64 // actual fee UTXO amount; L-BTC change returned if > Fee
	// Pool swap-variant binaries, CMRs, and control blocks
	PoolABinaryHex    string
	PoolBBinaryHex    string
	PoolACMRHex       string
	PoolBCMRHex       string
	PoolAControlBlock string
	PoolBControlBlock string
}

SwapParams holds inputs for a swap transaction.

type SwapResult

type SwapResult struct {
	TxHex        string
	AmountOut    uint64
	PoolAWitness [][]byte // attach to input[0] after wallet signing
	PoolBWitness [][]byte // attach to input[1] after wallet signing
}

SwapResult holds the completed swap transaction outputs.

func BuildSwap

func BuildSwap(params *SwapParams) (*SwapResult, error)

BuildSwap builds a swap transaction.

Input layout: [pool_a(0), pool_b(1), user_input(2), fee_input(3, optional)] Output layout: [new_pool_a(0), new_pool_b(1), user_output(2),

input_change(3, if any), lbtc_change(4, if any), fee(last)]

When the input asset is L-BTC, the user UTXO covers both AmountIn and Fee. When the input asset is NOT L-BTC, a separate fee UTXO (FeeTxID) must be provided.

Pool witnesses are returned separately and must be attached AFTER wallet signing.

type UserInput

type UserInput struct {
	TxID   string
	Vout   uint32
	Amount uint64 // satoshis
}

UserInput holds a single wallet UTXO to spend.

Jump to

Keyboard shortcuts

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