@drift-labs/sdk 0.2.0-master.29 → 0.2.0-master.30
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/README.md +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +10 -11
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +4 -4
- package/lib/accounts/types.d.ts +4 -6
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +10 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +6 -8
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +5 -5
- package/lib/addresses/pda.js +5 -5
- package/lib/admin.d.ts +27 -28
- package/lib/admin.js +12 -37
- package/lib/clearingHouse.d.ts +61 -62
- package/lib/clearingHouse.js +92 -110
- package/lib/clearingHouseConfig.d.ts +2 -4
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +16 -16
- package/lib/config.d.ts +2 -4
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +7 -8
- package/lib/constants/numericConstants.js +17 -18
- package/lib/constants/perpMarkets.d.ts +2 -3
- package/lib/constants/perpMarkets.js +3 -3
- package/lib/constants/spotMarkets.d.ts +2 -1
- package/lib/constants/spotMarkets.js +6 -4
- package/lib/dlob/DLOB.d.ts +13 -13
- package/lib/dlob/DLOB.js +36 -40
- package/lib/dlob/DLOBNode.js +2 -2
- package/lib/events/sort.js +1 -1
- package/lib/examples/makeTradeExample.js +3 -3
- package/lib/idl/clearing_house.json +359 -310
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +10 -10
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.js +9 -9
- package/lib/math/margin.js +3 -3
- package/lib/math/market.d.ts +4 -4
- package/lib/math/market.js +7 -7
- package/lib/math/oracles.js +6 -8
- package/lib/math/position.d.ts +2 -2
- package/lib/math/position.js +9 -9
- package/lib/math/repeg.js +7 -6
- package/lib/math/spotBalance.js +5 -5
- package/lib/math/spotPosition.js +2 -2
- package/lib/math/trade.d.ts +6 -6
- package/lib/math/trade.js +15 -19
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/types.d.ts +63 -51
- package/lib/types.js +1 -1
- package/package.json +2 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +15 -15
- package/src/accounts/types.ts +4 -5
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +17 -25
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +10 -10
- package/src/admin.ts +42 -79
- package/src/clearingHouse.ts +164 -212
- package/src/clearingHouseConfig.ts +2 -3
- package/src/clearingHouseUser.ts +35 -35
- package/src/config.ts +3 -4
- package/src/constants/numericConstants.ts +19 -21
- package/src/constants/perpMarkets.ts +5 -5
- package/src/constants/spotMarkets.ts +8 -5
- package/src/dlob/DLOB.ts +54 -70
- package/src/dlob/DLOBNode.ts +5 -6
- package/src/events/sort.ts +1 -1
- package/src/examples/makeTradeExample.js +2 -2
- package/src/examples/makeTradeExample.ts +5 -8
- package/src/idl/clearing_house.json +359 -310
- package/src/math/amm.ts +14 -11
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +13 -11
- package/src/math/margin.ts +4 -5
- package/src/math/market.ts +5 -5
- package/src/math/oracles.ts +9 -9
- package/src/math/position.ts +11 -19
- package/src/math/repeg.ts +8 -7
- package/src/math/spotBalance.ts +6 -6
- package/src/math/spotPosition.ts +2 -2
- package/src/math/trade.ts +17 -21
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/types.ts +69 -51
- package/tests/dlob/helpers.ts +56 -4
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/events/txEventCache.js +0 -71
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/repeg.js +0 -128
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/userName.js +0 -20
- package/src/wallet.js +0 -35
package/lib/dlob/DLOB.js
CHANGED
|
@@ -25,8 +25,8 @@ class DLOB {
|
|
|
25
25
|
this.marketIndexToAccount.set('spot', new Map());
|
|
26
26
|
for (const market of perpMarkets) {
|
|
27
27
|
const marketIndex = market.marketIndex;
|
|
28
|
-
this.marketIndexToAccount.get('perp').set(marketIndex
|
|
29
|
-
this.orderLists.get('perp').set(marketIndex
|
|
28
|
+
this.marketIndexToAccount.get('perp').set(marketIndex, market);
|
|
29
|
+
this.orderLists.get('perp').set(marketIndex, {
|
|
30
30
|
limit: {
|
|
31
31
|
ask: new NodeList_1.NodeList('limit', 'asc'),
|
|
32
32
|
bid: new NodeList_1.NodeList('limit', 'desc'),
|
|
@@ -47,8 +47,8 @@ class DLOB {
|
|
|
47
47
|
}
|
|
48
48
|
for (const market of spotMarkets) {
|
|
49
49
|
const marketIndex = market.marketIndex;
|
|
50
|
-
this.marketIndexToAccount.get('spot').set(marketIndex
|
|
51
|
-
this.orderLists.get('spot').set(marketIndex
|
|
50
|
+
this.marketIndexToAccount.get('spot').set(marketIndex, market);
|
|
51
|
+
this.orderLists.get('spot').set(marketIndex, {
|
|
52
52
|
limit: {
|
|
53
53
|
ask: new NodeList_1.NodeList('limit', 'asc'),
|
|
54
54
|
bid: new NodeList_1.NodeList('limit', 'desc'),
|
|
@@ -103,6 +103,7 @@ class DLOB {
|
|
|
103
103
|
return true;
|
|
104
104
|
}
|
|
105
105
|
insertOrder(order, userAccount, onInsert) {
|
|
106
|
+
var _a;
|
|
106
107
|
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
107
108
|
return;
|
|
108
109
|
}
|
|
@@ -112,25 +113,21 @@ class DLOB {
|
|
|
112
113
|
.get(marketType)
|
|
113
114
|
.add((0, NodeList_1.getOrderSignature)(order.orderId, userAccount));
|
|
114
115
|
}
|
|
115
|
-
this.getListForOrder(order).insert(order, marketType, this.marketIndexToAccount
|
|
116
|
-
.get(marketType)
|
|
117
|
-
.get(order.marketIndex.toNumber()), userAccount);
|
|
116
|
+
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, this.marketIndexToAccount.get(marketType).get(order.marketIndex), userAccount);
|
|
118
117
|
if (onInsert) {
|
|
119
118
|
onInsert();
|
|
120
119
|
}
|
|
121
120
|
}
|
|
122
121
|
trigger(order, userAccount, onTrigger) {
|
|
122
|
+
var _a;
|
|
123
123
|
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
124
124
|
return;
|
|
125
125
|
}
|
|
126
126
|
const marketType = (0, __1.getVariant)(order.marketType);
|
|
127
|
-
const triggerList = this.orderLists
|
|
128
|
-
.
|
|
129
|
-
.get(order.marketIndex.toNumber()).trigger[(0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below'];
|
|
127
|
+
const triggerList = this.orderLists.get(marketType).get(order.marketIndex)
|
|
128
|
+
.trigger[(0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below'];
|
|
130
129
|
triggerList.remove(order, userAccount);
|
|
131
|
-
this.getListForOrder(order).insert(order, marketType, this.marketIndexToAccount
|
|
132
|
-
.get(marketType)
|
|
133
|
-
.get(order.marketIndex.toNumber()), userAccount);
|
|
130
|
+
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, this.marketIndexToAccount.get(marketType).get(order.marketIndex), userAccount);
|
|
134
131
|
if (onTrigger) {
|
|
135
132
|
onTrigger();
|
|
136
133
|
}
|
|
@@ -159,7 +156,10 @@ class DLOB {
|
|
|
159
156
|
subType = (0, __1.isVariant)(order.direction, 'long') ? 'bid' : 'ask';
|
|
160
157
|
}
|
|
161
158
|
const marketType = (0, __1.getVariant)(order.marketType);
|
|
162
|
-
|
|
159
|
+
if (!this.orderLists.has(marketType)) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
return this.orderLists.get(marketType).get(order.marketIndex)[type][subType];
|
|
163
163
|
}
|
|
164
164
|
findNodesToFill(marketIndex, vBid, vAsk, slot, marketType, oraclePriceData) {
|
|
165
165
|
// Find all the crossing nodes
|
|
@@ -244,14 +244,14 @@ class DLOB {
|
|
|
244
244
|
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
245
245
|
return this.orderLists
|
|
246
246
|
.get(marketTypeStr)
|
|
247
|
-
.get(marketIndex
|
|
247
|
+
.get(marketIndex)
|
|
248
248
|
.market.bid.getGenerator();
|
|
249
249
|
}
|
|
250
250
|
getMarketAsks(marketIndex, marketType) {
|
|
251
251
|
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
252
252
|
return this.orderLists
|
|
253
253
|
.get(marketTypeStr)
|
|
254
|
-
.get(marketIndex
|
|
254
|
+
.get(marketIndex)
|
|
255
255
|
.market.ask.getGenerator();
|
|
256
256
|
}
|
|
257
257
|
*getAsks(marketIndex, vAsk, slot, marketType, oraclePriceData) {
|
|
@@ -259,9 +259,7 @@ class DLOB {
|
|
|
259
259
|
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
260
260
|
}
|
|
261
261
|
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
262
|
-
const nodeLists = this.orderLists
|
|
263
|
-
.get(marketTypeStr)
|
|
264
|
-
.get(marketIndex.toNumber());
|
|
262
|
+
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
265
263
|
const generatorList = [
|
|
266
264
|
nodeLists.limit.ask.getGenerator(),
|
|
267
265
|
nodeLists.floatingLimit.ask.getGenerator(),
|
|
@@ -305,9 +303,7 @@ class DLOB {
|
|
|
305
303
|
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
306
304
|
}
|
|
307
305
|
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
308
|
-
const nodeLists = this.orderLists
|
|
309
|
-
.get(marketTypeStr)
|
|
310
|
-
.get(marketIndex.toNumber());
|
|
306
|
+
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
311
307
|
const generatorList = [
|
|
312
308
|
nodeLists.limit.bid.getGenerator(),
|
|
313
309
|
nodeLists.floatingLimit.bid.getGenerator(),
|
|
@@ -480,7 +476,7 @@ class DLOB {
|
|
|
480
476
|
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
481
477
|
for (const node of this.orderLists
|
|
482
478
|
.get(marketTypeStr)
|
|
483
|
-
.get(marketIndex
|
|
479
|
+
.get(marketIndex)
|
|
484
480
|
.trigger.above.getGenerator()) {
|
|
485
481
|
if (oraclePrice.gt(node.order.triggerPrice)) {
|
|
486
482
|
if ((0, __1.isAuctionComplete)(node.order, slot)) {
|
|
@@ -495,7 +491,7 @@ class DLOB {
|
|
|
495
491
|
}
|
|
496
492
|
for (const node of this.orderLists
|
|
497
493
|
.get(marketTypeStr)
|
|
498
|
-
.get(marketIndex
|
|
494
|
+
.get(marketIndex)
|
|
499
495
|
.trigger.below.getGenerator()) {
|
|
500
496
|
if (oraclePrice.lt(node.order.triggerPrice)) {
|
|
501
497
|
if ((0, __1.isAuctionComplete)(node.order, slot)) {
|
|
@@ -520,18 +516,18 @@ class DLOB {
|
|
|
520
516
|
const bestAsk = this.getBestAsk(marketIndex, vAsk, slot, marketType, oraclePriceData);
|
|
521
517
|
const bestBid = this.getBestBid(marketIndex, vBid, slot, marketType, oraclePriceData);
|
|
522
518
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
523
|
-
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.
|
|
524
|
-
(0, __1.convertToNumber)(oraclePriceData.price, __1.
|
|
519
|
+
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
|
|
520
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
525
521
|
1) *
|
|
526
522
|
100.0;
|
|
527
|
-
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.
|
|
528
|
-
(0, __1.convertToNumber)(oraclePriceData.price, __1.
|
|
523
|
+
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
|
|
524
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
529
525
|
1) *
|
|
530
526
|
100.0;
|
|
531
|
-
console.log(`Market ${sdkConfig.MARKETS[marketIndex
|
|
532
|
-
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.
|
|
533
|
-
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.
|
|
534
|
-
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.
|
|
527
|
+
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
528
|
+
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
529
|
+
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
530
|
+
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
535
531
|
}
|
|
536
532
|
else if ((0, __1.isVariant)(marketType, 'spot')) {
|
|
537
533
|
const slot = slotSubscriber.getSlot();
|
|
@@ -539,18 +535,18 @@ class DLOB {
|
|
|
539
535
|
const bestAsk = this.getBestAsk(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
540
536
|
const bestBid = this.getBestBid(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
541
537
|
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
542
|
-
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.
|
|
543
|
-
(0, __1.convertToNumber)(oraclePriceData.price, __1.
|
|
538
|
+
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
|
|
539
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
544
540
|
1) *
|
|
545
541
|
100.0;
|
|
546
|
-
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.
|
|
547
|
-
(0, __1.convertToNumber)(oraclePriceData.price, __1.
|
|
542
|
+
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
|
|
543
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
548
544
|
1) *
|
|
549
545
|
100.0;
|
|
550
|
-
console.log(`Market ${sdkConfig.MARKETS[marketIndex
|
|
551
|
-
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.
|
|
552
|
-
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.
|
|
553
|
-
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.
|
|
546
|
+
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
547
|
+
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
548
|
+
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
549
|
+
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
554
550
|
}
|
|
555
551
|
}
|
|
556
552
|
}
|
package/lib/dlob/DLOBNode.js
CHANGED
|
@@ -17,11 +17,11 @@ class OrderNode {
|
|
|
17
17
|
msg += ` ${(0, __1.isVariant)(this.order.direction, 'long') ? 'LONG' : 'SHORT'} `;
|
|
18
18
|
msg += `${(0, __1.convertToNumber)(this.order.baseAssetAmount, __1.AMM_RESERVE_PRECISION).toFixed(3)}`;
|
|
19
19
|
if (this.order.price.gt(__1.ZERO)) {
|
|
20
|
-
msg += ` @ ${(0, __1.convertToNumber)(this.order.price, __1.
|
|
20
|
+
msg += ` @ ${(0, __1.convertToNumber)(this.order.price, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
21
21
|
}
|
|
22
22
|
if (this.order.triggerPrice.gt(__1.ZERO)) {
|
|
23
23
|
msg += ` ${(0, __1.isVariant)(this.order.triggerCondition, 'below') ? 'BELOW' : 'ABOVE'}`;
|
|
24
|
-
msg += ` ${(0, __1.convertToNumber)(this.order.triggerPrice, __1.
|
|
24
|
+
msg += ` ${(0, __1.convertToNumber)(this.order.triggerPrice, __1.PRICE_PRECISION).toFixed(3)}`;
|
|
25
25
|
}
|
|
26
26
|
return msg;
|
|
27
27
|
}
|
package/lib/events/sort.js
CHANGED
|
@@ -15,7 +15,7 @@ function orderActionRecordSortFn(currentEvent, newEvent) {
|
|
|
15
15
|
var _a, _b;
|
|
16
16
|
const currentEventMarketIndex = currentEvent.marketIndex;
|
|
17
17
|
const newEventMarketIndex = newEvent.marketIndex;
|
|
18
|
-
if (
|
|
18
|
+
if (currentEventMarketIndex !== newEventMarketIndex) {
|
|
19
19
|
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
20
20
|
}
|
|
21
21
|
if (((_a = currentEvent.fillRecordId) === null || _a === void 0 ? void 0 : _a.gt(index_1.ZERO)) && ((_b = newEvent.fillRecordId) === null || _b === void 0 ? void 0 : _b.gt(index_1.ZERO))) {
|
|
@@ -63,13 +63,13 @@ const main = async () => {
|
|
|
63
63
|
await user.subscribe();
|
|
64
64
|
// Get current price
|
|
65
65
|
const solMarketInfo = sdkConfig.PERP_MARKETS.find((market) => market.baseAssetSymbol === 'SOL');
|
|
66
|
-
const currentMarketPrice = (0, __2.
|
|
67
|
-
const formattedPrice = (0, __2.convertToNumber)(currentMarketPrice, __2.
|
|
66
|
+
const currentMarketPrice = (0, __2.calculateReservePrice)(clearingHouse.getPerpMarketAccount(solMarketInfo.marketIndex), undefined);
|
|
67
|
+
const formattedPrice = (0, __2.convertToNumber)(currentMarketPrice, __2.PRICE_PRECISION);
|
|
68
68
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
69
69
|
// Estimate the slippage for a $5000 LONG trade
|
|
70
70
|
const solMarketAccount = clearingHouse.getPerpMarketAccount(solMarketInfo.marketIndex);
|
|
71
71
|
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
72
|
-
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote', undefined)[0], __2.
|
|
72
|
+
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote', undefined)[0], __2.PRICE_PRECISION);
|
|
73
73
|
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|
|
74
74
|
// Make a $5000 LONG trade
|
|
75
75
|
await clearingHouse.openPosition(__2.PositionDirection.LONG, longAmount, solMarketInfo.marketIndex);
|