@drift-labs/sdk 2.0.13 → 2.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dlob/DLOB.d.ts +8 -3
- package/lib/dlob/DLOB.js +209 -146
- package/lib/driftClient.d.ts +19 -0
- package/lib/driftClient.js +32 -0
- package/lib/math/orders.d.ts +1 -2
- package/lib/math/orders.js +4 -20
- package/package.json +1 -1
- package/src/dlob/DLOB.ts +381 -197
- package/src/driftClient.ts +41 -0
- package/src/math/orders.ts +5 -22
- package/tests/dlob/test.ts +295 -63
package/src/driftClient.ts
CHANGED
|
@@ -300,12 +300,31 @@ export class DriftClient {
|
|
|
300
300
|
return this.accountSubscriber.getStateAccountAndSlot().data;
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
+
/**
|
|
304
|
+
* Forces a fetch to rpc before returning accounts. Useful for anchor tests.
|
|
305
|
+
*/
|
|
306
|
+
public async forceGetStateAccount(): Promise<StateAccount> {
|
|
307
|
+
await this.accountSubscriber.fetch();
|
|
308
|
+
return this.accountSubscriber.getStateAccountAndSlot().data;
|
|
309
|
+
}
|
|
310
|
+
|
|
303
311
|
public getPerpMarketAccount(
|
|
304
312
|
marketIndex: number
|
|
305
313
|
): PerpMarketAccount | undefined {
|
|
306
314
|
return this.accountSubscriber.getMarketAccountAndSlot(marketIndex)?.data;
|
|
307
315
|
}
|
|
308
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Forces a fetch to rpc before returning accounts. Useful for anchor tests.
|
|
319
|
+
* @param marketIndex
|
|
320
|
+
*/
|
|
321
|
+
public async forceGetPerpMarketAccount(
|
|
322
|
+
marketIndex: number
|
|
323
|
+
): Promise<PerpMarketAccount | undefined> {
|
|
324
|
+
await this.accountSubscriber.fetch();
|
|
325
|
+
return this.accountSubscriber.getMarketAccountAndSlot(marketIndex)?.data;
|
|
326
|
+
}
|
|
327
|
+
|
|
309
328
|
public getPerpMarketAccounts(): PerpMarketAccount[] {
|
|
310
329
|
return this.accountSubscriber
|
|
311
330
|
.getMarketAccountsAndSlots()
|
|
@@ -318,6 +337,17 @@ export class DriftClient {
|
|
|
318
337
|
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
|
|
319
338
|
}
|
|
320
339
|
|
|
340
|
+
/**
|
|
341
|
+
* Forces a fetch to rpc before returning accounts. Useful for anchor tests.
|
|
342
|
+
* @param marketIndex
|
|
343
|
+
*/
|
|
344
|
+
public async forceGetSpotMarketAccount(
|
|
345
|
+
marketIndex: number
|
|
346
|
+
): Promise<SpotMarketAccount | undefined> {
|
|
347
|
+
await this.accountSubscriber.fetch();
|
|
348
|
+
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
|
|
349
|
+
}
|
|
350
|
+
|
|
321
351
|
public getSpotMarketAccounts(): SpotMarketAccount[] {
|
|
322
352
|
return this.accountSubscriber
|
|
323
353
|
.getSpotMarketAccountsAndSlots()
|
|
@@ -679,6 +709,17 @@ export class DriftClient {
|
|
|
679
709
|
return this.getUser(subAccountId).getUserAccount();
|
|
680
710
|
}
|
|
681
711
|
|
|
712
|
+
/**
|
|
713
|
+
* Forces a fetch to rpc before returning accounts. Useful for anchor tests.
|
|
714
|
+
* @param subAccountId
|
|
715
|
+
*/
|
|
716
|
+
public async forceGetUserAccount(
|
|
717
|
+
subAccountId?: number
|
|
718
|
+
): Promise<UserAccount | undefined> {
|
|
719
|
+
await this.getUser(subAccountId).fetchAccounts();
|
|
720
|
+
return this.getUser(subAccountId).getUserAccount();
|
|
721
|
+
}
|
|
722
|
+
|
|
682
723
|
public getUserAccountAndSlot(
|
|
683
724
|
subAccountId?: number
|
|
684
725
|
): DataAndSlot<UserAccount> | undefined {
|
package/src/math/orders.ts
CHANGED
|
@@ -127,8 +127,9 @@ export function standardizeBaseAssetAmount(
|
|
|
127
127
|
export function getLimitPrice(
|
|
128
128
|
order: Order,
|
|
129
129
|
oraclePriceData: OraclePriceData,
|
|
130
|
-
slot: number
|
|
131
|
-
|
|
130
|
+
slot: number,
|
|
131
|
+
fallbackPrice?: BN
|
|
132
|
+
): BN | undefined {
|
|
132
133
|
let limitPrice;
|
|
133
134
|
if (order.oraclePriceOffset !== 0) {
|
|
134
135
|
limitPrice = oraclePriceData.price.add(new BN(order.oraclePriceOffset));
|
|
@@ -138,13 +139,7 @@ export function getLimitPrice(
|
|
|
138
139
|
} else if (!order.price.eq(ZERO)) {
|
|
139
140
|
limitPrice = order.price;
|
|
140
141
|
} else {
|
|
141
|
-
|
|
142
|
-
const oraclePrice1Pct = oraclePriceData.price.div(new BN(100));
|
|
143
|
-
if (isVariant(order.direction, 'long')) {
|
|
144
|
-
limitPrice = oraclePriceData.price.add(oraclePrice1Pct);
|
|
145
|
-
} else {
|
|
146
|
-
limitPrice = oraclePriceData.price.sub(oraclePrice1Pct);
|
|
147
|
-
}
|
|
142
|
+
limitPrice = fallbackPrice;
|
|
148
143
|
}
|
|
149
144
|
} else {
|
|
150
145
|
limitPrice = order.price;
|
|
@@ -153,18 +148,6 @@ export function getLimitPrice(
|
|
|
153
148
|
return limitPrice;
|
|
154
149
|
}
|
|
155
150
|
|
|
156
|
-
export function getOptionalLimitPrice(
|
|
157
|
-
order: Order,
|
|
158
|
-
oraclePriceData: OraclePriceData,
|
|
159
|
-
slot: number
|
|
160
|
-
): BN | undefined {
|
|
161
|
-
if (hasLimitPrice(order, slot)) {
|
|
162
|
-
return getLimitPrice(order, oraclePriceData, slot);
|
|
163
|
-
} else {
|
|
164
|
-
return undefined;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
151
|
export function hasLimitPrice(order: Order, slot: number): boolean {
|
|
169
152
|
return (
|
|
170
153
|
order.price.gt(ZERO) ||
|
|
@@ -202,7 +185,7 @@ export function calculateBaseAssetAmountForAmmToFulfill(
|
|
|
202
185
|
return ZERO;
|
|
203
186
|
}
|
|
204
187
|
|
|
205
|
-
const limitPrice =
|
|
188
|
+
const limitPrice = getLimitPrice(order, oraclePriceData, slot);
|
|
206
189
|
let baseAssetAmount;
|
|
207
190
|
|
|
208
191
|
const updatedAMM = calculateUpdatedAMM(market.amm, oraclePriceData);
|