From 662a9cdcd6cd5ef538afc7bf6cb1a3213a8e5778 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 17 Nov 2025 12:48:16 -0500 Subject: [PATCH 1/5] clamp at values given to/taken from the user --- pallets/subtensor/src/staking/stake_utils.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 1cbd0d2b7..36b885904 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -707,6 +707,9 @@ impl Pallet { let protocol_tao_after = Self::get_protocol_tao(netuid); // This should decrease as we are removing TAO from the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after); + // Clamp the protocol TAO delta to the received TAO + let tao_flow = + protocol_tao_delta.clamp(TaoCurrency::ZERO, swap_result.amount_paid_out.into()); // Refund the unused alpha (in case if limit price is hit) let refund = actual_alpha_decrease.saturating_sub( @@ -734,7 +737,7 @@ impl Pallet { // } // Record TAO outflow - Self::record_tao_outflow(netuid, protocol_tao_delta); + Self::record_tao_outflow(netuid, tao_flow); LastColdkeyHotkeyStakeBlock::::insert(coldkey, hotkey, Self::get_current_block_as_u64()); @@ -784,6 +787,8 @@ impl Pallet { // This should increase as we are adding TAO to the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao); + // Clamp the protocol TAO delta to the stated input + let tao_flow = protocol_tao_delta.clamp(TaoCurrency::ZERO, tao); ensure!( !swap_result.amount_paid_out.is_zero(), @@ -820,7 +825,7 @@ impl Pallet { } // Record TAO inflow - Self::record_tao_inflow(netuid, protocol_tao_delta); + Self::record_tao_inflow(netuid, tao_flow); LastColdkeyHotkeyStakeBlock::::insert(coldkey, hotkey, Self::get_current_block_as_u64()); From ff904331d65dadb600b38a3d0ec14f1ed1aeacb7 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 17 Nov 2025 12:48:35 -0500 Subject: [PATCH 2/5] spec bump --- 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 f6843c50e..9ece1dd02 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 346, + spec_version: 347, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From e601cac43bdbe33bcee77abf7f05a0116c6c653f Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 17 Nov 2025 13:49:07 -0500 Subject: [PATCH 3/5] use min instead of clamp --- pallets/subtensor/src/staking/stake_utils.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 36b885904..82726fb81 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -708,8 +708,7 @@ impl Pallet { // This should decrease as we are removing TAO from the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after); // Clamp the protocol TAO delta to the received TAO - let tao_flow = - protocol_tao_delta.clamp(TaoCurrency::ZERO, swap_result.amount_paid_out.into()); + let tao_flow = protocol_tao_delta.min(swap_result.amount_paid_out.into()); // Refund the unused alpha (in case if limit price is hit) let refund = actual_alpha_decrease.saturating_sub( @@ -788,7 +787,7 @@ impl Pallet { // This should increase as we are adding TAO to the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao); // Clamp the protocol TAO delta to the stated input - let tao_flow = protocol_tao_delta.clamp(TaoCurrency::ZERO, tao); + let tao_flow = protocol_tao_delta.min(tao); ensure!( !swap_result.amount_paid_out.is_zero(), From 3e0587894d472499f7dab4f6e54bcd2737505d48 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 17 Nov 2025 13:51:00 -0500 Subject: [PATCH 4/5] overstate the outflow --- pallets/subtensor/src/staking/stake_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 82726fb81..5442b5873 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -708,7 +708,7 @@ impl Pallet { // This should decrease as we are removing TAO from the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after); // Clamp the protocol TAO delta to the received TAO - let tao_flow = protocol_tao_delta.min(swap_result.amount_paid_out.into()); + let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into()); // Refund the unused alpha (in case if limit price is hit) let refund = actual_alpha_decrease.saturating_sub( From 2feceb878771bfe6e55a1edffb1fe785f353016e Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Mon, 17 Nov 2025 13:51:44 -0500 Subject: [PATCH 5/5] update comments --- pallets/subtensor/src/staking/stake_utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 5442b5873..402f999db 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -707,7 +707,7 @@ impl Pallet { let protocol_tao_after = Self::get_protocol_tao(netuid); // This should decrease as we are removing TAO from the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after); - // Clamp the protocol TAO delta to the received TAO + // Use max to overstate the TAO flow from the protocol. let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into()); // Refund the unused alpha (in case if limit price is hit) @@ -786,7 +786,7 @@ impl Pallet { // This should increase as we are adding TAO to the protocol. let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao); - // Clamp the protocol TAO delta to the stated input + // Use min to understate the TAO flow into the protocol. let tao_flow = protocol_tao_delta.min(tao); ensure!(