From b927e0d3a2a8b88325c8340958b16e2a09fedf11 Mon Sep 17 00:00:00 2001 From: Myron <49134743+mmyyrroonn@users.noreply.github.com> Date: Wed, 21 Dec 2022 11:16:38 +0800 Subject: [PATCH] [Bridge] Add polkadot parachain pot (#917) --- cstrml/bridge-transfer/src/lib.rs | 31 +++++++++++++++++++++++++++++++ cstrml/bridge/src/lib.rs | 7 +++++++ 2 files changed, 38 insertions(+) diff --git a/cstrml/bridge-transfer/src/lib.rs b/cstrml/bridge-transfer/src/lib.rs index a970a7b2..ef82a892 100644 --- a/cstrml/bridge-transfer/src/lib.rs +++ b/cstrml/bridge-transfer/src/lib.rs @@ -124,6 +124,28 @@ decl_module! { >::transfer_fungible(dest_id, T::BridgeTokenId::get(), recipient, U256::from(amount.saturating_sub(fee).saturated_into::())) } + /// Transfers some amount of the native token to some recipient on a (whitelisted) destination chain. + #[weight = 195_000_000] + pub fn transfer_to_polkadot_parachain(origin, amount: BalanceOf, recipient: Vec) -> DispatchResult { + let source = ensure_signed(origin)?; + // Set polkadot parachain chain id to 101 + let dest_id = 101u8; + ensure!(>::chain_whitelisted(dest_id), Error::::InvalidTransfer); + let polkadot_parachain_pot = >::polkadot_parachain_pot(); + ensure!(BridgeFee::::contains_key(&dest_id), Error::::FeeOptionsMissiing); + let (min_fee, fee_scale) = Self::bridge_fee(dest_id); + let fee_estimated = amount * fee_scale.into() / 1000u32.into(); + let fee = if fee_estimated > min_fee { + fee_estimated + } else { + min_fee + }; + ensure!(amount > fee, Error::::LessThanFee); + T::Currency::transfer(&source, &polkadot_parachain_pot, amount.into(), AllowDeath)?; + + >::transfer_fungible(dest_id, T::BridgeTokenId::get(), recipient, U256::from(amount.saturating_sub(fee).saturated_into::())) + } + // // Executable calls. These can be triggered by a bridge transfer initiated on another chain // @@ -144,5 +166,14 @@ decl_module! { ::Currency::transfer(&elrond_pot, &to, amount.into(), AllowDeath)?; Ok(()) } + + /// Executes a simple currency transfer using the bridge account as the source + #[weight = 195_000_000] + pub fn transfer_from_polkadot_parachain(origin, to: T::AccountId, amount: BalanceOf, _rid: [u8; 32]) -> DispatchResult { + let _ = T::BridgeOrigin::ensure_origin(origin)?; + let polkadot_parachain_pot = >::polkadot_parachain_pot(); + ::Currency::transfer(&polkadot_parachain_pot, &to, amount.into(), AllowDeath)?; + Ok(()) + } } } diff --git a/cstrml/bridge/src/lib.rs b/cstrml/bridge/src/lib.rs index 4ee1e96d..024982c9 100644 --- a/cstrml/bridge/src/lib.rs +++ b/cstrml/bridge/src/lib.rs @@ -30,6 +30,7 @@ mod tests; const DEFAULT_RELAYER_THRESHOLD: u32 = 1; const MODULE_ID: ModuleId = ModuleId(*b"crust/bg"); const ELROND_ID: ModuleId = ModuleId(*b"elrondbg"); +const PARACHAIN_ID: ModuleId = ModuleId(*b"parac/bg"); /// Helper function to concatenate a chain ID and some bytes to produce a resource ID. @@ -214,6 +215,7 @@ decl_module! { const ProposalLifetime: T::BlockNumber = T::ProposalLifetime::get(); const BridgeAccountId: T::AccountId = MODULE_ID.into_account(); const ElrondAccountId: T::AccountId = ELROND_ID.into_account(); + const PolkadotParachainAccountId: T::AccountId = PARACHAIN_ID.into_account(); fn deposit_event() = default; @@ -358,6 +360,11 @@ impl Module { ELROND_ID.into_account() } + /// Provides an AccountId for the pallet to store polkadot parachain bridge. + pub fn polkadot_parachain_pot() -> T::AccountId { + PARACHAIN_ID.into_account() + } + /// Asserts if a resource is registered pub fn resource_exists(id: [u8; 32]) -> bool { return Self::resources(id) != None;