Skip to content

Commit

Permalink
Merge pull request #5127 from mnaamani/prevent-amm-buy-sell-while-rev…
Browse files Browse the repository at this point in the history
…enue-split

Amm buy/sell operations not allowed during revenue split
  • Loading branch information
mnaamani committed Apr 6, 2024
2 parents dc6d1a7 + 288a6ed commit a4625f2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions runtime-modules/project-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ decl_module! {
/// - user usable JOY balance must be enough for buying (+ existential deposit)
/// - slippage tolerance constraints respected if provided
/// - token total supply and amount value must be s.t. `eval` function doesn't overflow
/// - token supply can be modified (there is no active revenue split)
///
/// Postconditions
/// - `amount` CRT minted into account (which is created if necessary with existential deposit transferred to it)
Expand All @@ -911,6 +912,8 @@ decl_module! {
)?;

let token_data = Self::ensure_token_exists(token_id)?;
token_data.ensure_can_modify_supply::<T>()?;

let curve = token_data.amm_curve.ok_or(Error::<T>::NotInAmmState)?;

let user_account_data_exists = AccountInfoByTokenAndMember::<T>::contains_key(token_id, member_id);
Expand Down Expand Up @@ -975,6 +978,7 @@ decl_module! {
/// - slippage tolerance constraints respected if provided
/// - token total supply and amount value must be s.t. `eval` function doesn't overflow
/// - amm treasury account must have sufficient JOYs for the operation
/// - token supply can be modified (there is no active revenue split)
///
/// Postconditions
/// - `amount` burned from user account
Expand All @@ -997,6 +1001,8 @@ decl_module! {
)?;

let token_data = Self::ensure_token_exists(token_id)?;
token_data.ensure_can_modify_supply::<T>()?;

let curve = token_data.amm_curve.ok_or(Error::<T>::NotInAmmState)?;
let user_acc_data = Self::ensure_account_data_exists(token_id, &member_id)?;

Expand Down
51 changes: 51 additions & 0 deletions runtime-modules/project-token/src/tests/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,34 @@ fn amm_buy_succeeds_with_existing_user() {
})
}

#[test]
fn amm_buy_fails_with_revenue_split() {
let (user_member_id, user_account_id) = member!(1);
build_default_test_externalities_with_balances(vec![(
user_account_id,
DEFAULT_SPLIT_REVENUE
+ 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();

IssueRevenueSplitFixture::default().execute_call().unwrap();

let result = AmmBuyFixture::default()
.with_sender(user_account_id)
.with_amount(DEFAULT_AMM_BUY_AMOUNT)
.with_member_id(user_member_id)
.execute_call();

assert_err!(
result,
Error::<Test>::CannotModifySupplyWhenRevenueSplitsAreActive
);
})
}

#[test]
fn amm_buy_failed_with_slippage_constraint_violated() {
let slippage_tolerance = (Permill::zero(), Balance::zero());
Expand Down Expand Up @@ -875,6 +903,29 @@ fn amm_sell_ok_with_event_deposited() {
})
}

#[test]
fn amm_sell_fails_with_revenue_split() {
build_default_test_externalities_with_balances(vec![(
member!(1).1,
DEFAULT_AMM_BUY_AMOUNT + DEFAULT_SPLIT_REVENUE + ExistentialDeposit::get(),
)])
.execute_with(|| {
IssueTokenFixture::default().execute_call().unwrap();
TransferFixture::default().execute_call().unwrap();
IssueRevenueSplitFixture::default().execute_call().unwrap();
ActivateAmmFixture::default().execute_call().unwrap();

let result = AmmSellFixture::default()
.with_amount(DEFAULT_AMM_BUY_AMOUNT)
.execute_call();

assert_err!(
result,
Error::<Test>::CannotModifySupplyWhenRevenueSplitsAreActive
);
})
}

// ------------------- DEACTIVATE ---------------------------------------

#[test]
Expand Down

0 comments on commit a4625f2

Please sign in to comment.