Skip to content

Commit

Permalink
Added create_parachain_asset ext and removed sanity check from Reward…
Browse files Browse the repository at this point in the history
…s Pallet (#950)

## Describe your changes
Added create_parachain_asset ext and removed sanity check from Rewards
Pallet.
  • Loading branch information
Gauthamastro committed Apr 30, 2024
2 parents b89e840 + 078856e commit d5a2d91
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions pallets/rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,6 @@ impl<T: Config> Pallet<T> {
.saturating_sub(user_reward_info.claim_amount),
);

//ensure the claimable amount is greater than min claimable amount
ensure!(
rewards_claimable.saturated_into::<u128>() > MIN_REWARDS_CLAIMABLE_AMOUNT,
Error::<T>::AmountToLowToRedeem
);

//remove lock
T::NativeCurrency::remove_lock(user_reward_info.lock_id, &user);

Expand Down
6 changes: 0 additions & 6 deletions pallets/rewards/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,5 @@ pub fn claim_rewards_for_alice_at_multiple_intervals() {
System::set_block_number(
start_block.saturating_add(require_block_to_claim_50_percentage_of_rewards),
);

// assert_ok!(Rewards::claim(RuntimeOrigin::signed(alice_account.clone()), reward_id)); - disabled since the whole amount is claimed now
assert_noop!(
Rewards::claim(RuntimeOrigin::signed(alice_account), reward_id),
Error::<Test>::AmountToLowToRedeem
);
})
}
28 changes: 28 additions & 0 deletions pallets/thea-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate core;

use frame_support::pallet_prelude::Weight;
pub use pallet::*;
use xcm::v3::AssetId as XcmAssetId;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
Expand Down Expand Up @@ -89,6 +90,7 @@ pub mod pallet {
/// Assets Pallet
type Assets: frame_support::traits::tokens::fungibles::Mutate<Self::AccountId>
+ frame_support::traits::tokens::fungibles::Create<Self::AccountId>
+ frame_support::traits::tokens::fungibles::metadata::Mutate<Self::AccountId>
+ frame_support::traits::tokens::fungibles::Inspect<Self::AccountId>;
/// Asset Id
type AssetId: Member
Expand Down Expand Up @@ -470,6 +472,32 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(7)]
#[pallet::weight(< T as Config >::TheaExecWeightInfo::claim_deposit(1))]
#[transactional]
pub fn create_parachain_asset(
origin: OriginFor<T>,
asset: sp_std::boxed::Box<XcmAssetId>,
name: Vec<u8>,
symbol: Vec<u8>,
decimal: u8,
) -> DispatchResult {
T::GovernanceOrigin::ensure_origin(origin)?;
let asset_id = polkadex_primitives::assets::generate_asset_id_for_parachain(asset);
Self::resolve_create(asset_id.into(), Self::thea_account(), 1u128)?;
Self::set_token_metadata(
asset_id.into(),
&Self::thea_account(),
name,
symbol,
decimal,
)?;
let metadata = AssetMetadata::new(decimal).ok_or(Error::<T>::InvalidDecimal)?;
<Metadata<T>>::insert(asset_id, metadata);
Self::deposit_event(Event::<T>::AssetMetadataSet(metadata));
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
33 changes: 33 additions & 0 deletions pallets/thea-executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
mock::{new_test_ext, Assets, Test, *},
PendingWithdrawals, WithdrawalFees, *,
};
use frame_support::traits::fungibles::Inspect;
use frame_support::{
assert_noop, assert_ok,
traits::{fungible::Mutate as FungibleMutate, fungibles::Mutate as FungiblesMutate},
Expand All @@ -33,6 +34,7 @@ use sp_runtime::{
};
use thea_primitives::types::NewWithdraw;
use thea_primitives::types::{AssetMetadata, Deposit};
use xcm::v3::Junction;
use xcm::{opaque::lts::Junctions, v3::MultiLocation, VersionedMultiLocation};

fn assert_last_event<T: crate::Config>(generic_event: <T as crate::Config>::RuntimeEvent) {
Expand Down Expand Up @@ -517,6 +519,37 @@ fn test_claim_deposit_returns_asset_not_registered() {
})
}

