Skip to content
Snippets Groups Projects
Unverified Commit d1bf78f6 authored by Liam Aharon's avatar Liam Aharon
Browse files

add check for asset existence

parent b0cd8318
No related merge requests found
......@@ -230,6 +230,8 @@ pub mod pallet {
pub enum Error<T> {
/// An operation was attempted on a non-existent pool.
NonExistentPool,
/// An operation was attempted using a non-existent asset.
NonExistentAsset,
}
#[pallet::hooks]
......@@ -256,6 +258,16 @@ pub mod pallet {
// Ensure Origin is allowed to create pools.
T::PermissionedPoolCreator::ensure_origin(origin.clone())?;
// Ensure the assets exist.
ensure!(
T::Assets::asset_exists(*staked_asset_id.clone()),
Error::<T>::NonExistentAsset
);
ensure!(
T::Assets::asset_exists(*reward_asset_id.clone()),
Error::<T>::NonExistentAsset
);
// Get the admin, or try to use the origin as admin.
let origin_acc_id = ensure_signed(origin)?;
let admin = match admin {
......
......@@ -16,7 +16,7 @@
// limitations under the License.
use crate::{mock::*, *};
use frame_support::{assert_ok, traits::fungible::NativeOrWithId};
use frame_support::{assert_err, assert_ok, traits::fungible::NativeOrWithId};
fn create_tokens(owner: u128, tokens: Vec<NativeOrWithId<u32>>) {
create_tokens_with_ed(owner, tokens, 1)
......@@ -163,3 +163,44 @@ fn create_pool_works() {
);
});
}
#[test]
fn create_pool_with_non_existent_asset_fails() {
new_test_ext().execute_with(|| {
let valid_asset = NativeOrWithId::<u32>::WithId(1);
let invalid_asset = NativeOrWithId::<u32>::WithId(200);
assert_err!(
StakingRewards::create_pool(
RuntimeOrigin::signed(1),
Box::new(valid_asset.clone()),
Box::new(invalid_asset.clone()),
10,
None
),
Error::<MockRuntime>::NonExistentAsset
);
assert_err!(
StakingRewards::create_pool(
RuntimeOrigin::signed(1),
Box::new(invalid_asset.clone()),
Box::new(valid_asset.clone()),
10,
None
),
Error::<MockRuntime>::NonExistentAsset
);
assert_err!(
StakingRewards::create_pool(
RuntimeOrigin::signed(1),
Box::new(invalid_asset.clone()),
Box::new(invalid_asset.clone()),
10,
None
),
Error::<MockRuntime>::NonExistentAsset
);
})
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment