2026-05-06 17:08:15 -03:00
|
|
|
#![cfg_attr(not(test), no_main)]
|
2026-03-17 18:08:53 +01:00
|
|
|
|
|
|
|
|
use std::num::NonZeroU128;
|
|
|
|
|
|
|
|
|
|
use spel_framework::prelude::*;
|
2026-05-04 10:34:10 -03:00
|
|
|
use spel_framework::context::ProgramContext;
|
2026-03-17 18:08:53 +01:00
|
|
|
use nssa_core::{
|
|
|
|
|
account::{AccountId, AccountWithMetadata},
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-06 17:08:15 -03:00
|
|
|
#[cfg(not(test))]
|
2026-03-17 18:08:53 +01:00
|
|
|
risc0_zkvm::guest::entry!(main);
|
|
|
|
|
|
|
|
|
|
#[lez_program(instruction = "amm_core::Instruction")]
|
|
|
|
|
mod amm {
|
2026-05-06 17:08:15 -03:00
|
|
|
#[expect(
|
|
|
|
|
unused_imports,
|
|
|
|
|
reason = "SPEL instruction macro requires importing parent-scope handler types"
|
|
|
|
|
)]
|
2026-03-17 18:08:53 +01:00
|
|
|
use super::*;
|
|
|
|
|
|
2026-04-10 15:43:13 -03:00
|
|
|
/// Initializes a new Pool (or re-initializes an existing zero-supply Pool).
|
2026-04-15 14:55:04 -03:00
|
|
|
/// A fresh user LP holding must be explicitly authorized by the caller.
|
2026-05-06 17:08:15 -03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface requires explicit pool, vault, mint, lock, and user accounts"
|
|
|
|
|
)]
|
2026-03-17 18:08:53 +01:00
|
|
|
#[instruction]
|
|
|
|
|
pub fn new_definition(
|
2026-05-04 10:34:10 -03:00
|
|
|
ctx: ProgramContext,
|
2026-03-17 18:08:53 +01:00
|
|
|
pool: AccountWithMetadata,
|
|
|
|
|
vault_a: AccountWithMetadata,
|
|
|
|
|
vault_b: AccountWithMetadata,
|
|
|
|
|
pool_definition_lp: AccountWithMetadata,
|
2026-04-08 17:48:13 -03:00
|
|
|
lp_lock_holding: AccountWithMetadata,
|
2026-03-17 18:08:53 +01:00
|
|
|
user_holding_a: AccountWithMetadata,
|
|
|
|
|
user_holding_b: AccountWithMetadata,
|
|
|
|
|
user_holding_lp: AccountWithMetadata,
|
|
|
|
|
token_a_amount: u128,
|
|
|
|
|
token_b_amount: u128,
|
2026-03-31 20:45:57 -03:00
|
|
|
fees: u128,
|
2026-04-23 17:19:15 +02:00
|
|
|
deadline: u64,
|
2026-03-17 18:08:53 +01:00
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = amm_program::new_definition::new_definition(
|
|
|
|
|
pool,
|
|
|
|
|
vault_a,
|
|
|
|
|
vault_b,
|
|
|
|
|
pool_definition_lp,
|
2026-04-08 17:48:13 -03:00
|
|
|
lp_lock_holding,
|
2026-03-17 18:08:53 +01:00
|
|
|
user_holding_a,
|
|
|
|
|
user_holding_b,
|
|
|
|
|
user_holding_lp,
|
|
|
|
|
NonZeroU128::new(token_a_amount).expect("token_a_amount must be nonzero"),
|
|
|
|
|
NonZeroU128::new(token_b_amount).expect("token_b_amount must be nonzero"),
|
2026-03-31 20:45:57 -03:00
|
|
|
fees,
|
2026-05-04 10:34:10 -03:00
|
|
|
ctx.self_program_id,
|
2026-03-17 18:08:53 +01:00
|
|
|
);
|
2026-05-12 11:33:19 +02:00
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, chained_calls)
|
2026-04-23 17:19:15 +02:00
|
|
|
.with_timestamp_validity_window(..deadline))
|
2026-03-17 18:08:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Adds liquidity to the Pool.
|
2026-05-06 17:08:15 -03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface requires explicit pool, vault, and user accounts"
|
|
|
|
|
)]
|
2026-03-17 18:08:53 +01:00
|
|
|
#[instruction]
|
|
|
|
|
pub fn add_liquidity(
|
|
|
|
|
pool: AccountWithMetadata,
|
|
|
|
|
vault_a: AccountWithMetadata,
|
|
|
|
|
vault_b: AccountWithMetadata,
|
|
|
|
|
pool_definition_lp: AccountWithMetadata,
|
|
|
|
|
user_holding_a: AccountWithMetadata,
|
|
|
|
|
user_holding_b: AccountWithMetadata,
|
|
|
|
|
user_holding_lp: AccountWithMetadata,
|
|
|
|
|
min_amount_liquidity: u128,
|
|
|
|
|
max_amount_to_add_token_a: u128,
|
|
|
|
|
max_amount_to_add_token_b: u128,
|
2026-04-23 17:19:15 +02:00
|
|
|
deadline: u64,
|
2026-03-17 18:08:53 +01:00
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = amm_program::add::add_liquidity(
|
|
|
|
|
pool,
|
|
|
|
|
vault_a,
|
|
|
|
|
vault_b,
|
|
|
|
|
pool_definition_lp,
|
|
|
|
|
user_holding_a,
|
|
|
|
|
user_holding_b,
|
|
|
|
|
user_holding_lp,
|
|
|
|
|
NonZeroU128::new(min_amount_liquidity).expect("min_amount_liquidity must be nonzero"),
|
|
|
|
|
max_amount_to_add_token_a,
|
|
|
|
|
max_amount_to_add_token_b,
|
|
|
|
|
);
|
2026-05-12 11:33:19 +02:00
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, chained_calls)
|
2026-04-23 17:19:15 +02:00
|
|
|
.with_timestamp_validity_window(..deadline))
|
2026-03-17 18:08:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Removes liquidity from the Pool.
|
2026-05-06 17:08:15 -03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface requires explicit pool, vault, and user accounts"
|
|
|
|
|
)]
|
2026-03-17 18:08:53 +01:00
|
|
|
#[instruction]
|
|
|
|
|
pub fn remove_liquidity(
|
|
|
|
|
pool: AccountWithMetadata,
|
|
|
|
|
vault_a: AccountWithMetadata,
|
|
|
|
|
vault_b: AccountWithMetadata,
|
|
|
|
|
pool_definition_lp: AccountWithMetadata,
|
|
|
|
|
user_holding_a: AccountWithMetadata,
|
|
|
|
|
user_holding_b: AccountWithMetadata,
|
|
|
|
|
user_holding_lp: AccountWithMetadata,
|
|
|
|
|
remove_liquidity_amount: u128,
|
|
|
|
|
min_amount_to_remove_token_a: u128,
|
|
|
|
|
min_amount_to_remove_token_b: u128,
|
2026-04-23 17:19:15 +02:00
|
|
|
deadline: u64,
|
2026-03-17 18:08:53 +01:00
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = amm_program::remove::remove_liquidity(
|
|
|
|
|
pool,
|
|
|
|
|
vault_a,
|
|
|
|
|
vault_b,
|
|
|
|
|
pool_definition_lp,
|
|
|
|
|
user_holding_a,
|
|
|
|
|
user_holding_b,
|
|
|
|
|
user_holding_lp,
|
|
|
|
|
NonZeroU128::new(remove_liquidity_amount)
|
|
|
|
|
.expect("remove_liquidity_amount must be nonzero"),
|
|
|
|
|
min_amount_to_remove_token_a,
|
|
|
|
|
min_amount_to_remove_token_b,
|
|
|
|
|
);
|
2026-05-12 11:33:19 +02:00
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, chained_calls)
|
2026-04-23 17:19:15 +02:00
|
|
|
.with_timestamp_validity_window(..deadline))
|
2026-03-17 18:08:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap some quantity of tokens while maintaining the pool constant product.
|
2026-05-06 17:08:15 -03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface requires explicit pool, vault, user accounts, and bounds"
|
|
|
|
|
)]
|
2026-03-17 18:08:53 +01:00
|
|
|
#[instruction]
|
2026-04-07 09:31:32 +02:00
|
|
|
pub fn swap_exact_input(
|
2026-03-17 18:08:53 +01:00
|
|
|
pool: AccountWithMetadata,
|
|
|
|
|
vault_a: AccountWithMetadata,
|
|
|
|
|
vault_b: AccountWithMetadata,
|
|
|
|
|
user_holding_a: AccountWithMetadata,
|
|
|
|
|
user_holding_b: AccountWithMetadata,
|
|
|
|
|
swap_amount_in: u128,
|
|
|
|
|
min_amount_out: u128,
|
|
|
|
|
token_definition_id_in: AccountId,
|
2026-04-23 17:19:15 +02:00
|
|
|
deadline: u64,
|
2026-03-17 18:08:53 +01:00
|
|
|
) -> SpelResult {
|
2026-04-07 09:31:32 +02:00
|
|
|
let (post_states, chained_calls) = amm_program::swap::swap_exact_input(
|
2026-03-17 18:08:53 +01:00
|
|
|
pool,
|
|
|
|
|
vault_a,
|
|
|
|
|
vault_b,
|
|
|
|
|
user_holding_a,
|
|
|
|
|
user_holding_b,
|
|
|
|
|
swap_amount_in,
|
|
|
|
|
min_amount_out,
|
|
|
|
|
token_definition_id_in,
|
|
|
|
|
);
|
2026-05-12 11:33:19 +02:00
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, chained_calls)
|
2026-04-23 17:19:15 +02:00
|
|
|
.with_timestamp_validity_window(..deadline))
|
2026-03-17 18:08:53 +01:00
|
|
|
}
|
2026-04-08 10:57:47 -03:00
|
|
|
|
2026-04-02 17:16:53 +02:00
|
|
|
/// Swap tokens specifying the exact desired output amount.
|
2026-05-06 17:08:15 -03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::too_many_arguments,
|
|
|
|
|
reason = "instruction interface requires explicit pool, vault, user accounts, and bounds"
|
|
|
|
|
)]
|
2026-04-02 17:16:53 +02:00
|
|
|
#[instruction]
|
|
|
|
|
pub fn swap_exact_output(
|
|
|
|
|
pool: AccountWithMetadata,
|
|
|
|
|
vault_a: AccountWithMetadata,
|
|
|
|
|
vault_b: AccountWithMetadata,
|
|
|
|
|
user_holding_a: AccountWithMetadata,
|
|
|
|
|
user_holding_b: AccountWithMetadata,
|
|
|
|
|
exact_amount_out: u128,
|
|
|
|
|
max_amount_in: u128,
|
|
|
|
|
token_definition_id_in: AccountId,
|
2026-04-23 17:19:15 +02:00
|
|
|
deadline: u64,
|
2026-04-02 17:16:53 +02:00
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) = amm_program::swap::swap_exact_output(
|
|
|
|
|
pool,
|
|
|
|
|
vault_a,
|
|
|
|
|
vault_b,
|
|
|
|
|
user_holding_a,
|
|
|
|
|
user_holding_b,
|
|
|
|
|
exact_amount_out,
|
|
|
|
|
max_amount_in,
|
|
|
|
|
token_definition_id_in,
|
|
|
|
|
);
|
2026-05-12 11:33:19 +02:00
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, chained_calls)
|
2026-04-23 17:19:15 +02:00
|
|
|
.with_timestamp_validity_window(..deadline))
|
2026-04-02 17:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 10:57:47 -03:00
|
|
|
/// Sync pool reserves with current vault balances.
|
|
|
|
|
#[instruction]
|
|
|
|
|
pub fn sync_reserves(
|
|
|
|
|
pool: AccountWithMetadata,
|
|
|
|
|
vault_a: AccountWithMetadata,
|
|
|
|
|
vault_b: AccountWithMetadata,
|
|
|
|
|
) -> SpelResult {
|
|
|
|
|
let (post_states, chained_calls) =
|
|
|
|
|
amm_program::sync::sync_reserves(pool, vault_a, vault_b);
|
2026-05-12 11:33:19 +02:00
|
|
|
Ok(spel_framework::SpelOutput::execute(post_states, chained_calls))
|
2026-04-08 10:57:47 -03:00
|
|
|
}
|
2026-03-17 18:08:53 +01:00
|
|
|
}
|