Skip to content

Commit

Permalink
fix: 🐛 test
Browse files Browse the repository at this point in the history
  • Loading branch information
yooml committed Jun 14, 2022
1 parent 9c02455 commit 7af39eb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pallets/farming/src/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ where
.total_time_factor
.checked_sub(gauge_info.total_time_factor)
.ok_or(ArithmeticError::Overflow)?;
gauge_pool_info.gauge_amount = gauge_pool_info
.gauge_amount
.checked_sub(&gauge_info.gauge_amount)
.ok_or(ArithmeticError::Overflow)?;
} else {
*maybe_gauge_info = Some(gauge_info);
};
Expand Down
70 changes: 70 additions & 0 deletions pallets/farming/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ fn gauge() {
})
}

#[test]
fn gauge_withdraw() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let (pid, _tokens) = init_gauge();
assert_eq!(Tokens::free_balance(KSM, &ALICE), 1900);
if let Some(gauge_pool_infos) = Farming::gauge_pool_infos(0) {
assert_eq!(gauge_pool_infos.gauge_amount, 100)
};
Farming::on_initialize(0);
System::set_block_number(System::block_number() + 1);
Farming::on_initialize(0);
assert_ok!(Farming::gauge_withdraw(Origin::signed(ALICE), pid));
assert_eq!(Tokens::free_balance(KSM, &ALICE), 1918);
System::set_block_number(System::block_number() + 1000);
assert_ok!(Farming::gauge_withdraw(Origin::signed(ALICE), pid));
assert_eq!(Tokens::free_balance(KSM, &ALICE), 3782);
if let Some(gauge_pool_infos) = Farming::gauge_pool_infos(0) {
assert_eq!(gauge_pool_infos.gauge_amount, 0)
};
})
}

#[test]
fn retire() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
Expand All @@ -174,6 +196,7 @@ fn retire() {
assert_eq!(Farming::shares_and_withdrawn_rewards(pid, &ALICE), None);
})
}

#[test]
fn reset() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
Expand Down Expand Up @@ -305,3 +328,50 @@ fn init_no_gauge() -> (PoolId, BalanceOf<Runtime>) {
assert_ok!(Farming::deposit(Origin::signed(ALICE), pid, tokens.clone(), None));
(pid, tokens)
}

#[test]
fn create_farming_pool() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
let mut tokens_proportion_map = BTreeMap::<CurrencyIdOf<Runtime>, Perbill>::new();
tokens_proportion_map.entry(KSM).or_insert(Perbill::from_percent(100));
let tokens_proportion = vec![(KSM, Perbill::from_percent(100))];
let tokens = 1000;
let basic_rewards = vec![(KSM, 1000)];
let gauge_basic_rewards = vec![(KSM, 900)];

assert_ok!(Farming::create_farming_pool(
Origin::signed(ALICE),
tokens_proportion.clone(),
basic_rewards.clone(),
Some((KSM, 1000, gauge_basic_rewards)),
0,
0,
7,
6,
5
));

let pid = 0;
let charge_rewards = vec![(KSM, 300000)];
assert_ok!(Farming::charge(Origin::signed(BOB), pid, charge_rewards));
assert_ok!(Farming::deposit(Origin::signed(ALICE), pid, tokens.clone(), Some((100, 100))));

Farming::on_initialize(0);
Farming::on_initialize(0);
assert_err!(Farming::claim(Origin::signed(ALICE), pid), Error::<Runtime>::CanNotClaim);
System::set_block_number(System::block_number() + 6);
assert_ok!(Farming::claim(Origin::signed(ALICE), pid));
assert_eq!(Tokens::free_balance(KSM, &ALICE), 3008);
System::set_block_number(System::block_number() + 100);
assert_ok!(Farming::claim(Origin::signed(ALICE), pid));
assert_eq!(Tokens::free_balance(KSM, &ALICE), 4698);
assert_ok!(Farming::withdraw(Origin::signed(ALICE), pid, Some(800)));
System::set_block_number(System::block_number() + 6);
assert_ok!(Farming::claim(Origin::signed(ALICE), pid));
assert_err!(Farming::claim(Origin::signed(ALICE), pid), Error::<Runtime>::CanNotClaim);
assert_eq!(Tokens::free_balance(KSM, &ALICE), 4698);
System::set_block_number(System::block_number() + 6);
assert_ok!(Farming::claim(Origin::signed(ALICE), pid));
assert_eq!(Tokens::free_balance(KSM, &ALICE), 5498);
})
}

0 comments on commit 7af39eb

Please sign in to comment.