#[test]
fn test_create_parachain_asset() {
new_test_ext().execute_with(|| {
let multilocation =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(100)) };
let asset = xcm::v3::AssetId::Concrete(multilocation);
Balances::set_balance(&TheaExecutor::thea_account(), 1_000_000_000_000_000_000);
assert_ok!(TheaExecutor::create_parachain_asset(
RuntimeOrigin::root(),
Box::new(asset),
Default::default(),
Default::default(),
10
));
let asset_id =
polkadex_primitives::assets::generate_asset_id_for_parachain(Box::new(asset));
assert!(Assets::asset_exists(asset_id));
let expected_metadata = AssetMetadata::new(10);
let actual_metadata = <Metadata<Test>>::get(asset_id);
assert_eq!(expected_metadata, actual_metadata);
assert!(TheaExecutor::create_parachain_asset(
RuntimeOrigin::root(),
Box::new(asset),
Default::default(),
Default::default(),
10
)
.is_err());
})
}

fn setup_pool() {
let asset_id = 1u128;
let admin = 1u64;
Expand Down
3 changes: 3 additions & 0 deletions pallets/xcm-helper/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ benchmarks! {
// assert!(failed_withdrawals.is_empty());
// assert!(withdrawals.is_empty())
// }



}

#[cfg(test)]
Expand Down
6 changes: 4 additions & 2 deletions pallets/xcm-helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ pub mod pallet {
/// Assets
type Assets: frame_support::traits::tokens::fungibles::Mutate<Self::AccountId>
+ frame_support::traits::tokens::fungibles::Create<Self::AccountId>
+ frame_support::traits::tokens::fungibles::Inspect<Self::AccountId>;
+ frame_support::traits::tokens::fungibles::Inspect<Self::AccountId>
+ frame_support::traits::tokens::fungibles::metadata::Mutate<Self::AccountId>;
/// Asset Id
type AssetId: Member
+ Parameter
Expand Down Expand Up @@ -552,7 +553,8 @@ pub mod pallet {
return T::NativeAssetId::get().into();
}
// If it's not native, then hash and generate the asset id
let asset_id = u128::from_be_bytes(sp_io::hashing::blake2_128(&asset.encode()[..]));
let asset_id =
polkadex_primitives::assets::generate_asset_id_for_parachain(Box::new(asset));
if !<ParachainAssets<T>>::contains_key(asset_id) {
// Store the mapping
<ParachainAssets<T>>::insert(asset_id, asset);
Expand Down
2 changes: 2 additions & 0 deletions primitives/polkadex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rust_decimal = { git = "https://github.com/Polkadex-Substrate/rust-decimal.git",
"scale-codec",
"serde",
], default-features = false }
xcm = { workspace = true, default-features = false }

[dev-dependencies]
pretty_assertions = "1.2.1"
Expand All @@ -45,5 +46,6 @@ std = [
"sp-runtime/std",
"rust_decimal/std",
"rust_decimal/serde",
"xcm/std"
]
full_crypto = ['sp-core/full_crypto']
16 changes: 16 additions & 0 deletions primitives/polkadex/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::Balance;
#[cfg(not(feature = "std"))]
use codec::alloc::string::ToString;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::__private::sp_io;
use frame_support::{
ensure,
traits::{
Expand All @@ -46,6 +47,7 @@ pub trait Resolver<
+ frame_support::traits::tokens::fungible::Inspect<AccountId>,
Others: frame_support::traits::tokens::fungibles::Mutate<AccountId>
+ frame_support::traits::tokens::fungibles::Inspect<AccountId>
+ frame_support::traits::tokens::fungibles::metadata::Mutate<AccountId>
+ frame_support::traits::tokens::fungibles::Create<AccountId>,
AssetId: Into<Others::AssetId> + sp_std::cmp::PartialEq + Copy,
NativeAssetId: Get<AssetId>,
Expand Down Expand Up @@ -149,6 +151,16 @@ pub trait Resolver<
}
Ok(())
}

fn set_token_metadata(
asset: AssetId,
from: &AccountId,
name: sp_std::vec::Vec<u8>,
symbol: sp_std::vec::Vec<u8>,
decimal: u8,
) -> Result<(), DispatchError> {
Others::set(asset.into(), from, name, symbol, decimal)
}
}

/// Enumerated asset on chain
Expand Down Expand Up @@ -275,6 +287,10 @@ impl<'de> Visitor<'de> for AssetId {
}
}

pub fn generate_asset_id_for_parachain(asset: sp_std::boxed::Box<xcm::v3::AssetId>) -> u128 {
u128::from_be_bytes(sp_io::hashing::blake2_128(&asset.encode()[..]))
}

#[cfg(feature = "std")]
impl TryFrom<String> for AssetId {
type Error = anyhow::Error;
Expand Down

0 comments on commit d5a2d91

Please sign in to comment.