Skip to content

Commit

Permalink
Updated withdraw (#939)
Browse files Browse the repository at this point in the history
## Describe your changes
Updated withdraw.
  • Loading branch information
Gauthamastro authored Mar 28, 2024
2 parents 3d7ed6e + b641430 commit 4c16cf2
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 81 deletions.
9 changes: 6 additions & 3 deletions pallets/thea-executor/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use parity_scale_codec::Decode;
use polkadex_primitives::UNIT_BALANCE;
use sp_runtime::{traits::AccountIdConversion, SaturatedConversion};
use sp_std::{boxed::Box, collections::btree_set::BTreeSet, vec, vec::Vec};
use thea_primitives::types::{AssetMetadata, Deposit, Withdraw};
use thea_primitives::types::NewWithdraw;
use thea_primitives::types::{AssetMetadata, Deposit};
use xcm::VersionedMultiLocation;

fn create_deposit<T: Config>(recipient: T::AccountId) -> Vec<Deposit<T::AccountId>> {
Expand Down Expand Up @@ -107,7 +108,7 @@ benchmarks! {
<WithdrawalFees<T>>::insert(network_id, 1_000);
let multilocation = MultiLocation { parents: 1, interior: Junctions::Here };
let benificary = VersionedMultiLocation::V3(multilocation);
}: _(RawOrigin::Signed(account.clone()), 100, 1_000_000_000_000, Box::new(benificary), true, false)
}: _(RawOrigin::Signed(account.clone()), 100, 1_000_000_000_000, Box::new(benificary), None, None, true, false)
verify {
let ready_withdrawal = <ReadyWithdrawals<T>>::get(<frame_system::Pallet<T>>::block_number(), network_id);
assert_eq!(ready_withdrawal.len(), 1);
Expand Down Expand Up @@ -139,11 +140,13 @@ benchmarks! {
let y in 1 .. 1_000;
let network_len: usize = x as usize;
let network_len: u8 = network_len as u8;
let withdrawal = Withdraw {
let withdrawal = NewWithdraw {
id: vec![],
asset_id: 100,
amount: 1_000_000_000_000,
destination: vec![],
fee_asset_id: None,
fee_amount: None,
is_blocked: false,
extra: vec![],
};
Expand Down
45 changes: 40 additions & 5 deletions pallets/thea-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub trait TheaExecutorWeightInfo {
fn claim_deposit(_r: u32) -> Weight;
}

#[allow(clippy::too_many_arguments)]
#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -65,8 +66,9 @@ pub mod pallet {
use sp_core::{H160, H256};
use sp_runtime::{traits::AccountIdConversion, Saturating};
use sp_std::vec::Vec;
use thea_primitives::types::NewWithdraw;
use thea_primitives::{
types::{AssetMetadata, Deposit, Withdraw},
types::{AssetMetadata, Deposit},
Network, TheaBenchmarkHelper, TheaIncomingExecutor, TheaOutgoingExecutor, NATIVE_NETWORK,
};
use xcm::VersionedMultiLocation;
Expand Down Expand Up @@ -141,7 +143,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn pending_withdrawals)]
pub(super) type PendingWithdrawals<T: Config> =
StorageMap<_, Blake2_128Concat, Network, Vec<Withdraw>, ValueQuery>;
StorageMap<_, Blake2_128Concat, Network, Vec<NewWithdraw>, ValueQuery>;

/// Withdrawal Fees for each network
#[pallet::storage]
Expand All @@ -158,7 +160,7 @@ pub mod pallet {
BlockNumberFor<T>,
Blake2_128Concat,
Network,
Vec<Withdraw>,
Vec<NewWithdraw>,
ValueQuery,
>;

Expand Down Expand Up @@ -190,7 +192,7 @@ pub mod pallet {
/// Withdrawal Ready (Network id )
WithdrawalReady(Network),
/// Withdrawal Failed ( Network ,Vec<Withdrawal>)
WithdrawalFailed(Network, Vec<Withdraw>),
WithdrawalFailed(Network, Vec<NewWithdraw>),
/// Thea Public Key Updated ( network, new session id )
TheaKeyUpdated(Network, u32),
/// Withdrawal Fee Set (NetworkId, Amount)
Expand Down Expand Up @@ -220,6 +222,8 @@ pub mod pallet {
WithdrawalFeeConfigNotFound,
/// Asset Not Registered
AssetNotRegistered,
/// Fee Asset Not Registered
FeeAssetNotRegistered,
/// Amount cannot be Zero
AmountCannotBeZero,
/// Failed To Handle Parachain Deposit
Expand Down Expand Up @@ -289,6 +293,8 @@ pub mod pallet {
asset_id,
amount,
beneficiary,
None,
None,
pay_for_remaining,
network,
pay_with_tokens,
Expand Down Expand Up @@ -323,6 +329,8 @@ pub mod pallet {
asset_id: u128,
amount: u128,
beneficiary: sp_std::boxed::Box<VersionedMultiLocation>,
fee_asset_id: Option<u128>,
fee_amount: Option<u128>,
pay_for_remaining: bool,
pay_with_tokens: bool,
) -> DispatchResult {
Expand All @@ -333,6 +341,8 @@ pub mod pallet {
asset_id,
amount,
beneficiary.encode(),
fee_asset_id,
fee_amount,
pay_for_remaining,
network,
pay_with_tokens,
Expand Down Expand Up @@ -410,6 +420,8 @@ pub mod pallet {
asset_id,
amount,
beneficiary.encode(),
None,
None,
pay_for_remaining,
network,
pay_with_tokens,
Expand Down Expand Up @@ -480,6 +492,8 @@ pub mod pallet {
asset_id: u128,
mut amount: u128,
beneficiary: Vec<u8>,
fee_asset_id: Option<u128>,
fee_amount: Option<u128>,
pay_for_remaining: bool,
network: Network,
pay_with_tokens: bool,
Expand All @@ -488,6 +502,10 @@ pub mod pallet {
ensure!(network != 0, Error::<T>::WrongNetwork);
let mut pending_withdrawals = <PendingWithdrawals<T>>::get(network);
let metadata = <Metadata<T>>::get(asset_id).ok_or(Error::<T>::AssetNotRegistered)?;
if let Some(fee_asset_id) = fee_asset_id {
let _metadata = <crate::pallet::Metadata<T>>::get(fee_asset_id)
.ok_or(Error::<T>::FeeAssetNotRegistered)?;
}
ensure!(
pending_withdrawals.len() < T::WithdrawalSize::get() as usize,
Error::<T>::WithdrawalNotAllowed
Expand Down Expand Up @@ -535,11 +553,22 @@ pub mod pallet {
// Withdraw assets
Self::resolver_withdraw(asset_id.into(), amount, &user, Self::thea_account())?;

let mut withdraw = Withdraw {
if let (Some(fee_asset_id), Some(fee_amount)) = (fee_asset_id, fee_amount) {
Self::resolver_withdraw(
fee_asset_id.into(),
fee_amount,
&user,
Self::thea_account(),
)?;
}

let mut withdraw = NewWithdraw {
id: Self::new_random_id(),
asset_id,
amount,
destination: beneficiary.clone(),
fee_asset_id,
fee_amount,
is_blocked: false,
extra: Vec::new(),
};
Expand All @@ -556,6 +585,12 @@ pub mod pallet {
// Convert back to origin decimals
withdraw.amount = metadata.convert_from_native_decimals(amount);

if let (Some(fee_asset_id), Some(fee_amount)) = (fee_asset_id, fee_amount) {
let metadata = <crate::pallet::Metadata<T>>::get(fee_asset_id)
.ok_or(Error::<T>::FeeAssetNotRegistered)?;
withdraw.fee_amount = Some(metadata.convert_from_native_decimals(fee_amount));
}

pending_withdrawals.push(withdraw);

if (pending_withdrawals.len() >= T::WithdrawalSize::get() as usize) || pay_for_remaining
Expand Down
17 changes: 15 additions & 2 deletions pallets/thea-executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use sp_runtime::{
traits::{AccountIdConversion, BadOrigin},
SaturatedConversion,
};
use thea_primitives::types::{AssetMetadata, Deposit, Withdraw};
use thea_primitives::types::NewWithdraw;
use thea_primitives::types::{AssetMetadata, Deposit};
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 @@ -93,11 +94,13 @@ fn test_transfer_native_asset() {
));
// Verify
let pending_withdrawal = <PendingWithdrawals<Test>>::get(1);
let approved_withdraw = Withdraw {
let approved_withdraw = NewWithdraw {
id: Vec::from([179, 96, 16, 235, 40, 92, 21, 74, 140, 214]),
asset_id,
amount: 10_000_000_000_000u128,
destination: vec![1; 32],
fee_asset_id: None,
fee_amount: None,
is_blocked: false,
extra: vec![],
};
Expand Down Expand Up @@ -208,6 +211,8 @@ fn test_parachain_withdraw_full() {
u128::MAX,
1_000_000_000,
beneficiary.clone(),
None,
None,
false,
false
),
Expand All @@ -219,6 +224,8 @@ fn test_parachain_withdraw_full() {
u128::MAX,
1_000_000_000,
beneficiary.clone(),
None,
None,
false,
false
),
Expand All @@ -231,6 +238,8 @@ fn test_parachain_withdraw_full() {
u128::MAX,
1_000_000_000,
beneficiary.clone(),
None,
None,
false,
false
),
Expand All @@ -243,6 +252,8 @@ fn test_parachain_withdraw_full() {
asset_id,
1_000_000_000,
beneficiary.clone(),
None,
None,
false,
false
),
Expand All @@ -254,6 +265,8 @@ fn test_parachain_withdraw_full() {
asset_id,
1_000_000_000,
beneficiary.clone(),
None,
None,
false,
false
));
Expand Down
Loading

0 comments on commit 4c16cf2

Please sign in to comment.