Skip to content

Commit 98ef9f6

Browse files
committed
add stake instead of swap
1 parent 2b0a5f6 commit 98ef9f6

File tree

4 files changed

+97
-54
lines changed

4 files changed

+97
-54
lines changed

common/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use runtime_common::prod_or_fast;
99
use scale_info::TypeInfo;
1010
use serde::{Deserialize, Serialize};
1111
use sp_runtime::{
12-
MultiSignature,
12+
MultiSignature, Vec,
1313
traits::{IdentifyAccount, Verify},
1414
};
1515
use subtensor_macros::freeze_struct;
@@ -175,6 +175,9 @@ pub trait SubnetInfo<AccountId> {
175175
fn mechanism(netuid: NetUid) -> u16;
176176
fn is_owner(account_id: &AccountId, netuid: NetUid) -> bool;
177177
fn is_subtoken_enabled(netuid: NetUid) -> bool;
178+
fn get_validator_trust(netuid: NetUid) -> Vec<u16>;
179+
fn get_validator_permit(netuid: NetUid) -> Vec<bool>;
180+
fn hotkey_of_uid(netuid: NetUid, uid: u16) -> Option<AccountId>;
178181
}
179182

180183
pub trait BalanceOps<AccountId> {

pallets/subtensor/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,18 @@ impl<T: Config + pallet_balances::Config<Balance = u64>>
21462146
fn is_subtoken_enabled(netuid: NetUid) -> bool {
21472147
SubtokenEnabled::<T>::get(netuid)
21482148
}
2149+
2150+
fn get_validator_trust(netuid: NetUid) -> Vec<u16> {
2151+
ValidatorTrust::<T>::get(netuid)
2152+
}
2153+
2154+
fn get_validator_permit(netuid: NetUid) -> Vec<bool> {
2155+
ValidatorPermit::<T>::get(netuid)
2156+
}
2157+
2158+
fn hotkey_of_uid(netuid: NetUid, uid: u16) -> Option<T::AccountId> {
2159+
Keys::<T>::try_get(netuid, uid).ok()
2160+
}
21492161
}
21502162

21512163
impl<T: Config + pallet_balances::Config<Balance = u64>>

pallets/swap/src/mock.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ impl SubnetInfo<AccountId> for MockLiquidityProvider {
120120
fn is_subtoken_enabled(netuid: NetUid) -> bool {
121121
netuid.inner() != SUBTOKEN_DISABLED_NETUID
122122
}
123+
124+
fn get_validator_trust(netuid: NetUid) -> Vec<u16> {
125+
match netuid.into() {
126+
123u16 => vec![4000, 3000, 2000, 1000],
127+
WRAPPING_FEES_NETUID => vec![8000, 7000, 6000, 5000],
128+
_ => vec![1000, 800, 600, 400],
129+
}
130+
}
131+
132+
fn get_validator_permit(netuid: NetUid) -> Vec<bool> {
133+
match netuid.into() {
134+
123u16 => vec![true, true, false, true],
135+
WRAPPING_FEES_NETUID => vec![true, true, true, true],
136+
_ => vec![true, true, true, true],
137+
}
138+
}
139+
140+
fn hotkey_of_uid(_netuid: NetUid, uid: u16) -> Option<AccountId> {
141+
Some(uid as AccountId)
142+
}
123143
}
124144

125145
pub struct MockBalanceOps;

