Skip to content

Commit dae1677

Browse files
committed
Update tests in swap pallet
1 parent 7fae3a0 commit dae1677

File tree

4 files changed

+513
-507
lines changed

4 files changed

+513
-507
lines changed

pallets/swap-interface/src/order.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub trait Order: Clone {
1414
fn is_beyond_price_limit(&self, alpha_sqrt_price: U64F64, limit_sqrt_price: U64F64) -> bool;
1515
}
1616

17-
#[derive(Clone)]
17+
#[derive(Clone, Default)]
1818
pub struct AlphaForTao<ReserveIn, ReserveOut>
1919
where
2020
ReserveIn: CurrencyReserve<TaoCurrency>,
@@ -50,7 +50,7 @@ where
5050
}
5151
}
5252

53-
#[derive(Clone)]
53+
#[derive(Clone, Default)]
5454
pub struct TaoForAlpha<ReserveIn, ReserveOut>
5555
where
5656
ReserveIn: CurrencyReserve<AlphaCurrency>,

pallets/swap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ substrate-fixed.workspace = true
2323

2424
pallet-subtensor-swap-runtime-api.workspace = true
2525
subtensor-macros.workspace = true
26-
subtensor-runtime-common.workspace = true
26+
subtensor-runtime-common = { workspace = true, features = ["approx"] }
2727
subtensor-swap-interface.workspace = true
2828

2929
[dev-dependencies]

pallets/swap/src/mock.rs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ use sp_runtime::{
1414
BuildStorage, Vec,
1515
traits::{BlakeTwo256, IdentityLookup},
1616
};
17+
use substrate_fixed::types::U64F64;
1718
use subtensor_runtime_common::{
18-
AlphaCurrency, BalanceOps, CurrencyReserve, NetUid, SubnetInfo, TaoCurrency,
19+
AlphaCurrency, BalanceOps, Currency, CurrencyReserve, NetUid, SubnetInfo, TaoCurrency,
1920
};
21+
use subtensor_swap_interface::Order;
2022

21-
use crate::pallet::EnabledUserLiquidity;
23+
use crate::pallet::{EnabledUserLiquidity, FeeGlobalAlpha, FeeGlobalTao};
2224

2325
construct_runtime!(
2426
pub enum Test {
@@ -85,7 +87,8 @@ parameter_types! {
8587
pub const MinimumReserves: NonZeroU64 = NonZeroU64::new(1).unwrap();
8688
}
8789

88-
pub(crate) struct TaoReserve;
90+
#[derive(Clone)]
91+
pub struct TaoReserve;
8992

9093
impl CurrencyReserve<TaoCurrency> for TaoReserve {
9194
fn reserve(netuid: NetUid) -> TaoCurrency {
@@ -97,11 +100,12 @@ impl CurrencyReserve<TaoCurrency> for TaoReserve {
97100
.into()
98101
}
99102

100-
fn increase_provided(netuid: NetUid, amount: TaoCurrency) {}
101-
fn decrease_provided(netuid: NetUid, amount: TaoCurrency) {}
103+
fn increase_provided(_: NetUid, _: TaoCurrency) {}
104+
fn decrease_provided(_: NetUid, _: TaoCurrency) {}
102105
}
103106

104-
pub(crate) struct AlphaReserve;
107+
#[derive(Clone)]
108+
pub struct AlphaReserve;
105109

106110
impl CurrencyReserve<AlphaCurrency> for AlphaReserve {
107111
fn reserve(netuid: NetUid) -> AlphaCurrency {
@@ -112,8 +116,59 @@ impl CurrencyReserve<AlphaCurrency> for AlphaReserve {
112116
}
113117
}
114118

115-
fn increase_provided(netuid: NetUid, amount: AlphaCurrency) {}
116-
fn decrease_provided(netuid: NetUid, amount: AlphaCurrency) {}
119+
fn increase_provided(_: NetUid, _: AlphaCurrency) {}
120+
fn decrease_provided(_: NetUid, _: AlphaCurrency) {}
121+
}
122+
123+
pub type AlphaForTao = subtensor_swap_interface::AlphaForTao<TaoReserve, AlphaReserve>;
124+
pub type TaoForAlpha = subtensor_swap_interface::TaoForAlpha<AlphaReserve, TaoReserve>;
125+
126+
pub(crate) trait GlobalFeeInfo: Currency {
127+
fn global_fee(&self, netuid: NetUid) -> U64F64;
128+
}
129+
130+
impl GlobalFeeInfo for TaoCurrency {
131+
fn global_fee(&self, netuid: NetUid) -> U64F64 {
132+
FeeGlobalTao::<Test>::get(netuid)
133+
}
134+
}
135+
136+
impl GlobalFeeInfo for AlphaCurrency {
137+
fn global_fee(&self, netuid: NetUid) -> U64F64 {
138+
FeeGlobalAlpha::<Test>::get(netuid)
139+
}
140+
}
141+
142+
pub(crate) trait TestExt<O: Order> {
143+
fn approx_expected_swap_output(
144+
sqrt_current_price: f64,
145+
liquidity_before: f64,
146+
order_liquidity: f64,
147+
) -> f64;
148+
}
149+
150+
impl TestExt<AlphaForTao> for Test {
151+
fn approx_expected_swap_output(
152+
sqrt_current_price: f64,
153+
liquidity_before: f64,
154+
order_liquidity: f64,
155+
) -> f64 {
156+
let denom = sqrt_current_price * (sqrt_current_price * liquidity_before + order_liquidity);
157+
let per_order_liq = liquidity_before / denom;
158+
per_order_liq * order_liquidity
159+
}
160+
}
161+
162+
impl TestExt<TaoForAlpha> for Test {
163+
fn approx_expected_swap_output(
164+
sqrt_current_price: f64,
165+
liquidity_before: f64,
166+
order_liquidity: f64,
167+
) -> f64 {
168+
let denom = liquidity_before / sqrt_current_price + order_liquidity;
169+
let per_order_liq = sqrt_current_price * liquidity_before / denom;
170+
per_order_liq * order_liquidity
171+
}
117172
}
118173

119174
// Mock implementor of SubnetInfo trait

0 commit comments

Comments
 (0)