-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5543197
commit 7163a5e
Showing
10 changed files
with
407 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod success; |
259 changes: 259 additions & 0 deletions
259
scripts/swap_exact_output_script/tests/cases/success.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod cases; | ||
pub mod utils; |
Oops, something went wrong.