Skip to content

Commit

Permalink
Merge pull request #22 from aldrin-labs/critical-liquidity-dep-fix
Browse files Browse the repository at this point in the history
Fix decimal places used with asset's LP tokens
  • Loading branch information
rockbmb authored Apr 22, 2024
2 parents c350b31 + 461b57a commit f00bda1
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 72 deletions.
17 changes: 6 additions & 11 deletions ramm-sui/sources/math.move
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ module ramm_sui::math {
lp_tokens_issued: &VecMap<u8, u256>,
prices: &VecMap<u8, u256>,
factors_for_balances: &VecMap<u8, u256>,
factor_lpt: u256,
factors_for_prices: &VecMap<u8, u256>,
prec: u8,
max_prec: u8,
Expand All @@ -257,9 +256,10 @@ module ramm_sui::math {
let _N = (vec_map::size(balances) as u8);
let mut j: u8 = 0;
while (j < _N) {
let factor_j = *vec_map::get(factors_for_balances, &j);
let price_j = *vec_map::get(prices, &j) * *vec_map::get(factors_for_prices, &j);
_B = _B + mul(price_j, *vec_map::get(balances, &j) * *vec_map::get(factors_for_balances, &j), prec, max_prec);
_L = _L + mul(price_j, *vec_map::get(lp_tokens_issued, &j) * factor_lpt, prec, max_prec);
_B = _B + mul(price_j, *vec_map::get(balances, &j) * factor_j, prec, max_prec);
_L = _L + mul(price_j, *vec_map::get(lp_tokens_issued, &j) * factor_j, prec, max_prec);

j = j + 1;
};
Expand All @@ -275,7 +275,6 @@ module ramm_sui::math {
lp_tokens_issued: &VecMap<u8, u256>,
prices: &VecMap<u8, u256>,
factors_for_balances: &VecMap<u8, u256>,
factor_lpt: u256,
factors_for_prices: &VecMap<u8, u256>,
prec: u8,
max_prec: u8,
Expand All @@ -285,7 +284,6 @@ module ramm_sui::math {
lp_tokens_issued,
prices,
factors_for_balances,
factor_lpt,
factors_for_prices,
prec,
max_prec,
Expand All @@ -297,9 +295,10 @@ module ramm_sui::math {
let mut j: u8 = 0;
while (j < _N) {
if (*vec_map::get(lp_tokens_issued, &j) != 0) {
let factor_j = *vec_map::get(factors_for_balances, &j);
let val = div(
mul(*vec_map::get(balances, &j) * *vec_map::get(factors_for_balances, &j), _L, prec, max_prec),
mul(_B, *vec_map::get(lp_tokens_issued, &j) * factor_lpt, prec, max_prec),
mul(_B, *vec_map::get(lp_tokens_issued, &j) * factor_j, prec, max_prec),
prec, max_prec
);
vec_map::insert(&mut imbs, j, val);
Expand All @@ -317,7 +316,7 @@ module ramm_sui::math {
/// or if they would be closer to the range than before the trade.
///
/// As is the case with other functions in this module, the parameters
/// `factor_lpt, prec, max_prec, one, delta`
/// `prec, max_prec, one, delta`
/// will be constants defined in the `ramm.move` module, and passed to this function.
///
/// Sui Move does not permit sharing of constants between modules.
Expand All @@ -331,7 +330,6 @@ module ramm_sui::math {
ao: u256,
pr_fee: u256,
factors_for_balances: &VecMap<u8, u256>,
factor_lpt: u256,
factors_for_prices: &VecMap<u8, u256>,
prec: u8,
max_prec: u8,
Expand Down Expand Up @@ -361,7 +359,6 @@ module ramm_sui::math {
lp_tokens_issued,
prices,
factors_for_balances,
factor_lpt,
factors_for_prices,
prec,
max_prec
Expand All @@ -385,7 +382,6 @@ module ramm_sui::math {
i: u8,
o: u8,
factors_for_balances: &VecMap<u8, u256>,
factor_lpt: u256,
factors_for_prices: &VecMap<u8, u256>,
base_fee: u256,
base_leverage: u256,
Expand All @@ -397,7 +393,6 @@ module ramm_sui::math {
lp_tokens_issued,
prices,
factors_for_balances,
factor_lpt,
factors_for_prices,
prec,
max_prec,
Expand Down
22 changes: 8 additions & 14 deletions ramm-sui/sources/ramm.move
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ module ramm_sui::ramm {
/// Decimal places that LP tokens will be using; may yet change.
//const LP_TOKENS_DECIMAL_PLACES: u8 = 9;

/// Factor to apply to LP token amounts during calculations.
const FACTOR_LPT: u256 = 1_000_000_000_000 / 1_000_000_000; // FACTOR_LPT = 10**(PRECISION_DECIMAL_PLACES-LP_TOKENS_DECIMAL_PLACES)
/// Value of `1` using `PRECISION_DECIMAL_PLACES`; useful to scale other values to
/// the baseline precision.
///
Expand Down Expand Up @@ -1538,7 +1536,6 @@ module ramm_sui::ramm {
&self.lp_tokens_issued,
prices,
&self.factors_for_balances,
FACTOR_LPT,
factors_for_prices,
PRECISION_DECIMAL_PLACES,
MAX_PRECISION_DECIMAL_PLACES,
Expand All @@ -1559,7 +1556,6 @@ module ramm_sui::ramm {
&self.lp_tokens_issued,
prices,
&self.factors_for_balances,
FACTOR_LPT,
factors_for_prices,
PRECISION_DECIMAL_PLACES,
MAX_PRECISION_DECIMAL_PLACES,
Expand Down Expand Up @@ -1588,7 +1584,6 @@ module ramm_sui::ramm {
ao,
pr_fee,
&self.factors_for_balances,
FACTOR_LPT,
factors_for_prices,
PRECISION_DECIMAL_PLACES,
MAX_PRECISION_DECIMAL_PLACES,
Expand Down Expand Up @@ -1623,7 +1618,6 @@ module ramm_sui::ramm {
i,
o,
&self.factors_for_balances,
FACTOR_LPT,
factors_for_prices,
BASE_FEE,
BASE_LEVERAGE,
Expand Down Expand Up @@ -1891,7 +1885,7 @@ module ramm_sui::ramm {
let lpt: u64 = ai;
return lpt
} else {
let lpt: u256 = div(mul((ai as u256) * factor_i, _L), _B) / FACTOR_LPT;
let lpt: u256 = div(mul((ai as u256) * factor_i, _L), _B) / factor_i;
return (lpt as u64)
}
};
Expand All @@ -1906,10 +1900,10 @@ module ramm_sui::ramm {
mul3(
(ai as u256) * factor_i,
ri,
get_typed_lptok_issued<AssetIn>(self, i) * FACTOR_LPT
get_typed_lptok_issued<AssetIn>(self, i) * factor_i
),
bi
) / FACTOR_LPT;
) / factor_i;
return (lpt as u64)
} else {
return 0
Expand Down Expand Up @@ -1958,7 +1952,7 @@ module ramm_sui::ramm {
// Recall that in this case, the "Case 1" step below is skipped, and the withdrawal
// continues with different tokens - see below.
if (get_bal(self, o) == 0) {
*ao = div(mul(lpt * FACTOR_LPT, _B), _L) / factor_o;
*ao = div(mul(lpt * factor_o, _B), _L) / factor_o;
*a_remaining = *ao;
};

Expand All @@ -1969,14 +1963,14 @@ module ramm_sui::ramm {

// Case 1.1
if (lpt < get_typed_lptok_issued<AssetOut>(self, o)) {
*ao = div(mul(lpt * FACTOR_LPT, bo), mul(_Lo, ro)) / factor_o;
*ao = div(mul(lpt * factor_o, bo), mul(_Lo, ro)) / factor_o;
let max_token_o: &mut u256 = &mut 0;

if (ONE - DELTA < ro) {
let min_token_o: u256 = div(mul3(_B, (get_lptok_issued(self, o) - lpt) * FACTOR_LPT, ONE - DELTA), _L) / factor_o;
let min_token_o: u256 = div(mul3(_B, (get_lptok_issued(self, o) - lpt) * factor_o, ONE - DELTA), _L) / factor_o;
*max_token_o = get_bal(self, o) - min_token_o;
} else {
*max_token_o = div(mul(lpt * FACTOR_LPT, bo), _Lo) / factor_o;
*max_token_o = div(mul(lpt * factor_o, bo), _Lo) / factor_o;
};

let amount_out_o: &mut u256 = vec_map::get_mut(&mut amounts_out, &o);
Expand Down Expand Up @@ -2052,7 +2046,7 @@ module ramm_sui::ramm {
),
*vec_map::get(&prices, &k) * *vec_map::get(&factors_for_prices, &k)
) / factor_k;
let _Lk: u256 = get_lptok_issued(self, k) * FACTOR_LPT;
let _Lk: u256 = get_lptok_issued(self, k) * factor_k;
// Mk = bk-(1.0-DELTA)*Lk*B/L
let min_token_k: u256 = div(mul3(_B, _Lk, ONE - DELTA), _L) / factor_k;
let max_token_k: u256 = get_bal(self, k) - min_token_k;
Expand Down
10 changes: 5 additions & 5 deletions ramm-sui/tests/interface2_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ module ramm_sui::interface2_tests {
test_utils::assert_eq(ramm::get_balance<ETH>(&ramm), 480 * test_util::eth_factor());
test_utils::assert_eq(ramm::get_typed_balance<ETH>(&ramm), 480 * test_util::eth_factor());

let total_usdt: u256 = 940044_93561689;
let total_usdt: u256 = 940044_935617;
test_utils::assert_eq(ramm::get_balance<USDT>(&ramm), total_usdt);
test_utils::assert_eq(ramm::get_typed_balance<USDT>(&ramm), total_usdt);

// Later in the test, when accounting for all the USDT given to liquidity providers
// and comparing it to the pool's USDT balance, liquidity withdrawal fees count toward
// this tally, but protocol trading fees do not - they must be removed from the count.
let usdt_trade_fees = 1_201_708_581;
let usdt_trade_fees = 1_201_708_5;

test_utils::assert_eq(ramm::get_collected_protocol_fees<ETH>(&ramm), 0);
test_utils::assert_eq(ramm::get_collected_protocol_fees<USDT>(&ramm), usdt_trade_fees);
Expand Down Expand Up @@ -147,7 +147,7 @@ module ramm_sui::interface2_tests {
test_utils::assert_eq(coin::value(&eth), (47808 * test_util::eth_factor() / 100 as u64));

let usdt = test_scenario::take_from_address<Coin<USDT>>(scenario, ADMIN);
let first_usdt_wthdrwl: u256 = 39863_55571872;
let first_usdt_wthdrwl: u256 = 39863_555719;
test_utils::assert_eq((coin::value(&usdt) as u256), first_usdt_wthdrwl);

test_scenario::return_to_address(ADMIN, eth);
Expand Down Expand Up @@ -195,7 +195,7 @@ module ramm_sui::interface2_tests {
let snd_udst_wthdrwl: u256 = {
let usdt = test_scenario::take_from_address<Coin<USDT>>(scenario, ADMIN);

let snd_udst_wthdrwl: u256 = 896421_20015571;
let snd_udst_wthdrwl: u256 = 896421_200156;
test_utils::assert_eq((coin::value(&usdt) as u256), snd_udst_wthdrwl);
test_scenario::return_to_address(ADMIN, usdt);

Expand Down Expand Up @@ -245,7 +245,7 @@ module ramm_sui::interface2_tests {
let initial_eth_balance: u256 = ramm::get_typed_balance<ETH>(&ramm);
let initial_usdt_balance: u256 = ramm::get_typed_balance<USDT>(&ramm);
test_utils::assert_eq(initial_eth_balance, 500 * test_util::eth_factor());
test_utils::assert_eq(initial_usdt_balance, 900_000 * test_util::eth_factor());
test_utils::assert_eq(initial_usdt_balance, 900_000 * test_util::usdc_factor());

let amount_in = coin::mint_for_testing<ETH>(eth_trade_amount, test_scenario::ctx(scenario));
interface2::trade_amount_in_2<ETH, USDT>(
Expand Down
2 changes: 1 addition & 1 deletion ramm-sui/tests/interface3_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ module ramm_sui::interface3_tests {
let initial_usdt_balance: u256 = ramm::get_typed_balance<USDT>(&ramm);
test_utils::assert_eq(initial_eth_balance, 200 * test_util::eth_factor());
test_utils::assert_eq(initial_matic_balance, 200_000 * test_util::eth_factor());
test_utils::assert_eq(initial_usdt_balance, 400_000 * test_util::eth_factor());
test_utils::assert_eq(initial_usdt_balance, 400_000 * test_util::usdc_factor());

let amount_in = coin::mint_for_testing<ETH>(eth_trade_amount, test_scenario::ctx(scenario));
interface3::trade_amount_in_3<ETH, USDT, MATIC>(
Expand Down
Loading

0 comments on commit f00bda1

Please sign in to comment.