Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bridge] Add polkadot parachain pot #917

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cstrml/bridge-transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ decl_module! {
<bridge::Module<T>>::transfer_fungible(dest_id, T::BridgeTokenId::get(), recipient, U256::from(amount.saturating_sub(fee).saturated_into::<u128>()))
}

/// 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<T>, recipient: Vec<u8>) -> DispatchResult {
let source = ensure_signed(origin)?;
// Set polkadot parachain chain id to 101
let dest_id = 101u8;
ensure!(<bridge::Module<T>>::chain_whitelisted(dest_id), Error::<T>::InvalidTransfer);
let polkadot_parachain_pot = <bridge::Module<T>>::polkadot_parachain_pot();
ensure!(BridgeFee::<T>::contains_key(&dest_id), Error::<T>::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::<T>::LessThanFee);
T::Currency::transfer(&source, &polkadot_parachain_pot, amount.into(), AllowDeath)?;

<bridge::Module<T>>::transfer_fungible(dest_id, T::BridgeTokenId::get(), recipient, U256::from(amount.saturating_sub(fee).saturated_into::<u128>()))
}

//
// Executable calls. These can be triggered by a bridge transfer initiated on another chain
//
Expand All @@ -144,5 +166,14 @@ decl_module! {
<T as Config>::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<T>, _rid: [u8; 32]) -> DispatchResult {
let _ = T::BridgeOrigin::ensure_origin(origin)?;
let polkadot_parachain_pot = <bridge::Module<T>>::polkadot_parachain_pot();
<T as Config>::Currency::transfer(&polkadot_parachain_pot, &to, amount.into(), AllowDeath)?;
Ok(())
}
}
}
7 changes: 7 additions & 0 deletions cstrml/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -358,6 +360,11 @@ impl<T: Config> Module<T> {
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;
Expand Down