Skip to content

Commit

Permalink
feat: remove liquidity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarmatdev committed Aug 19, 2024
1 parent 21d352a commit a0b69e6
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 13 deletions.
Binary file modified fixtures/mira-amm/mira_amm_contract.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion libraries/interfaces/src/mira_amm.sw
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abi MiraAMM {

#[payable]
#[storage(read, write)]
fn burn(pool_id: PoolId, lp_asset_id: AssetId, liquidity: u64, to: Identity) -> (u64, u64);
fn burn(pool_id: PoolId, to: Identity) -> (u64, u64);

#[payable]
#[storage(read, write)]
Expand Down
11 changes: 4 additions & 7 deletions scripts/remove_liquidity_script/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ fn main(
let amm = abi(MiraAMM, AMM_CONTRACT_ID.into());

let (_, lp_asset_id) = get_lp_asset(AMM_CONTRACT_ID, pool_id);
transfer(
Identity::ContractId(AMM_CONTRACT_ID),
lp_asset_id,
liquidity,
);
let (amount_0, amount_1) = amm.burn(pool_id,lp_asset_id,liquidity, recipient);
let (amount_0, amount_1) = amm.burn {
asset_id: lp_asset_id.into(),
coins: liquidity,
}(pool_id, recipient);

require(amount_0 >= amount_0_min, "Insufficient amount");
require(amount_1 >= amount_1_min, "Insufficient amount");

(amount_0, amount_1)
}
1 change: 1 addition & 0 deletions scripts/remove_liquidity_script/tests/cases/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod revert;
pub mod success;
60 changes: 60 additions & 0 deletions scripts/remove_liquidity_script/tests/cases/revert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::utils::setup;
use fuels::programs::call_utils::TxDependencyExtension;
use test_harness::utils::common::to_9_decimal;
use test_harness::wallet::get_transaction_inputs_outputs;

#[tokio::test]
#[ignore]
#[should_panic(expected = "ZeroInputAmount")]
async fn removes_all_liquidity_passing_exact_a_and_b_values() {
let (
add_liquidity_script_instance,
remove_liquidity_script_instance,
amm,
_token_contract,
_provider,
pool_id,
wallet,
transaction_parameters,
deadline,
) = setup().await;

let token_0_amount: u64 = to_9_decimal(1);
let token_1_amount: u64 = to_9_decimal(1);

// adds initial liquidity
let added_liquidity = add_liquidity_script_instance
.main(
pool_id,
token_0_amount,
token_1_amount,
0,
0,
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(transaction_parameters.inputs)
.with_outputs(transaction_parameters.outputs)
.call()
.await
.unwrap()
.value;

let transaction_parameters = get_transaction_inputs_outputs(
&wallet,
&vec![added_liquidity.id],
&vec![added_liquidity.amount],
)
.await;

remove_liquidity_script_instance
.main(pool_id, 0, 0, 0, wallet.address().into(), deadline)
.with_contracts(&[&amm.instance])
.with_inputs(transaction_parameters.0)
.with_outputs(transaction_parameters.1)
.append_variable_outputs(2)
.call()
.await
.unwrap();
}
109 changes: 104 additions & 5 deletions scripts/remove_liquidity_script/tests/cases/success.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::utils::setup;
use fuels::programs::call_utils::TxDependencyExtension;
use test_harness::interface::amm::pool_metadata;
use test_harness::interface::BurnEvent;
use test_harness::interface::{Asset, BurnEvent};
use test_harness::utils::common::{
to_9_decimal, wallet_balances_for_pool_asset, MINIMUM_LIQUIDITY,
};
use test_harness::wallet::get_transaction_inputs_outputs;

#[tokio::test]
#[ignore]
async fn removes_all_liquidity_passing_exact_a_and_b_values() {
let (
add_liquidity_script_instance,
Expand Down Expand Up @@ -45,6 +44,9 @@ async fn removes_all_liquidity_passing_exact_a_and_b_values() {
.unwrap()
.value;

let lp_token_amount = added_liquidity.amount;
assert_eq!(lp_token_amount, expected_liquidity);

let initial_pool_metadata = pool_metadata(&amm.instance, pool_id).await.value.unwrap();
let initial_wallet_balances =
wallet_balances_for_pool_asset(&wallet, &(pool_id.0, pool_id.1), &added_liquidity.id).await;
Expand Down Expand Up @@ -98,11 +100,11 @@ async fn removes_all_liquidity_passing_exact_a_and_b_values() {
);
assert_eq!(
final_pool_metadata.reserve_0,
initial_pool_metadata.reserve_0 - token_0_amount
initial_pool_metadata.reserve_0 - token_0_amount + MINIMUM_LIQUIDITY
);
assert_eq!(
final_pool_metadata.reserve_1,
initial_pool_metadata.reserve_1 - token_1_amount
initial_pool_metadata.reserve_1 - token_1_amount + MINIMUM_LIQUIDITY
);
assert_eq!(
final_pool_metadata.liquidity.amount,
Expand All @@ -111,7 +113,6 @@ async fn removes_all_liquidity_passing_exact_a_and_b_values() {
}

#[tokio::test]
#[ignore]
async fn removes_all_liquidity_passing_exact_a_but_not_exact_b_values() {
let (
add_liquidity_script_instance,
Expand Down Expand Up @@ -205,3 +206,101 @@ async fn removes_all_liquidity_passing_exact_a_but_not_exact_b_values() {
initial_wallet_balances.asset_b + token_1_amount - 2000
);
}

#[tokio::test]
async fn removes_partial_liquidity() {
let (
add_liquidity_script_instance,
remove_liquidity_script_instance,
amm,
_token_contract,
_provider,
pool_id,
wallet,
transaction_parameters,
deadline,
) = setup().await;

let token_0_amount: u64 = to_9_decimal(1);
let token_1_amount: u64 = to_9_decimal(1);
let expected_liquidity: u64 = to_9_decimal(1) - MINIMUM_LIQUIDITY;

// adds initial liquidity
let added_liquidity = add_liquidity_script_instance
.main(
pool_id,
token_0_amount,
token_1_amount,
1,
1,
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(transaction_parameters.inputs)
.with_outputs(transaction_parameters.outputs)
.call()
.await
.unwrap()
.value;

let lp_token_amount = added_liquidity.amount;
assert_eq!(lp_token_amount, expected_liquidity);

let initial_wallet_balances =
wallet_balances_for_pool_asset(&wallet, &(pool_id.0, pool_id.1), &added_liquidity.id).await;

let transaction_parameters = get_transaction_inputs_outputs(
&wallet,
&vec![added_liquidity.id],
&vec![expected_liquidity],
)
.await;

let removed_liquidity = remove_liquidity_script_instance
.main(
pool_id,
added_liquidity.amount / 2,
0,
0,
wallet.address().into(),
deadline,
)
.with_contracts(&[&amm.instance])
.with_inputs(transaction_parameters.0)
.with_outputs(transaction_parameters.1)
.append_variable_outputs(2)
.call()
.await
.unwrap();

let log = removed_liquidity
.decode_logs_with_type::<BurnEvent>()
.unwrap();
let event = log.first().unwrap();

let final_wallet_balances =
wallet_balances_for_pool_asset(&wallet, &(pool_id.0, pool_id.1), &added_liquidity.id).await;

assert_eq!(
*event,
BurnEvent {
pool_id,
recipient: wallet.address().into(),
liquidity: Asset {
amount: added_liquidity.amount / 2,
id: added_liquidity.id
},
asset_0_out: removed_liquidity.value.0,
asset_1_out: removed_liquidity.value.1,
}
);
assert_eq!(
final_wallet_balances.asset_a,
initial_wallet_balances.asset_a + (token_0_amount / 2) - 500
);
assert_eq!(
final_wallet_balances.asset_b,
initial_wallet_balances.asset_b + (token_1_amount / 2) - 500
);
}

0 comments on commit a0b69e6

Please sign in to comment.