Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Jul 29, 2024
1 parent a3044a2 commit f6ebb53
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
6 changes: 5 additions & 1 deletion contracts/pool_stable/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ pub fn compute_swap(

let greatest_precision = get_greatest_precision(env);

soroban_sdk::testutils::arbitrary::std::dbg!("DBG");
let new_ask_pool = calc_y(
env,
amp as u128,
Expand All @@ -955,11 +956,14 @@ pub fn compute_swap(
scale_value(offer_pool, offer_pool_precision, DECIMAL_PRECISION),
scale_value(ask_pool, ask_pool_precision, DECIMAL_PRECISION),
],
greatest_precision,
ask_pool_precision,
);
soroban_sdk::testutils::arbitrary::std::dbg!("DBG");

soroban_sdk::testutils::arbitrary::std::dbg!("DBG", ask_pool, new_ask_pool);
let return_amount = ask_pool - new_ask_pool;
// We consider swap rate 1:1 in stable swap thus any difference is considered as spread.
soroban_sdk::testutils::arbitrary::std::dbg!("ALL GOOD");
let spread_amount = if offer_amount > return_amount {
convert_u128_to_i128(offer_amount - return_amount)
} else {
Expand Down
7 changes: 7 additions & 0 deletions contracts/pool_stable/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const DECIMAL_FRACTIONAL: u128 = 1_000_000_000_000_000_000;
const TOL: u128 = 1000000000000;

pub fn scale_value(atomics: u128, decimal_places: u32, target_decimal_places: u32) -> u128 {
soroban_sdk::testutils::arbitrary::std::dbg!(
atomics,
decimal_places,
target_decimal_places,
decimal_places < target_decimal_places
);
const TEN: u128 = 10;

if decimal_places < target_decimal_places {
Expand Down Expand Up @@ -151,6 +157,7 @@ pub(crate) fn calc_y(
xp: &[u128],
target_precision: u32,
) -> u128 {
soroban_sdk::testutils::arbitrary::std::dbg!(amp, new_amount, xp, target_precision);
let n_coins = U256::from_u128(env, N_COINS);
let new_amount = U256::from_u128(env, new_amount);

Expand Down
94 changes: 93 additions & 1 deletion contracts/pool_stable/src/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ fn simple_swap_should_panic_after_deadline() {
}

#[test]
fn simple_swap_with_two_tokens_both_with_18_decimals() {
fn simple_swap_with_two_tokens_both_with_7_decimals() {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();
Expand Down Expand Up @@ -573,3 +573,95 @@ fn simple_swap_with_two_tokens_both_with_18_decimals() {
assert_eq!(token1.balance(&user), 99);
assert_eq!(token2.balance(&user), 101);
}

#[test]
fn simple_swap_with_tokens_with_6_8_decimals() {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();

let admin = Address::generate(&env);
let manager = Address::generate(&env);
let factory = Address::generate(&env);
let user = Address::generate(&env);

let mut token1 = token_contract::Client::new(
&env,
&env.register_contract_wasm(None, token_contract::WASM),
);

token1.initialize(
&admin,
&6,
&"name1".into_val(&env),
&"symbol1".into_val(&env),
);

let mut token2 = token_contract::Client::new(
&env,
&env.register_contract_wasm(None, token_contract::WASM),
);

token2.initialize(
&admin,
&8,
&"name2".into_val(&env),
&"symbol2".into_val(&env),
);

if token2.address < token1.address {
std::mem::swap(&mut token1, &mut token2);
}

let swap_fees = 0i64;
let pool = deploy_stable_liquidity_pool_contract(
&env,
None,
(&token1.address, &token2.address),
swap_fees,
None,
None,
None,
manager,
factory,
None,
);

token1.mint(&user, &1_100);
token2.mint(&user, &1_100);
pool.provide_liquidity(&user, &1_000, &1_000, &None, &None::<u64>);

let spread = 100i64; // 1% maximum spread allowed
pool.swap(
&user,
&token1.address,
&1,
&None,
&Some(spread),
&None::<u64>,
);

let share_token_address = pool.query_share_token_address();
let result = pool.query_pool_info();

assert_eq!(
result,
PoolResponse {
asset_a: Asset {
address: token1.address.clone(),
amount: 1_001i128,
},
asset_b: Asset {
address: token2.address.clone(),
amount: 999i128,
},
asset_lp_share: Asset {
address: share_token_address.clone(),
amount: 1_000i128,
},
stake_address: pool.query_stake_contract_address(),
}
);
assert_eq!(token1.balance(&user), 99);
assert_eq!(token2.balance(&user), 101);
}

0 comments on commit f6ebb53

Please sign in to comment.