From 3d2a7b247d03a6c5614ec85dc96b368137eef950 Mon Sep 17 00:00:00 2001 From: Ignazio Bovo Date: Tue, 20 Feb 2024 12:08:55 +0100 Subject: [PATCH] fix: :bug: correct event deposit quantities --- runtime-modules/project-token/src/lib.rs | 4 +- .../project-token/src/tests/amm.rs | 46 ++++++++----------- runtime-modules/project-token/src/types.rs | 6 +-- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/runtime-modules/project-token/src/lib.rs b/runtime-modules/project-token/src/lib.rs index 849dc79447a..c3db8c8a466 100644 --- a/runtime-modules/project-token/src/lib.rs +++ b/runtime-modules/project-token/src/lib.rs @@ -900,7 +900,7 @@ decl_module! { // burn tx_fee * price let _ = burn_from_usable::(&amm_treasury_account, buy_tx_fee); - Self::deposit_event(RawEvent::TokensBoughtOnAmm(token_id, member_id, amount, price)); + Self::deposit_event(RawEvent::TokensBoughtOnAmm(token_id, member_id, amount, buy_price)); Ok(()) } @@ -976,7 +976,7 @@ decl_module! { // burn tx_fee * price let _ = burn_from_usable::(&amm_treasury_account, sell_tx_fee); - Self::deposit_event(RawEvent::TokensSoldOnAmm(token_id, member_id, amount, price)); + Self::deposit_event(RawEvent::TokensSoldOnAmm(token_id, member_id, amount, sell_price)); Ok(()) } diff --git a/runtime-modules/project-token/src/tests/amm.rs b/runtime-modules/project-token/src/tests/amm.rs index d16eb8987b1..f60e3d65384 100644 --- a/runtime-modules/project-token/src/tests/amm.rs +++ b/runtime-modules/project-token/src/tests/amm.rs @@ -281,27 +281,24 @@ fn user_joy_balance_correctly_decreased_during_amm_buy() { fn amm_buy_ok_with_event_deposit() { let token_id = token!(1); let (user_member_id, user_account_id) = member!(2); - let price = amm_function_values(DEFAULT_AMM_BUY_AMOUNT, Zero::zero(), AmmOperation::Buy); - build_default_test_externalities_with_balances(vec![( - user_account_id, - amm_function_buy_values_with_tx_fees(DEFAULT_AMM_BUY_AMOUNT, Zero::zero()) + ed(), - )]) - .execute_with(|| { - IssueTokenFixture::default().execute_call().unwrap(); - ActivateAmmFixture::default().execute_call().unwrap(); - - AmmBuyFixture::default() - .with_amount(DEFAULT_AMM_BUY_AMOUNT) - .execute_call() - .unwrap(); + let buy_price = amm_function_buy_values_with_tx_fees(DEFAULT_AMM_BUY_AMOUNT, Zero::zero()); + build_default_test_externalities_with_balances(vec![(user_account_id, buy_price + ed())]) + .execute_with(|| { + IssueTokenFixture::default().execute_call().unwrap(); + ActivateAmmFixture::default().execute_call().unwrap(); - last_event_eq!(RawEvent::TokensBoughtOnAmm( - token_id, - user_member_id, - DEFAULT_AMM_BUY_AMOUNT, - price, - )); - }) + AmmBuyFixture::default() + .with_amount(DEFAULT_AMM_BUY_AMOUNT) + .execute_call() + .unwrap(); + + last_event_eq!(RawEvent::TokensBoughtOnAmm( + token_id, + user_member_id, + DEFAULT_AMM_BUY_AMOUNT, + buy_price, + )); + }) } // --------------- ACTIVATION ---------------------------------- @@ -859,11 +856,8 @@ fn amm_sell_ok_with_event_deposited() { .amm_curve .unwrap() .provided_supply; - let price = amm_function_values( - DEFAULT_AMM_SELL_AMOUNT, - amm_provided_supply, - AmmOperation::Sell, - ); + let sell_price = + amm_function_sell_values_with_tx_fees(DEFAULT_AMM_SELL_AMOUNT, amm_provided_supply); AmmSellFixture::default() .with_amount(DEFAULT_AMM_SELL_AMOUNT) @@ -876,7 +870,7 @@ fn amm_sell_ok_with_event_deposited() { token_id, user_member_id, DEFAULT_AMM_SELL_AMOUNT, - price, + sell_price, )); }) } diff --git a/runtime-modules/project-token/src/types.rs b/runtime-modules/project-token/src/types.rs index 04ee66318ce..cb725943818 100644 --- a/runtime-modules/project-token/src/types.rs +++ b/runtime-modules/project-token/src/types.rs @@ -647,15 +647,13 @@ impl AmmCurve { self.provided_supply.add(amount) }; - let res = amm_eval_inner::( + amm_eval_inner::( provided_supply_pre, provided_supply_post, self.slope, self.intercept, ) - .ok_or(Error::::ArithmeticError.into()); - - res + .ok_or_else(|| Error::::ArithmeticError.into()) } }