@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26
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 +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +83 -42
- package/lib/clearingHouse.d.ts +69 -42
- package/lib/clearingHouse.js +753 -277
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +139 -119
- package/lib/config.d.ts +7 -7
- package/lib/config.js +20 -20
- 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/{src/constants/markets.js → lib/constants/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/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 +1008 -279
- package/lib/index.d.ts +5 -3
- package/lib/index.js +5 -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 +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -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 +123 -33
- package/lib/types.js +31 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +49 -44
- package/src/admin.js +1284 -0
- package/src/admin.ts +140 -47
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1083 -378
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +232 -168
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +29 -29
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/fetchLogs.js +117 -0
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +3 -0
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1008 -279
- package/src/index.js +75 -0
- package/src/index.ts +5 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +6 -3
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +110 -33
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
package/lib/types.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
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
|
+
}
|
|
15
|
+
export declare class ContractType {
|
|
16
|
+
static readonly PERPETUAL: {
|
|
17
|
+
perpetual: {};
|
|
18
|
+
};
|
|
19
|
+
static readonly FUTURE: {
|
|
20
|
+
future: {};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
4
23
|
export declare class SwapDirection {
|
|
5
24
|
static readonly ADD: {
|
|
6
25
|
add: {};
|
|
@@ -9,7 +28,7 @@ export declare class SwapDirection {
|
|
|
9
28
|
remove: {};
|
|
10
29
|
};
|
|
11
30
|
}
|
|
12
|
-
export declare class
|
|
31
|
+
export declare class SpotBalanceType {
|
|
13
32
|
static readonly DEPOSIT: {
|
|
14
33
|
deposit: {};
|
|
15
34
|
};
|
|
@@ -58,6 +77,14 @@ export declare class OrderType {
|
|
|
58
77
|
market: {};
|
|
59
78
|
};
|
|
60
79
|
}
|
|
80
|
+
export declare class MarketType {
|
|
81
|
+
static readonly SPOT: {
|
|
82
|
+
spot: {};
|
|
83
|
+
};
|
|
84
|
+
static readonly PERP: {
|
|
85
|
+
perp: {};
|
|
86
|
+
};
|
|
87
|
+
}
|
|
61
88
|
export declare class OrderStatus {
|
|
62
89
|
static readonly INIT: {
|
|
63
90
|
init: {};
|
|
@@ -125,6 +152,19 @@ export declare class OrderTriggerCondition {
|
|
|
125
152
|
below: {};
|
|
126
153
|
};
|
|
127
154
|
}
|
|
155
|
+
export declare class SpotFulfillmentType {
|
|
156
|
+
static readonly SERUM_v3: {
|
|
157
|
+
serumV3: {};
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export declare class SpotFulfillmentStatus {
|
|
161
|
+
static readonly ENABLED: {
|
|
162
|
+
enabled: {};
|
|
163
|
+
};
|
|
164
|
+
static readonly DISABLED: {
|
|
165
|
+
disabled: {};
|
|
166
|
+
};
|
|
167
|
+
}
|
|
128
168
|
export declare function isVariant(object: unknown, type: string): boolean;
|
|
129
169
|
export declare function isOneOfVariant(object: unknown, types: string[]): boolean;
|
|
130
170
|
export declare enum TradeSide {
|
|
@@ -149,7 +189,7 @@ export declare type DepositRecord = {
|
|
|
149
189
|
deposit?: any;
|
|
150
190
|
withdraw?: any;
|
|
151
191
|
};
|
|
152
|
-
|
|
192
|
+
marketIndex: BN;
|
|
153
193
|
amount: BN;
|
|
154
194
|
oraclePrice: BN;
|
|
155
195
|
referrer: PublicKey;
|
|
@@ -175,6 +215,18 @@ export declare type CurveRecord = {
|
|
|
175
215
|
oraclePrice: BN;
|
|
176
216
|
tradeId: BN;
|
|
177
217
|
};
|
|
218
|
+
export declare type InsuranceFundRecord = {
|
|
219
|
+
ts: BN;
|
|
220
|
+
bankIndex: BN;
|
|
221
|
+
marketIndex: BN;
|
|
222
|
+
userIfFactor: BN;
|
|
223
|
+
totalIfFactor: BN;
|
|
224
|
+
vaultAmountBefore: BN;
|
|
225
|
+
insuranceVaultAmountBefore: BN;
|
|
226
|
+
amount: BN;
|
|
227
|
+
totalIfSharesBefore: BN;
|
|
228
|
+
totalIfSharesAfter: BN;
|
|
229
|
+
};
|
|
178
230
|
export declare type LPRecord = {
|
|
179
231
|
ts: BN;
|
|
180
232
|
user: PublicKey;
|
|
@@ -273,26 +325,26 @@ export declare type LiquidatePerpRecord = {
|
|
|
273
325
|
fillRecordId: BN;
|
|
274
326
|
};
|
|
275
327
|
export declare type LiquidateBorrowRecord = {
|
|
276
|
-
|
|
328
|
+
assetMarketIndex: BN;
|
|
277
329
|
assetPrice: BN;
|
|
278
330
|
assetTransfer: BN;
|
|
279
|
-
|
|
331
|
+
liabilityMarketIndex: BN;
|
|
280
332
|
liabilityPrice: BN;
|
|
281
333
|
liabilityTransfer: BN;
|
|
282
334
|
};
|
|
283
335
|
export declare type LiquidateBorrowForPerpPnlRecord = {
|
|
284
|
-
|
|
336
|
+
perpMarketIndex: BN;
|
|
285
337
|
marketOraclePrice: BN;
|
|
286
338
|
pnlTransfer: BN;
|
|
287
|
-
|
|
339
|
+
liabilityMarketIndex: BN;
|
|
288
340
|
liabilityPrice: BN;
|
|
289
341
|
liabilityTransfer: BN;
|
|
290
342
|
};
|
|
291
343
|
export declare type LiquidatePerpPnlForDepositRecord = {
|
|
292
|
-
|
|
344
|
+
perpMarketIndex: BN;
|
|
293
345
|
marketOraclePrice: BN;
|
|
294
346
|
pnlTransfer: BN;
|
|
295
|
-
|
|
347
|
+
assetMarketIndex: BN;
|
|
296
348
|
assetPrice: BN;
|
|
297
349
|
assetTransfer: BN;
|
|
298
350
|
};
|
|
@@ -302,7 +354,7 @@ export declare type PerpBankruptcyRecord = {
|
|
|
302
354
|
cumulativeFundingRateDelta: BN;
|
|
303
355
|
};
|
|
304
356
|
export declare type BorrowBankruptcyRecord = {
|
|
305
|
-
|
|
357
|
+
marketIndex: BN;
|
|
306
358
|
borrowAmount: BN;
|
|
307
359
|
cumulativeDepositInterestDelta: BN;
|
|
308
360
|
};
|
|
@@ -313,7 +365,7 @@ export declare type SettlePnlRecord = {
|
|
|
313
365
|
pnl: BN;
|
|
314
366
|
baseAssetAmount: BN;
|
|
315
367
|
quoteAssetAmountAfter: BN;
|
|
316
|
-
|
|
368
|
+
quoteEntryAmount: BN;
|
|
317
369
|
settlePrice: BN;
|
|
318
370
|
};
|
|
319
371
|
export declare type OrderRecord = {
|
|
@@ -341,12 +393,14 @@ export declare type OrderActionRecord = {
|
|
|
341
393
|
quoteAssetAmountSurplus: BN | null;
|
|
342
394
|
taker: PublicKey | null;
|
|
343
395
|
takerOrderId: BN | null;
|
|
396
|
+
takerOrderDirection: PositionDirection | null;
|
|
344
397
|
takerOrderBaseAssetAmount: BN | null;
|
|
345
398
|
takerOrderBaseAssetAmountFilled: BN | null;
|
|
346
399
|
takerOrderQuoteAssetAmountFilled: BN | null;
|
|
347
400
|
takerOrderFee: BN | null;
|
|
348
401
|
maker: PublicKey | null;
|
|
349
402
|
makerOrderId: BN | null;
|
|
403
|
+
makerOrderDirection: PositionDirection | null;
|
|
350
404
|
makerOrderBaseAssetAmount: BN | null;
|
|
351
405
|
makerOrderBaseAssetAmountFilled: BN | null;
|
|
352
406
|
makerOrderQuoteAssetAmountFilled: BN | null;
|
|
@@ -370,7 +424,8 @@ export declare type StateAccount = {
|
|
|
370
424
|
fullLiquidationPenaltyPercentageDenominator: BN;
|
|
371
425
|
partialLiquidationLiquidatorShareDenominator: BN;
|
|
372
426
|
fullLiquidationLiquidatorShareDenominator: BN;
|
|
373
|
-
|
|
427
|
+
perpFeeStructure: FeeStructure;
|
|
428
|
+
spotFeeStructure: FeeStructure;
|
|
374
429
|
totalFee: BN;
|
|
375
430
|
totalFeeWithdrawn: BN;
|
|
376
431
|
whitelistMint: PublicKey;
|
|
@@ -378,15 +433,19 @@ export declare type StateAccount = {
|
|
|
378
433
|
oracleGuardRails: OracleGuardRails;
|
|
379
434
|
maxDeposit: BN;
|
|
380
435
|
numberOfMarkets: BN;
|
|
381
|
-
|
|
436
|
+
numberOfSpotMarkets: BN;
|
|
382
437
|
minOrderQuoteAssetAmount: BN;
|
|
383
438
|
signer: PublicKey;
|
|
384
439
|
signerNonce: number;
|
|
385
440
|
maxAuctionDuration: number;
|
|
386
441
|
minAuctionDuration: number;
|
|
442
|
+
liquidationMarginBufferRatio: number;
|
|
387
443
|
};
|
|
388
|
-
export declare type
|
|
389
|
-
|
|
444
|
+
export declare type PerpMarketAccount = {
|
|
445
|
+
status: MarketStatus;
|
|
446
|
+
contractType: ContractType;
|
|
447
|
+
expiryTs: BN;
|
|
448
|
+
settlementPrice: BN;
|
|
390
449
|
marketIndex: BN;
|
|
391
450
|
pubkey: PublicKey;
|
|
392
451
|
amm: AMM;
|
|
@@ -401,11 +460,17 @@ export declare type MarketAccount = {
|
|
|
401
460
|
liquidationFee: BN;
|
|
402
461
|
imfFactor: BN;
|
|
403
462
|
unrealizedImfFactor: BN;
|
|
463
|
+
unrealizedMaxImbalance: BN;
|
|
404
464
|
unrealizedInitialAssetWeight: number;
|
|
405
465
|
unrealizedMaintenanceAssetWeight: number;
|
|
466
|
+
revenueWithdrawSinceLastSettle: BN;
|
|
467
|
+
maxRevenueWithdrawPerPeriod: BN;
|
|
468
|
+
lastRevenueWithdrawTs: BN;
|
|
469
|
+
quoteSettledInsurance: BN;
|
|
470
|
+
quoteMaxInsurance: BN;
|
|
406
471
|
};
|
|
407
|
-
export declare type
|
|
408
|
-
|
|
472
|
+
export declare type SpotMarketAccount = {
|
|
473
|
+
marketIndex: BN;
|
|
409
474
|
pubkey: PublicKey;
|
|
410
475
|
mint: PublicKey;
|
|
411
476
|
vault: PublicKey;
|
|
@@ -438,6 +503,12 @@ export declare type BankAccount = {
|
|
|
438
503
|
depositTokenTwap: BN;
|
|
439
504
|
borrowTokenTwap: BN;
|
|
440
505
|
utilizationTwap: BN;
|
|
506
|
+
orderStepSize: BN;
|
|
507
|
+
nextFillRecordId: BN;
|
|
508
|
+
spotFeePool: {
|
|
509
|
+
balance: BN;
|
|
510
|
+
};
|
|
511
|
+
totalSpotFee: BN;
|
|
441
512
|
};
|
|
442
513
|
export declare type PoolBalance = {
|
|
443
514
|
balance: BN;
|
|
@@ -490,19 +561,22 @@ export declare type AMM = {
|
|
|
490
561
|
totalMmFee: BN;
|
|
491
562
|
netRevenueSinceLastFunding: BN;
|
|
492
563
|
lastUpdateSlot: BN;
|
|
564
|
+
lastOracleValid: boolean;
|
|
493
565
|
lastBidPriceTwap: BN;
|
|
494
566
|
lastAskPriceTwap: BN;
|
|
495
567
|
longSpread: BN;
|
|
496
568
|
shortSpread: BN;
|
|
497
569
|
maxSpread: number;
|
|
498
|
-
marketPosition:
|
|
499
|
-
marketPositionPerLp:
|
|
570
|
+
marketPosition: PerpPosition;
|
|
571
|
+
marketPositionPerLp: PerpPosition;
|
|
500
572
|
ammJitIntensity: number;
|
|
501
573
|
maxBaseAssetReserve: BN;
|
|
502
574
|
minBaseAssetReserve: BN;
|
|
575
|
+
cumulativeSocialLoss: BN;
|
|
503
576
|
};
|
|
504
|
-
export declare type
|
|
577
|
+
export declare type PerpPosition = {
|
|
505
578
|
baseAssetAmount: BN;
|
|
579
|
+
remainderBaseAssetAmount: BN;
|
|
506
580
|
lastCumulativeFundingRate: BN;
|
|
507
581
|
marketIndex: BN;
|
|
508
582
|
quoteAssetAmount: BN;
|
|
@@ -540,22 +614,26 @@ export declare type UserAccount = {
|
|
|
540
614
|
authority: PublicKey;
|
|
541
615
|
name: number[];
|
|
542
616
|
userId: number;
|
|
543
|
-
|
|
544
|
-
|
|
617
|
+
spotPositions: SpotPosition[];
|
|
618
|
+
perpPositions: PerpPosition[];
|
|
545
619
|
orders: Order[];
|
|
546
620
|
beingLiquidated: boolean;
|
|
547
621
|
bankrupt: boolean;
|
|
548
622
|
nextLiquidationId: number;
|
|
549
623
|
nextOrderId: BN;
|
|
550
624
|
};
|
|
551
|
-
export declare type
|
|
552
|
-
|
|
553
|
-
balanceType:
|
|
625
|
+
export declare type SpotPosition = {
|
|
626
|
+
marketIndex: BN;
|
|
627
|
+
balanceType: SpotBalanceType;
|
|
554
628
|
balance: BN;
|
|
629
|
+
openOrders: number;
|
|
630
|
+
openBids: BN;
|
|
631
|
+
openAsks: BN;
|
|
555
632
|
};
|
|
556
633
|
export declare type Order = {
|
|
557
634
|
status: OrderStatus;
|
|
558
635
|
orderType: OrderType;
|
|
636
|
+
marketType: MarketType;
|
|
559
637
|
ts: BN;
|
|
560
638
|
slot: BN;
|
|
561
639
|
orderId: BN;
|
|
@@ -582,6 +660,7 @@ export declare type Order = {
|
|
|
582
660
|
};
|
|
583
661
|
export declare type OrderParams = {
|
|
584
662
|
orderType: OrderType;
|
|
663
|
+
marketType: MarketType;
|
|
585
664
|
userOrderId: number;
|
|
586
665
|
direction: PositionDirection;
|
|
587
666
|
baseAssetAmount: BN;
|
|
@@ -596,10 +675,6 @@ export declare type OrderParams = {
|
|
|
596
675
|
oraclePriceOffset: BN;
|
|
597
676
|
padding0: boolean;
|
|
598
677
|
padding1: BN;
|
|
599
|
-
optionalAccounts: {
|
|
600
|
-
discountToken: boolean;
|
|
601
|
-
referrer: boolean;
|
|
602
|
-
};
|
|
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: {};
|
|
@@ -632,10 +710,6 @@ export declare const DefaultOrderParams: {
|
|
|
632
710
|
oraclePriceOffset: BN;
|
|
633
711
|
padding0: BN;
|
|
634
712
|
padding1: BN;
|
|
635
|
-
optionalAccounts: {
|
|
636
|
-
discountToken: boolean;
|
|
637
|
-
referrer: boolean;
|
|
638
|
-
};
|
|
639
713
|
};
|
|
640
714
|
export declare type MakerInfo = {
|
|
641
715
|
maker: PublicKey;
|
|
@@ -712,7 +786,7 @@ export declare type OrderFillerRewardStructure = {
|
|
|
712
786
|
};
|
|
713
787
|
export declare type MarginCategory = 'Initial' | 'Maintenance';
|
|
714
788
|
export declare type InsuranceFundStake = {
|
|
715
|
-
|
|
789
|
+
marketIndex: BN;
|
|
716
790
|
authority: PublicKey;
|
|
717
791
|
ifShares: BN;
|
|
718
792
|
ifBase: BN;
|
|
@@ -720,3 +794,19 @@ export declare type InsuranceFundStake = {
|
|
|
720
794
|
lastWithdrawRequestValue: BN;
|
|
721
795
|
lastWithdrawRequestTs: BN;
|
|
722
796
|
};
|
|
797
|
+
export declare type SerumV3FulfillmentConfigAccount = {
|
|
798
|
+
fulfillmentType: SpotFulfillmentType;
|
|
799
|
+
status: SpotFulfillmentStatus;
|
|
800
|
+
pubkey: PublicKey;
|
|
801
|
+
marketIndex: BN;
|
|
802
|
+
serumProgramId: PublicKey;
|
|
803
|
+
serumMarket: PublicKey;
|
|
804
|
+
serumRequestQueue: PublicKey;
|
|
805
|
+
serumEventQueue: PublicKey;
|
|
806
|
+
serumBids: PublicKey;
|
|
807
|
+
serumAsks: PublicKey;
|
|
808
|
+
serumBaseVault: PublicKey;
|
|
809
|
+
serumQuoteVault: PublicKey;
|
|
810
|
+
serumOpenOrders: PublicKey;
|
|
811
|
+
serumSignerNonce: BN;
|
|
812
|
+
};
|
package/lib/types.js
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
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.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
|
+
class ContractType {
|
|
13
|
+
}
|
|
14
|
+
exports.ContractType = ContractType;
|
|
15
|
+
ContractType.PERPETUAL = { perpetual: {} };
|
|
16
|
+
ContractType.FUTURE = { future: {} };
|
|
6
17
|
class SwapDirection {
|
|
7
18
|
}
|
|
8
19
|
exports.SwapDirection = SwapDirection;
|
|
9
20
|
SwapDirection.ADD = { add: {} };
|
|
10
21
|
SwapDirection.REMOVE = { remove: {} };
|
|
11
|
-
class
|
|
22
|
+
class SpotBalanceType {
|
|
12
23
|
}
|
|
13
|
-
exports.
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
exports.SpotBalanceType = SpotBalanceType;
|
|
25
|
+
SpotBalanceType.DEPOSIT = { deposit: {} };
|
|
26
|
+
SpotBalanceType.BORROW = { borrow: {} };
|
|
16
27
|
class PositionDirection {
|
|
17
28
|
}
|
|
18
29
|
exports.PositionDirection = PositionDirection;
|
|
@@ -36,6 +47,11 @@ OrderType.LIMIT = { limit: {} };
|
|
|
36
47
|
OrderType.TRIGGER_MARKET = { triggerMarket: {} };
|
|
37
48
|
OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
|
|
38
49
|
OrderType.MARKET = { market: {} };
|
|
50
|
+
class MarketType {
|
|
51
|
+
}
|
|
52
|
+
exports.MarketType = MarketType;
|
|
53
|
+
MarketType.SPOT = { spot: {} };
|
|
54
|
+
MarketType.PERP = { perp: {} };
|
|
39
55
|
class OrderStatus {
|
|
40
56
|
}
|
|
41
57
|
exports.OrderStatus = OrderStatus;
|
|
@@ -78,6 +94,15 @@ class OrderTriggerCondition {
|
|
|
78
94
|
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
79
95
|
OrderTriggerCondition.ABOVE = { above: {} };
|
|
80
96
|
OrderTriggerCondition.BELOW = { below: {} };
|
|
97
|
+
class SpotFulfillmentType {
|
|
98
|
+
}
|
|
99
|
+
exports.SpotFulfillmentType = SpotFulfillmentType;
|
|
100
|
+
SpotFulfillmentType.SERUM_v3 = { serumV3: {} };
|
|
101
|
+
class SpotFulfillmentStatus {
|
|
102
|
+
}
|
|
103
|
+
exports.SpotFulfillmentStatus = SpotFulfillmentStatus;
|
|
104
|
+
SpotFulfillmentStatus.ENABLED = { enabled: {} };
|
|
105
|
+
SpotFulfillmentStatus.DISABLED = { disabled: {} };
|
|
81
106
|
function isVariant(object, type) {
|
|
82
107
|
return object.hasOwnProperty(type);
|
|
83
108
|
}
|
|
@@ -119,6 +144,7 @@ LiquidationType.BORROW_BANKRUPTCY = {
|
|
|
119
144
|
};
|
|
120
145
|
exports.DefaultOrderParams = {
|
|
121
146
|
orderType: OrderType.MARKET,
|
|
147
|
+
marketType: MarketType.PERP,
|
|
122
148
|
userOrderId: 0,
|
|
123
149
|
direction: PositionDirection.LONG,
|
|
124
150
|
baseAssetAmount: _1.ZERO,
|
|
@@ -133,8 +159,4 @@ exports.DefaultOrderParams = {
|
|
|
133
159
|
oraclePriceOffset: _1.ZERO,
|
|
134
160
|
padding0: _1.ZERO,
|
|
135
161
|
padding1: _1.ZERO,
|
|
136
|
-
optionalAccounts: {
|
|
137
|
-
discountToken: false,
|
|
138
|
-
referrer: false,
|
|
139
|
-
},
|
|
140
162
|
};
|
package/my-script/.env
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#ANCHOR_WALLET=/Users/ww/.config/solana/id.json
|
|
2
|
+
|
|
3
|
+
#ANCHOR_WALLET=/Users/ww/.config/solana/bot2RzYy9oY5s3z71Yubo4MmRxyDBQP3qNNCqyqLcPp.json
|
|
4
|
+
|
|
5
|
+
ANCHOR_WALLET=./ww18NdhuLSQPCrHSx7V68eZJpe2y311heWeXJfSmP3Q.json
|
|
6
|
+
#ANCHOR_WALLET=./ww2z7N9TG1PLLUQGQF2VKzCFaPtQ5FBhRfeEAuy6c5C.json
|
|
7
|
+
#ANCHOR_WALLET=./ww3StJtTubhwssqAhvSSAc5ifCgKjzmF8hz7Gt2DmSa.json
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import * as anchor from '@project-serum/anchor';
|
|
2
|
+
import { PublicKey, clusterApiUrl } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
initialize,
|
|
6
|
+
BulkAccountLoader,
|
|
7
|
+
EventSubscriber,
|
|
8
|
+
DevnetMarkets,
|
|
9
|
+
ClearingHouse,
|
|
10
|
+
} from '..';
|
|
11
|
+
import { printUserStats } from './utils';
|
|
12
|
+
|
|
13
|
+
require('dotenv').config();
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async function main(provider: anchor.AnchorProvider) {
|
|
17
|
+
const connection = provider.connection;
|
|
18
|
+
const config = initialize({ env: 'devnet' });
|
|
19
|
+
const clearingHousePublicKey = new PublicKey(
|
|
20
|
+
config.CLEARING_HOUSE_PROGRAM_ID
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const bulkAccountLoader = new BulkAccountLoader(connection, 'confirmed', 1000);
|
|
24
|
+
const clearingHouse = new ClearingHouse({
|
|
25
|
+
connection,
|
|
26
|
+
wallet: provider.wallet,
|
|
27
|
+
programID: clearingHousePublicKey,
|
|
28
|
+
env: 'devnet',
|
|
29
|
+
// accountSubscription: {
|
|
30
|
+
// type: 'websocket',
|
|
31
|
+
// }
|
|
32
|
+
accountSubscription: {
|
|
33
|
+
type: 'polling',
|
|
34
|
+
accountLoader: bulkAccountLoader,
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
console.log(`clearingHouseProgramID: ${clearingHousePublicKey}`);
|
|
39
|
+
console.log(`provider.wallet: ${provider.wallet.publicKey.toBase58()}`);
|
|
40
|
+
|
|
41
|
+
const eventSubscriber = new EventSubscriber(connection, clearingHouse.program, {
|
|
42
|
+
maxTx: 8192,
|
|
43
|
+
maxEventsPerType: 4096,
|
|
44
|
+
orderBy: 'blockchain',
|
|
45
|
+
orderDir: 'desc',
|
|
46
|
+
commitment: 'confirmed',
|
|
47
|
+
logProviderConfig: {
|
|
48
|
+
type: 'websocket',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (!(await clearingHouse.getUser().exists())) {
|
|
53
|
+
console.error(`ClearingHouseUser for ${provider.wallet.publicKey} does not exist`);
|
|
54
|
+
console.info(`Creating ClearingHouseUser for ${provider.wallet.publicKey}`);
|
|
55
|
+
const [txSig] = await clearingHouse.initializeUserAccount();
|
|
56
|
+
console.log(`Initialized user account in transaction: ${txSig}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!await clearingHouse.subscribe()) {
|
|
60
|
+
throw new Error("fail clearingHouse.subscribe");
|
|
61
|
+
}
|
|
62
|
+
if (!await clearingHouse.accountSubscriber.subscribe()) {
|
|
63
|
+
throw new Error("fail clearingHouse.accountSubscriber.subscribe");
|
|
64
|
+
}
|
|
65
|
+
if (!eventSubscriber.subscribe()) {
|
|
66
|
+
throw new Error("fail eventSubscriber.subscribe");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const chUser = clearingHouse.getUser();
|
|
70
|
+
const chUserAccount = clearingHouse.getUserAccount();
|
|
71
|
+
console.log(`chUserAcc.authority: ${chUserAccount!.authority.toBase58()}`);
|
|
72
|
+
console.log(` chUserAccount pubkey: ${chUser.userAccountPublicKey.toBase58()}`);
|
|
73
|
+
|
|
74
|
+
const keyToSymbol = new Map<string, string>();
|
|
75
|
+
const marketIndexToSymbol = new Map<number, string>();
|
|
76
|
+
for (const market of DevnetMarkets) {
|
|
77
|
+
keyToSymbol.set(market.oracle.toBase58(), market.baseAssetSymbol);
|
|
78
|
+
marketIndexToSymbol.set(market.marketIndex.toNumber(), market.symbol);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const run = async () => {
|
|
82
|
+
await clearingHouse.fetchAccounts();
|
|
83
|
+
await chUser.fetchAccounts();
|
|
84
|
+
printUserStats(chUserAccount!, marketIndexToSymbol);
|
|
85
|
+
};
|
|
86
|
+
await run();
|
|
87
|
+
setInterval(run, 1000);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
if (!process.env.ANCHOR_WALLET) {
|
|
93
|
+
throw new Error('ANCHOR_WALLET must be set.');
|
|
94
|
+
}
|
|
95
|
+
main(
|
|
96
|
+
anchor.AnchorProvider.local(clusterApiUrl("devnet"), {
|
|
97
|
+
// anchor.AnchorProvider.local('https://devnet.genesysgo.net', {
|
|
98
|
+
preflightCommitment: 'confirmed',
|
|
99
|
+
skipPreflight: false,
|
|
100
|
+
commitment: 'confirmed',
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
// anchor.AnchorProvider.local('https://psytrbhymqlkfrhudd.dev.genesysgo.net:8899/');
|
|
104
|
+
} catch (e) {
|
|
105
|
+
console.error(e);
|
|
106
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import * as anchor from '@project-serum/anchor';
|
|
2
|
+
import { PublicKey, clusterApiUrl } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
initialize,
|
|
6
|
+
BulkAccountLoader,
|
|
7
|
+
EventSubscriber,
|
|
8
|
+
DevnetMarkets,
|
|
9
|
+
ClearingHouse,
|
|
10
|
+
} from '..';
|
|
11
|
+
import { printUserStats } from './utils';
|
|
12
|
+
|
|
13
|
+
require('dotenv').config();
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async function main(provider: anchor.AnchorProvider) {
|
|
17
|
+
const connection = provider.connection;
|
|
18
|
+
const config = initialize({ env: 'devnet' });
|
|
19
|
+
const clearingHousePublicKey = new PublicKey(
|
|
20
|
+
config.CLEARING_HOUSE_PROGRAM_ID
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const bulkAccountLoader = new BulkAccountLoader(
|
|
24
|
+
connection,
|
|
25
|
+
'confirmed',
|
|
26
|
+
1000
|
|
27
|
+
);
|
|
28
|
+
const clearingHouse = new ClearingHouse({
|
|
29
|
+
connection,
|
|
30
|
+
wallet: provider.wallet,
|
|
31
|
+
programID: clearingHousePublicKey,
|
|
32
|
+
env: 'devnet',
|
|
33
|
+
// accountSubscription: {
|
|
34
|
+
// type: 'websocket',
|
|
35
|
+
// }
|
|
36
|
+
accountSubscription: {
|
|
37
|
+
type: 'polling',
|
|
38
|
+
accountLoader: bulkAccountLoader,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(`clearingHouseProgramID: ${clearingHousePublicKey}`);
|
|
43
|
+
console.log(
|
|
44
|
+
`provider.wallet: ${provider.wallet.publicKey.toBase58()}`
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const eventSubscriber = new EventSubscriber(
|
|
48
|
+
connection,
|
|
49
|
+
clearingHouse.program,
|
|
50
|
+
{
|
|
51
|
+
maxTx: 8192,
|
|
52
|
+
maxEventsPerType: 4096,
|
|
53
|
+
orderBy: 'blockchain',
|
|
54
|
+
orderDir: 'desc',
|
|
55
|
+
commitment: 'confirmed',
|
|
56
|
+
logProviderConfig: {
|
|
57
|
+
type: 'websocket',
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
if (!(await clearingHouse.getUser().exists())) {
|
|
63
|
+
console.error(
|
|
64
|
+
`ClearingHouseUser for ${provider.wallet.publicKey} does not exist`
|
|
65
|
+
);
|
|
66
|
+
console.info(`Creating ClearingHouseUser for ${provider.wallet.publicKey}`);
|
|
67
|
+
const [txSig] = await clearingHouse.initializeUserAccount();
|
|
68
|
+
console.log(`Initialized user account in transaction: ${txSig}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!(await clearingHouse.subscribe())) {
|
|
72
|
+
throw new Error('fail clearingHouse.subscribe');
|
|
73
|
+
}
|
|
74
|
+
if (!(await clearingHouse.accountSubscriber.subscribe())) {
|
|
75
|
+
throw new Error('fail clearingHouse.accountSubscriber.subscribe');
|
|
76
|
+
}
|
|
77
|
+
if (!eventSubscriber.subscribe()) {
|
|
78
|
+
throw new Error('fail eventSubscriber.subscribe');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const chUser = clearingHouse.getUser();
|
|
82
|
+
const chUserAccount = clearingHouse.getUserAccount();
|
|
83
|
+
console.log(`chUserAcc.authority: ${chUserAccount!.authority.toBase58()}`);
|
|
84
|
+
console.log(
|
|
85
|
+
` chUserAccount pubkey: ${chUser.userAccountPublicKey.toBase58()}`
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const keyToSymbol = new Map<string, string>();
|
|
89
|
+
const marketIndexToSymbol = new Map<number, string>();
|
|
90
|
+
for (const market of DevnetMarkets) {
|
|
91
|
+
keyToSymbol.set(market.oracle.toBase58(), market.baseAssetSymbol);
|
|
92
|
+
marketIndexToSymbol.set(market.marketIndex.toNumber(), market.symbol);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const run = async () => {
|
|
96
|
+
await clearingHouse.fetchAccounts();
|
|
97
|
+
await chUser.fetchAccounts();
|
|
98
|
+
printUserStats(chUserAccount!, marketIndexToSymbol);
|
|
99
|
+
};
|
|
100
|
+
await run();
|
|
101
|
+
setInterval(run, 1000);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
if (!process.env.ANCHOR_WALLET) {
|
|
106
|
+
throw new Error('ANCHOR_WALLET must be set.');
|
|
107
|
+
}
|
|
108
|
+
main(
|
|
109
|
+
anchor.AnchorProvider.local(clusterApiUrl('devnet'), {
|
|
110
|
+
// anchor.AnchorProvider.local('https://devnet.genesysgo.net', {
|
|
111
|
+
preflightCommitment: 'confirmed',
|
|
112
|
+
skipPreflight: false,
|
|
113
|
+
commitment: 'confirmed',
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
// anchor.AnchorProvider.local('https://psytrbhymqlkfrhudd.dev.genesysgo.net:8899/');
|
|
117
|
+
} catch (e) {
|
|
118
|
+
console.error(e);
|
|
119
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
try {
|
|
2
|
+
// const msg = 'Program 65sz7dRiWDRPZjiRxcTxPM7AE6VK4Nag9HEK6oBJXhJn failed: custom program error: 0x179e'
|
|
3
|
+
const msg = 'Program log: AnchorError occurred. Error Code: OrderDoesNotExist. Error Number: 6046. Error Message: Order does not exist.';
|
|
4
|
+
const code = msg.match(
|
|
5
|
+
/Program log: AnchorError occurred. Error Code: ([0-9,a-z,A-Z]+). Error Number/
|
|
6
|
+
);
|
|
7
|
+
console.log(code[1]);
|
|
8
|
+
} catch (e) {
|
|
9
|
+
console.log("nothing");
|
|
10
|
+
// no problem if couldn't match error code
|
|
11
|
+
}
|