@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.27
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/accounts/pollingClearingHouseAccountSubscriber.d.ts +14 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +30 -27
- package/lib/accounts/types.d.ts +9 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +15 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +38 -34
- package/lib/addresses/pda.d.ts +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +13 -7
- package/lib/admin.js +111 -44
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +767 -278
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +24 -22
- package/lib/clearingHouseUser.js +273 -177
- package/lib/config.d.ts +7 -7
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/lib/constants/{markets.js → perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +557 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +7 -7
- package/lib/idl/clearing_house.json +1152 -503
- package/lib/index.d.ts +10 -3
- package/lib/index.js +10 -3
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +11 -10
- package/lib/math/market.js +30 -7
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +6 -6
- package/lib/math/orders.js +31 -16
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +193 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +148 -57
- package/lib/types.js +39 -11
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/package.json +6 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +42 -38
- package/src/accounts/types.ts +12 -9
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +65 -52
- package/src/addresses/pda.ts +49 -44
- package/src/admin.ts +190 -55
- package/src/clearingHouse.ts +1092 -365
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.ts +518 -255
- package/src/config.ts +30 -30
- package/src/constants/numericConstants.ts +17 -15
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/dlob/DLOB.ts +884 -0
- package/src/dlob/DLOBNode.ts +163 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/events/types.ts +3 -0
- package/src/examples/makeTradeExample.js +152 -75
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/idl/clearing_house.json +1152 -503
- package/src/index.ts +10 -3
- package/src/math/amm.ts +6 -3
- package/src/math/funding.ts +7 -7
- package/src/math/margin.ts +34 -23
- package/src/math/market.ts +72 -20
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.ts +33 -25
- package/src/math/position.ts +31 -31
- package/src/math/spotBalance.ts +316 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.ts +4 -4
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.ts +2 -1
- package/src/types.ts +135 -56
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/tests/bn/test.ts +2 -3
- package/tests/dlob/helpers.ts +322 -0
- package/tests/dlob/test.ts +2865 -0
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/constants/numericConstants.js +0 -41
- package/src/math/bankBalance.ts +0 -258
- package/src/math/oracles.js +0 -26
- package/src/math/state.js +0 -15
- package/src/orderParams.js +0 -20
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/tokenFaucet.js +0 -189
package/lib/types.d.ts
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
3
3
|
import { BN } from '.';
|
|
4
|
+
export declare class MarketStatus {
|
|
5
|
+
static readonly INITIALIZED: {
|
|
6
|
+
initialized: {};
|
|
7
|
+
};
|
|
8
|
+
static readonly REDUCEONLY: {
|
|
9
|
+
reduceonly: {};
|
|
10
|
+
};
|
|
11
|
+
static readonly SETTLEMENT: {
|
|
12
|
+
settlement: {};
|
|
13
|
+
};
|
|
14
|
+
static readonly DELISTED: {
|
|
15
|
+
delisted: {};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export declare class ContractType {
|
|
19
|
+
static readonly PERPETUAL: {
|
|
20
|
+
perpetual: {};
|
|
21
|
+
};
|
|
22
|
+
static readonly FUTURE: {
|
|
23
|
+
future: {};
|
|
24
|
+
};
|
|
25
|
+
}
|
|
4
26
|
export declare class SwapDirection {
|
|
5
27
|
static readonly ADD: {
|
|
6
28
|
add: {};
|
|
@@ -9,7 +31,7 @@ export declare class SwapDirection {
|
|
|
9
31
|
remove: {};
|
|
10
32
|
};
|
|
11
33
|
}
|
|
12
|
-
export declare class
|
|
34
|
+
export declare class SpotBalanceType {
|
|
13
35
|
static readonly DEPOSIT: {
|
|
14
36
|
deposit: {};
|
|
15
37
|
};
|
|
@@ -58,6 +80,15 @@ export declare class OrderType {
|
|
|
58
80
|
market: {};
|
|
59
81
|
};
|
|
60
82
|
}
|
|
83
|
+
export declare type MarketTypeStr = 'perp' | 'spot';
|
|
84
|
+
export declare class MarketType {
|
|
85
|
+
static readonly SPOT: {
|
|
86
|
+
spot: {};
|
|
87
|
+
};
|
|
88
|
+
static readonly PERP: {
|
|
89
|
+
perp: {};
|
|
90
|
+
};
|
|
91
|
+
}
|
|
61
92
|
export declare class OrderStatus {
|
|
62
93
|
static readonly INIT: {
|
|
63
94
|
init: {};
|
|
@@ -125,8 +156,22 @@ export declare class OrderTriggerCondition {
|
|
|
125
156
|
below: {};
|
|
126
157
|
};
|
|
127
158
|
}
|
|
159
|
+
export declare class SpotFulfillmentType {
|
|
160
|
+
static readonly SERUM_v3: {
|
|
161
|
+
serumV3: {};
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
export declare class SpotFulfillmentStatus {
|
|
165
|
+
static readonly ENABLED: {
|
|
166
|
+
enabled: {};
|
|
167
|
+
};
|
|
168
|
+
static readonly DISABLED: {
|
|
169
|
+
disabled: {};
|
|
170
|
+
};
|
|
171
|
+
}
|
|
128
172
|
export declare function isVariant(object: unknown, type: string): boolean;
|
|
129
173
|
export declare function isOneOfVariant(object: unknown, types: string[]): boolean;
|
|
174
|
+
export declare function getVariant(object: unknown): string;
|
|
130
175
|
export declare enum TradeSide {
|
|
131
176
|
None = 0,
|
|
132
177
|
Buy = 1,
|
|
@@ -149,7 +194,7 @@ export declare type DepositRecord = {
|
|
|
149
194
|
deposit?: any;
|
|
150
195
|
withdraw?: any;
|
|
151
196
|
};
|
|
152
|
-
|
|
197
|
+
marketIndex: BN;
|
|
153
198
|
amount: BN;
|
|
154
199
|
oraclePrice: BN;
|
|
155
200
|
referrer: PublicKey;
|
|
@@ -175,6 +220,18 @@ export declare type CurveRecord = {
|
|
|
175
220
|
oraclePrice: BN;
|
|
176
221
|
tradeId: BN;
|
|
177
222
|
};
|
|
223
|
+
export declare type InsuranceFundRecord = {
|
|
224
|
+
ts: BN;
|
|
225
|
+
bankIndex: BN;
|
|
226
|
+
marketIndex: BN;
|
|
227
|
+
userIfFactor: BN;
|
|
228
|
+
totalIfFactor: BN;
|
|
229
|
+
vaultAmountBefore: BN;
|
|
230
|
+
insuranceVaultAmountBefore: BN;
|
|
231
|
+
amount: BN;
|
|
232
|
+
totalIfSharesBefore: BN;
|
|
233
|
+
totalIfSharesAfter: BN;
|
|
234
|
+
};
|
|
178
235
|
export declare type LPRecord = {
|
|
179
236
|
ts: BN;
|
|
180
237
|
user: PublicKey;
|
|
@@ -231,6 +288,7 @@ export declare type LiquidationRecord = {
|
|
|
231
288
|
marginRequirement: BN;
|
|
232
289
|
totalCollateral: BN;
|
|
233
290
|
liquidationId: number;
|
|
291
|
+
canceledOrderIds: BN[];
|
|
234
292
|
liquidatePerp: LiquidatePerpRecord;
|
|
235
293
|
liquidateBorrow: LiquidateBorrowRecord;
|
|
236
294
|
liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
|
|
@@ -260,39 +318,39 @@ export declare class LiquidationType {
|
|
|
260
318
|
}
|
|
261
319
|
export declare type LiquidatePerpRecord = {
|
|
262
320
|
marketIndex: BN;
|
|
263
|
-
orderIds: BN[];
|
|
264
321
|
oraclePrice: BN;
|
|
265
322
|
baseAssetAmount: BN;
|
|
266
323
|
quoteAssetAmount: BN;
|
|
267
324
|
lpShares: BN;
|
|
268
325
|
userPnl: BN;
|
|
269
326
|
liquidatorPnl: BN;
|
|
270
|
-
canceledOrdersFee: BN;
|
|
271
327
|
userOrderId: BN;
|
|
272
328
|
liquidatorOrderId: BN;
|
|
273
329
|
fillRecordId: BN;
|
|
330
|
+
ifFee: BN;
|
|
274
331
|
};
|
|
275
332
|
export declare type LiquidateBorrowRecord = {
|
|
276
|
-
|
|
333
|
+
assetMarketIndex: BN;
|
|
277
334
|
assetPrice: BN;
|
|
278
335
|
assetTransfer: BN;
|
|
279
|
-
|
|
336
|
+
liabilityMarketIndex: BN;
|
|
280
337
|
liabilityPrice: BN;
|
|
281
338
|
liabilityTransfer: BN;
|
|
339
|
+
ifFee: BN;
|
|
282
340
|
};
|
|
283
341
|
export declare type LiquidateBorrowForPerpPnlRecord = {
|
|
284
|
-
|
|
342
|
+
perpMarketIndex: BN;
|
|
285
343
|
marketOraclePrice: BN;
|
|
286
344
|
pnlTransfer: BN;
|
|
287
|
-
|
|
345
|
+
liabilityMarketIndex: BN;
|
|
288
346
|
liabilityPrice: BN;
|
|
289
347
|
liabilityTransfer: BN;
|
|
290
348
|
};
|
|
291
349
|
export declare type LiquidatePerpPnlForDepositRecord = {
|
|
292
|
-
|
|
350
|
+
perpMarketIndex: BN;
|
|
293
351
|
marketOraclePrice: BN;
|
|
294
352
|
pnlTransfer: BN;
|
|
295
|
-
|
|
353
|
+
assetMarketIndex: BN;
|
|
296
354
|
assetPrice: BN;
|
|
297
355
|
assetTransfer: BN;
|
|
298
356
|
};
|
|
@@ -302,7 +360,7 @@ export declare type PerpBankruptcyRecord = {
|
|
|
302
360
|
cumulativeFundingRateDelta: BN;
|
|
303
361
|
};
|
|
304
362
|
export declare type BorrowBankruptcyRecord = {
|
|
305
|
-
|
|
363
|
+
marketIndex: BN;
|
|
306
364
|
borrowAmount: BN;
|
|
307
365
|
cumulativeDepositInterestDelta: BN;
|
|
308
366
|
};
|
|
@@ -313,7 +371,7 @@ export declare type SettlePnlRecord = {
|
|
|
313
371
|
pnl: BN;
|
|
314
372
|
baseAssetAmount: BN;
|
|
315
373
|
quoteAssetAmountAfter: BN;
|
|
316
|
-
|
|
374
|
+
quoteEntryAmount: BN;
|
|
317
375
|
settlePrice: BN;
|
|
318
376
|
};
|
|
319
377
|
export declare type OrderRecord = {
|
|
@@ -341,12 +399,14 @@ export declare type OrderActionRecord = {
|
|
|
341
399
|
quoteAssetAmountSurplus: BN | null;
|
|
342
400
|
taker: PublicKey | null;
|
|
343
401
|
takerOrderId: BN | null;
|
|
402
|
+
takerOrderDirection: PositionDirection | null;
|
|
344
403
|
takerOrderBaseAssetAmount: BN | null;
|
|
345
404
|
takerOrderBaseAssetAmountFilled: BN | null;
|
|
346
405
|
takerOrderQuoteAssetAmountFilled: BN | null;
|
|
347
406
|
takerOrderFee: BN | null;
|
|
348
407
|
maker: PublicKey | null;
|
|
349
408
|
makerOrderId: BN | null;
|
|
409
|
+
makerOrderDirection: PositionDirection | null;
|
|
350
410
|
makerOrderBaseAssetAmount: BN | null;
|
|
351
411
|
makerOrderBaseAssetAmountFilled: BN | null;
|
|
352
412
|
makerOrderQuoteAssetAmountFilled: BN | null;
|
|
@@ -359,18 +419,8 @@ export declare type StateAccount = {
|
|
|
359
419
|
exchangePaused: boolean;
|
|
360
420
|
adminControlsPrices: boolean;
|
|
361
421
|
insuranceVault: PublicKey;
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
marginRatioPartial: BN;
|
|
365
|
-
partialLiquidationClosePercentageNumerator: BN;
|
|
366
|
-
partialLiquidationClosePercentageDenominator: BN;
|
|
367
|
-
partialLiquidationPenaltyPercentageNumerator: BN;
|
|
368
|
-
partialLiquidationPenaltyPercentageDenominator: BN;
|
|
369
|
-
fullLiquidationPenaltyPercentageNumerator: BN;
|
|
370
|
-
fullLiquidationPenaltyPercentageDenominator: BN;
|
|
371
|
-
partialLiquidationLiquidatorShareDenominator: BN;
|
|
372
|
-
fullLiquidationLiquidatorShareDenominator: BN;
|
|
373
|
-
feeStructure: FeeStructure;
|
|
422
|
+
perpFeeStructure: FeeStructure;
|
|
423
|
+
spotFeeStructure: FeeStructure;
|
|
374
424
|
totalFee: BN;
|
|
375
425
|
totalFeeWithdrawn: BN;
|
|
376
426
|
whitelistMint: PublicKey;
|
|
@@ -378,15 +428,20 @@ export declare type StateAccount = {
|
|
|
378
428
|
oracleGuardRails: OracleGuardRails;
|
|
379
429
|
maxDeposit: BN;
|
|
380
430
|
numberOfMarkets: BN;
|
|
381
|
-
|
|
431
|
+
numberOfSpotMarkets: BN;
|
|
382
432
|
minOrderQuoteAssetAmount: BN;
|
|
383
433
|
signer: PublicKey;
|
|
384
434
|
signerNonce: number;
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
435
|
+
defaultMarketOrderTimeInForce: number;
|
|
436
|
+
minPerpAuctionDuration: number;
|
|
437
|
+
defaultSpotAuctionDuration: number;
|
|
438
|
+
liquidationMarginBufferRatio: number;
|
|
439
|
+
};
|
|
440
|
+
export declare type PerpMarketAccount = {
|
|
441
|
+
status: MarketStatus;
|
|
442
|
+
contractType: ContractType;
|
|
443
|
+
expiryTs: BN;
|
|
444
|
+
settlementPrice: BN;
|
|
390
445
|
marketIndex: BN;
|
|
391
446
|
pubkey: PublicKey;
|
|
392
447
|
amm: AMM;
|
|
@@ -398,14 +453,21 @@ export declare type MarketAccount = {
|
|
|
398
453
|
marginRatioMaintenance: number;
|
|
399
454
|
nextFillRecordId: BN;
|
|
400
455
|
pnlPool: PoolBalance;
|
|
401
|
-
|
|
456
|
+
liquidatorFee: BN;
|
|
457
|
+
ifLiquidationFee: BN;
|
|
402
458
|
imfFactor: BN;
|
|
403
459
|
unrealizedImfFactor: BN;
|
|
460
|
+
unrealizedMaxImbalance: BN;
|
|
404
461
|
unrealizedInitialAssetWeight: number;
|
|
405
462
|
unrealizedMaintenanceAssetWeight: number;
|
|
463
|
+
revenueWithdrawSinceLastSettle: BN;
|
|
464
|
+
maxRevenueWithdrawPerPeriod: BN;
|
|
465
|
+
lastRevenueWithdrawTs: BN;
|
|
466
|
+
quoteSettledInsurance: BN;
|
|
467
|
+
quoteMaxInsurance: BN;
|
|
406
468
|
};
|
|
407
|
-
export declare type
|
|
408
|
-
|
|
469
|
+
export declare type SpotMarketAccount = {
|
|
470
|
+
marketIndex: BN;
|
|
409
471
|
pubkey: PublicKey;
|
|
410
472
|
mint: PublicKey;
|
|
411
473
|
vault: PublicKey;
|
|
@@ -416,7 +478,7 @@ export declare type BankAccount = {
|
|
|
416
478
|
userIfShares: BN;
|
|
417
479
|
userIfFactor: BN;
|
|
418
480
|
totalIfFactor: BN;
|
|
419
|
-
|
|
481
|
+
ifLiquidationFee: BN;
|
|
420
482
|
decimals: number;
|
|
421
483
|
optimalUtilization: BN;
|
|
422
484
|
optimalBorrowRate: BN;
|
|
@@ -432,12 +494,18 @@ export declare type BankAccount = {
|
|
|
432
494
|
maintenanceAssetWeight: BN;
|
|
433
495
|
initialLiabilityWeight: BN;
|
|
434
496
|
maintenanceLiabilityWeight: BN;
|
|
435
|
-
|
|
497
|
+
liquidatorFee: BN;
|
|
436
498
|
imfFactor: BN;
|
|
437
499
|
withdrawGuardThreshold: BN;
|
|
438
500
|
depositTokenTwap: BN;
|
|
439
501
|
borrowTokenTwap: BN;
|
|
440
502
|
utilizationTwap: BN;
|
|
503
|
+
orderStepSize: BN;
|
|
504
|
+
nextFillRecordId: BN;
|
|
505
|
+
spotFeePool: {
|
|
506
|
+
balance: BN;
|
|
507
|
+
};
|
|
508
|
+
totalSpotFee: BN;
|
|
441
509
|
};
|
|
442
510
|
export declare type PoolBalance = {
|
|
443
511
|
balance: BN;
|
|
@@ -490,19 +558,22 @@ export declare type AMM = {
|
|
|
490
558
|
totalMmFee: BN;
|
|
491
559
|
netRevenueSinceLastFunding: BN;
|
|
492
560
|
lastUpdateSlot: BN;
|
|
561
|
+
lastOracleValid: boolean;
|
|
493
562
|
lastBidPriceTwap: BN;
|
|
494
563
|
lastAskPriceTwap: BN;
|
|
495
564
|
longSpread: BN;
|
|
496
565
|
shortSpread: BN;
|
|
497
566
|
maxSpread: number;
|
|
498
|
-
marketPosition:
|
|
499
|
-
marketPositionPerLp:
|
|
567
|
+
marketPosition: PerpPosition;
|
|
568
|
+
marketPositionPerLp: PerpPosition;
|
|
500
569
|
ammJitIntensity: number;
|
|
501
570
|
maxBaseAssetReserve: BN;
|
|
502
571
|
minBaseAssetReserve: BN;
|
|
572
|
+
cumulativeSocialLoss: BN;
|
|
503
573
|
};
|
|
504
|
-
export declare type
|
|
574
|
+
export declare type PerpPosition = {
|
|
505
575
|
baseAssetAmount: BN;
|
|
576
|
+
remainderBaseAssetAmount: BN;
|
|
506
577
|
lastCumulativeFundingRate: BN;
|
|
507
578
|
marketIndex: BN;
|
|
508
579
|
quoteAssetAmount: BN;
|
|
@@ -540,22 +611,27 @@ export declare type UserAccount = {
|
|
|
540
611
|
authority: PublicKey;
|
|
541
612
|
name: number[];
|
|
542
613
|
userId: number;
|
|
543
|
-
|
|
544
|
-
|
|
614
|
+
spotPositions: SpotPosition[];
|
|
615
|
+
perpPositions: PerpPosition[];
|
|
545
616
|
orders: Order[];
|
|
546
617
|
beingLiquidated: boolean;
|
|
547
618
|
bankrupt: boolean;
|
|
548
619
|
nextLiquidationId: number;
|
|
549
620
|
nextOrderId: BN;
|
|
621
|
+
customMarginRatio: number;
|
|
550
622
|
};
|
|
551
|
-
export declare type
|
|
552
|
-
|
|
553
|
-
balanceType:
|
|
623
|
+
export declare type SpotPosition = {
|
|
624
|
+
marketIndex: BN;
|
|
625
|
+
balanceType: SpotBalanceType;
|
|
554
626
|
balance: BN;
|
|
627
|
+
openOrders: number;
|
|
628
|
+
openBids: BN;
|
|
629
|
+
openAsks: BN;
|
|
555
630
|
};
|
|
556
631
|
export declare type Order = {
|
|
557
632
|
status: OrderStatus;
|
|
558
633
|
orderType: OrderType;
|
|
634
|
+
marketType: MarketType;
|
|
559
635
|
ts: BN;
|
|
560
636
|
slot: BN;
|
|
561
637
|
orderId: BN;
|
|
@@ -579,9 +655,11 @@ export declare type Order = {
|
|
|
579
655
|
auctionDuration: number;
|
|
580
656
|
auctionStartPrice: BN;
|
|
581
657
|
auctionEndPrice: BN;
|
|
658
|
+
timeInForce: number;
|
|
582
659
|
};
|
|
583
660
|
export declare type OrderParams = {
|
|
584
661
|
orderType: OrderType;
|
|
662
|
+
marketType: MarketType;
|
|
585
663
|
userOrderId: number;
|
|
586
664
|
direction: PositionDirection;
|
|
587
665
|
baseAssetAmount: BN;
|
|
@@ -594,12 +672,9 @@ export declare type OrderParams = {
|
|
|
594
672
|
triggerCondition: OrderTriggerCondition;
|
|
595
673
|
positionLimit: BN;
|
|
596
674
|
oraclePriceOffset: BN;
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
discountToken: boolean;
|
|
601
|
-
referrer: boolean;
|
|
602
|
-
};
|
|
675
|
+
auctionDuration: number | null;
|
|
676
|
+
timeInForce: number | null;
|
|
677
|
+
auctionStartPrice: BN | null;
|
|
603
678
|
};
|
|
604
679
|
export declare type NecessaryOrderParams = {
|
|
605
680
|
orderType: OrderType;
|
|
@@ -614,6 +689,9 @@ export declare const DefaultOrderParams: {
|
|
|
614
689
|
orderType: {
|
|
615
690
|
market: {};
|
|
616
691
|
};
|
|
692
|
+
marketType: {
|
|
693
|
+
perp: {};
|
|
694
|
+
};
|
|
617
695
|
userOrderId: number;
|
|
618
696
|
direction: {
|
|
619
697
|
long: {};
|
|
@@ -630,12 +708,9 @@ export declare const DefaultOrderParams: {
|
|
|
630
708
|
};
|
|
631
709
|
positionLimit: BN;
|
|
632
710
|
oraclePriceOffset: BN;
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
discountToken: boolean;
|
|
637
|
-
referrer: boolean;
|
|
638
|
-
};
|
|
711
|
+
auctionDuration: any;
|
|
712
|
+
timeInForce: any;
|
|
713
|
+
auctionStartPrice: any;
|
|
639
714
|
};
|
|
640
715
|
export declare type MakerInfo = {
|
|
641
716
|
maker: PublicKey;
|
|
@@ -691,7 +766,7 @@ export declare type FeeStructure = {
|
|
|
691
766
|
makerRebateNumerator: BN;
|
|
692
767
|
makerRebateDenominator: BN;
|
|
693
768
|
fillerRewardStructure: OrderFillerRewardStructure;
|
|
694
|
-
|
|
769
|
+
flatFillerFee: BN;
|
|
695
770
|
};
|
|
696
771
|
export declare type OracleGuardRails = {
|
|
697
772
|
priceDivergence: {
|
|
@@ -712,7 +787,7 @@ export declare type OrderFillerRewardStructure = {
|
|
|
712
787
|
};
|
|
713
788
|
export declare type MarginCategory = 'Initial' | 'Maintenance';
|
|
714
789
|
export declare type InsuranceFundStake = {
|
|
715
|
-
|
|
790
|
+
marketIndex: BN;
|
|
716
791
|
authority: PublicKey;
|
|
717
792
|
ifShares: BN;
|
|
718
793
|
ifBase: BN;
|
|
@@ -720,3 +795,19 @@ export declare type InsuranceFundStake = {
|
|
|
720
795
|
lastWithdrawRequestValue: BN;
|
|
721
796
|
lastWithdrawRequestTs: BN;
|
|
722
797
|
};
|
|
798
|
+
export declare type SerumV3FulfillmentConfigAccount = {
|
|
799
|
+
fulfillmentType: SpotFulfillmentType;
|
|
800
|
+
status: SpotFulfillmentStatus;
|
|
801
|
+
pubkey: PublicKey;
|
|
802
|
+
marketIndex: BN;
|
|
803
|
+
serumProgramId: PublicKey;
|
|
804
|
+
serumMarket: PublicKey;
|
|
805
|
+
serumRequestQueue: PublicKey;
|
|
806
|
+
serumEventQueue: PublicKey;
|
|
807
|
+
serumBids: PublicKey;
|
|
808
|
+
serumAsks: PublicKey;
|
|
809
|
+
serumBaseVault: PublicKey;
|
|
810
|
+
serumQuoteVault: PublicKey;
|
|
811
|
+
serumOpenOrders: PublicKey;
|
|
812
|
+
serumSignerNonce: BN;
|
|
813
|
+
};
|
package/lib/types.js
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultOrderParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.
|
|
3
|
+
exports.DefaultOrderParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.ContractType = exports.MarketStatus = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
|
+
class MarketStatus {
|
|
7
|
+
}
|
|
8
|
+
exports.MarketStatus = MarketStatus;
|
|
9
|
+
MarketStatus.INITIALIZED = { initialized: {} };
|
|
10
|
+
MarketStatus.REDUCEONLY = { reduceonly: {} };
|
|
11
|
+
MarketStatus.SETTLEMENT = { settlement: {} };
|
|
12
|
+
MarketStatus.DELISTED = { delisted: {} };
|
|
13
|
+
class ContractType {
|
|
14
|
+
}
|
|
15
|
+
exports.ContractType = ContractType;
|
|
16
|
+
ContractType.PERPETUAL = { perpetual: {} };
|
|
17
|
+
ContractType.FUTURE = { future: {} };
|
|
6
18
|
class SwapDirection {
|
|
7
19
|
}
|
|
8
20
|
exports.SwapDirection = SwapDirection;
|
|
9
21
|
SwapDirection.ADD = { add: {} };
|
|
10
22
|
SwapDirection.REMOVE = { remove: {} };
|
|
11
|
-
class
|
|
23
|
+
class SpotBalanceType {
|
|
12
24
|
}
|
|
13
|
-
exports.
|
|
14
|
-
|
|
15
|
-
|
|
25
|
+
exports.SpotBalanceType = SpotBalanceType;
|
|
26
|
+
SpotBalanceType.DEPOSIT = { deposit: {} };
|
|
27
|
+
SpotBalanceType.BORROW = { borrow: {} };
|
|
16
28
|
class PositionDirection {
|
|
17
29
|
}
|
|
18
30
|
exports.PositionDirection = PositionDirection;
|
|
@@ -36,6 +48,11 @@ OrderType.LIMIT = { limit: {} };
|
|
|
36
48
|
OrderType.TRIGGER_MARKET = { triggerMarket: {} };
|
|
37
49
|
OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
|
|
38
50
|
OrderType.MARKET = { market: {} };
|
|
51
|
+
class MarketType {
|
|
52
|
+
}
|
|
53
|
+
exports.MarketType = MarketType;
|
|
54
|
+
MarketType.SPOT = { spot: {} };
|
|
55
|
+
MarketType.PERP = { perp: {} };
|
|
39
56
|
class OrderStatus {
|
|
40
57
|
}
|
|
41
58
|
exports.OrderStatus = OrderStatus;
|
|
@@ -78,6 +95,15 @@ class OrderTriggerCondition {
|
|
|
78
95
|
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
79
96
|
OrderTriggerCondition.ABOVE = { above: {} };
|
|
80
97
|
OrderTriggerCondition.BELOW = { below: {} };
|
|
98
|
+
class SpotFulfillmentType {
|
|
99
|
+
}
|
|
100
|
+
exports.SpotFulfillmentType = SpotFulfillmentType;
|
|
101
|
+
SpotFulfillmentType.SERUM_v3 = { serumV3: {} };
|
|
102
|
+
class SpotFulfillmentStatus {
|
|
103
|
+
}
|
|
104
|
+
exports.SpotFulfillmentStatus = SpotFulfillmentStatus;
|
|
105
|
+
SpotFulfillmentStatus.ENABLED = { enabled: {} };
|
|
106
|
+
SpotFulfillmentStatus.DISABLED = { disabled: {} };
|
|
81
107
|
function isVariant(object, type) {
|
|
82
108
|
return object.hasOwnProperty(type);
|
|
83
109
|
}
|
|
@@ -88,6 +114,10 @@ function isOneOfVariant(object, types) {
|
|
|
88
114
|
}, false);
|
|
89
115
|
}
|
|
90
116
|
exports.isOneOfVariant = isOneOfVariant;
|
|
117
|
+
function getVariant(object) {
|
|
118
|
+
return Object.keys(object)[0];
|
|
119
|
+
}
|
|
120
|
+
exports.getVariant = getVariant;
|
|
91
121
|
var TradeSide;
|
|
92
122
|
(function (TradeSide) {
|
|
93
123
|
TradeSide[TradeSide["None"] = 0] = "None";
|
|
@@ -119,6 +149,7 @@ LiquidationType.BORROW_BANKRUPTCY = {
|
|
|
119
149
|
};
|
|
120
150
|
exports.DefaultOrderParams = {
|
|
121
151
|
orderType: OrderType.MARKET,
|
|
152
|
+
marketType: MarketType.PERP,
|
|
122
153
|
userOrderId: 0,
|
|
123
154
|
direction: PositionDirection.LONG,
|
|
124
155
|
baseAssetAmount: _1.ZERO,
|
|
@@ -131,10 +162,7 @@ exports.DefaultOrderParams = {
|
|
|
131
162
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
132
163
|
positionLimit: _1.ZERO,
|
|
133
164
|
oraclePriceOffset: _1.ZERO,
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
discountToken: false,
|
|
138
|
-
referrer: false,
|
|
139
|
-
},
|
|
165
|
+
auctionDuration: null,
|
|
166
|
+
timeInForce: null,
|
|
167
|
+
auctionStartPrice: null,
|
|
140
168
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ClearingHouseUser, ClearingHouse, OrderRecord, ClearingHouseUserAccountSubscriptionConfig } from '..';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
export declare class UserMap {
|
|
4
|
+
private userMap;
|
|
5
|
+
private clearingHouse;
|
|
6
|
+
private accountSubscription;
|
|
7
|
+
constructor(clearingHouse: ClearingHouse, accountSubscription: ClearingHouseUserAccountSubscriptionConfig);
|
|
8
|
+
fetchAllUsers(): Promise<void>;
|
|
9
|
+
addPubkey(userAccountPublicKey: PublicKey): Promise<void>;
|
|
10
|
+
has(key: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* gets the ClearingHouseUser for a particular userAccountPublicKey, if no ClearingHouseUser exists, undefined is returned
|
|
13
|
+
* @param key userAccountPublicKey to get ClearngHouseUserFor
|
|
14
|
+
* @returns user ClearingHouseUser | undefined
|
|
15
|
+
*/
|
|
16
|
+
get(key: string): ClearingHouseUser | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* gets the ClearingHouseUser for a particular userAccountPublicKey, if no ClearingHouseUser exists, new one is created
|
|
19
|
+
* @param key userAccountPublicKey to get ClearngHouseUserFor
|
|
20
|
+
* @returns ClearingHouseUser
|
|
21
|
+
*/
|
|
22
|
+
mustGet(key: string): Promise<ClearingHouseUser>;
|
|
23
|
+
updateWithOrderRecord(record: OrderRecord): Promise<void>;
|
|
24
|
+
values(): IterableIterator<ClearingHouseUser>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserMap = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
class UserMap {
|
|
7
|
+
constructor(clearingHouse, accountSubscription) {
|
|
8
|
+
this.userMap = new Map();
|
|
9
|
+
this.clearingHouse = clearingHouse;
|
|
10
|
+
this.accountSubscription = accountSubscription;
|
|
11
|
+
}
|
|
12
|
+
async fetchAllUsers() {
|
|
13
|
+
const userArray = [];
|
|
14
|
+
const programUserAccounts = (await this.clearingHouse.program.account.user.all());
|
|
15
|
+
for (const programUserAccount of programUserAccounts) {
|
|
16
|
+
if (this.userMap.has(programUserAccount.publicKey.toString())) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const user = new __1.ClearingHouseUser({
|
|
20
|
+
clearingHouse: this.clearingHouse,
|
|
21
|
+
userAccountPublicKey: programUserAccount.publicKey,
|
|
22
|
+
accountSubscription: this.accountSubscription,
|
|
23
|
+
});
|
|
24
|
+
userArray.push(user);
|
|
25
|
+
}
|
|
26
|
+
if (this.accountSubscription.type === 'polling') {
|
|
27
|
+
await (0, __1.bulkPollingUserSubscribe)(userArray, this.accountSubscription.accountLoader);
|
|
28
|
+
}
|
|
29
|
+
for (const user of userArray) {
|
|
30
|
+
this.userMap.set(user.getUserAccountPublicKey().toString(), user);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async addPubkey(userAccountPublicKey) {
|
|
34
|
+
const user = new __1.ClearingHouseUser({
|
|
35
|
+
clearingHouse: this.clearingHouse,
|
|
36
|
+
userAccountPublicKey,
|
|
37
|
+
accountSubscription: this.accountSubscription,
|
|
38
|
+
});
|
|
39
|
+
await user.subscribe();
|
|
40
|
+
this.userMap.set(userAccountPublicKey.toString(), user);
|
|
41
|
+
}
|
|
42
|
+
has(key) {
|
|
43
|
+
return this.userMap.has(key);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* gets the ClearingHouseUser for a particular userAccountPublicKey, if no ClearingHouseUser exists, undefined is returned
|
|
47
|
+
* @param key userAccountPublicKey to get ClearngHouseUserFor
|
|
48
|
+
* @returns user ClearingHouseUser | undefined
|
|
49
|
+
*/
|
|
50
|
+
get(key) {
|
|
51
|
+
return this.userMap.get(key);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* gets the ClearingHouseUser for a particular userAccountPublicKey, if no ClearingHouseUser exists, new one is created
|
|
55
|
+
* @param key userAccountPublicKey to get ClearngHouseUserFor
|
|
56
|
+
* @returns ClearingHouseUser
|
|
57
|
+
*/
|
|
58
|
+
async mustGet(key) {
|
|
59
|
+
if (!this.has(key)) {
|
|
60
|
+
await this.addPubkey(new web3_js_1.PublicKey(key));
|
|
61
|
+
}
|
|
62
|
+
const user = this.userMap.get(key);
|
|
63
|
+
await user.fetchAccounts();
|
|
64
|
+
return user;
|
|
65
|
+
}
|
|
66
|
+
async updateWithOrderRecord(record) {
|
|
67
|
+
await this.addPubkey(record.user);
|
|
68
|
+
}
|
|
69
|
+
values() {
|
|
70
|
+
return this.userMap.values();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.UserMap = UserMap;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ClearingHouse, OrderRecord, ClearingHouseUserStats, ClearingHouseUserStatsAccountSubscriptionConfig } from '..';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { UserMap } from './userMap';
|
|
4
|
+
export declare class UserStatsMap {
|
|
5
|
+
/**
|
|
6
|
+
* map from authority pubkey to ClearingHouseUserStats
|
|
7
|
+
*/
|
|
8
|
+
private userStatsMap;
|
|
9
|
+
private clearingHouse;
|
|
10
|
+
private accountSubscription;
|
|
11
|
+
constructor(clearingHouse: ClearingHouse, accountSubscription: ClearingHouseUserStatsAccountSubscriptionConfig);
|
|
12
|
+
fetchAllUserStats(): Promise<void>;
|
|
13
|
+
addUserStat(authority: PublicKey): Promise<void>;
|
|
14
|
+
updateWithOrderRecord(record: OrderRecord, userMap: UserMap): Promise<void>;
|
|
15
|
+
has(authorityPublicKey: string): boolean;
|
|
16
|
+
get(authorityPublicKey: string): ClearingHouseUserStats;
|
|
17
|
+
mustGet(authorityPublicKey: string): Promise<ClearingHouseUserStats>;
|
|
18
|
+
values(): IterableIterator<ClearingHouseUserStats>;
|
|
19
|
+
}
|