@@ -32,7 +32,7 @@ import {
3232 StateAccount ,
3333} from '../types' ;
3434import { isUserProtectedMaker } from '../math/userStatus' ;
35- import { OraclePriceData } from '../oracles/types' ;
35+ import { MMOraclePriceData , OraclePriceData } from '../oracles/types' ;
3636import { ProtectMakerParamsMap } from './types' ;
3737import { SlotSubscriber } from '../slot/SlotSubscriber' ;
3838import { UserMap } from '../userMap/userMap' ;
@@ -446,16 +446,20 @@ export class DLOB {
446446 return undefined ;
447447 }
448448
449- public findNodesToFill (
449+ public findNodesToFill < T extends MarketType > (
450450 marketIndex : number ,
451451 fallbackBid : BN | undefined ,
452452 fallbackAsk : BN | undefined ,
453453 slot : number ,
454454 ts : number ,
455- marketType : MarketType ,
456- oraclePriceData : OraclePriceData ,
455+ marketType : T ,
456+ oraclePriceData : T extends { spot : unknown }
457+ ? OraclePriceData
458+ : MMOraclePriceData ,
457459 stateAccount : StateAccount ,
458- marketAccount : PerpMarketAccount | SpotMarketAccount
460+ marketAccount : T extends { spot : unknown }
461+ ? SpotMarketAccount
462+ : PerpMarketAccount
459463 ) : NodeToFill [ ] {
460464 if ( fillPaused ( stateAccount , marketAccount ) ) {
461465 return [ ] ;
@@ -572,11 +576,13 @@ export class DLOB {
572576 return Array . from ( mergedNodesToFill . values ( ) ) ;
573577 }
574578
575- public findRestingLimitOrderNodesToFill (
579+ public findRestingLimitOrderNodesToFill < T extends MarketType > (
576580 marketIndex : number ,
577581 slot : number ,
578- marketType : MarketType ,
579- oraclePriceData : OraclePriceData ,
582+ marketType : T ,
583+ oraclePriceData : T extends { spot : unknown }
584+ ? OraclePriceData
585+ : MMOraclePriceData ,
580586 isAmmPaused : boolean ,
581587 minAuctionDuration : number ,
582588 makerRebateNumerator : number ,
@@ -656,11 +662,13 @@ export class DLOB {
656662 return nodesToFill ;
657663 }
658664
659- public findTakingNodesToFill (
665+ public findTakingNodesToFill < T extends MarketType > (
660666 marketIndex : number ,
661667 slot : number ,
662- marketType : MarketType ,
663- oraclePriceData : OraclePriceData ,
668+ marketType : T ,
669+ oraclePriceData : T extends { spot : unknown }
670+ ? OraclePriceData
671+ : MMOraclePriceData ,
664672 isAmmPaused : boolean ,
665673 minAuctionDuration : number ,
666674 fallbackAsk : BN | undefined ,
@@ -782,17 +790,21 @@ export class DLOB {
782790 return nodesToFill ;
783791 }
784792
785- public findTakingNodesCrossingMakerNodes (
793+ public findTakingNodesCrossingMakerNodes < T extends MarketType > (
786794 marketIndex : number ,
787795 slot : number ,
788- marketType : MarketType ,
789- oraclePriceData : OraclePriceData ,
796+ marketType : T ,
797+ oraclePriceData : T extends { spot : unknown }
798+ ? OraclePriceData
799+ : MMOraclePriceData ,
790800 takerNodeGenerator : Generator < DLOBNode > ,
791801 makerNodeGeneratorFn : (
792802 marketIndex : number ,
793803 slot : number ,
794804 marketType : MarketType ,
795- oraclePriceData : OraclePriceData
805+ oraclePriceData : T extends { spot : unknown }
806+ ? OraclePriceData
807+ : MMOraclePriceData
796808 ) => Generator < DLOBNode > ,
797809 doesCross : ( takerPrice : BN | undefined , makerPrice : BN ) => boolean
798810 ) : NodeToFill [ ] {
@@ -878,10 +890,12 @@ export class DLOB {
878890 return nodesToFill ;
879891 }
880892
881- public findNodesCrossingFallbackLiquidity (
882- marketType : MarketType ,
893+ public findNodesCrossingFallbackLiquidity < T extends MarketType > (
894+ marketType : T ,
883895 slot : number ,
884- oraclePriceData : OraclePriceData ,
896+ oraclePriceData : T extends { spot : unknown }
897+ ? OraclePriceData
898+ : MMOraclePriceData ,
885899 nodeGenerator : Generator < DLOBNode > ,
886900 doesCross : ( nodePrice : BN | undefined ) => boolean ,
887901 minAuctionDuration : number
@@ -997,11 +1011,13 @@ export class DLOB {
9971011 return nodesToFill ;
9981012 }
9991013
1000- * getTakingBids (
1014+ * getTakingBids < T extends MarketType > (
10011015 marketIndex : number ,
1002- marketType : MarketType ,
1016+ marketType : T ,
10031017 slot : number ,
1004- oraclePriceData : OraclePriceData ,
1018+ oraclePriceData : T extends { spot : unknown }
1019+ ? OraclePriceData
1020+ : MMOraclePriceData ,
10051021 filterFcn ?: DLOBFilterFcn
10061022 ) : Generator < DLOBNode > {
10071023 const marketTypeStr = getVariant ( marketType ) as MarketTypeStr ;
@@ -1032,11 +1048,13 @@ export class DLOB {
10321048 ) ;
10331049 }
10341050
1035- * getTakingAsks (
1051+ * getTakingAsks < T extends MarketType > (
10361052 marketIndex : number ,
1037- marketType : MarketType ,
1053+ marketType : T ,
10381054 slot : number ,
1039- oraclePriceData : OraclePriceData ,
1055+ oraclePriceData : T extends { spot : unknown }
1056+ ? OraclePriceData
1057+ : MMOraclePriceData ,
10401058 filterFcn ?: DLOBFilterFcn
10411059 ) : Generator < DLOBNode > {
10421060 const marketTypeStr = getVariant ( marketType ) as MarketTypeStr ;
@@ -1138,11 +1156,13 @@ export class DLOB {
11381156 }
11391157 }
11401158
1141- * getRestingLimitAsks (
1159+ * getRestingLimitAsks < T extends MarketType > (
11421160 marketIndex : number ,
11431161 slot : number ,
1144- marketType : MarketType ,
1145- oraclePriceData : OraclePriceData ,
1162+ marketType : T ,
1163+ oraclePriceData : T extends { spot : unknown }
1164+ ? OraclePriceData
1165+ : MMOraclePriceData ,
11461166 filterFcn ?: DLOBFilterFcn
11471167 ) : Generator < DLOBNode > {
11481168 if ( isVariant ( marketType , 'spot' ) && ! oraclePriceData ) {
@@ -1180,11 +1200,13 @@ export class DLOB {
11801200 ) ;
11811201 }
11821202
1183- * getRestingLimitBids (
1203+ * getRestingLimitBids < T extends MarketType > (
11841204 marketIndex : number ,
11851205 slot : number ,
1186- marketType : MarketType ,
1187- oraclePriceData : OraclePriceData ,
1206+ marketType : T ,
1207+ oraclePriceData : T extends { spot : unknown }
1208+ ? OraclePriceData
1209+ : MMOraclePriceData ,
11881210 filterFcn ?: DLOBFilterFcn
11891211 ) : Generator < DLOBNode > {
11901212 if ( isVariant ( marketType , 'spot' ) && ! oraclePriceData ) {
@@ -1231,12 +1253,14 @@ export class DLOB {
12311253 * @param oraclePriceData
12321254 * @param filterFcn
12331255 */
1234- * getAsks (
1256+ * getAsks < T extends MarketType > (
12351257 marketIndex : number ,
12361258 _fallbackAsk : BN | undefined ,
12371259 slot : number ,
1238- marketType : MarketType ,
1239- oraclePriceData : OraclePriceData ,
1260+ marketType : T ,
1261+ oraclePriceData : T extends { spot : unknown }
1262+ ? OraclePriceData
1263+ : MMOraclePriceData ,
12401264 filterFcn ?: DLOBFilterFcn
12411265 ) : Generator < DLOBNode > {
12421266 if ( isVariant ( marketType , 'spot' ) && ! oraclePriceData ) {
@@ -1276,12 +1300,14 @@ export class DLOB {
12761300 * @param oraclePriceData
12771301 * @param filterFcn
12781302 */
1279- * getBids (
1303+ * getBids < T extends MarketType > (
12801304 marketIndex : number ,
12811305 _fallbackBid : BN | undefined ,
12821306 slot : number ,
1283- marketType : MarketType ,
1284- oraclePriceData : OraclePriceData ,
1307+ marketType : T ,
1308+ oraclePriceData : T extends { spot : unknown }
1309+ ? OraclePriceData
1310+ : MMOraclePriceData ,
12851311 filterFcn ?: DLOBFilterFcn
12861312 ) : Generator < DLOBNode > {
12871313 if ( isVariant ( marketType , 'spot' ) && ! oraclePriceData ) {
@@ -1313,11 +1339,13 @@ export class DLOB {
13131339 ) ;
13141340 }
13151341
1316- findCrossingRestingLimitOrders (
1342+ findCrossingRestingLimitOrders < T extends MarketType > (
13171343 marketIndex : number ,
13181344 slot : number ,
1319- marketType : MarketType ,
1320- oraclePriceData : OraclePriceData
1345+ marketType : T ,
1346+ oraclePriceData : T extends { spot : unknown }
1347+ ? OraclePriceData
1348+ : MMOraclePriceData
13211349 ) : NodeToFill [ ] {
13221350 const nodesToFill = new Array < NodeToFill > ( ) ;
13231351
@@ -1439,11 +1467,13 @@ export class DLOB {
14391467 }
14401468 }
14411469
1442- public getBestAsk (
1470+ public getBestAsk < T extends MarketType > (
14431471 marketIndex : number ,
14441472 slot : number ,
1445- marketType : MarketType ,
1446- oraclePriceData : OraclePriceData
1473+ marketType : T ,
1474+ oraclePriceData : T extends { spot : unknown }
1475+ ? OraclePriceData
1476+ : MMOraclePriceData
14471477 ) : BN | undefined {
14481478 const bestAsk = this . getRestingLimitAsks (
14491479 marketIndex ,
@@ -1458,11 +1488,13 @@ export class DLOB {
14581488 return undefined ;
14591489 }
14601490
1461- public getBestBid (
1491+ public getBestBid < T extends MarketType > (
14621492 marketIndex : number ,
14631493 slot : number ,
1464- marketType : MarketType ,
1465- oraclePriceData : OraclePriceData
1494+ marketType : T ,
1495+ oraclePriceData : T extends { spot : unknown }
1496+ ? OraclePriceData
1497+ : MMOraclePriceData
14661498 ) : BN | undefined {
14671499 const bestBid = this . getRestingLimitBids (
14681500 marketIndex ,
@@ -1636,7 +1668,7 @@ export class DLOB {
16361668 if ( isVariant ( marketType , 'perp' ) ) {
16371669 const slot = slotSubscriber . getSlot ( ) ;
16381670 const oraclePriceData =
1639- driftClient . getOracleDataForPerpMarket ( marketIndex ) ;
1671+ driftClient . getMMOracleDataForPerpMarket ( marketIndex ) ;
16401672
16411673 const bestAsk = this . getBestAsk (
16421674 marketIndex ,
@@ -1681,18 +1713,18 @@ export class DLOB {
16811713 } else if ( isVariant ( marketType , 'spot' ) ) {
16821714 const slot = slotSubscriber . getSlot ( ) ;
16831715 const oraclePriceData =
1684- driftClient . getOracleDataForPerpMarket ( marketIndex ) ;
1716+ driftClient . getOracleDataForSpotMarket ( marketIndex ) ;
16851717
16861718 const bestAsk = this . getBestAsk (
16871719 marketIndex ,
16881720 slot ,
1689- marketType ,
1721+ MarketType . SPOT ,
16901722 oraclePriceData
16911723 ) ;
16921724 const bestBid = this . getBestBid (
16931725 marketIndex ,
16941726 slot ,
1695- marketType ,
1727+ MarketType . SPOT ,
16961728 oraclePriceData
16971729 ) ;
16981730 const mid = bestAsk . add ( bestBid ) . div ( new BN ( 2 ) ) ;
@@ -1783,7 +1815,7 @@ export class DLOB {
17831815 * @param depth how many levels of the order book to return
17841816 * @param fallbackL2Generators L2 generators for fallback liquidity e.g. vAMM {@link getVammL2Generator}, openbook {@link SerumSubscriber}
17851817 */
1786- public getL2 ( {
1818+ public getL2 < T extends MarketType > ( {
17871819 marketIndex,
17881820 marketType,
17891821 slot,
@@ -1792,9 +1824,11 @@ export class DLOB {
17921824 fallbackL2Generators = [ ] ,
17931825 } : {
17941826 marketIndex : number ;
1795- marketType : MarketType ;
1827+ marketType : T ;
17961828 slot : number ;
1797- oraclePriceData : OraclePriceData ;
1829+ oraclePriceData : T extends { spot : unknown }
1830+ ? OraclePriceData
1831+ : MMOraclePriceData ;
17981832 depth : number ;
17991833 fallbackL2Generators ?: L2OrderBookGenerator [ ] ;
18001834 } ) : L2OrderBook {
@@ -1853,16 +1887,18 @@ export class DLOB {
18531887 * @param slot
18541888 * @param oraclePriceData
18551889 */
1856- public getL3 ( {
1890+ public getL3 < T extends MarketType > ( {
18571891 marketIndex,
18581892 marketType,
18591893 slot,
18601894 oraclePriceData,
18611895 } : {
18621896 marketIndex : number ;
1863- marketType : MarketType ;
1897+ marketType : T ;
18641898 slot : number ;
1865- oraclePriceData : OraclePriceData ;
1899+ oraclePriceData : T extends { spot : unknown }
1900+ ? OraclePriceData
1901+ : MMOraclePriceData ;
18661902 } ) : L3OrderBook {
18671903 const bids : L3Level [ ] = [ ] ;
18681904 const asks : L3Level [ ] = [ ] ;
@@ -1945,7 +1981,7 @@ export class DLOB {
19451981 * @param param.oraclePriceData the oracle price data
19461982 * @returns the estimated quote amount filled: QUOTE_PRECISION
19471983 */
1948- public estimateFillWithExactBaseAmount ( {
1984+ public estimateFillWithExactBaseAmount < T extends MarketType > ( {
19491985 marketIndex,
19501986 marketType,
19511987 baseAmount,
@@ -1954,11 +1990,13 @@ export class DLOB {
19541990 oraclePriceData,
19551991 } : {
19561992 marketIndex : number ;
1957- marketType : MarketType ;
1993+ marketType : T ;
19581994 baseAmount : BN ;
19591995 orderDirection : PositionDirection ;
19601996 slot : number ;
1961- oraclePriceData : OraclePriceData ;
1997+ oraclePriceData : T extends { spot : unknown }
1998+ ? OraclePriceData
1999+ : MMOraclePriceData ;
19622000 } ) : BN {
19632001 if ( isVariant ( orderDirection , 'long' ) ) {
19642002 return this . estimateFillExactBaseAmountInForSide (
@@ -1977,7 +2015,7 @@ export class DLOB {
19772015 }
19782016 }
19792017
1980- public getBestMakers ( {
2018+ public getBestMakers < T extends MarketType > ( {
19812019 marketIndex,
19822020 marketType,
19832021 direction,
@@ -1986,10 +2024,12 @@ export class DLOB {
19862024 numMakers,
19872025 } : {
19882026 marketIndex : number ;
1989- marketType : MarketType ;
2027+ marketType : T ;
19902028 direction : PositionDirection ;
19912029 slot : number ;
1992- oraclePriceData : OraclePriceData ;
2030+ oraclePriceData : T extends { spot : unknown }
2031+ ? OraclePriceData
2032+ : MMOraclePriceData ;
19932033 numMakers : number ;
19942034 } ) : PublicKey [ ] {
19952035 const makers = new Map < string , PublicKey > ( ) ;
0 commit comments