Skip to content

Commit

Permalink
migrate transfer-allowlist
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Mar 12, 2024
1 parent 7587053 commit a9fdc19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 84 deletions.
11 changes: 5 additions & 6 deletions pallets/transfer-allowlist/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ use frame_support::{
pallet_prelude::Get,
traits::{fungible::Unbalanced, tokens::Precision, Currency, ReservableCurrency},
};
use frame_system::RawOrigin;
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use parity_scale_codec::EncodeLike;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Bounded, CheckedAdd, One},
traits::{CheckedAdd, One},
Saturating,
};

Expand All @@ -37,7 +36,7 @@ benchmarks! {
<T as pallet::Config>::Location: From<<T as frame_system::Config>::AccountId> + EncodeLike<<T as pallet::Config>::Location>,
<T as pallet::Config>::ReserveCurrency: Currency<<T as frame_system::Config>::AccountId> + ReservableCurrency<<T as frame_system::Config>::AccountId>,
T: pallet::Config<CurrencyId = FilterCurrency>,
<T as frame_system::Config>::BlockNumber: AtLeast32BitUnsigned + Bounded + TypeInfo,
BlockNumberFor<T>: One,
<<T as pallet::Config>::ReserveCurrency as frame_support::traits::fungible::Inspect<<T as frame_system::Config>::AccountId,>>::Balance: From<u64>
}

