Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dex swap test #3246

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
56 changes: 56 additions & 0 deletions apps/rooch_dex/sources/swap.move
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ module rooch_dex::swap {
use moveos_std::object::{Object, ObjectID, named_object_id};
use rooch_framework::coin_store::{CoinStore, balance, deposit, withdraw};
use rooch_framework::coin_store;

#[test_only]
use std::signer::address_of;
#[test_only]
use moveos_std::object::to_shared;
#[test_only]
use rooch_framework::account::create_account_for_testing;
#[test_only]
use rooch_framework::coin::Coin;


Expand Down Expand Up @@ -659,4 +664,55 @@ module rooch_dex::swap {
to_shared(coin_info);
return lp_coin
}

#[test]
fun test_swap(){
rooch_framework::genesis::init_for_test();
let signer = create_account_for_testing(@0x42);
let sender = address_of(&signer);
let receiver_signer = create_account_for_testing(@0x43);
let receiver = address_of(&receiver_signer);
let x_coin_info = coin::register_extend<TestCoinX>(
string::utf8(b"TestCoinX"),
string::utf8(b"TCX"),
none(),
8
);
let y_coin_info = coin::register_extend<TestCoinY>(
string::utf8(b"TestCoinY"),
string::utf8(b"TCY"),
none(),
8
);

let x_coin = coin::mint(&mut x_coin_info, 100000000);
account_coin_store::deposit(sender, x_coin);
let y_coin = coin::mint(&mut y_coin_info, 100000000);
account_coin_store::deposit(sender, y_coin);
to_shared(x_coin_info);
to_shared(y_coin_info);
create_pair<TestCoinX, TestCoinY>(&signer);
let (a_x, a_y, lp_amount)= add_liquidity<TestCoinX, TestCoinY>(&signer, 10000, 10000);
assert!(a_x == 10000, 0);
assert!(a_y == 10000, 1);
assert!(lp_amount == 9000, 2);
let (amount_x, amount_y) = remove_liquidity<TestCoinX, TestCoinY>(&signer, 100);
assert!(amount_x == 100, 3);
assert!(amount_y == 100, 4);
let (a_x, a_y, lp_amount)= add_liquidity<TestCoinX, TestCoinY>(&signer, 10100, 20200);
assert!(a_x == 10100, 5);
assert!(a_y == 10100, 6);
assert!(lp_amount == 10100, 7);
let total_lp = total_lp_supply<TestCoinX, TestCoinY>();
assert!(total_lp == 20000, 8);
let amount_out = swap_exact_x_to_y<TestCoinX, TestCoinY>(&signer, 1000, receiver);
assert!(amount_out == 950, 9);
let amount_out = swap_exact_y_to_x<TestCoinX, TestCoinY>(&signer, 1000, receiver);
assert!(amount_out == 1044, 10);
let total_lp = total_lp_supply<TestCoinX, TestCoinY>();
assert!(total_lp == 20000, 11);
let (amount_x, amount_y) = remove_liquidity<TestCoinX, TestCoinY>(&signer, 1000);
assert!(amount_x == 997, 12);
assert!(amount_y == 1002, 13);
}
}
Loading