Skip to content

Commit

Permalink
Swap exact output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoplavkov committed Aug 26, 2024
1 parent 5543197 commit 7163a5e
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 1 deletion.
9 changes: 9 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"./test-harness",
"./scripts/remove_liquidity_script",
"./scripts/swap_exact_input_script",
"./scripts/swap_exact_output_script",
]

[workspace.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion scripts/swap_exact_input_script/tests/cases/success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn swap_between_two_volatile_tokens() {
_,
wallet,
deadline,
(token_0_id, token_1_id, token_2_id),
(token_0_id, token_1_id, _),
) = setup().await;

let amount_0_desired: u64 = 1_000_000;
Expand Down
14 changes: 14 additions & 0 deletions scripts/swap_exact_output_script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "swap-exact-output-script"
version.workspace = true
edition.workspace = true

[dev-dependencies]
fuels = { workspace = true }
test-harness = { path = "../../test-harness" }
tokio = { workspace = true }

[[test]]
harness = true
name = "tests"
path = "tests/harness.rs"
1 change: 1 addition & 0 deletions scripts/swap_exact_output_script/tests/cases/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod success;
259 changes: 259 additions & 0 deletions scripts/swap_exact_output_script/tests/cases/success.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
use crate::utils::setup;
use fuels::prelude::VariableOutputPolicy;
use test_harness::interface::amm::pool_metadata;
use test_harness::interface::scripts::get_transaction_inputs_outputs;
use test_harness::utils::common::{pool_assets_balance, MINIMUM_LIQUIDITY};

#[tokio::test]
async fn swap_between_two_volatile_tokens() {
let (
add_liquidity_script,
swap_exact_output_script,
amm,
pool_id,
_,
wallet,
deadline,
(token_0_id, token_1_id, _),
) = setup().await;

let amount_0_desired: u64 = 1_000_000;
let amount_1_desired: u64 = 1_000_000;
let expected_liquidity: u64 = 1_000_000 - MINIMUM_LIQUIDITY;

let (inputs, outputs) = get_transaction_inputs_outputs(
&wallet,
&vec![
(token_0_id, amount_0_desired),
(token_1_id, amount_1_desired),
],
)
.await;

// adds initial liquidity
let added_liquidity = add_liquidity_script
.main(
pool_id,
amount_0_desired,
amount_1_desired,
0,
0,
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(inputs)
.with_outputs(outputs)
.with_variable_output_policy(VariableOutputPolicy::Exactly(2))
.call()
.await
.unwrap()
.value;

assert_eq!(added_liquidity.amount, expected_liquidity);

let token_1_output = 1_000;
let token_0_input_expected = 1006;

let (inputs, outputs) =
get_transaction_inputs_outputs(&wallet, &vec![(token_0_id, token_0_input_expected)]).await;

let wallet_balances_before = pool_assets_balance(&wallet, &pool_id, amm.id).await;
let pool_metadata_before = pool_metadata(&amm.instance, pool_id).await.value.unwrap();
let amounts_in = swap_exact_output_script
.main(
token_1_output,
token_1_id,
token_0_input_expected * 2,
vec![pool_id],
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(inputs)
.with_outputs(outputs)
.with_variable_output_policy(VariableOutputPolicy::Exactly(1))
.call()
.await
.unwrap()
.value;

assert_eq!(
amounts_in,
vec![
(token_1_output, token_1_id),
(token_0_input_expected, token_0_id),
]
);
let wallet_balances_after = pool_assets_balance(&wallet, &pool_id, amm.id).await;
let pool_metadata_after = pool_metadata(&amm.instance, pool_id).await.value.unwrap();

assert_eq!(
wallet_balances_after.asset_a,
wallet_balances_before.asset_a - token_0_input_expected
);
assert_eq!(
wallet_balances_after.asset_b,
wallet_balances_before.asset_b + token_1_output
);
assert_eq!(
pool_metadata_after.reserve_0,
pool_metadata_before.reserve_0 + token_0_input_expected
);
assert_eq!(
pool_metadata_after.reserve_1,
pool_metadata_before.reserve_1 - token_1_output
);
}

#[tokio::test]
async fn swap_between_three_volatile_tokens() {
let (
add_liquidity_script,
swap_exact_output_script,
amm,
pool_id_0,
pool_id_1,
wallet,
deadline,
(token_0_id, token_1_id, token_2_id),
) = setup().await;

let amount_0_desired: u64 = 1_000_000;
let amount_1_desired: u64 = 1_000_000;
let expected_liquidity: u64 = 1_000_000 - MINIMUM_LIQUIDITY;

let (inputs, outputs) = get_transaction_inputs_outputs(
&wallet,
&vec![
(token_0_id, amount_0_desired),
(token_1_id, amount_1_desired),
],
)
.await;

// adds initial liquidity
let added_liquidity = add_liquidity_script
.main(
pool_id_0,
amount_0_desired,
amount_1_desired,
0,
0,
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(inputs)
.with_outputs(outputs)
.with_variable_output_policy(VariableOutputPolicy::Exactly(2))
.call()
.await
.unwrap()
.value;

assert_eq!(added_liquidity.amount, expected_liquidity);

let (inputs, outputs) = get_transaction_inputs_outputs(
&wallet,
&vec![
(token_1_id, amount_0_desired),
(token_2_id, amount_1_desired),
],
)
.await;

// adds initial liquidity
let added_liquidity = add_liquidity_script
.main(
pool_id_1,
amount_0_desired,
amount_1_desired,
0,
0,
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(inputs)
.with_outputs(outputs)
.with_variable_output_policy(VariableOutputPolicy::Exactly(2))
.call()
.await
.unwrap()
.value;

assert_eq!(added_liquidity.amount, expected_liquidity);

let token_0_input_expected = 1012;
let token_1_input_expected = 1006;
let token_2_output = 1000;

let (inputs, outputs) =
get_transaction_inputs_outputs(&wallet, &vec![(token_0_id, token_0_input_expected)]).await;

let wallet_balances_0_before = pool_assets_balance(&wallet, &pool_id_0, amm.id).await;
let wallet_balances_1_before = pool_assets_balance(&wallet, &pool_id_1, amm.id).await;
let pool_metadata_0_before = pool_metadata(&amm.instance, pool_id_0).await.value.unwrap();
let pool_metadata_1_before = pool_metadata(&amm.instance, pool_id_1).await.value.unwrap();
let amounts_in = swap_exact_output_script
.main(
token_2_output,
token_2_id,
token_0_input_expected * 2,
vec![pool_id_0, pool_id_1],
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(inputs)
.with_outputs(outputs)
.with_variable_output_policy(VariableOutputPolicy::Exactly(1))
.call()
.await
.unwrap()
.value;
let pool_metadata_0_after = pool_metadata(&amm.instance, pool_id_0).await.value.unwrap();
let pool_metadata_1_after = pool_metadata(&amm.instance, pool_id_1).await.value.unwrap();
let wallet_balances_0_after = pool_assets_balance(&wallet, &pool_id_0, amm.id).await;
let wallet_balances_1_after = pool_assets_balance(&wallet, &pool_id_1, amm.id).await;

assert_eq!(
amounts_in,
vec![
(token_2_output, token_2_id),
(token_1_input_expected, pool_id_0.1),
(token_0_input_expected, pool_id_0.0),
]
);

assert_eq!(
wallet_balances_0_after.asset_a,
wallet_balances_0_before.asset_a - token_0_input_expected
);
assert_eq!(
wallet_balances_0_after.asset_b,
wallet_balances_0_before.asset_b
);
assert_eq!(
wallet_balances_1_after.asset_b,
wallet_balances_1_before.asset_b + token_2_output
);

assert_eq!(
pool_metadata_0_after.reserve_0,
pool_metadata_0_before.reserve_0 + token_0_input_expected
);
assert_eq!(
pool_metadata_0_after.reserve_1,
pool_metadata_0_before.reserve_1 - token_1_input_expected
);
assert_eq!(
pool_metadata_1_after.reserve_0,
pool_metadata_1_before.reserve_0 + token_1_input_expected
);
assert_eq!(
pool_metadata_1_after.reserve_1,
pool_metadata_1_before.reserve_1 - token_2_output
);
}
2 changes: 2 additions & 0 deletions scripts/swap_exact_output_script/tests/harness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cases;
pub mod utils;
Loading

0 comments on commit 7163a5e

Please sign in to comment.