Constructor called by the factory when deploying a new pool.
env - The contract environmentfactory - Address of the factory contract that created this pooltoken0 - Address of the first token in the pairtoken1 - Address of the second token in the pairfee - Fee tier for this pool (in basis points)tick_spacing - Minimum tick spacing for this poolflash_executor - Address of the authorized FlashExecutor contract (immutable)fn __constructor(
env: soroban_sdk::Env,
factory: soroban_sdk::Address,
token0: soroban_sdk::Address,
token1: soroban_sdk::Address,
fee: u32,
tick_spacing: i32,
flash_executor: soroban_sdk::Address,
)
Validates that tick range is properly ordered and within bounds.
env - The contract environmenttick_lower - Lower tick of the rangetick_upper - Upper tick of the rangeOk(()) if ticks are validErr if ticks are invalid (wrong order, out of bounds, or misaligned with tick_spacing)fn check_ticks(
env: soroban_sdk::Env,
tick_lower: i32,
tick_upper: i32,
) -> Result<(), soroban_sdk::Error>
Returns the current ledger timestamp.
env - The contract environmentCurrent timestamp in seconds
fn block_timestamp(env: soroban_sdk::Env) -> u64
Initializes the pool with a starting price.
Must be called before any liquidity operations. Can only be called once.
env - The contract environmentsqrt_price_x96 - Initial sqrt price in Q64.96 formatOk(()) on successErr(AlreadyInitialized) if pool is already initializedfn initialize(
env: soroban_sdk::Env,
sqrt_price_x96: soroban_sdk::U256,
) -> Result<(), soroban_sdk::Error>
Swaps tokens in the pool.
Transfers tokens from sender and sends output tokens to recipient.
env - The contract environmentsender - Address providing input tokens (requires auth)recipient - Address receiving output tokenszero_for_one - True if swapping token0 for token1, false otherwiseamount_specified - Amount to swap (positive for exact input, negative for exact output)sqrt_price_limit_x96 - Price limit for the swap in Q64.96 formatOk(SwapResult) containing amounts swapped and final priceErr if swap fails (locked, invalid price limit, etc.)fn swap(
env: soroban_sdk::Env,
sender: soroban_sdk::Address,
recipient: soroban_sdk::Address,
zero_for_one: bool,
amount_specified: i128,
sqrt_price_limit_x96: soroban_sdk::U256,
hints: OracleHints,
) -> Result
Swaps tokens using prefunded input already held by the pool.
Assumes input tokens have been transferred to the pool before calling. Only authorized routers can call this function. Verifies available delta via baseline accounting system before executing the swap.
env - The contract environmentrouter - Address of the authorized router (requires auth)recipient - Address receiving output tokenszero_for_one - True if swapping token0 for token1, false otherwiseamount_specified - Amount to swap (positive for exact input, negative for exact output)sqrt_price_limit_x96 - Price limit for the swap in Q64.96 formatOk(SwapResult) containing amounts swapped and final priceErr(Unauthorized) if router is not authorizedErr on other failures (locked, invalid price limit, insufficient balance)fn swap_prefunded(
env: soroban_sdk::Env,
router: soroban_sdk::Address,
recipient: soroban_sdk::Address,
zero_for_one: bool,
amount_specified: i128,
sqrt_price_limit_x96: soroban_sdk::U256,
hints: OracleHints,
) -> Result
Manages authorization for routers allowed to call swap_prefunded.
Only the pool's factory can call this function.
env - The contract environmentfactory - Address of the factory (requires auth, must match pool's factory)router - Address of the router to authorize/unauthorizeallowed - True to authorize, false to remove authorizationOk(()) on successErr(Unauthorized) if caller is not the pool's factoryfn set_router_authorized(
env: soroban_sdk::Env,
factory: soroban_sdk::Address,
router: soroban_sdk::Address,
allowed: bool,
) -> Result<(), soroban_sdk::Error>
Burns liquidity from a position.
Removes liquidity from the specified tick range and credits the owed
tokens to the position. Tokens must be collected separately via collect.
env - The contract environmentowner - Address owning the position (requires auth)tick_lower - Lower tick of the position rangetick_upper - Upper tick of the position rangeamount - Amount of liquidity to burnOk((amount0, amount1)) - Amounts of token0 and token1 owed to the positionErr(Locked) if pool is currently lockedErr on other failures (invalid ticks, position not found)fn burn(
env: soroban_sdk::Env,
owner: soroban_sdk::Address,
tick_lower: i32,
tick_upper: i32,
amount: u128,
hints: OracleHints,
) -> Result<(u128, u128), soroban_sdk::Error>
Collects fees accumulated by a liquidity position.
Transfers owed tokens to the recipient. Fees are computed when liquidity is added or removed via mint/burn, not when collecting.
env - The contract environmentrecipient - Address receiving the fees (requires auth, must be position owner)tick_lower - Lower tick of the position rangetick_upper - Upper tick of the position rangeamount0_requested - Maximum amount of token0 to collectamount1_requested - Maximum amount of token1 to collectOk((amount0, amount1)) - Actual amounts collected (min of requested vs owed)Err(Error) if position fee update failsfn collect(
env: soroban_sdk::Env,
owner: soroban_sdk::Address,
recipient: soroban_sdk::Address,
tick_lower: i32,
tick_upper: i32,
amount0_requested: u128,
amount1_requested: u128,
) -> Result<(u128, u128), soroban_sdk::Error>
Collects protocol fees accumulated by the pool.
Only the factory owner can call this function. Keeps a minimum of 1 token in the slot to save on storage gas costs.
env - The contract environmentrecipient - Address receiving the protocol fees (requires factory owner auth)amount0_requested - Maximum amount of token0 to collectamount1_requested - Maximum amount of token1 to collectOk((amount0, amount1)) - Actual amounts collectedErr(Error) if pool is not initialized or params are missingfn collect_protocol(
env: soroban_sdk::Env,
recipient: soroban_sdk::Address,
amount0_requested: u128,
amount1_requested: u128,
) -> Result<(u128, u128), soroban_sdk::Error>
Begin a flash loan
Only the authorized FlashExecutor can call this. Must be followed by flash_end() in the same transaction.
recipient - Address to receive borrowed tokensamount0 - Amount of token0 to borrowamount1 - Amount of token1 to borrowinitiator - Address of the flash loan initiator (must be FlashExecutor)Ok((fee0, fee1, oracle_hints)) - Fees that must be repaid and oracle hints for swapsfn flash_begin(
env: soroban_sdk::Env,
recipient: soroban_sdk::Address,
amount0: u128,
amount1: u128,
initiator: soroban_sdk::Address,
) -> Result<(u128, u128, OracleHints), soroban_sdk::Error>
End a flash loan
Must be called by the same initiator that called flash_begin(). Verifies repayment and clears the flash lock.
Ok(()) if repayment is sufficientErr(InsufficientRepayment0/1) if repayment is insufficientfn flash_end(env: soroban_sdk::Env) -> Result<(), soroban_sdk::Error>
Returns the current pool state.
env - The contract environmentSlot0 containing current sqrt price, tick, and lock status
fn slot0(env: soroban_sdk::Env) -> Slot0
Returns whether the pool has been initialized.
env - The contract environmenttrue if initialized, false otherwise
fn is_initialized(env: soroban_sdk::Env) -> bool
Returns the factory address that created this pool.
env - The contract environmentAddress of the factory contract
fn factory(env: soroban_sdk::Env) -> soroban_sdk::Address
Returns the address of token0 in the pair.
env - The contract environmentAddress of token0
fn token0(env: soroban_sdk::Env) -> soroban_sdk::Address
Returns the address of token1 in the pair.
env - The contract environmentAddress of token1
fn token1(env: soroban_sdk::Env) -> soroban_sdk::Address
Returns the pool's fee tier.
env - The contract environmentFee in basis points (e.g., 3000 = 0.3%)
fn fee(env: soroban_sdk::Env) -> u32
Returns the pool's tick spacing.
env - The contract environmentTick spacing (e.g., 60 means positions must be on multiples of 60)
fn tick_spacing(env: soroban_sdk::Env) -> i32
Returns the flash executor address for this pool.
The flash executor is immutable and set once during pool deployment. This is the only authorized contract that can initiate flash loans on this pool.
env - The contract environmentAddress of the FlashExecutor contract
fn flash_executor(env: soroban_sdk::Env) -> soroban_sdk::Address
Returns the protocol fee for zero_for_one swaps from the factory.
env - The contract environmentProtocol fee in basis points
fn get_protocol_fee_0(env: soroban_sdk::Env) -> u32
Returns the protocol fee for one_for_zero swaps from the factory.
env - The contract environmentProtocol fee in basis points
fn get_protocol_fee_1(env: soroban_sdk::Env) -> u32
Returns a tick bitmap word for off-chain quoters and lens contracts.
env - The contract environmentword_pos - The word position in the bitmap256-bit word from the tick bitmap
fn get_tick_bitmap(env: soroban_sdk::Env, word_pos: i32) -> soroban_sdk::U256
Returns the current active liquidity.
env - The contract environmentTotal liquidity currently active at the current tick
fn liquidity(env: soroban_sdk::Env) -> u128
Returns the global fee growth for token0.
env - The contract environmentFee growth in Q128.128 format
fn fee_growth_global_0_x128(env: soroban_sdk::Env) -> FixedPoint128
Returns the global fee growth for token1.
env - The contract environmentFee growth in Q128.128 format
fn fee_growth_global_1_x128(env: soroban_sdk::Env) -> FixedPoint128
Returns the accumulated protocol fees.
env - The contract environmentProtocolFees struct containing amounts for both tokens
fn protocol_fees(env: soroban_sdk::Env) -> ProtocolFees
Returns information about a specific tick.
env - The contract environmenttick - The tick index to queryTickInfo struct, or default (uninitialized) if tick hasn't been used
fn ticks(env: soroban_sdk::Env, tick: i32) -> TickInfo
Returns all data needed for position fee calculations in a single call.
env - The contract environmenttick_lower - The lower tick of the positiontick_upper - The upper tick of the positionPositionFeeData struct containing slot0, global fee growth, and tick info
fn get_position_fee_data(
env: soroban_sdk::Env,
tick_lower: i32,
tick_upper: i32,
) -> PositionFeeData
Returns complete pool state in a single call for efficient batch queries.
This function is designed for the PoolLens contract to minimize cross-contract
call overhead. Returns None if the pool is not initialized, eliminating the
need for a separate is_initialized check.
env - The contract environmentSome(PoolState) if pool is initialized with all config and trading stateNone if pool is not initializedfn get_full_pool_state(env: soroban_sdk::Env) -> Option
Returns complete pool state with token balances (reserves) in a single call.
Similar to get_full_pool_state but also fetches the token balances held by
the pool. This requires two additional cross-contract calls to the token
contracts, adding ~1M CPU instructions per pool.
env - The contract environmentSome(PoolStateWithBalances) if pool is initialized with state and reservesNone if pool is not initializedUse this when you need TVL/reserve information. If you don't need balances,
prefer get_full_pool_state.
fn get_pool_state_with_balances(env: soroban_sdk::Env) -> Option
Returns cumulative values inside a tick range for a given position.
Used to compute time-weighted averages and liquidity-weighted time within a price range for liquidity mining and other analytics.
env - The contract environmenttick_lower - Lower tick of the rangetick_upper - Upper tick of the rangeOk((tick_cumulative, seconds_per_liquidity_cumulative, seconds_inside))Err(TickNotInitialized) if either tick hasn't been initializedfn snapshot_cumulatives_inside(
env: soroban_sdk::Env,
tick_lower: i32,
tick_upper: i32,
hints: OracleHints,
) -> Result<(i64, FixedPoint128, u32), soroban_sdk::Error>
Observes oracle data for a single time point.
Returns tick cumulative and seconds per liquidity cumulative values for computing time-weighted averages.
env - The contract environmentseconds_ago - How many seconds in the past to observe (0 = current)Ok((tick_cumulative, seconds_per_liquidity_cumulative_x128))Err if observation doesn't exist or is too oldfn observe_single(
env: soroban_sdk::Env,
seconds_ago: u32,
hints: OracleHints,
) -> Result<(i64, FixedPoint128), soroban_sdk::Error>
Observes oracle data for multiple time points.
Returns tick cumulative and seconds per liquidity cumulative values for each requested time point, for computing time-weighted averages.
env - The contract environmentseconds_agos - Vector of seconds in the past to observe (0 = current)Ok((tick_cumulatives, seconds_per_liquidity_cumulatives_x128))Err if any observation doesn't exist or is too oldfn observe(
env: soroban_sdk::Env,
seconds_agos: soroban_sdk::Vec,
hints: OracleHints,
) -> Result<
(soroban_sdk::Vec, soroban_sdk::Vec),
soroban_sdk::Error,
>
Permissionless oracle poke to record a fresh observation.
Writes a new oracle observation using the current pool tick and liquidity.
If called within MIN_OBSERVATION_INTERVAL of the last write, this is a no-op.
Returns a tuple (updated, last_timestamp) where:
updated is true if a new observation was recordedlast_timestamp is the timestamp of the latest observation after this callfn poke_oracle(
env: soroban_sdk::Env,
oracle_slot_hint: u128,
) -> Result<(bool, u64), soroban_sdk::Error>
Returns oracle hints for deterministic queries.
Convenience function that returns both slot and checkpoint hints in a single call, ready to pass to swap/mint/burn/observe functions.
Works both before and after pool initialization:
{ slot: computed, checkpoint: 0 }{ slot: computed, checkpoint: actual_count }fn get_oracle_hints(env: soroban_sdk::Env) -> OracleHints
Get oracle status for UI decisions.
Returns information about the oracle's current state to help the frontend decide when to show/enable the "Refresh price" button.
A tuple (last_timestamp, age_seconds, can_poke) where:
last_timestamp: Unix timestamp of the most recent observationage_seconds: How many seconds have elapsed since the last observationcan_poke: Whether calling poke_oracle would update the oracle
(respects MIN_OBSERVATION_INTERVAL - at least 1 ledger ≈ 5 seconds)fn get_oracle_status(
env: soroban_sdk::Env,
) -> Result<(u64, u64, bool), soroban_sdk::Error>
Mints liquidity to a position.
Creates or adds to a liquidity position in the specified tick range. Transfers the required amounts of both tokens from the sender.
env - The contract environmentsender - Address initiating the mint and providing tokens (requires auth)recipient - Address receiving the liquidity position (position owner)tick_lower - Lower tick of the position rangetick_upper - Upper tick of the position rangeamount - Amount of liquidity to mintOk((amount0, amount1)) - Amounts of tokens depositedErr(AmountShouldBeGreaterThanZero) if amount is zeroErr(Locked) if pool is currently lockedErr(InsufficientToken0/Token1) if token transfer failsfn mint(
env: soroban_sdk::Env,
sender: soroban_sdk::Address,
recipient: soroban_sdk::Address,
tick_lower: i32,
tick_upper: i32,
amount: u128,
hints: OracleHints,
) -> Result<(u128, u128), soroban_sdk::Error>
Returns a tick bitmap word (public variant).
env - The contract environmentword_pos - The word position in the bitmap256-bit word from the tick bitmap, or zero if uninitialized
fn get_tick_bitmap_public(env: soroban_sdk::Env, word_pos: i32) -> soroban_sdk::U256
Quotes an exact input swap without executing it.
Simulates a swap to determine output amount and final price without transferring tokens or modifying state.
env - The contract environmentzero_for_one - True if swapping token0 for token1, false otherwiseamount_in - Exact amount of input tokenssqrt_price_limit_x96 - Price limit for the swap in Q64.96 formatOk(SwapResult) containing output amount and final sqrt priceErr if swap would fail (invalid price limit, etc.)fn quote_exact_input(
env: soroban_sdk::Env,
zero_for_one: bool,
amount_in: i128,
sqrt_price_limit_x96: soroban_sdk::U256,
) -> Result
Quotes an exact output swap without executing it.
Simulates a swap to determine input amount required for a desired output without transferring tokens or modifying state.
env - The contract environmentzero_for_one - True if swapping token0 for token1, false otherwiseamount_out - Exact amount of output tokens desiredsqrt_price_limit_x96 - Price limit for the swap in Q64.96 formatOk(SwapResult) containing required input amount and final sqrt priceErr if swap would fail (invalid price limit, insufficient liquidity)fn quote_exact_output(
env: soroban_sdk::Env,
zero_for_one: bool,
amount_out: i128,
sqrt_price_limit_x96: soroban_sdk::U256,
) -> Result
Returns position data for a specific owner and tick range.
env - The contract environmentrecipient - Address of the position ownertick_lower - Lower tick of the position rangetick_upper - Upper tick of the position rangePositionData containing liquidity, fee growth, and tokens owed
Panics if position doesn't exist
fn positions(
env: soroban_sdk::Env,
recipient: soroban_sdk::Address,
tick_lower: i32,
tick_upper: i32,
) -> PositionData