diff --git a/currencies/src/mock.rs b/currencies/src/mock.rs index 1af8d35f936e1eda94f20b6020be92476a66ea57..b3f20d38fe0b65cd8a4b35232c5613db74f2b376 100644 --- a/currencies/src/mock.rs +++ b/currencies/src/mock.rs @@ -50,20 +50,17 @@ type Balance = u64; parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 2; } impl pallet_balances::Trait for Runtime { type Balance = Balance; - type OnFreeBalanceZero = (); type OnNewAccount = (); type OnReapAccount = (); type TransferPayment = (); type DustRemoval = (); type Event = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -147,7 +144,6 @@ impl ExtBuilder { .filter(|(_, currency_id, _)| *currency_id == X_TOKEN_ID) .map(|(account_id, _, initial_balance)| (account_id, initial_balance)) .collect::<Vec<_>>(), - vesting: vec![], } .assimilate_storage(&mut t) .unwrap(); diff --git a/oracle/rpc/runtime-api/src/lib.rs b/oracle/rpc/runtime-api/src/lib.rs index 437fa40eb0b39d2c7d8381720013345a08d8aa0a..2dd6ee9fac80a5bb95657971ee3082ef4be97bef 100644 --- a/oracle/rpc/runtime-api/src/lib.rs +++ b/oracle/rpc/runtime-api/src/lib.rs @@ -9,6 +9,6 @@ sp_api::decl_runtime_apis! { Key: Codec, Value: Codec, { - fn get_no_op(key: Key) -> Option<Value>; + fn get_value(key: Key) -> Option<Value>; } } diff --git a/oracle/rpc/src/lib.rs b/oracle/rpc/src/lib.rs index 124dd6dfba0b82954962d79075e254fa61ca072e..eb2bde8d1d29913a6f1945b14ece72d736ecb612 100644 --- a/oracle/rpc/src/lib.rs +++ b/oracle/rpc/src/lib.rs @@ -55,7 +55,7 @@ where let at = BlockId::hash(at.unwrap_or_else(|| // If the block hash is not supplied assume the best block. self.client.info().best_hash)); - api.get_no_op(&at, key) + api.get_value(&at, key) .map_err(|e| RpcError { code: ErrorCode::ServerError(Error::RuntimeError.into()), message: "Unable to get value.".into(), diff --git a/utilities/src/fixed128.rs b/utilities/src/fixed128.rs index 8b0d314a126ceb17b8bb9a133eeb0ee878236f6c..4e9d7db8669cbbc63da23b70b49aa4323a1ea354 100644 --- a/utilities/src/fixed128.rs +++ b/utilities/src/fixed128.rs @@ -1,10 +1,6 @@ use codec::{Decode, Encode}; use primitives::U256; -use rstd::{ - convert::{Into, TryFrom, TryInto}, - result::Result, - vec::Vec, -}; +use rstd::convert::{Into, TryFrom, TryInto}; use sp_runtime::{ traits::{Bounded, Saturating, UniqueSaturatedInto}, Perbill, Percent, Permill, Perquintill, diff --git a/vesting/src/lib.rs b/vesting/src/lib.rs index 395a4bb20e7f6c16eda9c643d59a7924c103b190..63d7d66e867f946edbc0a5f9644543f1edcf9ba2 100644 --- a/vesting/src/lib.rs +++ b/vesting/src/lib.rs @@ -158,32 +158,21 @@ const VESTING_LOCK_ID: LockIdentifier = *b"ormlvest"; impl<T: Trait> Module<T> { fn do_claim(who: &T::AccountId) -> BalanceOf<T> { - if let Some((amount, until)) = Self::locked(who) { - T::Currency::set_lock(VESTING_LOCK_ID, who, amount, until, WithdrawReasons::all()); - amount - } else { + let locked = Self::locked_balance(who); + if locked.is_zero() { T::Currency::remove_lock(VESTING_LOCK_ID, who); - Zero::zero() + } else { + T::Currency::set_lock(VESTING_LOCK_ID, who, locked, WithdrawReasons::all()); } + locked } - /// Returns locked balance info based on current block number: if no remaining locked balance, - /// returns `None`, or returns `Some((amount, until))` - fn locked(who: &T::AccountId) -> Option<(BalanceOf<T>, T::BlockNumber)> { + /// Returns locked balance based on current block number. + fn locked_balance(who: &T::AccountId) -> BalanceOf<T> { let now = <frame_system::Module<T>>::block_number(); - Self::vesting_schedules(who).iter().fold(None, |acc, s| { - let locked_amount = s.locked_amount(now); - if locked_amount.is_zero() { - return acc; - } - - let s_end = s.end().expect("ensured not overflow while adding; qed"); - if let Some((amount, until)) = acc { - Some((amount + locked_amount, until.max(s_end))) - } else { - Some((locked_amount, s_end)) - } - }) + Self::vesting_schedules(who) + .iter() + .fold(Zero::zero(), |acc, s| acc + s.locked_amount(now)) } fn do_add_vesting_schedule( @@ -193,17 +182,13 @@ impl<T: Trait> Module<T> { ) -> DispatchResult { Self::ensure_lockable(to)?; - let (schedule_amount, schedule_end) = Self::ensure_valid_vesting_schedule(&schedule)?; - let (mut total_amount, mut until) = (schedule_amount, schedule_end); - if let Some((curr_amount, curr_until)) = Self::locked(to) { - total_amount = curr_amount - .checked_add(&schedule_amount.into()) - .ok_or(Error::<T>::NumOverflow)?; - until = until.max(curr_until); - } + let schedule_amount = Self::ensure_valid_vesting_schedule(&schedule)?; + let total_amount = Self::locked_balance(to) + .checked_add(&schedule_amount.into()) + .ok_or(Error::<T>::NumOverflow)?; T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?; - T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, until, WithdrawReasons::all()); + T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all()); <VestingSchedules<T>>::mutate(to, |v| (*v).push(schedule)); Ok(()) @@ -212,36 +197,31 @@ impl<T: Trait> Module<T> { fn do_update_vesting_schedules(who: &T::AccountId, schedules: Vec<VestingScheduleOf<T>>) -> DispatchResult { Self::ensure_lockable(who)?; - let (total_amount, until) = schedules - .iter() - .try_fold::<_, _, Result<(BalanceOf<T>, T::BlockNumber), Error<T>>>( - (Zero::zero(), Zero::zero()), - |(acc_amount, acc_end), schedule| { - let (amount, end) = Self::ensure_valid_vesting_schedule(schedule)?; - Ok((acc_amount + amount, acc_end.max(end))) - }, - )?; + let total_amount = schedules.iter().try_fold::<_, _, Result<BalanceOf<T>, Error<T>>>( + Zero::zero(), + |acc_amount, schedule| { + let amount = Self::ensure_valid_vesting_schedule(schedule)?; + Ok(acc_amount + amount) + }, + )?; ensure!( T::Currency::free_balance(who) >= total_amount, Error::<T>::InsufficientBalanceToLock, ); - T::Currency::set_lock(VESTING_LOCK_ID, who, total_amount, until, WithdrawReasons::all()); + T::Currency::set_lock(VESTING_LOCK_ID, who, total_amount, WithdrawReasons::all()); <VestingSchedules<T>>::insert(who, schedules); Ok(()) } - /// Returns `Ok((amount, end))` if valid schedule, or error. - fn ensure_valid_vesting_schedule( - schedule: &VestingScheduleOf<T>, - ) -> Result<(BalanceOf<T>, T::BlockNumber), Error<T>> { + /// Returns `Ok(amount)` if valid schedule, or error. + fn ensure_valid_vesting_schedule(schedule: &VestingScheduleOf<T>) -> Result<BalanceOf<T>, Error<T>> { ensure!(!schedule.period.is_zero(), Error::<T>::ZeroVestingPeriod); ensure!(!schedule.period_count.is_zero(), Error::<T>::ZeroVestingPeriodCount); + ensure!(schedule.end().is_some(), Error::<T>::NumOverflow); - let amount = schedule.total_amount().ok_or(Error::<T>::NumOverflow)?; - let schedule_end = schedule.end().ok_or(Error::<T>::NumOverflow)?; - Ok((amount, schedule_end)) + schedule.total_amount().ok_or(Error::<T>::NumOverflow) } /// Ensure no other types of locks except `VESTING_LOCK_ID`. @@ -252,7 +232,7 @@ impl<T: Trait> Module<T> { let _ = Self::do_claim(who); let balance = T::Currency::free_balance(who); - let locked = Self::locked(who).map_or(Zero::zero(), |(amount, _)| amount); + let locked = Self::locked_balance(who); let usable = balance.saturating_sub(locked); T::Currency::ensure_can_withdraw(who, usable, WithdrawReasons::all(), locked) .map_err(|_| Error::<T>::HasNonVestingLocks.into()) diff --git a/vesting/src/mock.rs b/vesting/src/mock.rs index 5c8abd36c9e9d19d7a8626d27d8ded67a9dc859d..dc4c38226ee078bdbfa9604b657c4117fe295602 100644 --- a/vesting/src/mock.rs +++ b/vesting/src/mock.rs @@ -57,20 +57,17 @@ type Balance = u64; parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Runtime { type Balance = Balance; - type OnFreeBalanceZero = (); type OnNewAccount = (); type OnReapAccount = (); type TransferPayment = (); type DustRemoval = (); type Event = TestEvent; type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } pub type PalletBalances = pallet_balances::Module<Runtime>; @@ -117,7 +114,6 @@ impl ExtBuilder { .into_iter() .map(|(account_id, initial_balance)| (account_id, initial_balance)) .collect::<Vec<_>>(), - vesting: vec![], } .assimilate_storage(&mut t) .unwrap(); diff --git a/vesting/src/tests.rs b/vesting/src/tests.rs index 45fff337acdf432865150c01b1e85386ef7b45fb..a8a74e7325b1098adfd0bff68d0eed8c150f8e63 100644 --- a/vesting/src/tests.rs +++ b/vesting/src/tests.rs @@ -5,7 +5,7 @@ use super::*; use frame_support::{assert_err, assert_noop, assert_ok, traits::WithdrawReason}; use mock::{ExtBuilder, Origin, PalletBalances, Runtime, System, TestEvent, Vesting, ALICE, BOB}; -use pallet_balances::BalanceLock; +use pallet_balances::{BalanceLock, Reasons}; #[test] fn add_vesting_schedule_works() { @@ -58,8 +58,7 @@ fn add_new_vesting_schedule_merges_with_current_locked_balance_and_until() { Some(BalanceLock { id: VESTING_LOCK_ID, amount: 17u64, - until: 23u64, - reasons: WithdrawReasons::all(), + reasons: Reasons::All, }) ); }); @@ -114,7 +113,7 @@ fn add_vesting_schedule_fails_if_zero_period_or_count() { fn add_vesting_schedule_fails_if_unexpected_existing_locks() { ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| { assert_ok!(PalletBalances::transfer(Origin::signed(ALICE), BOB, 1)); - PalletBalances::set_lock(*b"prelocks", &BOB, 10u64, 10u64, WithdrawReasons::all()); + PalletBalances::set_lock(*b"prelocks", &BOB, 10u64, WithdrawReasons::all()); let schedule = VestingSchedule { start: 1u64, period: 1u64, @@ -248,7 +247,7 @@ fn update_vesting_schedules_works() { fn update_vesting_schedules_fails_if_unexpected_existing_locks() { ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| { assert_ok!(PalletBalances::transfer(Origin::signed(ALICE), BOB, 1)); - PalletBalances::set_lock(*b"prelocks", &BOB, 0u64, 10u64, WithdrawReasons::all()); + PalletBalances::set_lock(*b"prelocks", &BOB, 0u64, WithdrawReasons::all()); }); } @@ -256,7 +255,7 @@ fn update_vesting_schedules_fails_if_unexpected_existing_locks() { fn update_vesting_schedules_fails_if_insufficient_balance_to_lock() { ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| { assert_ok!(PalletBalances::transfer(Origin::signed(ALICE), BOB, 10)); - PalletBalances::set_lock(*b"prelocks", &BOB, 10u64, 10u64, WithdrawReasons::all()); + PalletBalances::set_lock(*b"prelocks", &BOB, 10u64, WithdrawReasons::all()); let schedule = VestingSchedule { start: 1u64, period: 1u64,