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

Added Coin Registration into Swap Functions #146

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
24 changes: 20 additions & 4 deletions liquidswap_router_v2/sources/scripts_v2.move
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ module liquidswap::scripts_v2 {
);

let account_addr = signer::address_of(account);
coin::deposit(account_addr, coin_y);

// register coin if not registered
if (!coin::is_account_registered<Y>(account_addr)) {
coin::register<Y>(account);
};
coin::deposit<Y>(account_addr, coin_y);
}

/// Swap maximum coin `X` for exact coin `Y`.
Expand All @@ -134,8 +139,14 @@ module liquidswap::scripts_v2 {
);

let account_addr = signer::address_of(account);
coin::deposit(account_addr, coin_x);
coin::deposit(account_addr, coin_y);

// register coin if not registered
if (!coin::is_account_registered<Y>(account_addr)) {
coin::register<Y>(account);
};
//
coin::deposit<X>(account_addr, coin_x);
coin::deposit<Y>(account_addr, coin_y);
}

/// Swap `coin_in` of X for a `coin_out` of Y.
Expand All @@ -152,6 +163,11 @@ module liquidswap::scripts_v2 {
let coin_y = router_v2::swap_coin_for_coin_unchecked<X, Y, Curve>(coin_x, coin_out);

let account_addr = signer::address_of(account);
coin::deposit(account_addr, coin_y);

// register coin if not registered
if (!coin::is_account_registered<Y>(account_addr)) {
coin::register<Y>(account);
};
coin::deposit<Y>(account_addr, coin_y);
}
}
62 changes: 62 additions & 0 deletions liquidswap_router_v2/tests/scripts_v2_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ module liquidswap::scripts_v2_tests {
assert!(coin::balance<USDT>(lp_owner_addr) == 907, 2);
}

// test swap with unregistered target coins
#[test]
public entry fun test_swap_exact_btc_for_usdt_unregistered() {
let (coin_admin, lp_owner) = register_pool_with_existing_liquidity(101, 10100);

let btc_coins_to_swap = test_coins::mint<BTC>(&coin_admin, 10);
coin::register<BTC>(&lp_owner);

let lp_owner_addr = signer::address_of(&lp_owner);
coin::deposit(lp_owner_addr, btc_coins_to_swap);

scripts_v2::swap<BTC, USDT, Uncorrelated>(
&lp_owner,
10,
900,
);

assert!(coin::balance<BTC>(lp_owner_addr) == 0, 1);
assert!(coin::balance<USDT>(lp_owner_addr) == 907, 2);
}

#[test]
public entry fun test_swap_btc_for_exact_usdt() {
let (coin_admin, lp_owner) = register_pool_with_existing_liquidity(101, 10100);
Expand All @@ -172,6 +193,26 @@ module liquidswap::scripts_v2_tests {
assert!(coin::balance<USDT>(lp_owner_addr) == 700, 2);
}

#[test]
public entry fun test_swap_btc_for_exact_usdt_unregistered() {
let (coin_admin, lp_owner) = register_pool_with_existing_liquidity(101, 10100);

let btc_coins_to_swap = test_coins::mint<BTC>(&coin_admin, 10);
coin::register<BTC>(&lp_owner);

let lp_owner_addr = signer::address_of(&lp_owner);
coin::deposit(lp_owner_addr, btc_coins_to_swap);

scripts_v2::swap_into<BTC, USDT, Uncorrelated>(
&lp_owner,
10,
700,
);

assert!(coin::balance<BTC>(lp_owner_addr) == 2, 1);
assert!(coin::balance<USDT>(lp_owner_addr) == 700, 2);
}

#[test]
public entry fun test_unchecked_swap_common() {
let (coin_admin, lp_owner) = register_pool_with_existing_liquidity(101, 10100);
Expand All @@ -193,7 +234,28 @@ module liquidswap::scripts_v2_tests {
assert!(coin::balance<USDT>(lp_owner_addr) == 907, 2);

}

#[test]
public entry fun test_unchecked_swap_common_unregistered() {
let (coin_admin, lp_owner) = register_pool_with_existing_liquidity(101, 10100);

let btc_coins_to_swap = test_coins::mint<BTC>(&coin_admin, 10);
coin::register<BTC>(&lp_owner);

let lp_owner_addr = signer::address_of(&lp_owner);
coin::deposit(lp_owner_addr, btc_coins_to_swap);

scripts_v2::swap_unchecked<BTC, USDT, Uncorrelated>(
&lp_owner,
10,
907,
);

assert!(coin::balance<BTC>(lp_owner_addr) == 0, 1);
assert!(coin::balance<USDT>(lp_owner_addr) == 907, 2);

}

#[test]
public entry fun test_unchecked_swap_can_use_worse_price() {
let (coin_admin, lp_owner) = register_pool_with_existing_liquidity(101, 10100);
Expand Down