Expand Down Expand Up @@ -101,7 +100,7 @@ benchmarks! {

remove_transfer_allowance_delay_present {
let (sender, receiver) = set_up_users::<T>();
let delay = T::BlockNumber::one();
let delay = BlockNumberFor::<T>::one();
Pallet::<T>::add_allowance_delay(RawOrigin::Signed(sender.clone()).into(), BENCHMARK_CURRENCY_ID, delay.clone())?;
Pallet::<T>::add_transfer_allowance(RawOrigin::Signed(sender.clone()).into(), BENCHMARK_CURRENCY_ID, receiver.clone().into())?;

Expand Down Expand Up @@ -154,4 +153,4 @@ fn set_up_second_receiver<T: Config>() -> T::AccountId {
account::<T::AccountId>("Receiver_1", 3, 0)
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime,);
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
42 changes: 21 additions & 21 deletions pallets/transfer-allowlist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub mod pallet {
<T as frame_system::Config>::AccountId,
>>::Balance;

/// AllowanceDetails where `BlockNumber` is of type T::BlockNumber
pub type AllowanceDetailsOf<T> = AllowanceDetails<<T as frame_system::Config>::BlockNumber>;
/// AllowanceDetails where `BlockNumber` is of type BlockNumberFor<T>
pub type AllowanceDetailsOf<T> = AllowanceDetails<BlockNumberFor<T>>;

/// Resons for holding as defined by the fungible::hold::Inspect trait
pub type ReasonOf<T> = <<T as Config>::ReserveCurrency as fungible::hold::Inspect<
Expand Down Expand Up @@ -178,14 +178,14 @@ pub mod pallet {
/// Storage item containing number of allowances set, delay for sending
/// account/currency, and block number delay is modifiable at. Contains an
/// instance of AllowanceMetadata with allowance count as `u64`,
/// current_delay as `Option<T::BlockNumber>`, and modifiable_at as
/// `Option<T::BlockNumber>`. If a delay is set, but no allowances have been
/// created, `allowance_count` will be set to `0`. A double map is used here
/// as we need to know whether there is a restriction set for the account
/// and currency in the case where there is no allowance for destination
/// location. Using an StorageNMap would not allow us to look up whether
/// there was a restriction for the sending account and currency, given
/// that:
/// current_delay as `Option<BlockNumberFor<T>>`, and modifiable_at as
/// `Option<BlockNumberFor<T>>`. If a delay is set, but no allowances have
/// been created, `allowance_count` will be set to `0`. A double map is used
/// here as we need to know whether there is a restriction set for the
/// account and currency in the case where there is no allowance for
/// destination location. Using an StorageNMap would not allow us to look up
/// whether there was a restriction for the sending account and currency,
/// given that:
/// - we're checking whether there's an allowance specified for the receiver
/// location
/// - we would only find whether a restriction was set for the account in
Expand All @@ -205,7 +205,7 @@ pub mod pallet {
T::AccountId,
Twox64Concat,
T::CurrencyId,
AllowanceMetadata<T::BlockNumber>,
AllowanceMetadata<BlockNumberFor<T>>,
OptionQuery,
>;

Expand All @@ -220,7 +220,7 @@ pub mod pallet {
NMapKey<Twox64Concat, T::CurrencyId>,
NMapKey<Blake2_128Concat, T::Location>,
),
AllowanceDetails<T::BlockNumber>,
AllowanceDetails<BlockNumberFor<T>>,
OptionQuery,
>;

Expand Down Expand Up @@ -260,16 +260,16 @@ pub mod pallet {
sender_account_id: T::AccountId,
currency_id: T::CurrencyId,
receiver: T::Location,
allowed_at: T::BlockNumber,
blocked_at: T::BlockNumber,
allowed_at: BlockNumberFor<T>,
blocked_at: BlockNumberFor<T>,
},
/// Event for successful removal of transfer allowance perms
TransferAllowanceRemoved {
sender_account_id: T::AccountId,
currency_id: T::CurrencyId,
receiver: T::Location,
allowed_at: T::BlockNumber,
blocked_at: T::BlockNumber,
allowed_at: BlockNumberFor<T>,
blocked_at: BlockNumberFor<T>,
},
/// Event for successful removal of transfer allowance perms
TransferAllowancePurged {
Expand All @@ -281,19 +281,19 @@ pub mod pallet {
TransferAllowanceDelayAdd {
sender_account_id: T::AccountId,
currency_id: T::CurrencyId,
delay: T::BlockNumber,
delay: BlockNumberFor<T>,
},
/// Event for Allowance delay update
TransferAllowanceDelayUpdate {
sender_account_id: T::AccountId,
currency_id: T::CurrencyId,
delay: T::BlockNumber,
delay: BlockNumberFor<T>,
},
/// Event for Allowance delay future modification allowed
ToggleTransferAllowanceDelayFutureModifiable {
sender_account_id: T::AccountId,
currency_id: T::CurrencyId,
modifiable_once_after: Option<T::BlockNumber>,
modifiable_once_after: Option<BlockNumberFor<T>>,
},
/// Event for Allowance delay removal
TransferAllowanceDelayPurge {
Expand Down Expand Up @@ -456,7 +456,7 @@ pub mod pallet {
pub fn add_allowance_delay(
origin: OriginFor<T>,
currency_id: T::CurrencyId,
delay: T::BlockNumber,
delay: BlockNumberFor<T>,
) -> DispatchResult {
let account_id = ensure_signed(origin)?;
let count_delay = match Self::get_account_currency_restriction_count_delay(
Expand Down Expand Up @@ -500,7 +500,7 @@ pub mod pallet {
pub fn update_allowance_delay(
origin: OriginFor<T>,
currency_id: T::CurrencyId,
delay: T::BlockNumber,
delay: BlockNumberFor<T>,
) -> DispatchResult {
let account_id = ensure_signed(origin)?;
let current_block = <frame_system::Pallet<T>>::block_number();
Expand Down
70 changes: 13 additions & 57 deletions pallets/transfer-allowlist/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@

use cfg_types::tokens::FilterCurrency;
use frame_support::{
parameter_types,
derive_impl, parameter_types,
traits::{ConstU32, ConstU64},
Deserialize, Serialize,
};
use frame_system::pallet_prelude::BlockNumberFor;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, CheckedAdd, IdentityLookup},
};
use sp_runtime::{traits::CheckedAdd, BuildStorage};

use crate as transfer_allowlist;

Expand All @@ -32,52 +29,19 @@ pub(crate) const ACCOUNT_RECEIVER: u64 = 0x2;
pub(crate) const FEE_DEFICIENT_SENDER: u64 = 0x3;

type Balance = u64;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
type Block = frame_system::mocking::MockBlock<Runtime>;
pub type MockAccountId = u64;

frame_support::construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
pub enum Runtime {
Balances: pallet_balances,
System: frame_system,
TransferAllowList: transfer_allowlist,
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Runtime {
type AccountData = pallet_balances::AccountData<Balance>;
type AccountId = MockAccountId;
type BaseCallFilter = frame_support::traits::Everything;
type BlockHashCount = BlockHashCount;
type BlockLength = ();
type BlockNumber = u64;
type BlockWeights = ();
type DbWeight = ();
type Hash = H256;
type Hashing = BlakeTwo256;
type Header = Header;
type Index = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type OnKilledAccount = ();
type OnNewAccount = ();
type OnSetCode = ();
type PalletInfo = PalletInfo;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type SS58Prefix = SS58Prefix;
type SystemWeightInfo = ();
type Version = ();
type Block = frame_system::mocking::MockBlock<Runtime>;
}

#[derive(
Expand All @@ -103,22 +67,14 @@ impl From<u64> for Location {
Self::TestLocal(a)
}
}
// Used to handle reserve/unreserve for allowance creation.
// Loosely coupled with transfer_allowlist

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
impl pallet_balances::Config for Runtime {
type AccountStore = System;
type Balance = u64;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<1>;
type FreezeIdentifier = ();
type HoldIdentifier = ();
type MaxFreezes = ();
type MaxHolds = frame_support::traits::ConstU32<1>;
type MaxLocks = ();
type MaxReserves = ConstU32<50>;
type ReserveIdentifier = [u8; 8];
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxHolds = ConstU32<1>;
type RuntimeHoldReason = ();
}

parameter_types! {
Expand All @@ -136,8 +92,8 @@ impl transfer_allowlist::Config for Runtime {
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
let mut t = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
Expand All @@ -155,7 +111,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
e
}

pub fn advance_n_blocks<T: frame_system::Config>(n: <T as frame_system::Config>::BlockNumber) {
pub fn advance_n_blocks<T: frame_system::Config>(n: BlockNumberFor<T>) {
let b = frame_system::Pallet::<T>::block_number()
.checked_add(&n)
.expect("Mock block advancement failed.");
Expand Down

0 comments on commit a9fdc19

Please sign in to comment.