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

feat: adding bridge #237

Merged
merged 5 commits into from
Dec 23, 2024
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
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