Sergio Chouhy 4bcffafe27 refactor!: rename nssa crate to lee
BREAKING CHANGE:
- Crate `nssa` renamed to `lee`; update `Cargo.toml` dependencies from `nssa = { workspace = true }` to `lee = { workspace = true }`.
- Crate `nssa_core` renamed to `lee_core`; update similarly.
- Crate `key_protocol` moved under `lee`; update `Cargo.toml` dependencies from `key_protocol = { workspace = true }` to `lee_key_protocol = { workspace = true }`.
- Type `NSSATransaction` (in `common`) renamed to `LeeTransaction`.
- Error type `nssa::error::NssaError` renamed to `lee::error::LeeError`.
- Error type `nssa_core::error::NssaCoreError` renamed to `lee_core::error::LeeCoreError`.
- All `use nssa::` and `use nssa_core::` import paths must be updated to `use lee::` and `use lee_core::` respectively.
- Guest programs must replace `write_nssa_outputs` with `write_lee_outputs`.
- The sequencer RocksDB column family for the chain state was renamed. Existing databases are incompatible and must be wiped before running the new version.
- Domain separators updated: `"NSSA_seed"` → `"LEE_seed"` (key derivation), `"NSSA/v0.2/KDF-SHA256/"` → `"LEE/v0.2/KDF-SHA256/"` (encryption KDF), `"/NSSA/v0.2/AccountId/PDA/"` →
  `"/LEE/v0.2/AccountId/PDA/"` (public PDA address derivation). All previously derived keys, encrypted outputs, and public PDA addresses are invalidated.
2026-06-01 17:11:42 -03:00

167 lines
5.2 KiB
Rust

//! The AMM Program.
//!
//! This program implements a simple AMM that supports multiple AMM pools (a single pool per
//! token pair).
//!
//! AMM program accepts [`Instruction`] as input, refer to the corresponding documentation
//! for more details.
use std::num::NonZero;
use amm_core::Instruction;
use lee_core::program::{ProgramInput, ProgramOutput, read_lee_inputs};
fn main() {
let (
ProgramInput {
self_program_id,
caller_program_id,
pre_states,
instruction,
},
instruction_words,
) = read_lee_inputs::<Instruction>();
let pre_states_clone = pre_states.clone();
let (post_states, chained_calls) = match instruction {
Instruction::NewDefinition {
token_a_amount,
token_b_amount,
amm_program_id,
} => {
let [
pool,
vault_a,
vault_b,
pool_definition_lp,
user_holding_a,
user_holding_b,
user_holding_lp,
] = pre_states
.try_into()
.expect("Transfer instruction requires exactly seven accounts");
amm_program::new_definition::new_definition(
pool,
vault_a,
vault_b,
pool_definition_lp,
user_holding_a,
user_holding_b,
user_holding_lp,
NonZero::new(token_a_amount).expect("Token A should have a nonzero amount"),
NonZero::new(token_b_amount).expect("Token B should have a nonzero amount"),
amm_program_id,
)
}
Instruction::AddLiquidity {
min_amount_liquidity,
max_amount_to_add_token_a,
max_amount_to_add_token_b,
} => {
let [
pool,
vault_a,
vault_b,
pool_definition_lp,
user_holding_a,
user_holding_b,
user_holding_lp,
] = pre_states
.try_into()
.expect("Transfer instruction requires exactly seven accounts");
amm_program::add::add_liquidity(
pool,
vault_a,
vault_b,
pool_definition_lp,
user_holding_a,
user_holding_b,
user_holding_lp,
NonZero::new(min_amount_liquidity)
.expect("Min amount of liquidity should be nonzero"),
max_amount_to_add_token_a,
max_amount_to_add_token_b,
)
}
Instruction::RemoveLiquidity {
remove_liquidity_amount,
min_amount_to_remove_token_a,
min_amount_to_remove_token_b,
} => {
let [
pool,
vault_a,
vault_b,
pool_definition_lp,
user_holding_a,
user_holding_b,
user_holding_lp,
] = pre_states
.try_into()
.expect("Transfer instruction requires exactly seven accounts");
amm_program::remove::remove_liquidity(
pool,
vault_a,
vault_b,
pool_definition_lp,
user_holding_a,
user_holding_b,
user_holding_lp,
NonZero::new(remove_liquidity_amount)
.expect("Remove liquidity amount must be nonzero"),
min_amount_to_remove_token_a,
min_amount_to_remove_token_b,
)
}
Instruction::SwapExactInput {
swap_amount_in,
min_amount_out,
token_definition_id_in,
} => {
let [pool, vault_a, vault_b, user_holding_a, user_holding_b] = pre_states
.try_into()
.expect("SwapExactInput instruction requires exactly five accounts");
amm_program::swap::swap_exact_input(
pool,
vault_a,
vault_b,
user_holding_a,
user_holding_b,
swap_amount_in,
min_amount_out,
token_definition_id_in,
)
}
Instruction::SwapExactOutput {
exact_amount_out,
max_amount_in,
token_definition_id_in,
} => {
let [pool, vault_a, vault_b, user_holding_a, user_holding_b] = pre_states
.try_into()
.expect("SwapExactOutput instruction requires exactly five accounts");
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,
)
}
};
ProgramOutput::new(
self_program_id,
caller_program_id,
instruction_words,
pre_states_clone,
post_states,
)
.with_chained_calls(chained_calls)
.write();
}