Skip to content

Commit

Permalink
try check scale factor and improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Feb 24, 2025
1 parent 305c470 commit 14fb0bf
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 160 deletions.

Large diffs are not rendered by default.

53 changes: 39 additions & 14 deletions onchain/cairo/launchpad/src/launchpad/launchpad.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1564,14 +1564,20 @@ pub mod LaunchpadMarketplace {

// TODO: check if this is correct
// Adjust scale factor

let scale_factor = pow_256(10, 18);
// Initial pool need to be more than liquidity raised
// CHECK liquidity raised and initial pool supply to avoir division by zero, overflow, rounding or 0 value
// CHECK liquidity raised and initial pool supply to avoir division by zero, overflow,
// rounding or 0 value
let mut x_y = if is_token1_quote {
(launch.liquidity_raised ) / launch.initial_pool_supply
// (launch.liquidity_raised * pow_256(10, 18)) / (launch.initial_pool_supply * pow_256(10, 18))
// (launch.liquidity_raised ) / launch.initial_pool_supply
// (launch.liquidity_raised * pow_256(10, 18)) / (launch.initial_pool_supply *
// pow_256(10, 18))
// (launch.liquidity_raised * scale_factor) / (launch.initial_pool_supply *
// scale_factor)
(launch.liquidity_raised * scale_factor) / launch.initial_pool_supply
} else {
(launch.initial_pool_supply) / launch.liquidity_raised

};

println!("x_y {}", x_y.clone());
Expand Down Expand Up @@ -1601,6 +1607,13 @@ pub mod LaunchpadMarketplace {

// Simple sqrt unfixed
// Fixed point sqrt_ratio
println!("x_y before unscale {}", x_y.clone());
println!("is_token1_quote {}", is_token1_quote.clone());
if is_token1_quote {
x_y = x_y / scale_factor;
}
println!("x_y after unscale {}", x_y.clone());

let mut sqrt_ratio = sqrt(x_y) * pow_256(2, 96);
// println!("sqrt_ratio {}", sqrt_ratio.clone());

Expand All @@ -1609,17 +1622,29 @@ pub mod LaunchpadMarketplace {
let min_sqrt_ratio_limit = MIN_SQRT_RATIO;
let max_sqrt_ratio_limit = MAX_SQRT_RATIO;

sqrt_ratio = if sqrt_ratio < min_sqrt_ratio_limit {
println!("sqrt_ratio < min_sqrt_ratio_limit");
min_sqrt_ratio_limit
} else if sqrt_ratio > max_sqrt_ratio_limit {
println!("sqrt_ratio > max_sqrt_ratio_limit");
max_sqrt_ratio_limit
} else {
println!("sqrt_ratio is between min and max");
sqrt_ratio
};
// Assert range for sqrt ratio order, magnitude and min max

// Unscale sqrt_ratio with the factor before using the sqrt function
// println!("sqrt_ratio before unscale {}", sqrt_ratio.clone());

// if is_token1_quote {
// sqrt_ratio = sqrt_ratio / scale_factor;
// }
// println!("sqrt_ratio after unscale {}", sqrt_ratio.clone());

sqrt_ratio =
if sqrt_ratio < min_sqrt_ratio_limit {
println!("sqrt_ratio < min_sqrt_ratio_limit");
min_sqrt_ratio_limit
} else if sqrt_ratio > max_sqrt_ratio_limit {
println!("sqrt_ratio > max_sqrt_ratio_limit");
max_sqrt_ratio_limit
} else {
println!("sqrt_ratio is between min and max");
sqrt_ratio
};

println!("sqrt_ratio after assert {}", sqrt_ratio.clone());
// Define the minimum and maximum sqrt ratios
// Convert to a tick value
let mut call_data: Array<felt252> = array![];
Expand Down
15 changes: 9 additions & 6 deletions onchain/cairo/launchpad/src/launchpad/unrug.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ pub mod UnrugLiquidity {

let is_token1_quote = launch_params.quote_address == token1;
println!("is_token1_quote {:?}", is_token1_quote);
let (initial_tick, full_range_bounds_initial) = get_initial_tick_from_starting_price(
let (initial_tick, full_range_bounds_initial) =
get_initial_tick_from_starting_price(
launch_params.pool_params.starting_price,
launch_params.pool_params.bound,
is_token1_quote
Expand All @@ -585,7 +586,7 @@ pub mod UnrugLiquidity {
// let min_tick = MIN_TICK.try_into().unwrap();
// let max_tick = MAX_TICK.try_into().unwrap();

// TODO
// TODO
// Check align ticks based on tick spacing and fee
let min_tick = MIN_TICK_U128.try_into().unwrap();
let max_tick = MAX_TICK_U128.try_into().unwrap();
Expand Down Expand Up @@ -613,13 +614,15 @@ pub mod UnrugLiquidity {
};
// let bounds_full_range = if is_token1_quote {
// Bounds {
// lower: i129 { mag: aligned_min_tick.try_into().unwrap(), sign: true },
// upper: i129 { mag: aligned_max_tick.try_into().unwrap(), sign: false }
// lower: i129 { mag: aligned_min_tick.try_into().unwrap(), sign: true
// }, upper: i129 { mag: aligned_max_tick.try_into().unwrap(), sign:
// false }
// }
// } else {
// Bounds {
// lower: i129 { mag: aligned_min_tick.try_into().unwrap(), sign: true },
// upper: i129 { mag: aligned_max_tick.try_into().unwrap(), sign: false }
// lower: i129 { mag: aligned_min_tick.try_into().unwrap(), sign: true
// }, upper: i129 { mag: aligned_max_tick.try_into().unwrap(), sign:
// false }
// }
// };

Expand Down
2 changes: 1 addition & 1 deletion onchain/cairo/launchpad/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub mod tokens {

#[cfg(test)]
pub mod tests {
pub mod edge_cases_tests;
pub mod end_to_end_tests;
pub mod exponential_tests;
pub mod launchpad_tests;
pub mod linear_tests;
pub mod liquidity_tests;
pub mod unrug_tests;
pub mod edge_cases_tests;
}
71 changes: 36 additions & 35 deletions onchain/cairo/launchpad/src/tests/edge_cases_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ mod edge_cases_tests {
use afk_launchpad::interfaces::launchpad::{
ILaunchpadMarketplaceDispatcher, ILaunchpadMarketplaceDispatcherTrait,
};
use afk_launchpad::launchpad::utils::{
sort_tokens, get_initial_tick_from_starting_price, get_next_tick_bounds, unique_count,
calculate_aligned_bound_mag, align_tick, MIN_TICK, MAX_TICK, MAX_SQRT_RATIO, MIN_SQRT_RATIO,
align_tick_with_max_tick_and_min_tick
};
use afk_launchpad::utils::{sqrt};
use afk_launchpad::interfaces::unrug::{
IUnrugLiquidityDispatcher, IUnrugLiquidityDispatcherTrait,
};
Expand All @@ -19,6 +13,11 @@ mod edge_cases_tests {
// use afk_launchpad::launchpad::errors;
use afk_launchpad::launchpad::launchpad::LaunchpadMarketplace::{Event as LaunchpadEvent};
use afk_launchpad::launchpad::math::{PercentageMath};
use afk_launchpad::launchpad::utils::{
sort_tokens, get_initial_tick_from_starting_price, get_next_tick_bounds, unique_count,
calculate_aligned_bound_mag, align_tick, MIN_TICK, MAX_TICK, MAX_SQRT_RATIO, MIN_SQRT_RATIO,
align_tick_with_max_tick_and_min_tick
};
use afk_launchpad::tokens::erc20::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use afk_launchpad::tokens::memecoin::{IMemecoin, IMemecoinDispatcher, IMemecoinDispatcherTrait};
use afk_launchpad::types::launchpad_types::{
Expand All @@ -30,7 +29,9 @@ mod edge_cases_tests {
};

use afk_launchpad::types::launchpad_types::{MINTER_ROLE, ADMIN_ROLE};
use afk_launchpad::utils::{sqrt};
use core::num::traits::Zero;
use core::starknet::SyscallResultTrait;
use core::traits::Into;
use ekubo::interfaces::core::{ICoreDispatcher, ICoreDispatcherTrait};
use ekubo::interfaces::positions::{IPositionsDispatcher, IPositionsDispatcherTrait};
Expand All @@ -47,7 +48,6 @@ mod edge_cases_tests {
stop_cheat_caller_address_global, start_cheat_block_timestamp, DeclareResultTrait,
EventSpyAssertionsTrait
};
use core::starknet::SyscallResultTrait;

use starknet::syscalls::{call_contract_syscall, library_call_syscall};
use starknet::{ContractAddress, ClassHash, class_hash::class_hash_const};
Expand Down Expand Up @@ -507,7 +507,8 @@ mod edge_cases_tests {
// // quote_token_address: erc20.contract_address,
// // }
// // );
// // spy.assert_emitted(@array![(launchpad.contract_address, expected_launch_token_event)]);
// // spy.assert_emitted(@array![(launchpad.contract_address,
// expected_launch_token_event)]);
// let launched_token = launchpad.get_coin_launch(token_address);
// let default_supply = DEFAULT_INITIAL_SUPPLY();
// // assert(launched_token.owner == OWNER(), 'wrong owner');
Expand All @@ -525,7 +526,8 @@ mod edge_cases_tests {
// // 'wrong token holded'
// // );
// assert(
// launched_token.token_quote.token_address == erc20.contract_address, 'wrong token quote'
// launched_token.token_quote.token_address == erc20.contract_address, 'wrong token
// quote'
// );
// }

Expand Down Expand Up @@ -604,11 +606,15 @@ mod edge_cases_tests {
// println!("buy threshold liquidity");
// println!("buy threshold liquidity {:?}", THRESHOLD_LIQUIDITY/2_u256);
run_buy_by_amount(
launchpad, quote_token, memecoin, THRESHOLD_LIQUIDITY/2_u256, token_address, OWNER(),
launchpad,
quote_token,
memecoin,
THRESHOLD_LIQUIDITY / 2_u256,
token_address,
OWNER(),
);
// let balance_quote_launch = quote_token.balance_of(launchpad.contract_address);


// // Sell
let share_user = launchpad
.get_share_of_user_by_contract(sender.clone(), token_address.clone());
Expand All @@ -624,7 +630,7 @@ mod edge_cases_tests {

// println!("balance quote in loop {:?}", balance_quote_launch);

// Last buy before launch
// Last buy before launch

let mut liquidity_raised = launchpad.get_coin_launch(token_address).liquidity_raised;
println!("liquidity_raised before last {:?}", liquidity_raised);
Expand All @@ -643,7 +649,6 @@ mod edge_cases_tests {
init_supplies.at(i).clone()
);


liquidity_raised = launchpad.get_coin_launch(token_address).liquidity_raised;
println!("liquidity_raised {:?}", liquidity_raised);

Expand All @@ -660,28 +665,25 @@ mod edge_cases_tests {
let bound_spacing = 88719042;
// let bound_spacing = 887272;


// let fee = fee_percent.try_into().unwrap();
let (token0, token1) = sort_tokens(
token_address, quote_token.contract_address.clone()
);
let (token0, token1) = sort_tokens(token_address, quote_token.contract_address.clone());

let total_supply = memecoin.total_supply();
let is_token1_quote = quote_token.contract_address.clone() == token1.clone();
let INITIAL_POOL_SUPPLY = total_supply / LIQUIDITY_RATIO;
let launch = launchpad.get_coin_launch(token_address);
// TODO
// edge case related to scaling factor

let launch = launchpad.get_coin_launch(token_address);
// TODO
// edge case related to scaling factor
let mut x_y = if is_token1_quote {
// TODO scaling factor?
// (THRESHOLD_LIQUIDITY ) / INITIAL_POOL_SUPPLY
(launch.liquidity_raised ) / launch.initial_pool_supply
// (THRESHOLD_LIQUIDITY * pow_256(10, 18)) / INITIAL_POOL_SUPPLY * pow_256(10, 18)
// (launch.liquidity_raised ) / launch.initial_pool_supply
(THRESHOLD_LIQUIDITY * pow_256(10, 18)) / (INITIAL_POOL_SUPPLY * pow_256(10, 18))
} else {
// TODO scaling factor?
// INITIAL_POOL_SUPPLY / THRESHOLD_LIQUIDITY
(launch.initial_pool_supply ) / launch.liquidity_raised
(launch.initial_pool_supply) / launch.liquidity_raised
};

let sqrt_ratio = sqrt(x_y) * pow_256(2, 96);
Expand All @@ -708,8 +710,8 @@ mod edge_cases_tests {
let pool_key = PoolKey {
// token0: token_address.clone(),
// token1: erc20.contract_address.clone(),
token0:token0.clone(),
token1:token1.clone(),
token0: token0.clone(),
token1: token1.clone(),
fee: fee.clone(),
tick_spacing: tick_spacing.try_into().unwrap(),
extension: 0.try_into().unwrap(),
Expand All @@ -719,40 +721,39 @@ mod edge_cases_tests {
let core = ICoreDispatcher { contract_address: EKUBO_CORE() };
let liquidity = core.get_pool_liquidity(pool_key);
println!("pool liquidity {:?}", liquidity);
let position_dispatcher = IPositionsDispatcher { contract_address: EKUBO_POSITIONS() };
let position_dispatcher = IPositionsDispatcher { contract_address: EKUBO_POSITIONS() };
let price = core.get_pool_price(pool_key);
let pool_price = position_dispatcher.get_pool_price(pool_key);

let reserve_quote = IERC20Dispatcher { contract_address: quote_address }
.balance_of(EKUBO_POSITIONS());

let reserve_memecoin = memecoin.balance_of(core.contract_address);

let reserve_quote = IERC20Dispatcher { contract_address: quote_address }
.balance_of(EKUBO_POSITIONS());

println!("reserve_memecoin {:?}", reserve_memecoin);
println!("reserve_quote {:?}", reserve_quote);

assert(
reserve_memecoin >= PercentageMath::percent_mul(INITIAL_POOL_SUPPLY, 9800),
'reserve too low meme'
);

assert(
reserve_quote >= PercentageMath::percent_mul(THRESHOLD_LIQUIDITY, 9800),
'reserve too low quote'
);



// let lp_meme_supply = total_supply / LIQUIDITY_RATIO;
// let total_token_holded = total_supply / LIQUIDITY_RATIO;
// println!("lp_meme_supply {:?}", lp_meme_supply);
println!("pool.sqrt_ratio {:?}", pool_price.sqrt_ratio);
println!("sqrt_ratio {:?}", sqrt_ratio);
println!("initial_tick mag {:?}", initial_tick.mag);
println!("initial_tick mag {:?}", initial_tick.mag);
println!("initial_tick sign {:?}", initial_tick.sign);
println!("tick mag {:?}", pool_price.tick.mag);
println!("tick mag {:?}", pool_price.tick.mag);
println!("tick sign {:?}", pool_price.tick.sign);

// // assert(lp_meme_supply == INITIAL_POOL_SUPPLY, "wrong initial pool supply");
Expand Down
12 changes: 6 additions & 6 deletions onchain/cairo/launchpad/src/tests/end_to_end_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ mod end_to_end_tests {
use afk_launchpad::interfaces::launchpad::{
ILaunchpadMarketplaceDispatcher, ILaunchpadMarketplaceDispatcherTrait,
};

use afk_launchpad::launchpad::utils::{
sort_tokens, get_initial_tick_from_starting_price, get_next_tick_bounds, unique_count,
calculate_aligned_bound_mag, align_tick, MIN_TICK, MAX_TICK, MAX_SQRT_RATIO, MIN_SQRT_RATIO,
align_tick_with_max_tick_and_min_tick
};
use afk_launchpad::interfaces::unrug::{
IUnrugLiquidityDispatcher, IUnrugLiquidityDispatcherTrait,
};
Expand All @@ -19,6 +13,12 @@ mod end_to_end_tests {
// use afk_launchpad::launchpad::errors;
use afk_launchpad::launchpad::launchpad::LaunchpadMarketplace::{Event as LaunchpadEvent};
use afk_launchpad::launchpad::math::{PercentageMath};

use afk_launchpad::launchpad::utils::{
sort_tokens, get_initial_tick_from_starting_price, get_next_tick_bounds, unique_count,
calculate_aligned_bound_mag, align_tick, MIN_TICK, MAX_TICK, MAX_SQRT_RATIO, MIN_SQRT_RATIO,
align_tick_with_max_tick_and_min_tick
};
use afk_launchpad::tokens::erc20::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use afk_launchpad::tokens::memecoin::{IMemecoin, IMemecoinDispatcher, IMemecoinDispatcherTrait};
use afk_launchpad::types::launchpad_types::{
Expand Down
Loading

0 comments on commit 14fb0bf

Please sign in to comment.