use alloc::vec::Vec;
use codec::{Decode, Encode};
use frame_support::{
pallet_prelude::*,
traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, VestingSchedule},
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use scale_info::TypeInfo;
use sp_core::sr25519;
use sp_runtime::{
traits::{CheckedAdd, Saturating, Verify, Zero},
AnySignature, DispatchError, DispatchResult, Permill, RuntimeDebug,
};
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub enum AccountValidity {
Invalid,
Initiated,
Pending,
ValidLow,
ValidHigh,
Completed,
}
impl Default for AccountValidity {
fn default() -> Self {
AccountValidity::Invalid
}
}
impl AccountValidity {
fn is_valid(&self) -> bool {
match self {
Self::Invalid => false,
Self::Initiated => false,
Self::Pending => false,
Self::ValidLow => true,
Self::ValidHigh => true,
Self::Completed => false,
}
}
}
#[derive(Encode, Decode, Default, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct AccountStatus<Balance> {
validity: AccountValidity,
free_balance: Balance,
locked_balance: Balance,
signature: Vec<u8>,
vat: Permill,
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: Currency<Self::AccountId>;
type VestingSchedule: VestingSchedule<
Self::AccountId,
Moment = BlockNumberFor<Self>,
Currency = Self::Currency,
>;
type ValidityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type ConfigurationOrigin: EnsureOrigin<Self::RuntimeOrigin>;
#[pallet::constant]
type MaxStatementLength: Get<u32>;
#[pallet::constant]
type UnlockedProportion: Get<Permill>;
#[pallet::constant]
type MaxUnlocked: Get<BalanceOf<Self>>;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
AccountCreated { who: T::AccountId },
ValidityUpdated { who: T::AccountId, validity: AccountValidity },
BalanceUpdated { who: T::AccountId, free: BalanceOf<T>, locked: BalanceOf<T> },
PaymentComplete { who: T::AccountId, free: BalanceOf<T>, locked: BalanceOf<T> },
PaymentAccountSet { who: T::AccountId },
StatementUpdated,
UnlockBlockUpdated { block_number: BlockNumberFor<T> },
}
#[pallet::error]
pub enum Error<T> {
InvalidAccount,
ExistingAccount,
InvalidSignature,
AlreadyCompleted,
Overflow,
InvalidStatement,
InvalidUnlockBlock,
VestingScheduleExists,
}
#[pallet::storage]
pub(super) type Accounts<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, AccountStatus<BalanceOf<T>>, ValueQuery>;
#[pallet::storage]
pub(super) type PaymentAccount<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
#[pallet::storage]
pub(super) type Statement<T> = StorageValue<_, Vec<u8>, ValueQuery>;
#[pallet::storage]
pub(super) type UnlockBlock<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(200_000_000, 0) + T::DbWeight::get().reads_writes(4, 1))]
pub fn create_account(
origin: OriginFor<T>,
who: T::AccountId,
signature: Vec<u8>,
) -> DispatchResult {
T::ValidityOrigin::ensure_origin(origin)?;
ensure!(!Accounts::<T>::contains_key(&who), Error::<T>::ExistingAccount);
ensure!(
T::VestingSchedule::vesting_balance(&who).is_none(),
Error::<T>::VestingScheduleExists
);
Self::verify_signature(&who, &signature)?;
let status = AccountStatus {
validity: AccountValidity::Initiated,
signature,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
vat: Permill::zero(),
};
Accounts::<T>::insert(&who, status);
Self::deposit_event(Event::<T>::AccountCreated { who });
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn update_validity_status(
origin: OriginFor<T>,
who: T::AccountId,
validity: AccountValidity,
) -> DispatchResult {
T::ValidityOrigin::ensure_origin(origin)?;
ensure!(Accounts::<T>::contains_key(&who), Error::<T>::InvalidAccount);
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
ensure!(
status.validity != AccountValidity::Completed,
Error::<T>::AlreadyCompleted
);
status.validity = validity;
Ok(())
},
)?;
Self::deposit_event(Event::<T>::ValidityUpdated { who, validity });
Ok(())
}
#[pallet::call_index(2)]
#[pallet::weight(T::DbWeight::get().reads_writes(2, 1))]
pub fn update_balance(
origin: OriginFor<T>,
who: T::AccountId,
free_balance: BalanceOf<T>,
locked_balance: BalanceOf<T>,
vat: Permill,
) -> DispatchResult {
T::ValidityOrigin::ensure_origin(origin)?;
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
ensure!(status.validity.is_valid(), Error::<T>::InvalidAccount);
free_balance.checked_add(&locked_balance).ok_or(Error::<T>::Overflow)?;
status.free_balance = free_balance;
status.locked_balance = locked_balance;
status.vat = vat;
Ok(())
},
)?;
Self::deposit_event(Event::<T>::BalanceUpdated {
who,
free: free_balance,
locked: locked_balance,
});
Ok(())
}
#[pallet::call_index(3)]
#[pallet::weight(T::DbWeight::get().reads_writes(4, 2))]
pub fn payout(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
let payment_account = ensure_signed(origin)?;
let test_against = PaymentAccount::<T>::get().ok_or(DispatchError::BadOrigin)?;
ensure!(payment_account == test_against, DispatchError::BadOrigin);
ensure!(
T::VestingSchedule::vesting_balance(&who).is_none(),
Error::<T>::VestingScheduleExists
);
Accounts::<T>::try_mutate(
&who,
|status: &mut AccountStatus<BalanceOf<T>>| -> DispatchResult {
ensure!(status.validity.is_valid(), Error::<T>::InvalidAccount);
let total_balance = status
.free_balance
.checked_add(&status.locked_balance)
.ok_or(Error::<T>::Overflow)?;
T::Currency::transfer(
&payment_account,
&who,
total_balance,
ExistenceRequirement::AllowDeath,
)?;
if !status.locked_balance.is_zero() {
let unlock_block = UnlockBlock::<T>::get();
let unlocked = (T::UnlockedProportion::get() * status.locked_balance)
.min(T::MaxUnlocked::get());
let locked = status.locked_balance.saturating_sub(unlocked);
let _ = T::VestingSchedule::add_vesting_schedule(
&who,
locked,
locked,
unlock_block,
);
}
status.validity = AccountValidity::Completed;
Self::deposit_event(Event::<T>::PaymentComplete {
who: who.clone(),
free: status.free_balance,
locked: status.locked_balance,
});
Ok(())
},
)?;
Ok(())
}
#[pallet::call_index(4)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_payment_account(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
T::ConfigurationOrigin::ensure_origin(origin)?;
PaymentAccount::<T>::put(who.clone());
Self::deposit_event(Event::<T>::PaymentAccountSet { who });
Ok(())
}
#[pallet::call_index(5)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_statement(origin: OriginFor<T>, statement: Vec<u8>) -> DispatchResult {
T::ConfigurationOrigin::ensure_origin(origin)?;
ensure!(
(statement.len() as u32) < T::MaxStatementLength::get(),
Error::<T>::InvalidStatement
);
Statement::<T>::set(statement);
Self::deposit_event(Event::<T>::StatementUpdated);
Ok(())
}
#[pallet::call_index(6)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_unlock_block(
origin: OriginFor<T>,
unlock_block: BlockNumberFor<T>,
) -> DispatchResult {
T::ConfigurationOrigin::ensure_origin(origin)?;
ensure!(
unlock_block > frame_system::Pallet::<T>::block_number(),
Error::<T>::InvalidUnlockBlock
);
UnlockBlock::<T>::set(unlock_block);
Self::deposit_event(Event::<T>::UnlockBlockUpdated { block_number: unlock_block });
Ok(())
}
}
}
impl<T: Config> Pallet<T> {
fn verify_signature(who: &T::AccountId, signature: &[u8]) -> Result<(), DispatchError> {
let signature: AnySignature = sr25519::Signature::try_from(signature)
.map_err(|_| Error::<T>::InvalidSignature)?
.into();
let account_bytes: [u8; 32] = account_to_bytes(who)?;
let public_key = sr25519::Public::from_raw(account_bytes);
let message = Statement::<T>::get();
match signature.verify(message.as_slice(), &public_key) {
true => Ok(()),
false => Err(Error::<T>::InvalidSignature)?,
}
}
}
fn account_to_bytes<AccountId>(account: &AccountId) -> Result<[u8; 32], DispatchError>
where
AccountId: Encode,
{
let account_vec = account.encode();
ensure!(account_vec.len() == 32, "AccountId must be 32 bytes.");
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&account_vec);
Ok(bytes)
}
pub fn remove_pallet<T>() -> frame_support::weights::Weight
where
T: frame_system::Config,
{
#[allow(deprecated)]
use frame_support::migration::remove_storage_prefix;
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"Accounts", b"");
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"PaymentAccount", b"");
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"Statement", b"");
#[allow(deprecated)]
remove_storage_prefix(b"Purchase", b"UnlockBlock", b"");
<T as frame_system::Config>::BlockWeights::get().max_block
}
#[cfg(test)]
mod tests {
use super::*;
use sp_core::{crypto::AccountId32, H256};
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
use crate::purchase;
use frame_support::{
assert_noop, assert_ok, derive_impl, ord_parameter_types, parameter_types,
traits::{Currency, WithdrawReasons},
};
use sp_runtime::{
traits::{BlakeTwo256, Dispatchable, Identity, IdentityLookup},
ArithmeticError, BuildStorage,
DispatchError::BadOrigin,
};
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
Vesting: pallet_vesting,
Purchase: purchase,
}
);
type AccountId = AccountId32;
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type AccountStore = System;
}
parameter_types! {
pub const MinVestedTransfer: u64 = 1;
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
}
impl pallet_vesting::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type BlockNumberToBalance = Identity;
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}
parameter_types! {
pub const MaxStatementLength: u32 = 1_000;
pub const UnlockedProportion: Permill = Permill::from_percent(10);
pub const MaxUnlocked: u64 = 10;
}
ord_parameter_types! {
pub const ValidityOrigin: AccountId = AccountId32::from([0u8; 32]);
pub const PaymentOrigin: AccountId = AccountId32::from([1u8; 32]);
pub const ConfigurationOrigin: AccountId = AccountId32::from([2u8; 32]);
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type VestingSchedule = Vesting;
type ValidityOrigin = frame_system::EnsureSignedBy<ValidityOrigin, AccountId>;
type ConfigurationOrigin = frame_system::EnsureSignedBy<ConfigurationOrigin, AccountId>;
type MaxStatementLength = MaxStatementLength;
type UnlockedProportion = UnlockedProportion;
type MaxUnlocked = MaxUnlocked;
}
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| setup());
ext
}
fn setup() {
let statement = b"Hello, World".to_vec();
let unlock_block = 100;
Purchase::set_statement(RuntimeOrigin::signed(configuration_origin()), statement).unwrap();
Purchase::set_unlock_block(RuntimeOrigin::signed(configuration_origin()), unlock_block)
.unwrap();
Purchase::set_payment_account(
RuntimeOrigin::signed(configuration_origin()),
payment_account(),
)
.unwrap();
Balances::make_free_balance_be(&payment_account(), 100_000);
}
fn alice() -> AccountId {
Sr25519Keyring::Alice.to_account_id()
}
fn alice_ed25519() -> AccountId {
Ed25519Keyring::Alice.to_account_id()
}
fn bob() -> AccountId {
Sr25519Keyring::Bob.to_account_id()
}
fn alice_signature() -> [u8; 64] {
hex_literal::hex!("20e0faffdf4dfe939f2faa560f73b1d01cde8472e2b690b7b40606a374244c3a2e9eb9c8107c10b605138374003af8819bd4387d7c24a66ee9253c2e688ab881")
}
fn bob_signature() -> [u8; 64] {
hex_literal::hex!("d6d460187ecf530f3ec2d6e3ac91b9d083c8fbd8f1112d92a82e4d84df552d18d338e6da8944eba6e84afaacf8a9850f54e7b53a84530d649be2e0119c7ce889")
}
fn alice_signature_ed25519() -> [u8; 64] {
hex_literal::hex!("ee3f5a6cbfc12a8f00c18b811dc921b550ddf272354cda4b9a57b1d06213fcd8509f5af18425d39a279d13622f14806c3e978e2163981f2ec1c06e9628460b0e")
}
fn validity_origin() -> AccountId {
ValidityOrigin::get()
}
fn configuration_origin() -> AccountId {
ConfigurationOrigin::get()
}
fn payment_account() -> AccountId {
[42u8; 32].into()
}
#[test]
fn set_statement_works_and_handles_basic_errors() {
new_test_ext().execute_with(|| {
let statement = b"Test Set Statement".to_vec();
assert_noop!(
Purchase::set_statement(RuntimeOrigin::signed(alice()), statement.clone()),
BadOrigin,
);
let long_statement = [0u8; 10_000].to_vec();
assert_noop!(
Purchase::set_statement(
RuntimeOrigin::signed(configuration_origin()),
long_statement
),
Error::<Test>::InvalidStatement,
);
assert_ok!(Purchase::set_statement(
RuntimeOrigin::signed(configuration_origin()),
statement.clone()
));
assert_eq!(Statement::<Test>::get(), statement);
});
}
#[test]
fn set_unlock_block_works_and_handles_basic_errors() {
new_test_ext().execute_with(|| {
let unlock_block = 69;
assert_noop!(
Purchase::set_unlock_block(RuntimeOrigin::signed(alice()), unlock_block),
BadOrigin,
);
let bad_unlock_block = 50;
System::set_block_number(bad_unlock_block);
assert_noop!(
Purchase::set_unlock_block(
RuntimeOrigin::signed(configuration_origin()),
bad_unlock_block
),
Error::<Test>::InvalidUnlockBlock,
);
assert_ok!(Purchase::set_unlock_block(
RuntimeOrigin::signed(configuration_origin()),
unlock_block
));
assert_eq!(UnlockBlock::<Test>::get(), unlock_block);
});
}
#[test]
fn set_payment_account_works_and_handles_basic_errors() {
new_test_ext().execute_with(|| {
let payment_account: AccountId = [69u8; 32].into();
assert_noop!(
Purchase::set_payment_account(
RuntimeOrigin::signed(alice()),
payment_account.clone()
),
BadOrigin,
);
assert_ok!(Purchase::set_payment_account(
RuntimeOrigin::signed(configuration_origin()),
payment_account.clone()
));
assert_eq!(PaymentAccount::<Test>::get(), Some(payment_account));
});
}
#[test]
fn signature_verification_works() {
new_test_ext().execute_with(|| {
assert_ok!(Purchase::verify_signature(&alice(), &alice_signature()));
assert_ok!(Purchase::verify_signature(&alice_ed25519(), &alice_signature_ed25519()));
assert_ok!(Purchase::verify_signature(&bob(), &bob_signature()));
assert_noop!(
Purchase::verify_signature(&alice(), &bob_signature()),
Error::<Test>::InvalidSignature
);
assert_noop!(
Purchase::verify_signature(&bob(), &alice_signature()),
Error::<Test>::InvalidSignature
);
});
}
#[test]
fn account_creation_works() {
new_test_ext().execute_with(|| {
assert!(!Accounts::<Test>::contains_key(alice()));
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec(),
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::Initiated,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
});
}
#[test]
fn account_creation_handles_basic_errors() {
new_test_ext().execute_with(|| {
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(alice()),
alice(),
alice_signature().to_vec()
),
BadOrigin,
);
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
bob_signature().to_vec()
),
Error::<Test>::InvalidSignature,
);
Balances::make_free_balance_be(&alice(), 100);
assert_ok!(<Test as Config>::VestingSchedule::add_vesting_schedule(
&alice(),
100,
1,
50
));
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
),
Error::<Test>::VestingScheduleExists,
);
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
bob(),
bob_signature().to_vec()
));
assert_noop!(
Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
bob(),
bob_signature().to_vec()
),
Error::<Test>::ExistingAccount,
);
});
}
#[test]
fn update_validity_status_works() {
new_test_ext().execute_with(|| {
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec(),
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Pending,
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Invalid,
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::Invalid,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidLow,
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::ValidLow,
free_balance: Zero::zero(),
locked_balance: Zero::zero(),
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
});
}
#[test]
fn update_validity_status_handles_basic_errors() {
new_test_ext().execute_with(|| {
assert_noop!(
Purchase::update_validity_status(
RuntimeOrigin::signed(alice()),
alice(),
AccountValidity::Pending,
),
BadOrigin
);
assert_noop!(
Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Pending,
),
Error::<Test>::InvalidAccount
);
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec(),
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Completed,
));
assert_noop!(
Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::Pending,
),
Error::<Test>::AlreadyCompleted
);
});
}
#[test]
fn update_balance_works() {
new_test_ext().execute_with(|| {
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidLow,
));
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
50,
50,
Permill::from_rational(77u32, 1000u32),
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::ValidLow,
free_balance: 50,
locked_balance: 50,
signature: alice_signature().to_vec(),
vat: Permill::from_parts(77000),
}
);
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
25,
50,
Permill::zero(),
));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::ValidLow,
free_balance: 25,
locked_balance: 50,
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
});
}
#[test]
fn update_balance_handles_basic_errors() {
new_test_ext().execute_with(|| {
assert_noop!(
Purchase::update_balance(
RuntimeOrigin::signed(alice()),
alice(),
50,
50,
Permill::zero(),
),
BadOrigin
);
assert_noop!(
Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
50,
50,
Permill::zero(),
),
Error::<Test>::InvalidAccount
);
assert_noop!(
Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
u64::MAX,
u64::MAX,
Permill::zero(),
),
Error::<Test>::InvalidAccount
);
});
}
#[test]
fn payout_works() {
new_test_ext().execute_with(|| {
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
));
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
bob(),
bob_signature().to_vec()
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidLow,
));
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
bob(),
AccountValidity::ValidHigh,
));
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
50,
50,
Permill::zero(),
));
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
bob(),
100,
150,
Permill::zero(),
));
assert_ok!(Purchase::payout(RuntimeOrigin::signed(payment_account()), alice(),));
assert_ok!(Purchase::payout(RuntimeOrigin::signed(payment_account()), bob(),));
assert_eq!(<Test as Config>::Currency::free_balance(&payment_account()), 99_650);
assert_eq!(<Test as Config>::Currency::free_balance(&alice()), 100);
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&alice()), Some(45));
assert_eq!(<Test as Config>::Currency::free_balance(&bob()), 250);
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&bob()), Some(140));
assert_eq!(
Accounts::<Test>::get(alice()),
AccountStatus {
validity: AccountValidity::Completed,
free_balance: 50,
locked_balance: 50,
signature: alice_signature().to_vec(),
vat: Permill::zero(),
}
);
assert_eq!(
Accounts::<Test>::get(bob()),
AccountStatus {
validity: AccountValidity::Completed,
free_balance: 100,
locked_balance: 150,
signature: bob_signature().to_vec(),
vat: Permill::zero(),
}
);
System::set_block_number(100);
let vest_call = RuntimeCall::Vesting(pallet_vesting::Call::<Test>::vest {});
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(alice())));
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(bob())));
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&alice()), Some(45));
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&bob()), Some(140));
System::set_block_number(101);
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(alice())));
assert_ok!(vest_call.clone().dispatch(RuntimeOrigin::signed(bob())));
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&alice()), None);
assert_eq!(<Test as Config>::VestingSchedule::vesting_balance(&bob()), None);
});
}
#[test]
fn payout_handles_basic_errors() {
new_test_ext().execute_with(|| {
assert_noop!(Purchase::payout(RuntimeOrigin::signed(alice()), alice(),), BadOrigin);
Balances::make_free_balance_be(&bob(), 100);
assert_ok!(
<Test as Config>::VestingSchedule::add_vesting_schedule(&bob(), 100, 1, 50,)
);
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), bob(),),
Error::<Test>::VestingScheduleExists
);
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), alice(),),
Error::<Test>::InvalidAccount
);
assert_ok!(Purchase::create_account(
RuntimeOrigin::signed(validity_origin()),
alice(),
alice_signature().to_vec()
));
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), alice(),),
Error::<Test>::InvalidAccount
);
assert_ok!(Purchase::update_validity_status(
RuntimeOrigin::signed(validity_origin()),
alice(),
AccountValidity::ValidHigh,
));
assert_ok!(Purchase::update_balance(
RuntimeOrigin::signed(validity_origin()),
alice(),
100_000,
100_000,
Permill::zero(),
));
assert_noop!(
Purchase::payout(RuntimeOrigin::signed(payment_account()), alice()),
ArithmeticError::Underflow
);
});
}
#[test]
fn remove_pallet_works() {
new_test_ext().execute_with(|| {
let account_status = AccountStatus {
validity: AccountValidity::Completed,
free_balance: 1234,
locked_balance: 4321,
signature: b"my signature".to_vec(),
vat: Permill::from_percent(50),
};
Accounts::<Test>::insert(alice(), account_status.clone());
Accounts::<Test>::insert(bob(), account_status);
PaymentAccount::<Test>::put(alice());
Statement::<Test>::put(b"hello, world!".to_vec());
UnlockBlock::<Test>::put(4);
assert_eq!(Accounts::<Test>::iter().count(), 2);
assert!(PaymentAccount::<Test>::exists());
assert!(Statement::<Test>::exists());
assert!(UnlockBlock::<Test>::exists());
remove_pallet::<Test>();
assert_eq!(Accounts::<Test>::iter().count(), 0);
assert!(!PaymentAccount::<Test>::exists());
assert!(!Statement::<Test>::exists());
assert!(!UnlockBlock::<Test>::exists());
});
}
}