From bf8e74555a16c629e4d284aabd42a7f1c9cee6cb Mon Sep 17 00:00:00 2001 From: Honza <115138297+Supremesource@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:22:32 -0300 Subject: [PATCH 1/5] wip --- pallets/subspace/src/selections/dispatches.rs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/pallets/subspace/src/selections/dispatches.rs b/pallets/subspace/src/selections/dispatches.rs index 4e771de7c..450e27f0a 100644 --- a/pallets/subspace/src/selections/dispatches.rs +++ b/pallets/subspace/src/selections/dispatches.rs @@ -186,5 +186,57 @@ 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, amount: u64) -> DispatchResult { + let key = ensure_signed(origin)?; + + ensure!( + Self::has_enough_balance(&key, amount), + Error::::NotEnoughBalance + ); + + // 1. Remove the balance from the account + let Some(removed_balance_as_currency) = Self::u64_to_balance(amount) else { + return Err(Error::::CouldNotConvertToBalance.into()); + }; + + Self::remove_balance_from_account(&key, removed_balance_as_currency)?; + + Bridged::::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, amount: u64) -> DispatchResult { + let key = ensure_signed(origin)?; + + // Check if user has enough bridged tokens + let bridged_amount = Bridged::::get(&key); + ensure!( + bridged_amount >= amount && amount > 0, + Error::::NotEnoughBridgedTokens + ); + + // Convert amount to balance + let Some(amount_as_currency) = Self::u64_to_balance(amount) else { + return Err(Error::::CouldNotConvertToBalance.into()); + }; + + // Add balance back to account + Self::add_balance_to_account(&key, amount_as_currency); + + // Reduce bridged amount + Bridged::::mutate(&key, |bridged| *bridged = bridged.saturating_sub(amount)); + + Self::deposit_event(Event::BridgeWithdrawn(key, amount)); + + Ok(()) + } } } From f7eb87e55f5de54a776cb9a6022fec36adee11e0 Mon Sep 17 00:00:00 2001 From: Honza <115138297+Supremesource@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:26:55 -0300 Subject: [PATCH 2/5] feat: adding full bridge fn --- pallets/subspace/src/lib.rs | 2 ++ pallets/subspace/src/selections/errors.rs | 4 ++++ pallets/subspace/src/selections/events.rs | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/pallets/subspace/src/lib.rs b/pallets/subspace/src/lib.rs index b4a4fabe3..f5e50904e 100644 --- a/pallets/subspace/src/lib.rs +++ b/pallets/subspace/src/lib.rs @@ -469,6 +469,8 @@ pub mod pallet { pub type WeightSettingDelegation = StorageDoubleMap<_, Identity, u16, Identity, T::AccountId, T::AccountId>; + #[pallet::storage] + pub type Bridged = StorageMap<_, Identity, T::AccountId, u64, ValueQuery>; // --- Module Fees --- /// Default values for fees used throughout the module diff --git a/pallets/subspace/src/selections/errors.rs b/pallets/subspace/src/selections/errors.rs index 85cc2ea49..0c580128a 100644 --- a/pallets/subspace/src/selections/errors.rs +++ b/pallets/subspace/src/selections/errors.rs @@ -209,5 +209,9 @@ 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, } } diff --git a/pallets/subspace/src/selections/events.rs b/pallets/subspace/src/selections/events.rs index 7702fcb9a..41a6bdd4f 100644 --- a/pallets/subspace/src/selections/events.rs +++ b/pallets/subspace/src/selections/events.rs @@ -28,5 +28,9 @@ pub mod events { GlobalParamsUpdated(GlobalParams), /// 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), } } From e1133406504a16bbaccfbadfbe9d0008f384b0d5 Mon Sep 17 00:00:00 2001 From: Honza <115138297+Supremesource@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:08:41 -0300 Subject: [PATCH 3/5] bump spec_version --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 50fc74a8c..926cf3ad8 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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, From 86417bd3998e9093628d62826f3846230900231d Mon Sep 17 00:00:00 2001 From: Honza Date: Mon, 23 Dec 2024 19:06:53 +0100 Subject: [PATCH 4/5] feat: added bridge time limitation --- pallets/subspace/src/lib.rs | 6 ++++++ pallets/subspace/src/selections/dispatches.rs | 12 ++++++++++++ pallets/subspace/src/selections/errors.rs | 2 ++ 3 files changed, 20 insertions(+) diff --git a/pallets/subspace/src/lib.rs b/pallets/subspace/src/lib.rs index f5e50904e..0282d0acf 100644 --- a/pallets/subspace/src/lib.rs +++ b/pallets/subspace/src/lib.rs @@ -561,4 +561,10 @@ pub mod pallet { Ok(()) } } + impl Pallet { + /// 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; + } } diff --git a/pallets/subspace/src/selections/dispatches.rs b/pallets/subspace/src/selections/dispatches.rs index 450e27f0a..eb4267fd6 100644 --- a/pallets/subspace/src/selections/dispatches.rs +++ b/pallets/subspace/src/selections/dispatches.rs @@ -192,6 +192,12 @@ pub mod dispatches { pub fn bridge(origin: OriginFor, 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(¤t_block), + Error::::OutsideValidBlockRange + ); + ensure!( Self::has_enough_balance(&key, amount), Error::::NotEnoughBalance @@ -216,6 +222,12 @@ pub mod dispatches { pub fn bridge_withdraw(origin: OriginFor, 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(¤t_block), + Error::::OutsideValidBlockRange + ); + // Check if user has enough bridged tokens let bridged_amount = Bridged::::get(&key); ensure!( diff --git a/pallets/subspace/src/selections/errors.rs b/pallets/subspace/src/selections/errors.rs index 0c580128a..8af066b16 100644 --- a/pallets/subspace/src/selections/errors.rs +++ b/pallets/subspace/src/selections/errors.rs @@ -213,5 +213,7 @@ pub mod errors { NotEnoughBalance, /// Not having enough tokens to bridge back NotEnoughBridgedTokens, + /// User is trying to bridge tokens in closed period + OutsideValidBlockRange, } } From 111b627e866cc57daefccb08ccc691c42d946947 Mon Sep 17 00:00:00 2001 From: Honza Date: Mon, 23 Dec 2024 19:08:56 +0100 Subject: [PATCH 5/5] feat: bump spec version --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 926cf3ad8..0897fb9ee 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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,