Skip to content

Commit bd479be

Browse files
moosecat20xbigz
authored andcommitted
mm oracle sdk additions (#1824)
* strict typing for more MM oracle contact points * add comments to auction.ts * prettify
1 parent cc88d50 commit bd479be

File tree

6 files changed

+60
-23
lines changed

6 files changed

+60
-23
lines changed

sdk/src/dlob/DLOB.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,15 +1096,15 @@ export class DLOB {
10961096
}
10971097
}
10981098

1099-
protected *getBestNode(
1099+
protected *getBestNode<T extends MarketTypeStr>(
11001100
generatorList: Array<Generator<DLOBNode>>,
1101-
oraclePriceData: OraclePriceData,
1101+
oraclePriceData: T extends 'spot' ? OraclePriceData : MMOraclePriceData,
11021102
slot: number,
11031103
compareFcn: (
11041104
bestDLOBNode: DLOBNode,
11051105
currentDLOBNode: DLOBNode,
11061106
slot: number,
1107-
oraclePriceData: OraclePriceData
1107+
oraclePriceData: T extends 'spot' ? OraclePriceData : MMOraclePriceData
11081108
) => boolean,
11091109
filterFcn?: DLOBFilterFcn
11101110
): Generator<DLOBNode> {

sdk/src/dlob/DLOBNode.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import {
55
ZERO,
66
} from '../constants/numericConstants';
77
import { getLimitPrice } from '../math/orders';
8-
import { isVariant, Order, ProtectedMakerParams } from '../types';
9-
import { OraclePriceData } from '../oracles/types';
8+
import {
9+
isVariant,
10+
MarketTypeStr,
11+
Order,
12+
ProtectedMakerParams,
13+
} from '../types';
14+
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
1015
import { convertToNumber } from '../math/conversion';
1116
import { getOrderSignature } from './NodeList';
1217

@@ -81,8 +86,11 @@ export abstract class OrderNode implements DLOBNode {
8186
return msg;
8287
}
8388

84-
getPrice(oraclePriceData: OraclePriceData, slot: number): BN {
85-
return getLimitPrice(
89+
getPrice<T extends MarketTypeStr>(
90+
oraclePriceData: T extends 'spot' ? OraclePriceData : MMOraclePriceData,
91+
slot: number
92+
): BN {
93+
return getLimitPrice<T>(
8694
this.order,
8795
oraclePriceData,
8896
slot,

sdk/src/driftClient.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,21 +1590,26 @@ export class DriftClient {
15901590
public async getUpdateUserDelegateIx(
15911591
delegate: PublicKey,
15921592
overrides: {
1593-
subAccountId?: number,
1594-
userAccountPublicKey?: PublicKey,
1595-
authority?: PublicKey,
1593+
subAccountId?: number;
1594+
userAccountPublicKey?: PublicKey;
1595+
authority?: PublicKey;
15961596
}
15971597
): Promise<TransactionInstruction> {
15981598
const subAccountId = overrides.subAccountId ?? this.activeSubAccountId;
1599-
const userAccountPublicKey = overrides.userAccountPublicKey ?? await this.getUserAccountPublicKey();
1599+
const userAccountPublicKey =
1600+
overrides.userAccountPublicKey ?? (await this.getUserAccountPublicKey());
16001601
const authority = overrides.authority ?? this.wallet.publicKey;
16011602

1602-
return await this.program.instruction.updateUserDelegate(subAccountId, delegate, {
1603-
accounts: {
1604-
user: userAccountPublicKey,
1605-
authority,
1606-
},
1607-
});
1603+
return await this.program.instruction.updateUserDelegate(
1604+
subAccountId,
1605+
delegate,
1606+
{
1607+
accounts: {
1608+
user: userAccountPublicKey,
1609+
authority,
1610+
},
1611+
}
1612+
);
16081613
}
16091614

16101615
public async updateUserDelegate(

sdk/src/math/auction.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export function isFallbackAvailableLiquiditySource(
2929
return new BN(slot).sub(order.slot).gt(new BN(minAuctionDuration));
3030
}
3131

32+
/**
33+
*
34+
* @param order
35+
* @param slot
36+
* @param oraclePrice Use MMOraclePriceData source for perp orders, OraclePriceData for spot
37+
* @returns BN
38+
*/
3239
export function getAuctionPrice(
3340
order: Order,
3441
slot: number,
@@ -92,6 +99,13 @@ export function getAuctionPriceForFixedAuction(order: Order, slot: number): BN {
9299
return price;
93100
}
94101

102+
/**
103+
*
104+
* @param order
105+
* @param slot
106+
* @param oraclePrice Use MMOraclePriceData source for perp orders, OraclePriceData for spot
107+
* @returns
108+
*/
95109
export function getAuctionPriceForOracleOffsetAuction(
96110
order: Order,
97111
slot: number,
@@ -186,6 +200,11 @@ export function deriveOracleAuctionParams({
186200
};
187201
}
188202

203+
/**
204+
*
205+
* @param params Use OraclePriceData.price for oraclePrice param
206+
* @returns
207+
*/
189208
export function getTriggerAuctionStartPrice(params: {
190209
perpMarket: PerpMarketAccount;
191210
direction: PositionDirection;

sdk/src/math/orders.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Order,
88
PositionDirection,
99
ProtectedMakerParams,
10+
MarketTypeStr,
1011
} from '../types';
1112
import { ZERO, TWO, ONE } from '../constants/numericConstants';
1213
import { BN } from '@coral-xyz/anchor';
@@ -151,9 +152,9 @@ export function standardizePrice(
151152
}
152153
}
153154

154-
export function getLimitPrice(
155+
export function getLimitPrice<T extends MarketTypeStr>(
155156
order: Order,
156-
oraclePriceData: OraclePriceData | MMOraclePriceData,
157+
oraclePriceData: T extends 'spot' ? OraclePriceData : MMOraclePriceData,
157158
slot: number,
158159
fallbackPrice?: BN,
159160
protectedMakerParams?: ProtectedMakerParams

sdk/src/user.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ export class User {
671671
)[0];
672672

673673
const perpMarket = this.driftClient.getPerpMarketAccount(marketIndex);
674-
const oraclePriceData = this.getMMOracleDataForPerpMarket(marketIndex);
674+
const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
675675
const worstCaseBaseAssetAmount = perpPosition
676676
? calculateWorstCaseBaseAssetAmount(
677677
perpPosition,
@@ -1447,7 +1447,7 @@ export class User {
14471447
)[0];
14481448
}
14491449

1450-
let valuationPrice = this.getMMOracleDataForPerpMarket(
1450+
let valuationPrice = this.getOracleDataForPerpMarket(
14511451
market.marketIndex
14521452
).price;
14531453

@@ -3426,7 +3426,7 @@ export class User {
34263426
this.getEmptyPosition(targetMarketIndex);
34273427

34283428
const perpMarket = this.driftClient.getPerpMarketAccount(targetMarketIndex);
3429-
const oracleData = this.getMMOracleDataForPerpMarket(targetMarketIndex);
3429+
const oracleData = this.getOracleDataForPerpMarket(targetMarketIndex);
34303430

34313431
let {
34323432
// eslint-disable-next-line prefer-const
@@ -4106,7 +4106,7 @@ export class User {
41064106
!!marginCategory
41074107
)[0] || this.getEmptyPosition(marketToIgnore);
41084108

4109-
const oracleData = this.getMMOracleDataForPerpMarket(marketToIgnore);
4109+
const oracleData = this.getOracleDataForPerpMarket(marketToIgnore);
41104110

41114111
let currentPerpPositionValueUSDC = ZERO;
41124112
if (currentPerpPosition) {
@@ -4128,6 +4128,10 @@ export class User {
41284128
return this.driftClient.getMMOracleDataForPerpMarket(marketIndex);
41294129
}
41304130

4131+
private getOracleDataForPerpMarket(marketIndex: number): OraclePriceData {
4132+
return this.driftClient.getOracleDataForPerpMarket(marketIndex);
4133+
}
4134+
41314135
private getOracleDataForSpotMarket(marketIndex: number): OraclePriceData {
41324136
return this.driftClient.getOracleDataForSpotMarket(marketIndex);
41334137
}

0 commit comments

Comments
 (0)