Skip to content

Commit 6d4c600

Browse files
committed
Add ActiveTickIndexManager
1 parent 8560168 commit 6d4c600

File tree

6 files changed

+482
-374
lines changed

6 files changed

+482
-374
lines changed

pallets/swap/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use safe_math::*;
1010
use substrate_fixed::types::U64F64;
1111
use uuid::Uuid;
1212

13-
use self::tick::{Layer, Tick, TickIndex, TickIndexBitmap};
13+
use self::tick::{LayerLevel, Tick, TickIndex, TickIndexBitmap};
1414
use crate::pallet::{Config, Error};
1515

1616
pub mod pallet;
@@ -28,7 +28,6 @@ pub struct RemoveLiquidityResult {
2828
fee_alpha: u64,
2929
}
3030

31-
3231
#[derive(
3332
Clone, Copy, Decode, Default, Encode, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo,
3433
)]

pallets/swap/src/pallet/impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ impl<T: Config> Pallet<T> {
107107
// New position
108108
let position = Position {
109109
id: PositionId::new(),
110+
netuid,
110111
tick_low,
111112
tick_high,
112113
liquidity,
@@ -184,7 +185,7 @@ impl<T: Config> Pallet<T> {
184185
let current_tick_index = CurrentTickIndex::<T>::get(netuid);
185186

186187
// Collect fees and get tao and alpha amounts
187-
// let (fee_tao, fee_alpha) = self.collect_fees(&mut pos);
188+
let (fee_tao, fee_alpha) = pos.collect_fees::<T>();
188189
// let current_price: SqrtPrice = self.state_ops.get_alpha_sqrt_price();
189190
// let (tao, alpha) = pos.to_token_amounts(current_price)?;
190191

pallets/swap/src/pallet/mod.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use substrate_fixed::types::U64F64;
66
use crate::{
77
NetUid,
88
position::{Position, PositionId},
9-
tick::{Tick, TickIndex},
9+
tick::{LayerLevel, Tick, TickIndex},
1010
};
1111

1212
pub use pallet::*;
@@ -76,7 +76,6 @@ mod pallet {
7676
/// Storage for user positions, using subnet ID and account ID as keys
7777
/// The value is a bounded vector of Position structs with details about the liquidity positions
7878
#[pallet::storage]
79-
#[pallet::getter(fn positions)]
8079
pub type Positions<T: Config> = StorageNMap<
8180
_,
8281
(
@@ -88,6 +87,27 @@ mod pallet {
8887
OptionQuery,
8988
>;
9089

90+
// Global accrued fees in tao per subnet
91+
#[pallet::storage]
92+
pub type FeeGlobalTao<T> = StorageMap<_, Twox64Concat, NetUid, U64F64>;
93+
94+
// Global accrued fees in alpha per subnet
95+
#[pallet::storage]
96+
pub type FeeGlobalAlpha<T> = StorageMap<_, Twox64Concat, NetUid, U64F64>;
97+
98+
/// Tick index bitmap words storage
99+
#[pallet::storage]
100+
pub type TickIndexBitmapWords<T: Config> = StorageNMap<
101+
_,
102+
(
103+
NMapKey<Twox64Concat, NetUid>, // Subnet ID
104+
NMapKey<Twox64Concat, LayerLevel>, // Layer level
105+
NMapKey<Twox64Concat, u32>, // word index
106+
),
107+
u128,
108+
ValueQuery,
109+
>;
110+
91111
#[pallet::event]
92112
#[pallet::generate_deposit(pub(super) fn deposit_event)]
93113
pub enum Event<T: Config> {

pallets/swap/src/position.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use safe_math::*;
44
use substrate_fixed::types::U64F64;
55
use uuid::Uuid;
66

7-
use crate::SqrtPrice;
8-
use crate::pallet::{Config, Error};
7+
use crate::pallet::{Config, Error, FeeGlobalAlpha, FeeGlobalTao};
98
use crate::tick::TickIndex;
9+
use crate::{NetUid, SqrtPrice};
1010

1111
/// Position designates one liquidity position.
1212
///
@@ -21,6 +21,7 @@ use crate::tick::TickIndex;
2121
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen, Default)]
2222
pub struct Position {
2323
pub id: PositionId,
24+
pub netuid: NetUid,
2425
pub tick_low: TickIndex,
2526
pub tick_high: TickIndex,
2627
pub liquidity: u64,
@@ -88,6 +89,38 @@ impl Position {
8889
)
8990
})
9091
}
92+
93+
/// Collect fees for a position
94+
/// Updates the position
95+
pub fn collect_fees<T: Config>(&mut self) -> (u64, u64) {
96+
let mut fee_tao = self.fees_in_range::<T>(true);
97+
let mut fee_alpha = self.fees_in_range::<T>(false);
98+
99+
fee_tao = fee_tao.saturating_sub(self.fees_tao);
100+
fee_alpha = fee_alpha.saturating_sub(self.fees_alpha);
101+
102+
self.fees_tao = fee_tao;
103+
self.fees_alpha = fee_alpha;
104+
105+
fee_tao = self.liquidity.saturating_mul(fee_tao);
106+
fee_alpha = self.liquidity.saturating_mul(fee_alpha);
107+
108+
(fee_tao, fee_alpha)
109+
}
110+
111+
/// Get fees in a position's range
112+
///
113+
/// If quote flag is true, Tao is returned, otherwise alpha.
114+
fn fees_in_range<T: Config>(&self, quote: bool) -> u64 {
115+
if quote {
116+
FeeGlobalTao::<T>::get(self.netuid).unwrap_or_default()
117+
} else {
118+
FeeGlobalAlpha::<T>::get(self.netuid).unwrap_or_default()
119+
}
120+
.saturating_sub(self.tick_low.fees_below::<T>(self.netuid, quote))
121+
.saturating_sub(self.tick_high.fees_above::<T>(self.netuid, quote))
122+
.saturating_to_num::<u64>()
123+
}
91124
}
92125

93126
#[derive(

0 commit comments

Comments
 (0)