pallets/swap/src/pallet/impls.rs

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use frame_support::storage::{TransactionOutcome, transactional};
55
use frame_support::{ensure, pallet_prelude::DispatchError, traits::Get};
66
use safe_math::*;
77
use sp_arithmetic::helpers_128bit;
8-
use sp_runtime::{DispatchResult, traits::AccountIdConversion};
8+
use sp_runtime::{DispatchResult, Vec, traits::AccountIdConversion};
99
use substrate_fixed::types::{I64F64, U64F64, U96F32};
1010
use subtensor_runtime_common::{
1111
AlphaCurrency, BalanceOps, Currency, NetUid, SubnetInfo, TaoCurrency,
@@ -1233,15 +1233,36 @@ impl<T: Config> Pallet<T> {
12331233
.sort_by(|a, b| (a.owner == protocol_account).cmp(&(b.owner == protocol_account)));
12341234

12351235
let mut user_refunded_tao = TaoCurrency::ZERO;
1236+
let mut user_staked_alpha = AlphaCurrency::ZERO;
12361237
let mut burned_tao = TaoCurrency::ZERO;
12371238
let mut burned_alpha = AlphaCurrency::ZERO;
12381239

1239-
// Helper: build a very lax sqrt price limit.
1240-
// Mirrors the wrapper’s transformation: price_limit / 1e9, then sqrt().
1241-
let compute_limit = || {
1242-
SqrtPrice::saturating_from_num(u64::MAX)
1243-
.safe_div(SqrtPrice::saturating_from_num(1_000_000_000u64))
1244-
.checked_sqrt(SqrtPrice::saturating_from_num(0.0000000001f64))
1240+
let trust: Vec<u16> = T::SubnetInfo::get_validator_trust(netuid.into());
1241+
let permit: Vec<bool> = T::SubnetInfo::get_validator_permit(netuid.into());
1242+
1243+
if trust.len() != permit.len() {
1244+
log::debug!(
1245+
"dissolve_all_lp: ValidatorTrust/Permit length mismatch: netuid={:?}, trust_len={}, permit_len={}",
1246+
netuid,
1247+
trust.len(),
1248+
permit.len()
1249+
);
1250+
return Err(sp_runtime::DispatchError::Other(
1251+
"validator_meta_len_mismatch",
1252+
));
1253+
}
1254+
1255+
// Helper: pick target validator uid, only among permitted validators, by highest trust.
1256+
let pick_target_uid = |trust: &Vec<u16>, permit: &Vec<bool>| -> Option<u16> {
1257+
let mut best_uid: Option<usize> = None;
1258+
let mut best_trust: u16 = 0;
1259+
for (i, (&t, &p)) in trust.iter().zip(permit.iter()).enumerate() {
1260+
if p && (best_uid.is_none() || t > best_trust) {
1261+
best_uid = Some(i);
1262+
best_trust = t;
1263+
}
1264+
}
1265+
best_uid.map(|i| i as u16)
12451266
};
12461267

12471268
for CloseItem { owner, pos_id } in to_close.into_iter() {
@@ -1259,14 +1280,12 @@ impl<T: Config> Pallet<T> {
12591280
if alpha_total_from_pool > AlphaCurrency::ZERO {
12601281
burned_alpha = burned_alpha.saturating_add(alpha_total_from_pool);
12611282
}
1262-
12631283
log::debug!(
1264-
"dissolve_all_lp: burned protocol position: netuid={:?}, pos_id={:?}, τ={:?}, α_principal={:?}, α_fees={:?}",
1284+
"dissolve_all_lp: burned protocol pos: netuid={:?}, pos_id={:?}, τ={:?}, α_total={:?}",
12651285
netuid,
12661286
pos_id,
12671287
rm.tao,
1268-
rm.alpha,
1269-
rm.fee_alpha
1288+
alpha_total_from_pool
12701289
);
12711290
} else {
12721291
// ---------------- USER: refund τ and convert α → τ ----------------
@@ -1278,41 +1297,40 @@ impl<T: Config> Pallet<T> {
12781297
T::BalanceOps::decrease_provided_tao_reserve(netuid, rm.tao);
12791298
}
12801299

1281-
// 2) Convert ALL α withdrawn (principal + fees) to τ and refund τ to user.
1300+
// 2) Stake ALL withdrawn α (principal + fees) to the best permitted validator.
12821301
if alpha_total_from_pool > AlphaCurrency::ZERO {
1283-
let sell_amount: u64 = alpha_total_from_pool.into();
1302+
if let Some(target_uid) = pick_target_uid(&trust, &permit) {
1303+
let validator_hotkey: T::AccountId =
1304+
T::SubnetInfo::hotkey_of_uid(netuid.into(), target_uid)
1305+
.ok_or(sp_runtime::DispatchError::Other(
1306+
"validator_hotkey_missing",
1307+
))?;
1308+
1309+
// Stake α from LP owner (coldkey) to chosen validator (hotkey).
1310+
T::BalanceOps::increase_stake(
1311+
&owner,
1312+
&validator_hotkey,
1313+
netuid,
1314+
alpha_total_from_pool,
1315+
)?;
1316+
1317+
user_staked_alpha =
1318+
user_staked_alpha.saturating_add(alpha_total_from_pool);
12841319

1285-
if let Some(limit_sqrt_price) = compute_limit() {
1286-
match Self::do_swap(
1320+
log::debug!(
1321+
"dissolve_all_lp: user dissolved & staked α: netuid={:?}, owner={:?}, pos_id={:?}, α_staked={:?}, target_uid={}",
12871322
netuid,
1288-
OrderType::Sell,
1289-
sell_amount,
1290-
limit_sqrt_price,
1291-
true,
1292-
false,
1293-
) {
1294-
Ok(sres) => {
1295-
let tao_out: TaoCurrency = sres.amount_paid_out.into();
1296-
if tao_out > TaoCurrency::ZERO {
1297-
T::BalanceOps::increase_balance(&owner, tao_out);
1298-
user_refunded_tao =
1299-
user_refunded_tao.saturating_add(tao_out);
1300-
}
1301-
}
1302-
Err(e) => {
1303-
log::debug!(
1304-
"dissolve_all_lp: α→τ swap failed on dissolve: netuid={:?}, owner={:?}, pos_id={:?}, α={:?}, err={:?}",
1305-
netuid,
1306-
owner,
1307-
pos_id,
1308-
alpha_total_from_pool,
1309-
e
1310-
);
1311-
}
1312-
}
1323+
owner,
1324+
pos_id,
1325+
alpha_total_from_pool,
1326+
target_uid
1327+
);
13131328
} else {
1329+
// No permitted validators; burn to avoid balance drift.
1330+
burned_alpha =
1331+
burned_alpha.saturating_add(alpha_total_from_pool);
13141332
log::debug!(
1315-
"dissolve_all_lp: invalid price limit during α→τ on dissolve: netuid={:?}, owner={:?}, pos_id={:?}, α={:?}",
1333+
"dissolve_all_lp: no permitted validators; α burned: netuid={:?}, owner={:?}, pos_id={:?}, α_total={:?}",
13161334
netuid,
13171335
owner,
13181336
pos_id,
@@ -1325,17 +1343,6 @@ impl<T: Config> Pallet<T> {
13251343
alpha_total_from_pool,
13261344
);
13271345
}
1328-
1329-
log::debug!(
1330-
"dissolve_all_lp: user dissolved: netuid={:?}, owner={:?}, pos_id={:?}, τ_refunded={:?}, α_total_converted={:?} (α_principal={:?}, α_fees={:?})",
1331-
netuid,
1332-
owner,
1333-
pos_id,
1334-
rm.tao,
1335-
alpha_total_from_pool,
1336-
rm.alpha,
1337-
rm.fee_alpha
1338-
);
13391346
}
13401347
}
13411348
Err(e) => {
@@ -1373,9 +1380,10 @@ impl<T: Config> Pallet<T> {
13731380
EnabledUserLiquidity::<T>::remove(netuid);
13741381

13751382
log::debug!(
1376-
"dissolve_all_liquidity_providers: netuid={:?}, users_refunded_total_τ={:?}; protocol_burned: τ={:?}, α={:?}; state cleared",
1383+
"dissolve_all_liquidity_providers: netuid={:?}, users_refunded_total_τ={:?}, users_staked_total_α={:?}; protocol_burned: τ={:?}, α={:?}; state cleared",
13771384
netuid,
13781385
user_refunded_tao,
1386+
user_staked_alpha,
13791387
burned_tao,
13801388
burned_alpha
13811389
);

0 commit comments

Comments
 (0)