Skip to content

Commit

Permalink
fix: 🐛 correct event deposit quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignazio Bovo committed Feb 20, 2024
1 parent 1931718 commit 3d2a7b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
4 changes: 2 additions & 2 deletions runtime-modules/project-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ decl_module! {
// burn tx_fee * price
let _ = burn_from_usable::<T>(&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(())
}
Expand Down Expand Up @@ -976,7 +976,7 @@ decl_module! {
// burn tx_fee * price
let _ = burn_from_usable::<T>(&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(())
}
Expand Down
46 changes: 20 additions & 26 deletions runtime-modules/project-token/src/tests/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------
Expand Down Expand Up @@ -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)
Expand All @@ -876,7 +870,7 @@ fn amm_sell_ok_with_event_deposited() {
token_id,
user_member_id,
DEFAULT_AMM_SELL_AMOUNT,
price,
sell_price,
));
})
}
Expand Down
6 changes: 2 additions & 4 deletions runtime-modules/project-token/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,15 +647,13 @@ impl<Balance: TokenBalanceTrait> AmmCurve<Balance> {
self.provided_supply.add(amount)
};

let res = amm_eval_inner::<Balance>(
amm_eval_inner::<Balance>(
provided_supply_pre,
provided_supply_post,
self.slope,
self.intercept,
)
.ok_or(Error::<T>::ArithmeticError.into());

res
.ok_or_else(|| Error::<T>::ArithmeticError.into())
}
}

Expand Down

0 comments on commit 3d2a7b2

Please sign in to comment.