Skip to content

Commit

Permalink
Merge pull request #237 from renlabs-dev/bridge
Browse files Browse the repository at this point in the history
feat: adding bridge
  • Loading branch information
functor-flow authored Dec 23, 2024
2 parents ebfda29 + a129317 commit eb9e496
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ pub mod pallet {
pub type WeightSettingDelegation<T: Config> =
StorageDoubleMap<_, Identity, u16, Identity, T::AccountId, T::AccountId>;

#[pallet::storage]
pub type Bridged<T: Config> = StorageMap<_, Identity, T::AccountId, u64, ValueQuery>;
// --- Module Fees ---

/// Default values for fees used throughout the module
Expand Down Expand Up @@ -559,4 +561,10 @@ pub mod pallet {
Ok(())
}
}
impl<T: Config> Pallet<T> {
/// Dec 24, 2024, 11:11 PM UTC
const START_BRIDGE_BLOCK: u64 = 3385177;
/// Dec 31, 2024, 11:11 PM UTC
const END_BRIDGE_BLOCK: u64 = 3460777;
}
}
64 changes: 64 additions & 0 deletions pallets/subspace/src/selections/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,69 @@ pub mod dispatches {
) -> DispatchResult {
Self::do_register_subnet(origin, name, metadata)
}

#[pallet::call_index(13)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))]
pub fn bridge(origin: OriginFor<T>, amount: u64) -> DispatchResult {
let key = ensure_signed(origin)?;

let current_block = Self::get_current_block_number();
ensure!(
(Self::START_BRIDGE_BLOCK..=Self::END_BRIDGE_BLOCK).contains(&current_block),
Error::<T>::OutsideValidBlockRange
);

ensure!(
Self::has_enough_balance(&key, amount),
Error::<T>::NotEnoughBalance
);

// 1. Remove the balance from the account
let Some(removed_balance_as_currency) = Self::u64_to_balance(amount) else {
return Err(Error::<T>::CouldNotConvertToBalance.into());
};

Self::remove_balance_from_account(&key, removed_balance_as_currency)?;

Bridged::<T>::mutate(&key, |bridged| *bridged = bridged.saturating_add(amount));

Self::deposit_event(Event::Bridged(key, amount));

Ok(())
}

#[pallet::call_index(14)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))]
pub fn bridge_withdraw(origin: OriginFor<T>, amount: u64) -> DispatchResult {
let key = ensure_signed(origin)?;

let current_block = Self::get_current_block_number();
ensure!(
(Self::START_BRIDGE_BLOCK..=Self::END_BRIDGE_BLOCK).contains(&current_block),
Error::<T>::OutsideValidBlockRange
);

// Check if user has enough bridged tokens
let bridged_amount = Bridged::<T>::get(&key);
ensure!(
bridged_amount >= amount && amount > 0,
Error::<T>::NotEnoughBridgedTokens
);

// Convert amount to balance
let Some(amount_as_currency) = Self::u64_to_balance(amount) else {
return Err(Error::<T>::CouldNotConvertToBalance.into());
};

// Add balance back to account
Self::add_balance_to_account(&key, amount_as_currency);

// Reduce bridged amount
Bridged::<T>::mutate(&key, |bridged| *bridged = bridged.saturating_sub(amount));

Self::deposit_event(Event::BridgeWithdrawn(key, amount));

Ok(())
}
}
}
6 changes: 6 additions & 0 deletions pallets/subspace/src/selections/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,11 @@ pub mod errors {
/// Cannot decrease fees below their current values.
/// Fees can only be increased to prevent economic attacks.
CannotDecreaseFee,
/// General error for not having enough balance
NotEnoughBalance,
/// Not having enough tokens to bridge back
NotEnoughBridgedTokens,
/// User is trying to bridge tokens in closed period
OutsideValidBlockRange,
}
}
4 changes: 4 additions & 0 deletions pallets/subspace/src/selections/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ pub mod events {
GlobalParamsUpdated(GlobalParams<T>),
/// Event created when subnet parameters are updated
SubnetParamsUpdated(u16),
/// Event created when assets were returned from the bridge
BridgeWithdrawn(T::AccountId, u64),
/// Event created when user bridged tokens
Bridged(T::AccountId, u64),
}
}
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node-subspace"),
impl_name: create_runtime_str!("node-subspace"),
authoring_version: 1,
spec_version: 512,
spec_version: 513,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand All @@ -217,7 +217,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node-subspace"),
impl_name: create_runtime_str!("node-subspace"),
authoring_version: 1,
spec_version: 126,
spec_version: 127,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down

0 comments on commit eb9e496

Please sign in to comment.