Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Fix claim w/ vesting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Feb 19, 2020
1 parent ac69c83 commit 15fe7cc
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use rstd::prelude::*;
use sp_io::{hashing::keccak_256, crypto::secp256k1_ecdsa_recover};
use frame_support::{decl_event, decl_storage, decl_module, decl_error};
use frame_support::{dispatch::DispatchResult, weights::SimpleDispatchInfo};
use frame_support::weights::SimpleDispatchInfo;
use frame_support::traits::{Currency, Get, VestingSchedule};
use system::{ensure_root, ensure_none};
use codec::{Encode, Decode};
Expand Down Expand Up @@ -161,10 +161,7 @@ decl_module! {
let balance_due = <Claims<T>>::get(&signer)
.ok_or(Error::<T>::SignerHasNoClaim)?;

<Total<T>>::mutate(|t| -> DispatchResult {
*t = t.checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
Ok(())
})?;
let new_total = Self::total().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;

// Check if this claim should have a vesting schedule.
if let Some(vs) = <Vesting<T>>::get(&signer) {
Expand All @@ -174,10 +171,8 @@ decl_module! {
.map_err(|_| Error::<T>::DestinationVesting)?;
}

// This must happen before the add_vesting_schedule otherwise the schedule will be
// nullified.
CurrencyOf::<T>::deposit_creating(&dest, balance_due);

<Total<T>>::put(new_total);
<Claims<T>>::remove(&signer);
<Vesting<T>>::remove(&signer);

Expand Down Expand Up @@ -491,6 +486,30 @@ mod tests {
});
}

#[test]
fn claiming_while_vested_doesnt_work() {
new_test_ext().execute_with(|| {
assert_eq!(Claims::total(), 100);
// A user is already vested
assert_ok!(<Test as Trait>::VestingSchedule::add_vesting_schedule(&69, 1000, 100, 10));
CurrencyOf::<Test>::make_free_balance_be(&69, 1000);
assert_eq!(Balances::free_balance(69), 1000);
assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1))));
// New total
assert_eq!(Claims::total(), 300);

// They should not be able to claim
assert_noop!(
Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
Error::<Test>::DestinationVesting
);
// Everything should be unchanged

This comment has been minimized.

Copy link
@gavofyork

gavofyork Feb 19, 2020

Member

assert_noop already guarantees nothing has changed.

This comment has been minimized.

Copy link
@shawntabrizi

shawntabrizi Feb 19, 2020

Author Member

GENIUS!!

		let h = $crate::storage_root();
		$crate::assert_err!($x, $y);
		assert_eq!(h, $crate::storage_root());

I didn't know it did this check!

assert_eq!(Claims::total(), 300);
assert_eq!(Balances::free_balance(69), 1000);
assert_eq!(Vesting::vesting_balance(&69), 1000);
});
}

#[test]
fn non_sender_sig_doesnt_work() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit 15fe7cc

Please sign in to comment.