Skip to content

Commit 02bc300

Browse files
committed
Merge branch 'testnet' into feat/settable-max-mech-count
2 parents 045d123 + dba7e53 commit 02bc300

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 23 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,11 @@ 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+
699711
// Refund the unused alpha (in case if limit price is hit)
700712
let refund = actual_alpha_decrease.saturating_sub(
701713
swap_result
@@ -722,7 +734,7 @@ impl<T: Config> Pallet<T> {
722734
// }
723735

724736
// Record TAO outflow
725-
Self::record_tao_outflow(netuid, swap_result.amount_paid_out.into());
737+
Self::record_tao_outflow(netuid, protocol_tao_delta);
726738

727739
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
728740

@@ -761,9 +773,18 @@ impl<T: Config> Pallet<T> {
761773
set_limit: bool,
762774
drop_fees: bool,
763775
) -> Result<AlphaCurrency, DispatchError> {
776+
// Record the protocol TAO before the swap.
777+
let protocol_tao = Self::get_protocol_tao(netuid);
778+
764779
// Swap the tao to alpha.
765780
let swap_result = Self::swap_tao_for_alpha(netuid, tao, price_limit, drop_fees)?;
766781

782+
// Record the protocol TAO after the swap.
783+
let protocol_tao_after = Self::get_protocol_tao(netuid);
784+
785+
// This should increase as we are adding TAO to the protocol.
786+
let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao);
787+
767788
ensure!(
768789
!swap_result.amount_paid_out.is_zero(),
769790
Error::<T>::AmountTooLow
@@ -799,7 +820,7 @@ impl<T: Config> Pallet<T> {
799820
}
800821

801822
// Record TAO inflow
802-
Self::record_tao_inflow(netuid, swap_result.amount_paid_in.into());
823+
Self::record_tao_inflow(netuid, protocol_tao_delta);
803824

804825
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
805826

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
}

0 commit comments

Comments
 (0)