Skip to content

Commit 197f717

Browse files
committed
Parametrize currencies in swap interface
1 parent cefa073 commit 197f717

File tree

6 files changed

+168
-99
lines changed

6 files changed

+168
-99
lines changed

common/src/currency.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ macro_rules! impl_approx {
227227
};
228228
}
229229

230-
pub trait Currency: ToFixed + Into<u64> + From<u64> + Clone + Copy {
230+
pub trait Currency:
231+
ToFixed + Into<u64> + From<u64> + Clone + Copy + Eq + Ord + PartialEq + PartialOrd + Display
232+
{
231233
const MAX: Self;
232234
const ZERO: Self;
233235

common/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,17 @@ impl Default for ProxyType {
169169
}
170170

171171
pub trait SubnetInfo<AccountId> {
172-
fn tao_reserve(netuid: NetUid) -> TaoCurrency;
173-
fn alpha_reserve(netuid: NetUid) -> AlphaCurrency;
174172
fn exists(netuid: NetUid) -> bool;
175173
fn mechanism(netuid: NetUid) -> u16;
176174
fn is_owner(account_id: &AccountId, netuid: NetUid) -> bool;
177175
}
178176

177+
pub trait CurrencyReserve<C: Currency> {
178+
fn reserve(netuid: NetUid) -> C;
179+
fn increase_provided(netuid: NetUid, amount: C);
180+
fn decrease_provided(netuid: NetUid, amount: C);
181+
}
182+
179183
pub trait BalanceOps<AccountId> {
180184
fn tao_balance(account_id: &AccountId) -> TaoCurrency;
181185
fn alpha_balance(netuid: NetUid, coldkey: &AccountId, hotkey: &AccountId) -> AlphaCurrency;
@@ -196,10 +200,6 @@ pub trait BalanceOps<AccountId> {
196200
netuid: NetUid,
197201
alpha: AlphaCurrency,
198202
) -> Result<AlphaCurrency, DispatchError>;
199-
fn increase_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency);
200-
fn decrease_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency);
201-
fn increase_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency);
202-
fn decrease_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency);
203203
}
204204

205205
pub mod time {

pallets/swap-interface/src/lib.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
use frame_support::pallet_prelude::*;
44
use substrate_fixed::types::U96F32;
5-
use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency};
5+
use subtensor_runtime_common::{AlphaCurrency, Currency, CurrencyReserve, NetUid, TaoCurrency};
6+
7+
pub mod order;
68

79
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
810
pub enum OrderType {
@@ -11,23 +13,33 @@ pub enum OrderType {
1113
}
1214

1315
pub trait SwapHandler<AccountId> {
14-
fn swap(
16+
fn swap<PaidIn, PaidOut, ReserveIn, ReserveOut>(
1517
netuid: NetUid,
1618
order_t: OrderType,
17-
amount: u64,
18-
price_limit: u64,
19+
amount: PaidIn,
20+
price_limit: TaoCurrency,
1921
drop_fees: bool,
2022
should_rollback: bool,
21-
) -> Result<SwapResult, DispatchError>;
22-
fn sim_swap(
23+
) -> Result<SwapResult<PaidIn, PaidOut>, DispatchError>
24+
where
25+
PaidIn: Currency,
26+
PaidOut: Currency,
27+
ReserveIn: CurrencyReserve<PaidIn>,
28+
ReserveOut: CurrencyReserve<PaidOut>;
29+
fn sim_swap<PaidIn, PaidOut, ReserveIn, ReserveOut>(
2330
netuid: NetUid,
2431
order_t: OrderType,
25-
amount: u64,
26-
) -> Result<SwapResult, DispatchError>;
27-
fn approx_fee_amount(netuid: NetUid, amount: u64) -> u64;
32+
amount: PaidIn,
33+
) -> Result<SwapResult<PaidIn, PaidOut>, DispatchError>
34+
where
35+
PaidIn: Currency,
36+
PaidOut: Currency,
37+
ReserveIn: CurrencyReserve<PaidIn>,
38+
ReserveOut: CurrencyReserve<PaidOut>;
39+
fn approx_fee_amount<T: Currency>(netuid: NetUid, amount: T) -> T;
2840
fn current_alpha_price(netuid: NetUid) -> U96F32;
29-
fn max_price() -> u64;
30-
fn min_price() -> u64;
41+
fn max_price<C: Currency>() -> C;
42+
fn min_price<C: Currency>() -> C;
3143
fn adjust_protocol_liquidity(
3244
netuid: NetUid,
3345
tao_delta: TaoCurrency,
@@ -36,12 +48,16 @@ pub trait SwapHandler<AccountId> {
3648
fn is_user_liquidity_enabled(netuid: NetUid) -> bool;
3749
}
3850

39-
#[derive(Debug, PartialEq)]
40-
pub struct SwapResult {
41-
pub amount_paid_in: u64,
42-
pub amount_paid_out: u64,
43-
pub fee_paid: u64,
51+
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
52+
pub struct SwapResult<PaidIn, PaidOut>
53+
where
54+
PaidIn: Currency,
55+
PaidOut: Currency,
56+
{
57+
pub amount_paid_in: PaidIn,
58+
pub amount_paid_out: PaidOut,
59+
pub fee_paid: PaidIn,
4460
// For calculation of new tao/alpha reserves
45-
pub tao_reserve_delta: i64,
46-
pub alpha_reserve_delta: i64,
61+
pub tao_reserve_delta: i128,
62+
pub alpha_reserve_delta: i128,
4763
}

pallets/swap-interface/src/order.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use core::marker::PhantomData;
2+
3+
use subtensor_runtime_common::Currency;
4+
5+
pub struct OrderType<PaidIn, PaidOut>
6+
where
7+
PaidIn: Currency,
8+
PaidOut: Currency,
9+
{
10+
amount: PaidIn,
11+
_paid_in: PhantomData<PaidIn>,
12+
_paid_out: PhantomData<PaidOut>,
13+
}
14+
15+
pub trait Order<PaidIn: Currency, PaidOut: Currency> {}

0 commit comments

Comments
 (0)