@@ -77,7 +77,7 @@ use frame_support::{
7777 ensure,
7878 storage:: { with_storage_layer, with_transaction} ,
7979 traits:: {
80- fungibles:: { Balanced , Create , Credit , Inspect , Mutate , MutateFreeze } ,
80+ fungibles:: { Balanced , Create , Credit , Inspect , Mutate , MutateFreeze , metadata :: Inspect as InspectMetadata } ,
8181 tokens:: {
8282 AssetId , Balance ,
8383 Fortitude :: Polite ,
@@ -86,7 +86,7 @@ use frame_support::{
8686 } ,
8787 AccountTouch , Incrementable , OnUnbalanced ,
8888 } ,
89- PalletId , Twox64Concat ,
89+ PalletId ,
9090} ;
9191pub use pallet_assets:: FrozenBalance ;
9292use scale_info:: TypeInfo ;
@@ -131,7 +131,7 @@ pub mod pallet {
131131
132132 /// The type which is used as `key` in `T::OrderBook`, `amount of Reserve`, `quantity of
133133 /// order`, etc..
134- type Unit : Balance + OrderBookIndex + From < AssetBalanceOf < Self > > ;
134+ type Unit : Balance + OrderBookIndex + From < AssetBalanceOf < Self > > + Normalize ;
135135
136136 /// A type used for calculations concerning the `Unit` type to avoid possible overflows.
137137 type HigherPrecisionUnit : IntegerSquareRoot
@@ -150,7 +150,8 @@ pub mod pallet {
150150 type Assets : Inspect < Self :: AccountId , AssetId = Self :: AssetKind , Balance = Self :: Unit >
151151 + Mutate < Self :: AccountId >
152152 + AccountTouch < Self :: AssetKind , Self :: AccountId , Balance = Self :: Unit >
153- + Balanced < Self :: AccountId > ;
153+ + Balanced < Self :: AccountId >
154+ + InspectMetadata < Self :: AccountId > ;
154155
155156 type AssetsFreezer : MutateFreeze <
156157 Self :: AccountId ,
@@ -214,6 +215,9 @@ pub mod pallet {
214215 #[ pallet:: constant]
215216 type MaxSwapPathLength : Get < u32 > ;
216217
218+ #[ pallet:: constant]
219+ type StandardDecimals : Get < u8 > ;
220+
217221 /// The pallet's id, used for deriving its sovereign account ID.
218222 #[ pallet:: constant]
219223 type PalletId : Get < PalletId > ;
@@ -446,6 +450,8 @@ pub mod pallet {
446450 NoPermission ,
447451 /// Quantity of order is greater than existed order
448452 OverOrderQuantity ,
453+ /// Some conversion error occurred
454+ ConversionError ,
449455 /// Operation can't be done
450456 NoOps ,
451457 }
@@ -497,6 +503,10 @@ pub mod pallet {
497503 ) ?;
498504 T :: PoolSetupFeeTarget :: on_unbalanced ( fee) ;
499505
506+
507+ let b_decimals_adjustment = Self :: get_decimals ( & base_asset) ;
508+ let q_decimals_adjustment = Self :: get_decimals ( & quote_asset) ;
509+
500510 if T :: Assets :: should_touch ( * base_asset. clone ( ) , & pool_account) {
501511 T :: Assets :: touch ( * base_asset, & pool_account, & sender) ?
502512 } ;
@@ -520,7 +530,14 @@ pub mod pallet {
520530
521531 Pools :: < T > :: insert (
522532 pool_id. clone ( ) ,
523- Pool :: < T > :: new ( lp_token. clone ( ) , taker_fee_rate, tick_size, lot_size) ,
533+ Pool :: < T > :: new (
534+ lp_token. clone ( ) ,
535+ taker_fee_rate,
536+ tick_size,
537+ lot_size,
538+ b_decimals_adjustment,
539+ q_decimals_adjustment,
540+ ) ,
524541 ) ;
525542 Self :: deposit_event ( Event :: PoolCreated {
526543 creator : sender,
@@ -897,6 +914,16 @@ pub mod pallet {
897914 Pools :: < T > :: get ( & pool_id) . ok_or ( Error :: < T > :: PoolNotFound . into ( ) )
898915 }
899916
917+ fn get_decimals ( asset_kind : & T :: AssetKind ) -> u8 {
918+ let asset_decimals = T :: Assets :: decimals ( asset_kind. clone ( ) ) ;
919+ let n_decimals = if T :: StandardDecimals :: get ( ) > asset_decimals {
920+ T :: StandardDecimals :: get ( )
921+ } else {
922+ asset_decimals - T :: StandardDecimals :: get ( )
923+ } ;
924+ n_decimals
925+ }
926+
900927 fn freeze_asset (
901928 who : & T :: AccountId ,
902929 asset : & T :: AssetKind ,
@@ -985,11 +1012,11 @@ pub mod pallet {
9851012 quote_asset : & T :: AssetKind ,
9861013 ) -> DispatchResult {
9871014 // Validity check
988- let pool_price =
989- Self :: pool_price ( base_asset, quote_asset) . map_err ( |_| Error :: < T > :: ZeroLiquidity ) ?;
9901015 let pool_id = T :: PoolLocator :: pool_id ( base_asset, quote_asset)
9911016 . map_err ( |_| Error :: < T > :: InvalidAssetPair ) ?;
9921017 let mut pool = Pools :: < T > :: get ( & pool_id) . ok_or ( Error :: < T > :: PoolNotFound ) ?;
1018+ let pool_price =
1019+ Self :: pool_price ( base_asset, pool. base_adjustment , quote_asset, pool. quote_adjustment ) . map_err ( |_| Error :: < T > :: ZeroLiquidity ) ?;
9931020 ensure ! (
9941021 order_quantity > Zero :: zero( ) ,
9951022 Error :: <T >:: WrongDesiredAmount
@@ -1138,7 +1165,9 @@ pub mod pallet {
11381165 is_bid,
11391166 orderbook_price,
11401167 base_asset,
1168+ pool. base_adjustment ,
11411169 quote_asset,
1170+ pool. quote_adjustment ,
11421171 remain_orders,
11431172 ) ?;
11441173 if_std ! {
@@ -1201,9 +1230,6 @@ pub mod pallet {
12011230 if remain_orders > Zero :: zero ( ) {
12021231 Self :: do_fill_pool ( is_bid, orderer, remain_orders, base_asset, quote_asset) ?;
12031232 }
1204- if_std ! {
1205- println!( "Final Pool Price after matching => {:?}, filled_orders => {:?}" , Self :: pool_price( base_asset, quote_asset) . unwrap( ) , filled_orders) ;
1206- }
12071233 Self :: handle_filled_orders (
12081234 is_bid,
12091235 base_asset. clone ( ) ,
@@ -1281,10 +1307,16 @@ pub mod pallet {
12811307 /// 1 * quote_reserve / base_reserve
12821308 pub fn pool_price (
12831309 base_asset : & T :: AssetKind ,
1310+ base_decimals_adjustment : u8 ,
12841311 quote_asset : & T :: AssetKind ,
1312+ quote_decimals_adjustment : u8 ,
12851313 ) -> Result < T :: Unit , Error < T > > {
12861314 let ( base_reserve, quote_reserve) = Self :: get_reserves ( base_asset, quote_asset) ?;
1287- Self :: quote ( & One :: one ( ) , & base_reserve, & quote_reserve)
1315+ Self :: quote (
1316+ & One :: one ( ) ,
1317+ & base_reserve. normalize ( base_decimals_adjustment) ,
1318+ & quote_reserve. normalize ( quote_decimals_adjustment)
1319+ )
12881320 }
12891321
12901322 /// Find the closest swap quantity for the given order quantity. `order_quantity` is always
@@ -1297,7 +1329,9 @@ pub mod pallet {
12971329 is_bid : bool ,
12981330 target : T :: Unit ,
12991331 base_asset : & T :: AssetKind ,
1332+ base_decimals_adjustment : u8 ,
13001333 quote_asset : & T :: AssetKind ,
1334+ quote_decimals_adjustment : u8 ,
13011335 order_quantity : T :: Unit ,
13021336 ) -> Result < T :: Unit , Error < T > > {
13031337 let ( b_r, q_r) = Self :: get_reserves ( base_asset, quote_asset) ?;
@@ -1311,13 +1345,13 @@ pub mod pallet {
13111345 // of `base_asset` quantity of `base_asset`
13121346 let amount_in = Self :: get_amount_in ( & mid, & q_r, & b_r) ?;
13131347 if_std ! { println!( "Bid order: amount_in => {:?}, K = {:?}" , amount_in, ( ( b_r-mid) * ( q_r+amount_in) ) ) ; }
1314- Self :: quote ( & One :: one ( ) , & ( b_r - mid) , & ( q_r + amount_in) ) ?
1348+ Self :: quote ( & One :: one ( ) , & ( b_r - mid) . normalize ( base_decimals_adjustment ) , & ( q_r + amount_in) . normalize ( quote_decimals_adjustment ) ) ?
13151349 } else {
13161350 // If it is ask order, get `amount_out` of `quote_asset` with given `amount_in`
13171351 // of `base_asset`
13181352 let amount_out = Self :: get_amount_out ( & mid, & b_r, & q_r) ?;
13191353 if_std ! { println!( "Ask order: amount_out => {:?}" , amount_out) ; }
1320- Self :: quote ( & One :: one ( ) , & ( b_r + mid) , & ( q_r - amount_out) ) ?
1354+ Self :: quote ( & One :: one ( ) , & ( b_r + mid) . normalize ( base_decimals_adjustment ) , & ( q_r - amount_out) . normalize ( quote_decimals_adjustment ) ) ?
13211355 } ;
13221356 if_std ! {
13231357 println!( "Pool Price => {:?}, Mid => {:?}" , pool_price, mid) ;
0 commit comments