Skip to content

Commit 8d198d3

Browse files
Merge pull request #666 from Cerebellum-Network/fix/pool-withdrawal-fix
Added pool-withdrawal-fix
2 parents 2baee51 + f677340 commit 8d198d3

File tree

8 files changed

+196
-4
lines changed

8 files changed

+196
-4
lines changed

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ members = [
2222
"pallets/ddc-clusters-gov",
2323
"pallets/fee-handler",
2424
"pallets/origins",
25+
"pallets/pool-withdrawal-fix",
2526
"runtime/cere",
2627
"runtime/cere-dev",
2728
"contracts/customer-deposit",
@@ -193,6 +194,7 @@ pallet-ddc-staking = { path = "pallets/ddc-staking", default-features = false }
193194
pallet-erc20 = { path = "pallets/erc20", default-features = false }
194195
pallet-erc721 = { path = "pallets/erc721", default-features = false }
195196
pallet-origins = { path = "pallets/origins", default-features = false }
197+
pallet-pool-withdrawal-fix = { path = "pallets/pool-withdrawal-fix", default-features = false }
196198
pallet-fee-handler = { path = "pallets/fee-handler", default-features = false }
197199

198200
# Cere External Dependencies
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[package]
2+
name = "pallet-pool-withdrawal-fix"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
homepage.workspace = true
7+
license.workspace = true
8+
readme.workspace = true
9+
repository.workspace = true
10+
11+
[dependencies]
12+
# 3rd-party dependencies
13+
codec = { workspace = true }
14+
scale-info = { workspace = true }
15+
serde = { workspace = true }
16+
17+
# Substrate dependencies
18+
frame-benchmarking = { workspace = true, optional = true }
19+
frame-support = { workspace = true }
20+
frame-system = { workspace = true }
21+
sp-core = { workspace = true }
22+
sp-runtime = { workspace = true }
23+
sp-staking = { workspace = true }
24+
sp-std = { workspace = true }
25+
26+
[features]
27+
default = ["std"]
28+
std = [
29+
"codec/std",
30+
"frame-benchmarking/std",
31+
"frame-support/std",
32+
"frame-system/std",
33+
"scale-info/std",
34+
"sp-runtime/std",
35+
"sp-std/std",
36+
"sp-core/std",
37+
"serde/std",
38+
"sp-staking/std",
39+
]
40+
runtime-benchmarks = [
41+
"frame-benchmarking/runtime-benchmarks",
42+
"frame-support/runtime-benchmarks",
43+
"frame-system/runtime-benchmarks",
44+
"sp-runtime/runtime-benchmarks",
45+
]
46+
try-runtime = [
47+
"frame-support/try-runtime",
48+
"frame-system/try-runtime",
49+
]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//! # Pool Withdrawal Fix Pallet
2+
//!
3+
//! A pallet for handling pool withdrawal operations through delegation pallet connector.
4+
//! This pallet provides withdrawal functionality that can be called by root or governance.
5+
6+
#![cfg_attr(not(feature = "std"), no_std)]
7+
8+
use frame_support::pallet_prelude::Weight;
9+
pub use pallet::*;
10+
11+
#[frame_support::pallet]
12+
pub mod pallet {
13+
use crate::WeightInfo;
14+
use frame_support::pallet_prelude::*;
15+
use frame_support::traits::Currency;
16+
use frame_support::traits::EnsureOrigin;
17+
use frame_support::traits::LockableCurrency;
18+
use frame_system::pallet_prelude::*;
19+
use sp_staking::OnStakingUpdate;
20+
21+
pub type BalanceOf<T> =
22+
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
23+
24+
#[pallet::pallet]
25+
pub struct Pallet<T>(_);
26+
27+
/// Configure the pallet by specifying the parameters and types on which it depends.
28+
#[pallet::config]
29+
pub trait Config: frame_system::Config {
30+
/// Because this pallet emits events, it depends on the runtime's definition of an event.
31+
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
32+
type Currency: LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>;
33+
/// Integrate Delegated Pallet
34+
type DelegationPalletConnector: OnStakingUpdate<Self::AccountId, BalanceOf<Self>>;
35+
/// Governance origin for withdrawal calls
36+
type GovernanceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
37+
/// Type representing the weight of this pallet
38+
type WeightInfo: WeightInfo;
39+
}
40+
41+
/// Pallets use events to inform users when important changes are made.
42+
/// https://docs.substrate.io/main-docs/build/events-errors/
43+
#[pallet::event]
44+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
45+
pub enum Event<T: Config> {
46+
/// Withdrawal was called for a stash account. [stash_account, amount]
47+
WithdrawCalled { stash_account: T::AccountId, amount: BalanceOf<T> },
48+
}
49+
50+
#[pallet::error]
51+
pub enum Error<T> {
52+
/// Withdrawal failed due to insufficient balance or other staking constraints.
53+
WithdrawalFailed,
54+
}
55+
56+
/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
57+
/// These functions materialize as "extrinsics", which are often compared to transactions.
58+
/// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
59+
#[pallet::call]
60+
impl<T: Config> Pallet<T> {
61+
/// Call withdrawal for a stash account through the delegation pallet connector.
62+
/// This function can be called by root or governance.
63+
#[pallet::call_index(0)]
64+
#[pallet::weight(T::WeightInfo::call_withdraw())]
65+
pub fn call_withdraw(
66+
origin: OriginFor<T>,
67+
stash_account: T::AccountId,
68+
amount: BalanceOf<T>,
69+
) -> DispatchResult {
70+
T::GovernanceOrigin::ensure_origin(origin)?;
71+
72+
// Call the delegation pallet connector to handle withdrawal
73+
T::DelegationPalletConnector::on_withdraw(&stash_account, amount);
74+
75+
// Emit an event.
76+
Self::deposit_event(Event::WithdrawCalled { stash_account, amount });
77+
78+
Ok(())
79+
}
80+
}
81+
}
82+
83+
/// Weight functions needed for pallet_pool_withdrawal_fix.
84+
pub trait WeightInfo {
85+
fn call_withdraw() -> Weight;
86+
}
87+
88+
/// Default weight implementation for the pallet.
89+
impl WeightInfo for () {
90+
fn call_withdraw() -> Weight {
91+
Weight::from_parts(10_000, 0)
92+
}
93+
}

runtime/cere-dev/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pallet-erc20 = { workspace = true }
108108
pallet-erc721 = { workspace = true }
109109
pallet-fee-handler = { workspace = true }
110110
pallet-origins = { workspace = true }
111+
pallet-pool-withdrawal-fix = { workspace = true }
111112

112113
# Hyperbridge Depedencies
113114
anyhow = { workspace = true }
@@ -208,6 +209,7 @@ std = [
208209
"pallet-ddc-clusters-gov/std",
209210
"sp-arithmetic/std",
210211
"pallet-origins/std",
212+
"pallet-pool-withdrawal-fix/std",
211213
"pallet-hyperbridge/std",
212214
"ismp/std",
213215
"pallet-ismp/std",
@@ -271,6 +273,7 @@ runtime-benchmarks = [
271273
"pallet-preimage/runtime-benchmarks",
272274
"pallet-ddc-clusters-gov/runtime-benchmarks",
273275
"pallet-origins/runtime-benchmarks",
276+
"pallet-pool-withdrawal-fix/runtime-benchmarks",
274277
"pallet-ddc-verification/runtime-benchmarks",
275278
"pallet-token-gateway/runtime-benchmarks",
276279
"pallet-migrations/runtime-benchmarks",
@@ -329,6 +332,7 @@ try-runtime = [
329332
"pallet-whitelist/try-runtime",
330333
"pallet-preimage/try-runtime",
331334
"pallet-origins/try-runtime",
335+
"pallet-pool-withdrawal-fix/try-runtime",
332336
"pallet-ddc-clusters-gov/try-runtime",
333337
"pallet-ddc-verification/try-runtime",
334338
"pallet-ismp/try-runtime",

runtime/cere-dev/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
173173
// and set impl_version to 0. If only runtime
174174
// implementation changes and behavior does not, then leave spec_version as
175175
// is and increment impl_version.
176-
spec_version: 73168,
176+
spec_version: 73169,
177177
impl_version: 0,
178178
apis: RUNTIME_API_VERSIONS,
179179
transaction_version: 26,
@@ -686,7 +686,7 @@ impl pallet_staking::Config for Runtime {
686686
type MaxUnlockingChunks = ConstU32<32>;
687687
type MaxControllersInDeprecationBatch = MaxControllersInDeprecationBatch;
688688
type HistoryDepth = frame_support::traits::ConstU32<84>;
689-
type EventListeners = NominationPools;
689+
type EventListeners = (NominationPools, DelegatedStaking);
690690
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
691691
type BenchmarkingConfig = StakingBenchmarkingConfig;
692692
type NominationsQuota = pallet_staking::FixedNominationsQuota<{ MaxNominations::get() }>;
@@ -1564,6 +1564,14 @@ impl pallet_fee_handler::Config for Runtime {
15641564
type WeightInfo = pallet_fee_handler::weights::SubstrateWeight<Runtime>;
15651565
}
15661566

1567+
impl pallet_pool_withdrawal_fix::Config for Runtime {
1568+
type RuntimeEvent = RuntimeEvent;
1569+
type Currency = Balances;
1570+
type DelegationPalletConnector = DelegatedStaking;
1571+
type GovernanceOrigin = EnsureRoot<AccountId>;
1572+
type WeightInfo = ();
1573+
}
1574+
15671575
#[frame_support::runtime]
15681576
mod runtime {
15691577
#[runtime::runtime]
@@ -1746,6 +1754,9 @@ mod runtime {
17461754
pub type FeeHandler = pallet_fee_handler::Pallet<Runtime>;
17471755
#[runtime::pallet_index(53)]
17481756
pub type DelegatedStaking = pallet_delegated_staking;
1757+
1758+
#[runtime::pallet_index(54)]
1759+
pub type PoolWithdrawalFix = pallet_pool_withdrawal_fix::Pallet<Runtime>;
17491760
}
17501761

17511762
/// The address format for describing accounts.

runtime/cere/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pallet-erc20 = { workspace = true }
106106
pallet-erc721 = { workspace = true }
107107
pallet-fee-handler = { workspace = true }
108108
pallet-origins = { workspace = true }
109+
pallet-pool-withdrawal-fix = { workspace = true }
109110
pallet-referenda = { workspace = true }
110111
pallet-whitelist = { workspace = true }
111112

@@ -205,6 +206,7 @@ std = [
205206
"pallet-whitelist/std",
206207
"pallet-preimage/std",
207208
"pallet-origins/std",
209+
"pallet-pool-withdrawal-fix/std",
208210
"pallet-hyperbridge/std",
209211
"ismp/std",
210212
"pallet-ismp/std",
@@ -268,6 +270,7 @@ runtime-benchmarks = [
268270
"pallet-preimage/runtime-benchmarks",
269271
"pallet-ddc-clusters-gov/runtime-benchmarks",
270272
"pallet-origins/runtime-benchmarks",
273+
"pallet-pool-withdrawal-fix/runtime-benchmarks",
271274
"pallet-ddc-verification/runtime-benchmarks",
272275
"pallet-token-gateway/runtime-benchmarks",
273276
"pallet-migrations/runtime-benchmarks",
@@ -327,6 +330,7 @@ try-runtime = [
327330
"pallet-whitelist/try-runtime",
328331
"pallet-preimage/try-runtime",
329332
"pallet-origins/try-runtime",
333+
"pallet-pool-withdrawal-fix/try-runtime",
330334
"pallet-ddc-clusters-gov/try-runtime",
331335
"pallet-ddc-verification/try-runtime",
332336
"pallet-ismp/try-runtime",

runtime/cere/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
163163
// and set impl_version to 0. If only runtime
164164
// implementation changes and behavior does not, then leave spec_version as
165165
// is and increment impl_version.
166-
spec_version: 73168,
166+
spec_version: 73169,
167167
impl_version: 0,
168168
apis: RUNTIME_API_VERSIONS,
169169
transaction_version: 26,
@@ -696,7 +696,7 @@ impl pallet_staking::Config for Runtime {
696696
type MaxUnlockingChunks = ConstU32<32>;
697697
type MaxControllersInDeprecationBatch = MaxControllersInDeprecationBatch;
698698
type HistoryDepth = HistoryDepth;
699-
type EventListeners = NominationPools;
699+
type EventListeners = (NominationPools, DelegatedStaking);
700700
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
701701
type BenchmarkingConfig = StakingBenchmarkingConfig;
702702
type NominationsQuota = pallet_staking::FixedNominationsQuota<{ MaxNominations::get() }>;
@@ -1553,6 +1553,14 @@ impl pallet_fee_handler::Config for Runtime {
15531553
type WeightInfo = pallet_fee_handler::weights::SubstrateWeight<Runtime>;
15541554
}
15551555

1556+
impl pallet_pool_withdrawal_fix::Config for Runtime {
1557+
type RuntimeEvent = RuntimeEvent;
1558+
type Currency = Balances;
1559+
type DelegationPalletConnector = DelegatedStaking;
1560+
type GovernanceOrigin = EnsureRoot<AccountId>;
1561+
type WeightInfo = ();
1562+
}
1563+
15561564
#[frame_support::runtime]
15571565
mod runtime {
15581566
#[runtime::runtime]
@@ -1734,6 +1742,9 @@ mod runtime {
17341742

17351743
#[runtime::pallet_index(53)]
17361744
pub type DelegatedStaking = pallet_delegated_staking;
1745+
1746+
#[runtime::pallet_index(54)]
1747+
pub type PoolWithdrawalFix = pallet_pool_withdrawal_fix::Pallet<Runtime>;
17371748
}
17381749

17391750
/// The address format for describing accounts.

0 commit comments

Comments
 (0)