Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ members = [
"ata",
"ata/methods",
"integration_tests",
"integration_tests/malicious_ata_methods",
"tools/idl-gen",
]
exclude = [
"token/methods/guest",
"amm/methods/guest",
"ata/methods/guest",
"integration_tests/malicious_ata_methods/guest",
]
resolver = "2"

Expand Down
1 change: 1 addition & 0 deletions amm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
nssa_core = { git = "https://github.com/logos-blockchain/logos-execution-zone.git", tag = "v0.2.0-rc1", features = ["host"] }
amm_core = { path = "core" }
ata_core = { path = "../ata/core" }
token_core = { path = "../token/core" }
30 changes: 18 additions & 12 deletions amm/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ pub enum Instruction {
/// - Vault Holding Account for Token A (initialized)
/// - Vault Holding Account for Token B (initialized)
/// - Pool Liquidity Token Definition (initialized)
/// - User Holding Account for Token A (authorized)
/// - User Holding Account for Token B (authorized)
/// - User Holding Account for Pool Liquidity
/// - Owner account (authorized)
/// - User ATA for Token A
/// - User ATA for Token B
/// - User ATA for Pool Liquidity
AddLiquidity {
min_amount_liquidity: u128,
max_amount_to_add_token_a: u128,
max_amount_to_add_token_b: u128,
ata_program_id: ProgramId,
},

/// Removes liquidity from the Pool
Expand All @@ -60,13 +62,15 @@ pub enum Instruction {
/// - Vault Holding Account for Token A (initialized)
/// - Vault Holding Account for Token B (initialized)
/// - Pool Liquidity Token Definition (initialized)
/// - User Holding Account for Token A (initialized)
/// - User Holding Account for Token B (initialized)
/// - User Holding Account for Pool Liquidity (authorized)
/// - Owner account (authorized)
/// - User ATA for Token A (initialized)
/// - User ATA for Token B (initialized)
/// - User ATA for Pool Liquidity
RemoveLiquidity {
remove_liquidity_amount: u128,
min_amount_to_remove_token_a: u128,
min_amount_to_remove_token_b: u128,
ata_program_id: ProgramId,
},

/// Swap some quantity of Tokens (either Token A or Token B)
Expand All @@ -76,13 +80,14 @@ pub enum Instruction {
/// - AMM Pool (initialized)
/// - Vault Holding Account for Token A (initialized)
/// - Vault Holding Account for Token B (initialized)
/// - User Holding Account for Token A
/// - User Holding Account for Token B Either User Holding Account for Token A or Token B is
/// authorized.
/// - Owner account (authorized)
/// - User ATA for Token A
/// - User ATA for Token B
SwapExactInput {
swap_amount_in: u128,
min_amount_out: u128,
token_definition_id_in: AccountId,
ata_program_id: ProgramId,
},

/// Swap tokens specifying the exact desired output amount,
Expand All @@ -92,13 +97,14 @@ pub enum Instruction {
/// - AMM Pool (initialized)
/// - Vault Holding Account for Token A (initialized)
/// - Vault Holding Account for Token B (initialized)
/// - User Holding Account for Token A
/// - User Holding Account for Token B Either User Holding Account for Token A or Token B is
/// authorized.
/// - Owner account (authorized)
/// - User ATA for Token A
/// - User ATA for Token B
SwapExactOutput {
exact_amount_out: u128,
max_amount_in: u128,
token_definition_id_in: AccountId,
ata_program_id: ProgramId,
},

/// Sync pool reserves with current vault balances.
Expand Down
12 changes: 12 additions & 0 deletions amm/methods/guest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amm/methods/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ spel-framework = { git = "https://github.com/logos-co/spel.git", tag = "v0.2.0-r
nssa_core = { git = "https://github.com/logos-blockchain/logos-execution-zone.git", tag = "v0.2.0-rc1" }
risc0-zkvm = { version = "=3.0.5", default-features = false }
amm_core = { path = "../../core" }
ata_core = { path = "../../../ata/core" }
amm_program = { path = "../..", package = "amm_program" }
token_core = { path = "../../../token/core" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
16 changes: 16 additions & 0 deletions amm/methods/guest/src/bin/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,28 @@ mod amm {
vault_a: AccountWithMetadata,
vault_b: AccountWithMetadata,
pool_definition_lp: AccountWithMetadata,
owner: 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,
ata_program_id: ProgramId,
) -> SpelResult {
let (post_states, chained_calls) = amm_program::add::add_liquidity(
pool,
vault_a,
vault_b,
pool_definition_lp,
owner,
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,
ata_program_id,
);
Ok(SpelOutput::with_chained_calls(post_states, chained_calls))
}
Expand All @@ -85,25 +89,29 @@ mod amm {
vault_a: AccountWithMetadata,
vault_b: AccountWithMetadata,
pool_definition_lp: AccountWithMetadata,
owner: 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,
ata_program_id: ProgramId,
) -> SpelResult {
let (post_states, chained_calls) = amm_program::remove::remove_liquidity(
pool,
vault_a,
vault_b,
pool_definition_lp,
owner,
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,
ata_program_id,
);
Ok(SpelOutput::with_chained_calls(post_states, chained_calls))
}
Expand All @@ -114,21 +122,25 @@ mod amm {
pool: AccountWithMetadata,
vault_a: AccountWithMetadata,
vault_b: AccountWithMetadata,
owner: AccountWithMetadata,
user_holding_a: AccountWithMetadata,
user_holding_b: AccountWithMetadata,
swap_amount_in: u128,
min_amount_out: u128,
token_definition_id_in: AccountId,
ata_program_id: ProgramId,
) -> SpelResult {
let (post_states, chained_calls) = amm_program::swap::swap_exact_input(
pool,
vault_a,
vault_b,
owner,
user_holding_a,
user_holding_b,
swap_amount_in,
min_amount_out,
token_definition_id_in,
ata_program_id,
);
Ok(SpelOutput::with_chained_calls(post_states, chained_calls))
}
Expand All @@ -139,21 +151,25 @@ mod amm {
pool: AccountWithMetadata,
vault_a: AccountWithMetadata,
vault_b: AccountWithMetadata,
owner: AccountWithMetadata,
user_holding_a: AccountWithMetadata,
user_holding_b: AccountWithMetadata,
exact_amount_out: u128,
max_amount_in: u128,
token_definition_id_in: AccountId,
ata_program_id: ProgramId,
) -> SpelResult {
let (post_states, chained_calls) = amm_program::swap::swap_exact_output(
pool,
vault_a,
vault_b,
owner,
user_holding_a,
user_holding_b,
exact_amount_out,
max_amount_in,
token_definition_id_in,
ata_program_id,
);
Ok(SpelOutput::with_chained_calls(post_states, chained_calls))
}
Expand Down
31 changes: 18 additions & 13 deletions amm/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use amm_core::{
};
use nssa_core::{
account::{AccountWithMetadata, Data},
program::{AccountPostState, ChainedCall},
program::{AccountPostState, ChainedCall, ProgramId},
};

#[expect(clippy::too_many_arguments, reason = "TODO: Fix later")]
Expand All @@ -15,12 +15,14 @@ pub fn add_liquidity(
vault_a: AccountWithMetadata,
vault_b: AccountWithMetadata,
pool_definition_lp: AccountWithMetadata,
owner: AccountWithMetadata,
user_holding_a: AccountWithMetadata,
user_holding_b: AccountWithMetadata,
user_holding_lp: AccountWithMetadata,
min_amount_liquidity: NonZeroU128,
max_amount_to_add_token_a: u128,
max_amount_to_add_token_b: u128,
ata_program_id: ProgramId,
) -> (Vec<AccountPostState>, Vec<ChainedCall>) {
// 1. Fetch Pool state
let pool_def_data = PoolDefinition::try_from(&pool.account.data)
Expand Down Expand Up @@ -138,25 +140,27 @@ pub fn add_liquidity(
};

pool_post.data = Data::from(&pool_post_definition);
let token_program_id = user_holding_a.account.program_owner;
let token_program_id = vault_a.account.program_owner;

// Chain call for Token A (UserHoldingA -> Vault_A)
// Chain call for Token A (owner's ATA -> Vault_A via ATA program)
let call_token_a = ChainedCall::new(
token_program_id,
vec![user_holding_a.clone(), vault_a.clone()],
&token_core::Instruction::Transfer {
amount_to_transfer: actual_amount_a,
ata_program_id,
vec![owner.clone(), user_holding_a.clone(), vault_a.clone()],
&ata_core::Instruction::Transfer {
ata_program_id,
amount: actual_amount_a,
},
);
// Chain call for Token B (UserHoldingB -> Vault_B)
// Chain call for Token B (owner's ATA -> Vault_B via ATA program)
let call_token_b = ChainedCall::new(
token_program_id,
vec![user_holding_b.clone(), vault_b.clone()],
&token_core::Instruction::Transfer {
amount_to_transfer: actual_amount_b,
ata_program_id,
vec![owner.clone(), user_holding_b.clone(), vault_b.clone()],
&ata_core::Instruction::Transfer {
ata_program_id,
amount: actual_amount_b,
},
);
// Chain call for LP (mint new tokens for user_holding_lp)
// Chain call for LP (mint new tokens for user's LP ATA)
let mut pool_definition_lp_auth = pool_definition_lp.clone();
pool_definition_lp_auth.is_authorized = true;
let call_token_lp = ChainedCall::new(
Expand All @@ -175,6 +179,7 @@ pub fn add_liquidity(
AccountPostState::new(vault_a.account.clone()),
AccountPostState::new(vault_b.account.clone()),
AccountPostState::new(pool_definition_lp.account.clone()),
AccountPostState::new(owner.account.clone()),
AccountPostState::new(user_holding_a.account.clone()),
AccountPostState::new(user_holding_b.account.clone()),
AccountPostState::new(user_holding_lp.account.clone()),
Expand Down
Loading
Loading