Skip to content

Commit

Permalink
add tests for deposit and withdraw edge cases
Browse files Browse the repository at this point in the history
Signed-off-by: Charles Ferrell <[email protected]>
  • Loading branch information
ferrell-code committed Nov 19, 2023
1 parent eb17f95 commit ed041c2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
6 changes: 4 additions & 2 deletions pallets/pallet-lottery/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use manta_primitives::{
},
constants::ASSET_MANAGER_PALLET_ID,
currencies::Currencies,
types::{BlockNumber, CalamariAssetId, Header},
types::{BlockNumber, CalamariAssetId, Header, PoolId},
};
use pallet_parachain_staking::{InflationInfo, Range};
use sp_core::H256;
Expand All @@ -63,11 +63,13 @@ pub const EVE: AccountId = 5;
pub const TREASURY_ACCOUNT: AccountId = 10;

pub const JUMBO: Balance = 1_000_000_000_000;
pub const INIT_JUMBO_AMOUNT: Balance = 1_000 * JUMBO;
pub const INIT_JUMBO_AMOUNT: Balance = 100 * JUMBO;
pub const INIT_V_MANTA_AMOUNT: Balance = JUMBO;
pub const V_MANTA_ID: CalamariAssetId = 8;
pub const JUMBO_ID: CalamariAssetId = 9;

pub const POOL_ID: PoolId = 0;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

Expand Down
90 changes: 87 additions & 3 deletions pallets/pallet-lottery/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::{
assert_last_event,
mock::{
roll_one_block, roll_to, roll_to_round_begin, roll_to_round_end, AccountId, Assets,
Balance, Balances, ExtBuilder, Lottery, ParachainStaking, RuntimeOrigin as Origin, System,
Test, ALICE, BOB, CHARLIE, DAVE, EVE, INIT_JUMBO_AMOUNT, INIT_V_MANTA_AMOUNT, JUMBO_ID,
TREASURY_ACCOUNT, V_MANTA_ID,
Balance, Balances, ExtBuilder, Farming, Lottery, ParachainStaking, RuntimeOrigin as Origin,
System, Test, ALICE, BOB, CHARLIE, DAVE, EVE, INIT_JUMBO_AMOUNT, INIT_V_MANTA_AMOUNT,
JUMBO_ID, POOL_ID, V_MANTA_ID,
},
Config, Error, FarmingParameters,
};
Expand Down Expand Up @@ -1029,6 +1029,14 @@ fn farming_deposit_withdraw() {
// surplus = balance - half_balance = half_balance
assert_eq!(half_balance, Lottery::surplus_unstaking_balance());

assert_eq!(
half_balance + INIT_V_MANTA_AMOUNT,
Assets::total_supply(V_MANTA_ID)
);
// no rewards when no time has past
assert_eq!(0, Assets::balance(JUMBO_ID, ALICE));
assert_eq!(0, Assets::balance(JUMBO_ID, ALICE));

roll_one_block();
assert_ok!(Lottery::request_withdraw(
Origin::signed(ALICE),
Expand All @@ -1038,6 +1046,14 @@ fn farming_deposit_withdraw() {
// surplus = half_balance - quarter_balance = quarter_balance
assert_eq!(quarter_balance, Lottery::surplus_unstaking_balance());

assert_eq!(
quarter_balance + INIT_V_MANTA_AMOUNT,
Assets::total_supply(V_MANTA_ID)
);
// no rewards when no time has past
assert_eq!(0, Assets::balance(V_MANTA_ID, ALICE));
assert_eq!(0, Assets::balance(JUMBO_ID, ALICE));

pallet_parachain_staking::AwardedPts::<Test>::insert(2, BOB, 20);
roll_to_round_begin(3);
// funds should be unlocked now and BOB is finished unstaking, so it's eligible for redepositing
Expand All @@ -1055,5 +1071,73 @@ fn farming_deposit_withdraw() {
assert_eq!(quarter_balance, Lottery::staked_collators(BOB));
assert_eq!(quarter_balance, Lottery::total_pot());
assert_eq!(quarter_balance, Lottery::sum_of_deposits());

assert_eq!(
quarter_balance + INIT_V_MANTA_AMOUNT,
Assets::total_supply(V_MANTA_ID)
);
// no rewards when no time has past
assert_eq!(0, Assets::balance(V_MANTA_ID, ALICE));
assert_eq!(0, Assets::balance(JUMBO_ID, ALICE));
});
}

#[test]
fn fails_withdrawing_more_than_vmanta() {
let balance = 500_000_000 * UNIT;
let half_balance = 250_000_000 * UNIT;
ExtBuilder::default()
.with_balances(vec![
(ALICE, HIGH_BALANCE),
(BOB, HIGH_BALANCE),
(CHARLIE, HIGH_BALANCE),
])
.with_candidates(vec![(BOB, balance)])
.with_funded_lottery_account(balance)
.with_farming()
.build()
.execute_with(|| {
assert_ok!(Lottery::deposit(Origin::signed(CHARLIE), balance));
assert_ok!(Lottery::deposit(Origin::signed(ALICE), balance));

assert_eq!(
(balance * 2) + INIT_V_MANTA_AMOUNT,
Assets::total_supply(V_MANTA_ID)
);
assert_eq!(0, Assets::balance(V_MANTA_ID, CHARLIE));
assert_ok!(Farming::withdraw(
Origin::signed(CHARLIE),
POOL_ID,
Some(half_balance)
));
assert_ok!(Farming::withdraw_claim(Origin::signed(CHARLIE), POOL_ID));
assert_eq!(half_balance, Assets::balance(V_MANTA_ID, CHARLIE));
assert_eq!(
(balance * 2) + INIT_V_MANTA_AMOUNT,
Assets::total_supply(V_MANTA_ID)
);

assert_ok!(Assets::transfer(
Origin::signed(CHARLIE),
V_MANTA_ID,
ALICE,
half_balance
));
assert_noop!(
Lottery::request_withdraw(Origin::signed(CHARLIE), balance),
pallet_assets::Error::<Test>::BalanceLow
);
assert_eq!(0, Assets::balance(V_MANTA_ID, CHARLIE));

assert_ok!(Assets::transfer(
Origin::signed(ALICE),
V_MANTA_ID,
CHARLIE,
half_balance
));
// will work if V_MANTA is not in farming pool but is in user account
assert_ok!(Lottery::request_withdraw(Origin::signed(CHARLIE), balance));
// no leftover V_MANTA
assert_eq!(0, Assets::balance(V_MANTA_ID, CHARLIE));
});
}

0 comments on commit ed041c2

Please sign in to comment.