Skip to content

Commit

Permalink
Added asset creator (#753)
Browse files Browse the repository at this point in the history
## Describe your changes
* If Minter fn doesn't find an asset, it will create a new one.
  • Loading branch information
Gauthamastro committed May 17, 2023
2 parents f6b4976 + 04d4986 commit 2d88c86
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
12 changes: 3 additions & 9 deletions pallets/asset-handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,6 @@ pub mod pallet {
pub(super) type AssetPrecision<T: Config> =
StorageMap<_, Blake2_128Concat, ResourceId, PrecisionType, ValueQuery>;

/// Thea Assets, asset_id(u128) -> (network_id(u8), identifier_length(u8),
/// identifier(BoundedVec<>))
#[pallet::storage]
#[pallet::getter(fn get_thea_assets)]
pub type TheaAssets<T: Config> =
StorageMap<_, Blake2_128Concat, u128, (u8, u8, BoundedVec<u8, ConstU32<1000>>), ValueQuery>;

// Pallets use events to inform users when important changes are made.
// https://substrate.dev/docs/en/knowledgebase/runtime/events
#[pallet::event]
Expand Down Expand Up @@ -609,7 +602,9 @@ pub mod pallet {
recipient: T::AccountId,
amount: u128,
) -> Result<(), DispatchError> {
ensure!(<TheaAssets<T>>::contains_key(asset_id), Error::<T>::AssetNotRegistered);
if !T::AssetManager::asset_exists(asset_id) {
T::AssetManager::create(asset_id, T::PDEXHolderAccount::get(), true, 1u128)?;
}
ensure!(amount > 0, Error::<T>::AmountCannotBeZero);
T::AssetManager::mint_into(asset_id, &recipient, amount)?;
Ok(())
Expand Down Expand Up @@ -654,7 +649,6 @@ pub mod pallet {
who: T::AccountId,
amount: u128,
) -> Result<(), DispatchError> {
ensure!(<TheaAssets<T>>::contains_key(asset_id), Error::<T>::AssetNotRegistered);
ensure!(amount > 0, Error::<T>::AmountCannotBeZero);
T::AssetManager::burn_from(asset_id, &who, amount)?;
Ok(())
Expand Down
9 changes: 3 additions & 6 deletions pallets/asset-handler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,12 @@ pub fn test_convert_amount_for_foreign_chain() {
}

#[test]
pub fn test_mint_thea_asset_with_not_registered_asset_will_return_asset_not_registered_error() {
pub fn test_mint_thea_asset_with_not_registered_asset_will_return_ok() {
let recipient = create_recipient_account();
let asset_id = create_thea_asset_id(0, 5);

new_test_ext().execute_with(|| {
assert_noop!(
AssetHandler::mint_thea_asset(asset_id, recipient, 1_000_000_000_000_0_u128),
Error::<Test>::AssetNotRegistered
);
assert_ok!(AssetHandler::mint_thea_asset(asset_id, recipient, 1_000_000_000_000_0_u128));
})
}

Expand All @@ -647,7 +644,7 @@ pub fn test_burn_thea_asset_with_not_registered_asset_will_return_asset_not_regi
new_test_ext().execute_with(|| {
assert_noop!(
AssetHandler::burn_thea_asset(non_register_asset_id, user, 100_u128),
Error::<Test>::AssetNotRegistered
pallet_assets::Error::<Test>::Unknown
);
})
}
Expand Down
8 changes: 2 additions & 6 deletions pallets/thea-executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
mock::{new_test_ext, Assets, RuntimeOrigin as Origin, Test, *},
PendingWithdrawals,
};
use asset_handler::pallet::TheaAssets;

use frame_support::{assert_err, assert_noop, assert_ok, traits::fungibles::Mutate};
use parity_scale_codec::Encode;
use sp_core::{H160, H256};
Expand Down Expand Up @@ -147,7 +147,6 @@ fn test_claim_deposit_returns_ok() {
));
let deposit =
Deposit { recipient, asset_id, amount: 1_000_000_000_000_000_000u128, extra: vec![] };
<TheaAssets<Test>>::insert(asset_id, (0, 0, BoundedVec::default()));
assert_ok!(TheaExecutor::do_deposit(1, vec![deposit].encode()));
assert_ok!(TheaExecutor::claim_deposit(RuntimeOrigin::signed(recipient), 1));
})
Expand Down Expand Up @@ -180,9 +179,6 @@ fn test_claim_deposit_returns_asset_not_registered() {
let deposit =
Deposit { recipient, asset_id, amount: 1_000_000_000_000_000_000u128, extra: vec![] };
assert_ok!(TheaExecutor::do_deposit(1, vec![deposit].encode()));
assert_err!(
TheaExecutor::claim_deposit(RuntimeOrigin::signed(recipient), 1),
asset_handler::pallet::Error::<Test>::AssetNotRegistered
);
assert_ok!(TheaExecutor::claim_deposit(RuntimeOrigin::signed(recipient), 1));
})
}

0 comments on commit 2d88c86

Please sign in to comment.