Skip to content

Commit 6304dbe

Browse files
authored
Merge pull request #2206 from opentensor/testnet
hotfix 11/18/2025
2 parents 8f33f8c + 11c6330 commit 6304dbe

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ impl<T: Config> Pallet<T> {
2121
SubnetAlphaIn::<T>::get(netuid).saturating_add(SubnetAlphaOut::<T>::get(netuid))
2222
}
2323

24+
pub fn get_protocol_tao(netuid: NetUid) -> TaoCurrency {
25+
T::SwapInterface::get_protocol_tao(netuid)
26+
}
27+
2428
pub fn get_moving_alpha_price(netuid: NetUid) -> U96F32 {
2529
let one = U96F32::saturating_from_num(1.0);
2630
if netuid.is_root() {
@@ -688,6 +692,9 @@ impl<T: Config> Pallet<T> {
688692
price_limit: TaoCurrency,
689693
drop_fees: bool,
690694
) -> Result<TaoCurrency, DispatchError> {
695+
// Record the protocol TAO before the swap.
696+
let protocol_tao = Self::get_protocol_tao(netuid);
697+
691698
// Decrease alpha on subnet
692699
let actual_alpha_decrease =
693700
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha);
@@ -696,6 +703,13 @@ impl<T: Config> Pallet<T> {
696703
let swap_result =
697704
Self::swap_alpha_for_tao(netuid, actual_alpha_decrease, price_limit, drop_fees)?;
698705

706+
// Record the protocol TAO after the swap.
707+
let protocol_tao_after = Self::get_protocol_tao(netuid);
708+
// This should decrease as we are removing TAO from the protocol.
709+
let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after);
710+
// Use max to overstate the TAO flow from the protocol.
711+
let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into());
712+
699713
// Refund the unused alpha (in case if limit price is hit)
700714
let refund = actual_alpha_decrease.saturating_sub(
701715
swap_result
@@ -722,7 +736,7 @@ impl<T: Config> Pallet<T> {
722736
// }
723737

724738
// Record TAO outflow
725-
Self::record_tao_outflow(netuid, swap_result.amount_paid_out.into());
739+
Self::record_tao_outflow(netuid, tao_flow);
726740

727741
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
728742

@@ -761,9 +775,20 @@ impl<T: Config> Pallet<T> {
761775
set_limit: bool,
762776
drop_fees: bool,
763777
) -> Result<AlphaCurrency, DispatchError> {
778+
// Record the protocol TAO before the swap.
779+
let protocol_tao = Self::get_protocol_tao(netuid);
780+
764781
// Swap the tao to alpha.
765782
let swap_result = Self::swap_tao_for_alpha(netuid, tao, price_limit, drop_fees)?;
766783

784+
// Record the protocol TAO after the swap.
785+
let protocol_tao_after = Self::get_protocol_tao(netuid);
786+
787+
// This should increase as we are adding TAO to the protocol.
788+
let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao);
789+
// Use min to understate the TAO flow into the protocol.
790+
let tao_flow = protocol_tao_delta.min(tao);
791+
767792
ensure!(
768793
!swap_result.amount_paid_out.is_zero(),
769794
Error::<T>::AmountTooLow
@@ -799,7 +824,7 @@ impl<T: Config> Pallet<T> {
799824
}
800825

801826
// Record TAO inflow
802-
Self::record_tao_inflow(netuid, swap_result.amount_paid_in.into());
827+
Self::record_tao_inflow(netuid, tao_flow);
803828

804829
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
805830

pallets/subtensor/src/tests/staking.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5596,14 +5596,13 @@ fn test_staking_records_flow() {
55965596
mock::setup_reserves(netuid, tao_reserve, alpha_in);
55975597

55985598
// Initialize swap v3
5599-
let order = GetAlphaForTao::<Test>::with_amount(0);
5600-
assert_ok!(<tests::mock::Test as pallet::Config>::SwapInterface::swap(
5601-
netuid.into(),
5602-
order,
5603-
TaoCurrency::MAX,
5599+
SubtensorModule::swap_tao_for_alpha(
5600+
netuid,
5601+
TaoCurrency::ZERO,
5602+
1_000_000_000_000.into(),
56045603
false,
5605-
true
5606-
));
5604+
)
5605+
.unwrap();
56075606

56085607
// Add stake with slippage safety and check if the result is ok
56095608
assert_ok!(SubtensorModule::stake_into_subnet(

pallets/swap-interface/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub trait SwapHandler {
3939

4040
fn approx_fee_amount<T: Currency>(netuid: NetUid, amount: T) -> T;
4141
fn current_alpha_price(netuid: NetUid) -> U96F32;
42+
fn get_protocol_tao(netuid: NetUid) -> TaoCurrency;
4243
fn max_price<C: Currency>() -> C;
4344
fn min_price<C: Currency>() -> C;
4445
fn adjust_protocol_liquidity(

pallets/swap/src/pallet/impls.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,24 @@ impl<T: Config> SwapHandler for Pallet<T> {
11121112
Self::current_price(netuid.into())
11131113
}
11141114

1115+
fn get_protocol_tao(netuid: NetUid) -> TaoCurrency {
1116+
let protocol_account_id = Self::protocol_account_id();
1117+
let mut positions =
1118+
Positions::<T>::iter_prefix_values((netuid, protocol_account_id.clone()))
1119+
.collect::<sp_std::vec::Vec<_>>();
1120+
1121+
if let Some(position) = positions.get_mut(0) {
1122+
let current_sqrt_price = AlphaSqrtPrice::<T>::get(netuid);
1123+
// Adjust liquidity
1124+
let maybe_token_amounts = position.to_token_amounts(current_sqrt_price);
1125+
if let Ok((tao, _)) = maybe_token_amounts {
1126+
return tao.into();
1127+
}
1128+
}
1129+
1130+
TaoCurrency::ZERO
1131+
}
1132+
11151133
fn min_price<C: Currency>() -> C {
11161134
Self::min_price_inner()
11171135
}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
220220
// `spec_version`, and `authoring_version` are the same between Wasm and native.
221221
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
222222
// the compatible custom types.
223-
spec_version: 345,
223+
spec_version: 347,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

0 commit comments

Comments
 (0)