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

[apps] Fix dex create_token_pair bug #3311

Merged
merged 5 commits into from
Feb 16, 2025
Merged
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
Binary file added apps/rooch_dex/released/3/package.rpd
Binary file not shown.
5 changes: 3 additions & 2 deletions apps/rooch_dex/sources/router.move
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module rooch_dex::router {
const ErrorInsufficientYAmount: u64 = 4;
const ErrorTokenPairNotExist: u64 = 5;
const ErrorTokenPairAlreadyExist: u64 = 6;
const ErrorZeroAmount: u64 = 7;


public entry fun create_token_pair<X:key+store, Y:key+store>(
Expand All @@ -30,7 +31,7 @@ module rooch_dex::router {
add_liquidity<X, Y>(sender, amount_x_desired, amount_y_desired, amount_x_min, amount_y_min);
} else {
swap::create_pair<Y, X>(sender);
add_liquidity<Y, X>(sender, amount_x_desired, amount_y_desired, amount_x_min, amount_y_min);
add_liquidity<Y, X>(sender, amount_y_desired, amount_x_desired, amount_y_min, amount_x_min);
};

}
Expand All @@ -43,7 +44,7 @@ module rooch_dex::router {
amount_x_min: u64,
amount_y_min: u64,
) {

assert!(amount_x_desired > 0 && amount_y_desired > 0, ErrorZeroAmount);
let amount_x;
let amount_y;
let _lp_amount;
Expand Down
58 changes: 55 additions & 3 deletions apps/rooch_dex/sources/swap_utils.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rooch_dex::swap_utils {
use std::string;
use moveos_std::type_info;
use moveos_std::compare::compare;
use moveos_std::compare::compare_vector_u8;


const EQUAL: u8 = 0;
Expand Down Expand Up @@ -52,13 +52,13 @@ module rooch_dex::swap_utils {

public fun get_token_info<T>(): vector<u8> {
let type_name = type_info::type_name<T>();
*string::bytes(&type_name)
string::into_bytes(type_name)
}

fun compare_struct<X, Y>(): u8 {
let struct_x_bytes: vector<u8> = get_token_info<X>();
let struct_y_bytes: vector<u8> = get_token_info<Y>();
compare(&struct_x_bytes, &struct_y_bytes)
compare_vector_u8(&struct_x_bytes, &struct_y_bytes)
}

public fun get_smaller_enum(): u8 {
Expand All @@ -78,4 +78,56 @@ module rooch_dex::swap_utils {
assert!(compare_x_y != get_equal_enum(), ErrorTokenPairAleardyExist);
(compare_x_y == get_smaller_enum())
}

#[test_only]
struct TokenA {}
#[test_only]
struct TokenB {}
#[test_only]
struct TokenAB {}

#[test_only]
const FEE_RATE: u64 = 9975; // 0.25% fee = 9975/10000

#[test]
fun test_sort_token_type() {
assert!(sort_token_type<TokenA, TokenB>(), 1);
assert!(sort_token_type<TokenAB, TokenB>(), 2);
}

#[test]
#[expected_failure(abort_code = ErrorTokenPairAleardyExist, location = rooch_dex::swap_utils)]
fun test_sort_token_type_same() {
sort_token_type<TokenA, TokenA>();
}

#[test]
fun test_get_amount_out() {
let amount_out = get_amount_out(100, 1000, 1000, FEE_RATE);
assert!(amount_out > 0, 0);
let expected = 90;
assert!(amount_out == expected, 1);
}

#[test]
#[expected_failure(abort_code = ErrorInputTokenAmount, location = rooch_dex::swap_utils)]
fun test_get_amount_out_zero_input() {
get_amount_out(0, 1000, 1000, FEE_RATE);
}

#[test]
#[expected_failure(abort_code = ErrorInsufficientLiquidity, location = rooch_dex::swap_utils)]
fun test_get_amount_out_zero_reserves() {
get_amount_out(100, 0, 0, FEE_RATE);
}
}

#[test_only]
module rooch_dex::test{
struct TokenC{}
#[test]
fun test_sort_token_type() {
// rooch_framework = 0x3, so it should be less than rooch_dex,
assert!(rooch_dex::swap_utils::sort_token_type<rooch_framework::gas_coin::RGas, TokenC>(), 1);
}
}
Loading