@@ -31,6 +31,8 @@ pub type AssetIdOf<T> =
3131pub type AssetBalanceOf < T > =
3232 <<T as Config >:: Assets as Inspect < <T as frame_system:: Config >:: AccountId > >:: Balance ;
3333
34+ type Orders < Q , A , B > = Vec < Order < Q , A , B > > ;
35+
3436impl < T : Config , AssetId , AccountId , Balance > FrozenBalance < AssetId , AccountId , Balance >
3537 for Pallet < T >
3638where
6769) ]
6870pub struct OrderId ( pub u64 ) ;
6971
72+ impl OrderId {
73+ fn start_bid_id ( ) -> Self {
74+ Self ( 0 )
75+ }
76+
77+ fn start_ask_id ( ) -> Self {
78+ Self ( 1 << u64:: BITS - 1 )
79+ }
80+
81+ fn max_bid_id ( & self ) -> Self {
82+ Self :: start_ask_id ( )
83+ }
84+
85+ fn max_ask_id ( & self ) -> Self {
86+ Self ( u64:: MAX )
87+ }
88+
89+ fn increase_by_one ( & self ) -> Option < Self > {
90+ let is_bid = self . is_bid ( ) ;
91+ let max_index = if is_bid {
92+ self . max_bid_id ( )
93+ } else {
94+ self . max_ask_id ( )
95+ } ;
96+ if self . eq ( & ( max_index - 1 . into ( ) ) ) {
97+ return None ;
98+ }
99+ let new = Self ( self . 0 + 1 ) ;
100+ Some ( new)
101+ }
102+
103+ fn is_bid ( & self ) -> bool {
104+ self . 0 & Self :: start_ask_id ( ) . 0 == 0
105+ }
106+ }
107+
70108#[ derive( Debug ) ]
71109pub enum OrderIdError {
72110 Overflow ,
@@ -78,35 +116,18 @@ impl OrderbookOrderId for OrderId {
78116
79117 fn new ( is_bid : bool ) -> Self {
80118 if is_bid {
81- Self ( 0 )
119+ Self :: start_ask_id ( )
82120 } else {
83- Self ( 1 << ( u64 :: BITS - 1 ) )
121+ Self :: start_bid_id ( )
84122 }
85123 }
86124
87125 fn checked_increase ( & self , is_bid : bool ) -> Option < Self > {
88- let max_index = if is_bid {
89- 1 << ( u64:: BITS - 1 )
90- } else {
91- u64:: MAX
92- } ;
93-
94- let new = self . 0 . checked_add ( 1 ) ;
95- match new {
96- Some ( new) => {
97- // Over max index
98- if new > max_index {
99- return None ;
100- }
101- Some ( new. into ( ) )
102- }
103- // Overflow
104- None => None ,
105- }
126+ self . increase_by_one ( )
106127 }
107128
108129 fn is_bid ( & self ) -> bool {
109- self . 0 & ( 1 << ( u64 :: BITS - 1 ) ) == 0
130+ self . is_bid ( )
110131 }
111132}
112133
@@ -305,7 +326,7 @@ impl<T: Config> Pool<T> {
305326 & self ,
306327 owner : & T :: AccountId ,
307328 is_bid : bool ,
308- ) -> Vec < Order < T :: Unit , T :: AccountId , BlockNumberFor < T > > > {
329+ ) -> Orders < T :: Unit , T :: AccountId , BlockNumberFor < T > > {
309330 if is_bid {
310331 self . bids . get_orders ( owner)
311332 } else {
@@ -569,14 +590,16 @@ pub mod traits {
569590 /// Type of error of orderbook
570591 type Error ;
571592
572- /// Create new instance
593+ /// Create new instance of `Self`
573594 fn new ( ) -> Self ;
574595
575596 /// Check if the orderbook is empty
576597 fn is_empty ( & self ) -> bool ;
577598
578- fn get_orders ( & self , owner : & Account ) -> Vec < Order < Unit , Account , BlockNumber > > ;
599+ /// Get all orders of given `owner`
600+ fn get_orders ( & self , owner : & Account ) -> Orders < Unit , Account , BlockNumber > ;
579601
602+ /// Try get all orders for given `key`
580603 fn open_orders_at ( & self , key : Unit ) -> Result < Option < Self :: Order > , Self :: Error > ;
581604
582605 /// Optionally return the minimum order of the orderbook which is (price, index).
@@ -673,7 +696,7 @@ pub mod traits {
673696 expired_at : BlockNumber ,
674697 ) -> Self ;
675698
676- fn find_order_of ( & self , owner : & Account ) -> Option < Vec < Order < Unit , Account , BlockNumber > > > ;
699+ fn find_order_of ( & self , owner : & Account ) -> Option < Orders < Unit , Account , BlockNumber > > ;
677700
678701 fn orders ( & self ) -> Self ;
679702
@@ -742,13 +765,13 @@ pub mod traits {
742765 }
743766
744767 // for test only
745- fn find_order_of ( & self , owner : & Account ) -> Option < Vec < Order < Unit , Account , BlockNumber > > > {
768+ fn find_order_of ( & self , owner : & Account ) -> Option < Orders < Unit , Account , BlockNumber > > {
746769 let matched = self
747770 . to_owned ( )
748771 . open_orders
749772 . into_values ( )
750773 . filter ( |o| o. owner ( ) == * owner)
751- . collect :: < Vec < Order < Unit , Account , BlockNumber > > > ( ) ;
774+ . collect :: < Orders < Unit , Account , BlockNumber > > ( ) ;
752775 if matched. is_empty ( ) {
753776 return None ;
754777 } else {
@@ -854,7 +877,17 @@ pub mod traits {
854877
855878#[ cfg( test) ]
856879mod tests {
857- use super :: OrderBookIndex ;
880+ use super :: { OrderBookIndex , OrderId } ;
881+
882+ #[ test]
883+ fn order_id_op_works ( ) {
884+ let order_id: OrderId = OrderId :: start_bid_id ( ) ;
885+ assert ! ( order_id. is_bid( ) ) ;
886+ let mut order_id: OrderId = OrderId :: start_ask_id ( ) ;
887+ assert ! ( !order_id. is_bid( ) ) ;
888+ order_id += 1 ;
889+ assert ! ( order_id. is_bid( ) ) ;
890+ }
858891
859892 #[ test]
860893 fn partition_index_works ( ) {
0 commit comments