@@ -4,9 +4,9 @@ use safe_math::*;
4
4
use substrate_fixed:: types:: U64F64 ;
5
5
use uuid:: Uuid ;
6
6
7
- use crate :: SqrtPrice ;
8
- use crate :: pallet:: { Config , Error } ;
7
+ use crate :: pallet:: { Config , Error , FeeGlobalAlpha , FeeGlobalTao } ;
9
8
use crate :: tick:: TickIndex ;
9
+ use crate :: { NetUid , SqrtPrice } ;
10
10
11
11
/// Position designates one liquidity position.
12
12
///
@@ -21,6 +21,7 @@ use crate::tick::TickIndex;
21
21
#[ derive( Clone , Encode , Decode , PartialEq , Eq , RuntimeDebug , TypeInfo , MaxEncodedLen , Default ) ]
22
22
pub struct Position {
23
23
pub id : PositionId ,
24
+ pub netuid : NetUid ,
24
25
pub tick_low : TickIndex ,
25
26
pub tick_high : TickIndex ,
26
27
pub liquidity : u64 ,
@@ -88,6 +89,38 @@ impl Position {
88
89
)
89
90
} )
90
91
}
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
+ }
91
124
}
92
125
93
126
#[ derive(
0 commit comments