Skip to content
Merged
1 change: 0 additions & 1 deletion js/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ const TESTS = (api: ApiPromise, config: ExtrinsicConfig): Extrinsic[] => {
args: [
ASSET_ID_A,
PINT.mul(ASSET_ID_A_UNITS),
PARENT_LOCATION,
PINT.mul(ASSET_ID_A_AMOUNT),
],
verify: async (before: Balance) => {
Expand Down
52 changes: 38 additions & 14 deletions pallets/asset-index/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ benchmarks! {
let origin = T::AdminOrigin::successful_origin();
let million = 1_000_000u32.into();
let location = MultiLocation::Null;

assert_ok!(
AssetIndex::<T>::register_asset(
origin.clone(),
asset_id,
AssetAvailability::Liquid(MultiLocation::Null)
)
);
let call = Call::<T>::add_asset(
asset_id,
million,
location.clone(),
million
);
let balance = T::Currency::total_balance(asset_id, &T::TreasuryPalletId::get().into_account());
Expand All @@ -56,11 +63,15 @@ benchmarks! {
let deposit_units = 1000_u32.into();

// create liquid assets
assert_ok!(<AssetIndex<T>>::add_asset(
assert_ok!(AssetIndex::<T>::register_asset(
origin.clone(),
asset_id,
AssetAvailability::Liquid(MultiLocation::Null)
));
assert_ok!(AssetIndex::<T>::add_asset(
origin.clone(),
asset_id,
units,
MultiLocation::Null,
tokens
));

Expand All @@ -69,7 +80,7 @@ benchmarks! {

// deposit some funds into the index from an user account
assert_ok!(T::Currency::deposit(asset_id, &origin_account_id, deposit_units));
assert_ok!(<AssetIndex<T>>::deposit(origin.clone(), asset_id, deposit_units));
assert_ok!(AssetIndex::<T>::deposit(origin.clone(), asset_id, deposit_units));

// advance the block number so that the lock expires
<frame_system::Pallet<T>>::set_block_number(
Expand All @@ -79,7 +90,7 @@ benchmarks! {
);

// start withdraw
assert_ok!(<AssetIndex<T>>::withdraw(
assert_ok!(AssetIndex::<T>::withdraw(
origin.clone(),
42_u32.into(),
));
Expand All @@ -95,11 +106,15 @@ benchmarks! {
let admin_deposit = 1_000_000u32;
let units = 1_000u32.into();

assert_ok!(AssetIndex::<T>::register_asset(
origin.clone(),
asset_id,
AssetAvailability::Liquid(MultiLocation::Null)
));
assert_ok!(AssetIndex::<T>::add_asset(
origin.clone(),
asset_id,
100u32.into(),
MultiLocation::Null,
admin_deposit.into(),
));

Expand Down Expand Up @@ -208,11 +223,15 @@ benchmarks! {
let deposit_units = 1_000_u32.into();

// create liquid assets
assert_ok!(<AssetIndex<T>>::add_asset(
assert_ok!(AssetIndex::<T>::register_asset(
origin.clone(),
asset_id,
AssetAvailability::Liquid(MultiLocation::Null)
));
assert_ok!(AssetIndex::<T>::add_asset(
origin.clone(),
asset_id,
units,
MultiLocation::Null,
tokens
));

Expand All @@ -221,7 +240,7 @@ benchmarks! {

// deposit some funds into the index from an user account
assert_ok!(T::Currency::deposit(asset_id, &origin_account_id, deposit_units));
assert_ok!(<AssetIndex<T>>::deposit(origin.clone(), asset_id, deposit_units));
assert_ok!(AssetIndex::<T>::deposit(origin.clone(), asset_id, deposit_units));

// advance the block number so that the lock expires
<frame_system::Pallet<T>>::set_block_number(
Expand All @@ -245,15 +264,20 @@ benchmarks! {
// create price feed
T::PriceFeedBenchmarks::create_feed(origin_account_id.clone(), asset_id).unwrap();

assert_ok!(AssetIndex::<T>::add_asset(origin.clone(), asset_id, units, MultiLocation::Null, amount));
assert_ok!(AssetIndex::<T>::register_asset(
origin.clone(),
asset_id,
AssetAvailability::Liquid(MultiLocation::Null)
));
assert_ok!(AssetIndex::<T>::add_asset(origin.clone(), asset_id, units, amount));
assert_ok!(T::Currency::deposit(asset_id, &origin_account_id, units));
assert_ok!(<AssetIndex<T>>::deposit(origin.clone(), asset_id, units));
assert_ok!(AssetIndex::<T>::deposit(origin.clone(), asset_id, units));

let call = Call::<T>::unlock();
}: { call.dispatch_bypass_filter(origin)? } verify {
assert_eq!(<pallet::IndexTokenLocks<T>>::get(&origin_account_id), vec![types::IndexTokenLock{
locked: <AssetIndex<T>>::index_token_equivalent(asset_id, units).unwrap(),
end_block: <frame_system::Pallet<T>>::block_number() + T::LockupPeriod::get() - 1u32.into()
assert_eq!(pallet::IndexTokenLocks::<T>::get(&origin_account_id), vec![types::IndexTokenLock{
locked: AssetIndex::<T>::index_token_equivalent(asset_id, units).unwrap(),
end_block: frame_system::Pallet::<T>::block_number() + T::LockupPeriod::get() - 1u32.into()
}]);
}
}
Expand Down
41 changes: 19 additions & 22 deletions pallets/asset-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ pub mod pallet {
NoPendingWithdrawals,
/// Thrown if the asset that should be added is already registered
AssetAlreadyExists,
/// Thrown if the asset that should be added has not been registered.
AssetNotExists,
/// This gets thrown if the total supply of index tokens is 0 so no NAV can be calculated to
/// determine the Asset/Index Token rate.
InsufficientIndexTokens,
Expand Down Expand Up @@ -352,10 +354,9 @@ pub mod pallet {
origin: OriginFor<T>,
asset_id: T::AssetId,
units: T::Balance,
location: MultiLocation,
amount: T::Balance,
) -> DispatchResultWithPostInfo {
Self::do_add_asset(T::AdminOrigin::ensure_origin(origin)?, asset_id, units, location, amount)
Self::do_add_asset(T::AdminOrigin::ensure_origin(origin)?, asset_id, units, amount)
}

/// Add liquid asset with root origin, see `add_asset`
Expand All @@ -364,12 +365,11 @@ pub mod pallet {
origin: OriginFor<T>,
asset_id: T::AssetId,
units: T::Balance,
location: MultiLocation,
amount: T::Balance,
recipient: T::AccountId,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
Self::do_add_asset(recipient, asset_id, units, location, amount)
Self::do_add_asset(recipient, asset_id, units, amount)
}

/// Dispatches transfer to move assets out of the index’s account,
Expand Down Expand Up @@ -417,6 +417,9 @@ pub mod pallet {
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// native asset can't be registered
Self::ensure_not_native_asset(&asset_id)?;

Assets::<T>::try_mutate(asset_id, |maybe_available| -> DispatchResult {
// allow new assets only
ensure!(maybe_available.replace(availability.clone()).is_none(), Error::<T>::AssetAlreadyExists);
Expand Down Expand Up @@ -754,32 +757,17 @@ pub mod pallet {
recipient: T::AccountId,
asset_id: T::AssetId,
units: T::Balance,
location: MultiLocation,
amount: T::Balance,
) -> DispatchResultWithPostInfo {
Assets::<T>::get(&asset_id).ok_or(Error::<T>::AssetNotExists)?;

if units.is_zero() {
return Ok(().into());
}

let availability = AssetAvailability::Liquid(location);

// check whether this is a new asset and make sure locations match otherwise
let is_new_asset = if let Some(asset) = Assets::<T>::get(&asset_id) {
ensure!(asset == availability, Error::<T>::AssetAlreadyExists);
false
} else {
true
};

// transfer the caller's fund into the treasury account
Self::add_liquid(&recipient, asset_id, units, amount)?;

// register asset if not yet known
if is_new_asset {
Assets::<T>::insert(asset_id, availability.clone());
Self::deposit_event(Event::AssetRegistered(asset_id, availability));
}

Self::deposit_event(Event::AssetAdded(asset_id, units, recipient, amount));
Ok(().into())
}
Expand Down Expand Up @@ -1157,7 +1145,16 @@ pub mod pallet {
let origin_account_id = T::AdminOrigin::ensure_origin(origin.clone()).unwrap();

T::PriceFeedBenchmarks::create_feed(origin_account_id, asset_id)?;
Self::add_asset(T::AdminOrigin::successful_origin(), asset_id, units, location, amount)

// the tests of benchmarks register assets by default
if Assets::<T>::get(asset_id).is_none() {
Self::register_asset(
T::AdminOrigin::successful_origin(),
asset_id,
AssetAvailability::Liquid(location),
)?;
}
Self::add_asset(T::AdminOrigin::successful_origin(), asset_id, units, amount)
}

/// deposit index tokens to the testing account with saft_nav
Expand Down
Loading