Skip to content

Commit 3da7e9c

Browse files
committed
Fix new swap's API integration issues
1 parent cb4ba55 commit 3da7e9c

File tree

32 files changed

+424
-345
lines changed

32 files changed

+424
-345
lines changed

pallets/admin-utils/src/tests/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ impl pallet_subtensor_swap::Config for Test {
344344
type SubnetInfo = SubtensorModule;
345345
type BalanceOps = SubtensorModule;
346346
type ProtocolId = SwapProtocolId;
347+
type TaoReserve = pallet_subtensor::TaoCurrencyReserve<Self>;
348+
type AlphaReserve = pallet_subtensor::AlphaCurrencyReserve<Self>;
347349
type MaxFeeRate = SwapMaxFeeRate;
348350
type MaxPositions = SwapMaxPositions;
349351
type MinimumLiquidity = SwapMinimumLiquidity;

pallets/subtensor/src/coinbase/block_emission.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use substrate_fixed::{
66
types::{I96F32, U96F32},
77
};
88
use subtensor_runtime_common::{NetUid, TaoCurrency};
9-
use subtensor_swap_interface::SwapExt;
9+
use subtensor_swap_interface::SwapHandler;
1010

1111
impl<T: Config> Pallet<T> {
1212
/// Calculates the dynamic TAO emission for a given subnet.
@@ -40,7 +40,7 @@ impl<T: Config> Pallet<T> {
4040
let float_alpha_block_emission: U96F32 = U96F32::saturating_from_num(alpha_block_emission);
4141

4242
// Get alpha price for subnet.
43-
let alpha_price = T::SwapExt::current_alpha_price(netuid.into());
43+
let alpha_price = T::SwapInterface::current_alpha_price(netuid.into());
4444
log::debug!("{netuid:?} - alpha_price: {alpha_price:?}");
4545

4646
// Get initial alpha_in

pallets/subtensor/src/coinbase/root.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use safe_math::*;
2222
use sp_core::Get;
2323
use substrate_fixed::types::{I64F64, U96F32};
2424
use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, NetUidStorageIndex, TaoCurrency};
25-
use subtensor_swap_interface::SwapExt;
25+
use subtensor_swap_interface::SwapHandler;
2626

2727
impl<T: Config> Pallet<T> {
2828
/// Fetches the total count of root network validators
@@ -373,9 +373,9 @@ impl<T: Config> Pallet<T> {
373373
);
374374

375375
// 2. --- Perform the cleanup before removing the network.
376-
T::SwapExt::dissolve_all_liquidity_providers(netuid)?;
376+
T::SwapInterface::dissolve_all_liquidity_providers(netuid)?;
377377
Self::destroy_alpha_in_out_stakes(netuid)?;
378-
T::SwapExt::clear_protocol_liquidity(netuid)?;
378+
T::SwapInterface::clear_protocol_liquidity(netuid)?;
379379
T::CommitmentsInterface::purge_netuid(netuid);
380380

381381
// 3. --- Remove the network

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use alloc::collections::BTreeMap;
33
use safe_math::*;
44
use substrate_fixed::types::U96F32;
55
use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency};
6-
use subtensor_swap_interface::SwapExt;
6+
use subtensor_swap_interface::SwapHandler;
77

88
// Distribute dividends to each hotkey
99
macro_rules! asfloat {
@@ -58,7 +58,7 @@ impl<T: Config> Pallet<T> {
5858
// Only calculate for subnets that we are emitting to.
5959
for netuid_i in subnets_to_emit_to.iter() {
6060
// Get subnet price.
61-
let price_i = T::SwapExt::current_alpha_price((*netuid_i).into());
61+
let price_i = T::SwapInterface::current_alpha_price((*netuid_i).into());
6262
log::debug!("price_i: {price_i:?}");
6363
// Get subnet TAO.
6464
let moving_price_i: U96F32 = Self::get_moving_alpha_price(*netuid_i);
@@ -91,7 +91,7 @@ impl<T: Config> Pallet<T> {
9191
let buy_swap_result = Self::swap_tao_for_alpha(
9292
*netuid_i,
9393
tou64!(difference_tao).into(),
94-
T::SwapExt::max_price(),
94+
T::SwapInterface::max_price(),
9595
true,
9696
);
9797
if let Ok(buy_swap_result_ok) = buy_swap_result {
@@ -159,7 +159,7 @@ impl<T: Config> Pallet<T> {
159159
*total = total.saturating_add(tao_in_i.into());
160160
});
161161
// Adjust protocol liquidity based on new reserves
162-
T::SwapExt::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);
162+
T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);
163163
}
164164

165165
// --- 5. Compute owner cuts and remove them from alpha_out remaining.
@@ -220,7 +220,7 @@ impl<T: Config> Pallet<T> {
220220
let swap_result = Self::swap_alpha_for_tao(
221221
*netuid_i,
222222
tou64!(root_alpha).into(),
223-
T::SwapExt::min_price(),
223+
T::SwapInterface::min_price(),
224224
true,
225225
);
226226
if let Ok(ok_result) = swap_result {

pallets/subtensor/src/macros/config.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use frame_support::pallet_macros::pallet_section;
66
#[pallet_section]
77
mod config {
88

9-
use crate::CommitmentsInterface;
9+
use crate::{CommitmentsInterface, GetAlphaForTao, GetTaoForAlpha};
1010
use pallet_commitments::GetCommitments;
11-
use subtensor_swap_interface::{Order as OrderT, SwapEngine, SwapExt};
11+
use subtensor_swap_interface::{SwapEngine, SwapHandler};
1212

1313
/// Configure the pallet by specifying the parameters and types on which it depends.
1414
#[pallet::config]
@@ -54,11 +54,10 @@ mod config {
5454
/// the preimage to store the call data.
5555
type Preimages: QueryPreimage<H = Self::Hashing> + StorePreimage;
5656

57-
/// Implementor of `SwapExt` interface from `subtensor_swap_interface`
58-
type SwapExt: SwapExt;
59-
60-
/// Implementor of `SwapEngine` interface from `subtensor_swap_interface`
61-
type SwapEngine<Order: OrderT>: SwapEngine<Order>;
57+
/// Implementor of `SwapHandler` interface from `subtensor_swap_interface`
58+
type SwapInterface: SwapHandler
59+
+ SwapEngine<GetAlphaForTao<Self>>
60+
+ SwapEngine<GetTaoForAlpha<Self>>;
6261

6362
/// Interface to allow interacting with the proxy pallet.
6463
type ProxyInterface: crate::ProxyInterface<Self::AccountId>;

pallets/subtensor/src/rpc_info/stake_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate alloc;
33
use codec::Compact;
44
use frame_support::pallet_prelude::{Decode, Encode};
55
use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency};
6-
use subtensor_swap_interface::SwapExt;
6+
use subtensor_swap_interface::SwapHandler;
77

88
use super::*;
99

@@ -128,7 +128,7 @@ impl<T: Config> Pallet<T> {
128128
0_u64
129129
} else {
130130
let netuid = destination.or(origin).map(|v| v.1).unwrap_or_default();
131-
T::SwapExt::approx_fee_amount(netuid.into(), TaoCurrency::from(amount)).to_u64()
131+
T::SwapInterface::approx_fee_amount(netuid.into(), TaoCurrency::from(amount)).to_u64()
132132
}
133133
}
134134
}

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use substrate_fixed::types::I96F32;
22
use subtensor_runtime_common::{NetUid, TaoCurrency};
3-
use subtensor_swap_interface::{Order, SwapEngine, SwapExt};
3+
use subtensor_swap_interface::{Order, SwapHandler};
44

55
use super::*;
66

@@ -74,7 +74,7 @@ impl<T: Config> Pallet<T> {
7474
&coldkey,
7575
netuid,
7676
tao_staked.saturating_to_num::<u64>().into(),
77-
T::SwapExt::max_price(),
77+
T::SwapInterface::max_price(),
7878
true,
7979
false,
8080
)?;
@@ -194,7 +194,7 @@ impl<T: Config> Pallet<T> {
194194

195195
// Use reverting swap to estimate max limit amount
196196
let order = GetAlphaForTao::<T>::with_amount(u64::MAX);
197-
let result = T::SwapEngine::swap(netuid.into(), order, limit_price, false, true)
197+
let result = T::SwapInterface::swap(netuid.into(), order, limit_price, false, true)
198198
.map(|r| r.amount_paid_in.saturating_add(r.fee_paid))
199199
.map_err(|_| Error::ZeroMaxStakeAmount)?;
200200

pallets/subtensor/src/staking/helpers.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use frame_support::traits::{
88
use safe_math::*;
99
use substrate_fixed::types::U96F32;
1010
use subtensor_runtime_common::{NetUid, TaoCurrency};
11-
use subtensor_swap_interface::{Order, SwapEngine, SwapExt};
11+
use subtensor_swap_interface::{Order, SwapHandler};
1212

1313
use super::*;
1414

@@ -51,8 +51,9 @@ impl<T: Config> Pallet<T> {
5151
let alpha = U96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(
5252
hotkey, netuid,
5353
));
54-
let alpha_price =
55-
U96F32::saturating_from_num(T::SwapExt::current_alpha_price(netuid.into()));
54+
let alpha_price = U96F32::saturating_from_num(
55+
T::SwapInterface::current_alpha_price(netuid.into()),
56+
);
5657
alpha.saturating_mul(alpha_price)
5758
})
5859
.sum::<U96F32>()
@@ -73,10 +74,12 @@ impl<T: Config> Pallet<T> {
7374
hotkey, coldkey, netuid,
7475
);
7576
let order = GetTaoForAlpha::<T>::with_amount(alpha_stake);
76-
T::SwapEngine::sim_swap(netuid.into(), order)
77+
T::SwapInterface::sim_swap(netuid.into(), order)
7778
.map(|r| {
7879
let fee: u64 = U96F32::saturating_from_num(r.fee_paid)
79-
.saturating_mul(T::SwapExt::current_alpha_price(netuid.into()))
80+
.saturating_mul(T::SwapInterface::current_alpha_price(
81+
netuid.into(),
82+
))
8083
.saturating_to_num();
8184
r.amount_paid_out.to_u64().saturating_add(fee)
8285
})
@@ -184,7 +187,7 @@ impl<T: Config> Pallet<T> {
184187
Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
185188
let min_alpha_stake =
186189
U96F32::saturating_from_num(Self::get_nominator_min_required_stake())
187-
.safe_div(T::SwapExt::current_alpha_price(netuid))
190+
.safe_div(T::SwapInterface::current_alpha_price(netuid))
188191
.saturating_to_num::<u64>();
189192
if alpha_stake > 0.into() && alpha_stake < min_alpha_stake.into() {
190193
// Log the clearing of a small nomination
@@ -196,7 +199,7 @@ impl<T: Config> Pallet<T> {
196199
coldkey,
197200
netuid,
198201
alpha_stake,
199-
T::SwapExt::min_price(),
202+
T::SwapInterface::min_price(),
200203
false,
201204
);
202205

@@ -313,7 +316,7 @@ impl<T: Config> Pallet<T> {
313316
}
314317

315318
pub fn is_user_liquidity_enabled(netuid: NetUid) -> bool {
316-
T::SwapExt::is_user_liquidity_enabled(netuid)
319+
T::SwapInterface::is_user_liquidity_enabled(netuid)
317320
}
318321

319322
pub fn recycle_subnet_alpha(netuid: NetUid, amount: AlphaCurrency) {

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use safe_math::*;
33
use sp_core::Get;
44
use substrate_fixed::types::U64F64;
55
use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency};
6-
use subtensor_swap_interface::SwapExt;
6+
use subtensor_swap_interface::SwapHandler;
77

88
impl<T: Config> Pallet<T> {
99
/// Moves stake from one hotkey to another across subnets.
@@ -360,7 +360,7 @@ impl<T: Config> Pallet<T> {
360360
origin_coldkey,
361361
origin_netuid,
362362
move_amount,
363-
T::SwapExt::min_price(),
363+
T::SwapInterface::min_price(),
364364
drop_fee_origin,
365365
)?;
366366

@@ -378,7 +378,7 @@ impl<T: Config> Pallet<T> {
378378
destination_coldkey,
379379
destination_netuid,
380380
tao_unstaked,
381-
T::SwapExt::max_price(),
381+
T::SwapInterface::max_price(),
382382
set_limit,
383383
drop_fee_destination,
384384
)?;
@@ -499,8 +499,9 @@ impl<T: Config> Pallet<T> {
499499
let limit_price_float: U64F64 = U64F64::saturating_from_num(limit_price)
500500
.checked_div(U64F64::saturating_from_num(1_000_000_000))
501501
.unwrap_or(U64F64::saturating_from_num(0));
502-
let current_price = T::SwapExt::current_alpha_price(origin_netuid.into())
503-
.safe_div(T::SwapExt::current_alpha_price(destination_netuid.into()));
502+
let current_price = T::SwapInterface::current_alpha_price(origin_netuid.into()).safe_div(
503+
T::SwapInterface::current_alpha_price(destination_netuid.into()),
504+
);
504505
if limit_price_float > current_price {
505506
return Err(Error::ZeroMaxStakeAmount);
506507
}

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use subtensor_swap_interface::{SwapEngine, SwapExt};
2-
31
use super::*;
42
use substrate_fixed::types::U96F32;
53
use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency};
6-
use subtensor_swap_interface::Order;
4+
use subtensor_swap_interface::{Order, SwapHandler};
75

86
impl<T: Config> Pallet<T> {
97
/// ---- The implementation for the extrinsic remove_stake: Removes stake from a hotkey account and adds it onto a coldkey.
@@ -74,7 +72,7 @@ impl<T: Config> Pallet<T> {
7472
&coldkey,
7573
netuid,
7674
alpha_unstaked,
77-
T::SwapExt::min_price(),
75+
T::SwapInterface::min_price(),
7876
false,
7977
)?;
8078

@@ -169,7 +167,7 @@ impl<T: Config> Pallet<T> {
169167
&coldkey,
170168
netuid,
171169
alpha_unstaked,
172-
T::SwapExt::min_price(),
170+
T::SwapInterface::min_price(),
173171
false,
174172
)?;
175173

@@ -262,7 +260,7 @@ impl<T: Config> Pallet<T> {
262260
&coldkey,
263261
netuid,
264262
alpha_unstaked,
265-
T::SwapExt::min_price(),
263+
T::SwapInterface::min_price(),
266264
false,
267265
)?;
268266

@@ -281,7 +279,7 @@ impl<T: Config> Pallet<T> {
281279
&coldkey,
282280
NetUid::ROOT,
283281
total_tao_unstaked,
284-
T::SwapExt::max_price(),
282+
T::SwapInterface::max_price(),
285283
false, // no limit for Root subnet
286284
false,
287285
)?;
@@ -407,7 +405,7 @@ impl<T: Config> Pallet<T> {
407405

408406
// Use reverting swap to estimate max limit amount
409407
let order = GetTaoForAlpha::<T>::with_amount(u64::MAX);
410-
let result = T::SwapEngine::swap(netuid.into(), order, limit_price.into(), false, true)
408+
let result = T::SwapInterface::swap(netuid.into(), order, limit_price.into(), false, true)
411409
.map(|r| r.amount_paid_in.saturating_add(r.fee_paid))
412410
.map_err(|_| Error::ZeroMaxStakeAmount)?;
413411

@@ -465,13 +463,13 @@ impl<T: Config> Pallet<T> {
465463

466464
let owner_emission_tao: TaoCurrency = if owner_alpha_u64 > 0 {
467465
let order = GetTaoForAlpha::<T>::with_amount(owner_alpha_u64);
468-
match T::SwapEngine::sim_swap(netuid.into(), order) {
466+
match T::SwapInterface::sim_swap(netuid.into(), order) {
469467
Ok(sim) => sim.amount_paid_out,
470468
Err(e) => {
471469
log::debug!(
472470
"destroy_alpha_in_out_stakes: sim_swap owner α→τ failed (netuid={netuid:?}, alpha={owner_alpha_u64}, err={e:?}); falling back to price multiply.",
473471
);
474-
let cur_price: U96F32 = T::SwapExt::current_alpha_price(netuid.into());
472+
let cur_price: U96F32 = T::SwapInterface::current_alpha_price(netuid.into());
475473
let val_u64: u64 = U96F32::from_num(owner_alpha_u64)
476474
.saturating_mul(cur_price)
477475
.floor()

0 commit comments

Comments
 (0)