Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ macro_rules! impl_approx {
};
}

pub trait Currency: ToFixed + Into<u64> + From<u64> + Clone + Copy {
pub trait Currency:
ToFixed + Into<u64> + From<u64> + Clone + Copy + Eq + Ord + PartialEq + PartialOrd + Display
{
const MAX: Self;
const ZERO: Self;

Expand Down
12 changes: 6 additions & 6 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,17 @@ impl Default for ProxyType {
}

pub trait SubnetInfo<AccountId> {
fn tao_reserve(netuid: NetUid) -> TaoCurrency;
fn alpha_reserve(netuid: NetUid) -> AlphaCurrency;
fn exists(netuid: NetUid) -> bool;
fn mechanism(netuid: NetUid) -> u16;
fn is_owner(account_id: &AccountId, netuid: NetUid) -> bool;
}

pub trait CurrencyReserve<C: Currency> {
fn reserve(netuid: NetUid) -> C;
fn increase_provided(netuid: NetUid, amount: C);
fn decrease_provided(netuid: NetUid, amount: C);
}

pub trait BalanceOps<AccountId> {
fn tao_balance(account_id: &AccountId) -> TaoCurrency;
fn alpha_balance(netuid: NetUid, coldkey: &AccountId, hotkey: &AccountId) -> AlphaCurrency;
Expand All @@ -196,10 +200,6 @@ pub trait BalanceOps<AccountId> {
netuid: NetUid,
alpha: AlphaCurrency,
) -> Result<AlphaCurrency, DispatchError>;
fn increase_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency);
fn decrease_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency);
fn increase_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency);
fn decrease_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency);
}

pub mod time {
Expand Down
86 changes: 60 additions & 26 deletions pallets/swap-interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
#![cfg_attr(not(feature = "std"), no_std)]
use core::ops::Neg;

use frame_support::pallet_prelude::*;
use substrate_fixed::types::U96F32;
use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency};
use subtensor_runtime_common::{AlphaCurrency, Currency, CurrencyReserve, NetUid, TaoCurrency};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderType {
Sell,
Buy,
}
pub use order::*;

mod order;

pub trait SwapHandler<AccountId> {
fn swap(
fn swap<PaidIn, PaidOut, ReserveIn, ReserveOut, OrderT>(
netuid: NetUid,
order_t: OrderType,
amount: u64,
price_limit: u64,
order: OrderT,
price_limit: TaoCurrency,
drop_fees: bool,
should_rollback: bool,
) -> Result<SwapResult, DispatchError>;
fn sim_swap(
) -> Result<SwapResult<PaidIn, PaidOut>, DispatchError>
where
PaidIn: Currency,
PaidOut: Currency,
ReserveIn: CurrencyReserve<PaidIn>,
ReserveOut: CurrencyReserve<PaidOut>,
OrderT: Order<PaidIn, PaidOut>;
fn sim_swap<PaidIn, PaidOut, ReserveIn, ReserveOut, OrderT>(
netuid: NetUid,
order_t: OrderType,
amount: u64,
) -> Result<SwapResult, DispatchError>;
fn approx_fee_amount(netuid: NetUid, amount: u64) -> u64;
order: OrderT,
amount: PaidIn,
) -> Result<SwapResult<PaidIn, PaidOut>, DispatchError>
where
PaidIn: Currency,
PaidOut: Currency,
ReserveIn: CurrencyReserve<PaidIn>,
ReserveOut: CurrencyReserve<PaidOut>,
OrderT: Order<PaidIn, PaidOut>,
Self: DefaultPriceLimit<PaidIn, PaidOut>;
fn approx_fee_amount<T: Currency>(netuid: NetUid, amount: T) -> T;
fn current_alpha_price(netuid: NetUid) -> U96F32;
fn max_price() -> u64;
fn min_price() -> u64;
fn max_price<C: Currency>() -> C;
fn min_price<C: Currency>() -> C;
fn adjust_protocol_liquidity(
netuid: NetUid,
tao_delta: TaoCurrency,
Expand All @@ -36,12 +47,35 @@ pub trait SwapHandler<AccountId> {
fn is_user_liquidity_enabled(netuid: NetUid) -> bool;
}

#[derive(Debug, PartialEq)]
pub struct SwapResult {
pub amount_paid_in: u64,
pub amount_paid_out: u64,
pub fee_paid: u64,
// For calculation of new tao/alpha reserves
pub tao_reserve_delta: i64,
pub alpha_reserve_delta: i64,
pub trait DefaultPriceLimit<PaidIn, PaidOut>
where
PaidIn: Currency,
PaidOut: Currency,
{
fn default_price_limit<C: Currency>() -> C;
}

#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
pub struct SwapResult<PaidIn, PaidOut>
where
PaidIn: Currency,
PaidOut: Currency,
{
pub amount_paid_in: PaidIn,
pub amount_paid_out: PaidOut,
pub fee_paid: PaidIn,
}

impl<PaidIn, PaidOut> SwapResult<PaidIn, PaidOut>
where
PaidIn: Currency,
PaidOut: Currency,
{
pub fn paid_in_reserve_delta(&self) -> i128 {
self.amount_paid_in.to_u64() as i128
}

pub fn paid_out_reserve_delta(&self) -> i128 {
(self.amount_paid_out.to_u64() as i128).neg()
}
}
39 changes: 39 additions & 0 deletions pallets/swap-interface/src/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use core::marker::PhantomData;

use substrate_fixed::types::U64F64;
use subtensor_runtime_common::{AlphaCurrency, Currency, TaoCurrency};

pub trait Order<PaidIn: Currency, PaidOut: Currency>: Clone {
fn amount(&self) -> PaidIn;
fn is_beyond_price_limit(&self, alpha_sqrt_price: U64F64, limit_sqrt_price: U64F64) -> bool;
}

#[derive(Clone)]
pub struct BasicOrder<PaidIn, PaidOut>
where
PaidIn: Currency,
PaidOut: Currency,
{
amount: PaidIn,
_phantom: PhantomData<(PaidIn, PaidOut)>,
}

impl Order<TaoCurrency, AlphaCurrency> for BasicOrder<TaoCurrency, AlphaCurrency> {
fn amount(&self) -> TaoCurrency {
self.amount
}

fn is_beyond_price_limit(&self, alpha_sqrt_price: U64F64, limit_sqrt_price: U64F64) -> bool {
alpha_sqrt_price < limit_sqrt_price
}
}

impl Order<AlphaCurrency, TaoCurrency> for BasicOrder<AlphaCurrency, TaoCurrency> {
fn amount(&self) -> AlphaCurrency {
self.amount
}

fn is_beyond_price_limit(&self, alpha_sqrt_price: U64F64, limit_sqrt_price: U64F64) -> bool {
alpha_sqrt_price > limit_sqrt_price
}
}
1 change: 0 additions & 1 deletion pallets/swap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]

use substrate_fixed::types::U64F64;
use subtensor_swap_interface::OrderType;

pub mod pallet;
pub mod position;
Expand Down
Loading