@@ -9,12 +9,12 @@ use substrate_fixed::types::{I64F64, U64F64, U96F32};
9
9
use subtensor_runtime_common:: {
10
10
AlphaCurrency , BalanceOps , Currency , CurrencyReserve , NetUid , SubnetInfo , TaoCurrency ,
11
11
} ;
12
- use subtensor_swap_interface:: { Order as OrderT , SwapHandler , SwapResult } ;
12
+ use subtensor_swap_interface:: { DefaultPriceLimit , Order as OrderT , SwapHandler , SwapResult } ;
13
13
14
14
use super :: pallet:: * ;
15
- use super :: swap_step:: { SwapStep , SwapStepAction } ;
15
+ use super :: swap_step:: { BasicSwapStep , SwapStep , SwapStepAction } ;
16
16
use crate :: {
17
- OrderType , SqrtPrice ,
17
+ SqrtPrice ,
18
18
position:: { Position , PositionId } ,
19
19
tick:: { ActiveTickIndexManager , Tick , TickIndex } ,
20
20
} ;
@@ -72,7 +72,8 @@ impl<T: Config> Pallet<T> {
72
72
}
73
73
74
74
// Initialize the v3:
75
- // Reserves are re-purposed, nothing to set, just query values for liquidity and price calculation
75
+ // Reserves are re-purposed, nothing to set, just query values for liquidity and price
76
+ // calculation
76
77
let tao_reserve = T :: TaoReserve :: reserve ( netuid. into ( ) ) ;
77
78
let alpha_reserve = T :: AlphaReserve :: reserve ( netuid. into ( ) ) ;
78
79
@@ -168,7 +169,8 @@ impl<T: Config> Pallet<T> {
168
169
/// - `order_type`: The type of the swap (e.g., Buy or Sell).
169
170
/// - `amount`: The amount of tokens to swap.
170
171
/// - `limit_sqrt_price`: A price limit (expressed as a square root) to bound the swap.
171
- /// - `simulate`: If `true`, the function runs in simulation mode and does not persist any changes.
172
+ /// - `simulate`: If `true`, the function runs in simulation mode and does not persist any
173
+ /// changes.
172
174
///
173
175
/// # Returns
174
176
/// Returns a [`Result`] with a [`SwapResult`] on success, or a [`DispatchError`] on failure.
@@ -180,7 +182,8 @@ impl<T: Config> Pallet<T> {
180
182
/// # Simulation Mode
181
183
/// When `simulate` is set to `true`, the function:
182
184
/// 1. Executes all logic without persisting any state changes (i.e., performs a dry run).
183
- /// 2. Skips reserve checks — it may return an `amount_paid_out` greater than the available reserve.
185
+ /// 2. Skips reserve checks — it may return an `amount_paid_out` greater than the available
186
+ /// reserve.
184
187
///
185
188
/// Use simulation mode to preview the outcome of a swap without modifying the blockchain state.
186
189
pub fn do_swap < PaidIn , PaidOut , ReserveIn , ReserveOut , Order > (
@@ -196,6 +199,7 @@ impl<T: Config> Pallet<T> {
196
199
ReserveIn : CurrencyReserve < PaidIn > ,
197
200
ReserveOut : CurrencyReserve < PaidOut > ,
198
201
Order : OrderT < PaidIn , PaidOut > ,
202
+ BasicSwapStep < T , PaidIn , PaidOut , Order > : SwapStep < T , PaidIn , PaidOut , Order > ,
199
203
{
200
204
transactional:: with_transaction ( || {
201
205
let reserve = ReserveOut :: reserve ( netuid. into ( ) ) ;
@@ -240,6 +244,7 @@ impl<T: Config> Pallet<T> {
240
244
ReserveIn : CurrencyReserve < PaidIn > ,
241
245
ReserveOut : CurrencyReserve < PaidOut > ,
242
246
Order : OrderT < PaidIn , PaidOut > ,
247
+ BasicSwapStep < T , PaidIn , PaidOut , Order > : SwapStep < T , PaidIn , PaidOut , Order > ,
243
248
{
244
249
ensure ! (
245
250
ReserveOut :: reserve( netuid) . to_u64( ) >= T :: MinimumReserve :: get( ) . get( ) ,
@@ -255,10 +260,10 @@ impl<T: Config> Pallet<T> {
255
260
) ;
256
261
257
262
let mut amount_remaining = order. amount ( ) ;
258
- let mut amount_paid_out: u64 = 0 ;
263
+ let mut amount_paid_out = PaidOut :: ZERO ;
259
264
let mut iteration_counter: u16 = 0 ;
260
- let mut in_acc: u64 = 0 ;
261
- let mut fee_acc: u64 = 0 ;
265
+ let mut in_acc = PaidIn :: ZERO ;
266
+ let mut fee_acc = PaidIn :: ZERO ;
262
267
263
268
log:: trace!( "======== Start Swap ========" ) ;
264
269
log:: trace!( "Amount Remaining: {amount_remaining}" ) ;
@@ -272,22 +277,27 @@ impl<T: Config> Pallet<T> {
272
277
) ;
273
278
274
279
// Create and execute a swap step
275
- let mut swap_step =
276
- SwapStep :: < T > :: new ( netuid, order, amount_remaining, limit_sqrt_price, drop_fees) ;
280
+ let mut swap_step = BasicSwapStep :: < T , PaidIn , PaidOut , Order > :: new (
281
+ netuid,
282
+ order. clone ( ) ,
283
+ amount_remaining,
284
+ limit_sqrt_price,
285
+ drop_fees,
286
+ ) ;
277
287
278
288
let swap_result = swap_step. execute ( ) ?;
279
289
280
290
in_acc = in_acc. saturating_add ( swap_result. delta_in ) ;
281
291
fee_acc = fee_acc. saturating_add ( swap_result. fee_paid ) ;
282
- amount_remaining = amount_remaining. saturating_sub ( swap_result. amount_to_take . into ( ) ) ;
292
+ amount_remaining = amount_remaining. saturating_sub ( swap_result. amount_to_take ) ;
283
293
amount_paid_out = amount_paid_out. saturating_add ( swap_result. delta_out ) ;
284
294
285
- if swap_step. action == SwapStepAction :: Stop {
295
+ if swap_step. action ( ) == SwapStepAction :: Stop {
286
296
amount_remaining = PaidIn :: ZERO ;
287
297
}
288
298
289
299
// The swap step didn't exchange anything
290
- if swap_result. amount_to_take == 0 {
300
+ if swap_result. amount_to_take . is_zero ( ) {
291
301
amount_remaining = PaidIn :: ZERO ;
292
302
}
293
303
@@ -302,17 +312,10 @@ impl<T: Config> Pallet<T> {
302
312
log:: trace!( "\n Amount Paid Out: {amount_paid_out}" ) ;
303
313
log:: trace!( "======== End Swap ========" ) ;
304
314
305
- let ( tao_reserve_delta, alpha_reserve_delta) = match order_type {
306
- OrderType :: Buy => ( in_acc as i128 , ( amount_paid_out as i128 ) . neg ( ) ) ,
307
- OrderType :: Sell => ( ( amount_paid_out as i128 ) . neg ( ) , in_acc as i128 ) ,
308
- } ;
309
-
310
315
Ok ( SwapResult {
311
- amount_paid_in : in_acc. into ( ) ,
312
- amount_paid_out : amount_paid_out. into ( ) ,
313
- fee_paid : fee_acc. into ( ) ,
314
- tao_reserve_delta,
315
- alpha_reserve_delta,
316
+ amount_paid_in : in_acc,
317
+ amount_paid_out,
318
+ fee_paid : fee_acc,
316
319
} )
317
320
}
318
321
@@ -788,11 +791,22 @@ impl<T: Config> Pallet<T> {
788
791
}
789
792
}
790
793
794
+ impl < T : Config > DefaultPriceLimit < TaoCurrency , AlphaCurrency > for Pallet < T > {
795
+ fn default_price_limit < C : Currency > ( ) -> C {
796
+ Self :: max_price_inner :: < C > ( )
797
+ }
798
+ }
799
+
800
+ impl < T : Config > DefaultPriceLimit < AlphaCurrency , TaoCurrency > for Pallet < T > {
801
+ fn default_price_limit < C : Currency > ( ) -> C {
802
+ Self :: min_price_inner :: < C > ( )
803
+ }
804
+ }
805
+
791
806
impl < T : Config > SwapHandler < T :: AccountId > for Pallet < T > {
792
- fn swap < PaidIn , PaidOut , ReserveIn , ReserveOut > (
807
+ fn swap < PaidIn , PaidOut , ReserveIn , ReserveOut , Order > (
793
808
netuid : NetUid ,
794
- order_t : OrderType ,
795
- amount : PaidIn ,
809
+ order : Order ,
796
810
price_limit : TaoCurrency ,
797
811
drop_fees : bool ,
798
812
should_rollback : bool ,
@@ -802,47 +816,45 @@ impl<T: Config> SwapHandler<T::AccountId> for Pallet<T> {
802
816
PaidOut : Currency ,
803
817
ReserveIn : CurrencyReserve < PaidIn > ,
804
818
ReserveOut : CurrencyReserve < PaidOut > ,
819
+ Order : OrderT < PaidIn , PaidOut > ,
820
+ BasicSwapStep < T , PaidIn , PaidOut , Order > : SwapStep < T , PaidIn , PaidOut , Order > ,
805
821
{
806
822
let limit_sqrt_price = SqrtPrice :: saturating_from_num ( price_limit. to_u64 ( ) )
807
823
. safe_div ( SqrtPrice :: saturating_from_num ( 1_000_000_000 ) )
808
824
. checked_sqrt ( SqrtPrice :: saturating_from_num ( 0.0000000001 ) )
809
825
. ok_or ( Error :: < T > :: PriceLimitExceeded ) ?;
810
826
811
- Self :: do_swap :: < PaidIn , PaidOut , ReserveIn , ReserveOut > (
827
+ Self :: do_swap :: < PaidIn , PaidOut , ReserveIn , ReserveOut , Order > (
812
828
NetUid :: from ( netuid) ,
813
- order_t,
814
- amount,
829
+ order,
815
830
limit_sqrt_price,
816
831
drop_fees,
817
832
should_rollback,
818
833
)
819
834
. map_err ( Into :: into)
820
835
}
821
836
822
- fn sim_swap < PaidIn , PaidOut , ReserveIn , ReserveOut > (
837
+ fn sim_swap < PaidIn , PaidOut , ReserveIn , ReserveOut , Order > (
823
838
netuid : NetUid ,
824
- order_t : OrderType ,
839
+ order : Order ,
825
840
amount : PaidIn ,
826
841
) -> Result < SwapResult < PaidIn , PaidOut > , DispatchError >
827
842
where
828
843
PaidIn : Currency ,
829
844
PaidOut : Currency ,
830
845
ReserveIn : CurrencyReserve < PaidIn > ,
831
846
ReserveOut : CurrencyReserve < PaidOut > ,
847
+ Order : OrderT < PaidIn , PaidOut > ,
848
+ Self : DefaultPriceLimit < PaidIn , PaidOut > ,
832
849
{
833
850
match T :: SubnetInfo :: mechanism ( netuid) {
834
851
1 => {
835
- let price_limit = match order_t {
836
- OrderType :: Buy => Self :: max_price :: < TaoCurrency > ( ) ,
837
- OrderType :: Sell => Self :: min_price :: < TaoCurrency > ( ) ,
838
- }
839
- . to_u64 ( ) ;
852
+ let price_limit = Self :: default_price_limit :: < TaoCurrency > ( ) ;
840
853
841
- Self :: swap :: < PaidIn , PaidOut , ReserveIn , ReserveOut > (
854
+ Self :: swap :: < PaidIn , PaidOut , ReserveIn , ReserveOut , Order > (
842
855
netuid,
843
- order_t,
844
- amount,
845
- price_limit. into ( ) ,
856
+ order,
857
+ price_limit,
846
858
false ,
847
859
true ,
848
860
)
@@ -851,8 +863,6 @@ impl<T: Config> SwapHandler<T::AccountId> for Pallet<T> {
851
863
amount_paid_in : amount,
852
864
amount_paid_out : amount. to_u64 ( ) . into ( ) ,
853
865
fee_paid : 0 . into ( ) ,
854
- tao_reserve_delta : 0 ,
855
- alpha_reserve_delta : 0 ,
856
866
} ) ,
857
867
}
858
868
}
@@ -866,11 +876,11 @@ impl<T: Config> SwapHandler<T::AccountId> for Pallet<T> {
866
876
}
867
877
868
878
fn min_price < C : Currency > ( ) -> C {
869
- Self :: min_price ( )
879
+ Self :: min_price_inner ( )
870
880
}
871
881
872
882
fn max_price < C : Currency > ( ) -> C {
873
- Self :: max_price ( )
883
+ Self :: max_price_inner ( )
874
884
}
875
885
876
886
fn adjust_protocol_liquidity (
0 commit comments