@drift-labs/sdk 0.2.0-master.24 → 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 +8 -6
- package/lib/addresses/pda.js +35 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +90 -54
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +765 -282
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +19 -17
- package/lib/clearingHouseUser.js +145 -123
- 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/{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/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/types.d.ts +4 -2
- package/lib/events/types.js +2 -0
- package/lib/examples/makeTradeExample.js +18 -6
- package/lib/idl/clearing_house.json +1128 -347
- package/lib/index.d.ts +6 -3
- package/lib/index.js +6 -3
- package/lib/math/amm.d.ts +5 -3
- package/lib/math/amm.js +42 -4
- 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 +148 -39
- package/lib/types.js +37 -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 +56 -42
- package/src/admin.js +1284 -0
- package/src/admin.ts +149 -75
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1097 -380
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +237 -172
- 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 +30 -30
- 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/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.js +117 -0
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/pollingLogProvider.ts +10 -2
- 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 +7 -1
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +27 -6
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1128 -347
- package/src/index.js +75 -0
- package/src/index.ts +6 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +73 -5
- 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 +131 -39
- 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,39 @@ 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
|
+
};
|
|
230
|
+
export declare type LPRecord = {
|
|
231
|
+
ts: BN;
|
|
232
|
+
user: PublicKey;
|
|
233
|
+
action: LPAction;
|
|
234
|
+
nShares: BN;
|
|
235
|
+
marketIndex: BN;
|
|
236
|
+
deltaBaseAssetAmount: BN;
|
|
237
|
+
deltaQuoteAssetAmount: BN;
|
|
238
|
+
pnl: BN;
|
|
239
|
+
};
|
|
240
|
+
export declare class LPAction {
|
|
241
|
+
static readonly ADD_LIQUIDITY: {
|
|
242
|
+
addLiquidity: {};
|
|
243
|
+
};
|
|
244
|
+
static readonly REMOVE_LIQUIDITY: {
|
|
245
|
+
removeLiquidity: {};
|
|
246
|
+
};
|
|
247
|
+
static readonly SETTLE_LIQUIDITY: {
|
|
248
|
+
settleLiquidity: {};
|
|
249
|
+
};
|
|
250
|
+
}
|
|
178
251
|
export declare type FundingRateRecord = {
|
|
179
252
|
ts: BN;
|
|
180
253
|
recordId: BN;
|
|
@@ -243,6 +316,7 @@ export declare type LiquidatePerpRecord = {
|
|
|
243
316
|
oraclePrice: BN;
|
|
244
317
|
baseAssetAmount: BN;
|
|
245
318
|
quoteAssetAmount: BN;
|
|
319
|
+
lpShares: BN;
|
|
246
320
|
userPnl: BN;
|
|
247
321
|
liquidatorPnl: BN;
|
|
248
322
|
canceledOrdersFee: BN;
|
|
@@ -251,26 +325,26 @@ export declare type LiquidatePerpRecord = {
|
|
|
251
325
|
fillRecordId: BN;
|
|
252
326
|
};
|
|
253
327
|
export declare type LiquidateBorrowRecord = {
|
|
254
|
-
|
|
328
|
+
assetMarketIndex: BN;
|
|
255
329
|
assetPrice: BN;
|
|
256
330
|
assetTransfer: BN;
|
|
257
|
-
|
|
331
|
+
liabilityMarketIndex: BN;
|
|
258
332
|
liabilityPrice: BN;
|
|
259
333
|
liabilityTransfer: BN;
|
|
260
334
|
};
|
|
261
335
|
export declare type LiquidateBorrowForPerpPnlRecord = {
|
|
262
|
-
|
|
336
|
+
perpMarketIndex: BN;
|
|
263
337
|
marketOraclePrice: BN;
|
|
264
338
|
pnlTransfer: BN;
|
|
265
|
-
|
|
339
|
+
liabilityMarketIndex: BN;
|
|
266
340
|
liabilityPrice: BN;
|
|
267
341
|
liabilityTransfer: BN;
|
|
268
342
|
};
|
|
269
343
|
export declare type LiquidatePerpPnlForDepositRecord = {
|
|
270
|
-
|
|
344
|
+
perpMarketIndex: BN;
|
|
271
345
|
marketOraclePrice: BN;
|
|
272
346
|
pnlTransfer: BN;
|
|
273
|
-
|
|
347
|
+
assetMarketIndex: BN;
|
|
274
348
|
assetPrice: BN;
|
|
275
349
|
assetTransfer: BN;
|
|
276
350
|
};
|
|
@@ -280,7 +354,7 @@ export declare type PerpBankruptcyRecord = {
|
|
|
280
354
|
cumulativeFundingRateDelta: BN;
|
|
281
355
|
};
|
|
282
356
|
export declare type BorrowBankruptcyRecord = {
|
|
283
|
-
|
|
357
|
+
marketIndex: BN;
|
|
284
358
|
borrowAmount: BN;
|
|
285
359
|
cumulativeDepositInterestDelta: BN;
|
|
286
360
|
};
|
|
@@ -291,7 +365,7 @@ export declare type SettlePnlRecord = {
|
|
|
291
365
|
pnl: BN;
|
|
292
366
|
baseAssetAmount: BN;
|
|
293
367
|
quoteAssetAmountAfter: BN;
|
|
294
|
-
|
|
368
|
+
quoteEntryAmount: BN;
|
|
295
369
|
settlePrice: BN;
|
|
296
370
|
};
|
|
297
371
|
export declare type OrderRecord = {
|
|
@@ -319,12 +393,14 @@ export declare type OrderActionRecord = {
|
|
|
319
393
|
quoteAssetAmountSurplus: BN | null;
|
|
320
394
|
taker: PublicKey | null;
|
|
321
395
|
takerOrderId: BN | null;
|
|
396
|
+
takerOrderDirection: PositionDirection | null;
|
|
322
397
|
takerOrderBaseAssetAmount: BN | null;
|
|
323
398
|
takerOrderBaseAssetAmountFilled: BN | null;
|
|
324
399
|
takerOrderQuoteAssetAmountFilled: BN | null;
|
|
325
400
|
takerOrderFee: BN | null;
|
|
326
401
|
maker: PublicKey | null;
|
|
327
402
|
makerOrderId: BN | null;
|
|
403
|
+
makerOrderDirection: PositionDirection | null;
|
|
328
404
|
makerOrderBaseAssetAmount: BN | null;
|
|
329
405
|
makerOrderBaseAssetAmountFilled: BN | null;
|
|
330
406
|
makerOrderQuoteAssetAmountFilled: BN | null;
|
|
@@ -337,8 +413,6 @@ export declare type StateAccount = {
|
|
|
337
413
|
exchangePaused: boolean;
|
|
338
414
|
adminControlsPrices: boolean;
|
|
339
415
|
insuranceVault: PublicKey;
|
|
340
|
-
insuranceVaultAuthority: PublicKey;
|
|
341
|
-
insuranceVaultNonce: number;
|
|
342
416
|
marginRatioInitial: BN;
|
|
343
417
|
marginRatioMaintenance: BN;
|
|
344
418
|
marginRatioPartial: BN;
|
|
@@ -350,7 +424,8 @@ export declare type StateAccount = {
|
|
|
350
424
|
fullLiquidationPenaltyPercentageDenominator: BN;
|
|
351
425
|
partialLiquidationLiquidatorShareDenominator: BN;
|
|
352
426
|
fullLiquidationLiquidatorShareDenominator: BN;
|
|
353
|
-
|
|
427
|
+
perpFeeStructure: FeeStructure;
|
|
428
|
+
spotFeeStructure: FeeStructure;
|
|
354
429
|
totalFee: BN;
|
|
355
430
|
totalFeeWithdrawn: BN;
|
|
356
431
|
whitelistMint: PublicKey;
|
|
@@ -358,13 +433,19 @@ export declare type StateAccount = {
|
|
|
358
433
|
oracleGuardRails: OracleGuardRails;
|
|
359
434
|
maxDeposit: BN;
|
|
360
435
|
numberOfMarkets: BN;
|
|
361
|
-
|
|
436
|
+
numberOfSpotMarkets: BN;
|
|
362
437
|
minOrderQuoteAssetAmount: BN;
|
|
438
|
+
signer: PublicKey;
|
|
439
|
+
signerNonce: number;
|
|
363
440
|
maxAuctionDuration: number;
|
|
364
441
|
minAuctionDuration: number;
|
|
442
|
+
liquidationMarginBufferRatio: number;
|
|
365
443
|
};
|
|
366
|
-
export declare type
|
|
367
|
-
|
|
444
|
+
export declare type PerpMarketAccount = {
|
|
445
|
+
status: MarketStatus;
|
|
446
|
+
contractType: ContractType;
|
|
447
|
+
expiryTs: BN;
|
|
448
|
+
settlementPrice: BN;
|
|
368
449
|
marketIndex: BN;
|
|
369
450
|
pubkey: PublicKey;
|
|
370
451
|
amm: AMM;
|
|
@@ -379,19 +460,21 @@ export declare type MarketAccount = {
|
|
|
379
460
|
liquidationFee: BN;
|
|
380
461
|
imfFactor: BN;
|
|
381
462
|
unrealizedImfFactor: BN;
|
|
463
|
+
unrealizedMaxImbalance: BN;
|
|
382
464
|
unrealizedInitialAssetWeight: number;
|
|
383
465
|
unrealizedMaintenanceAssetWeight: number;
|
|
466
|
+
revenueWithdrawSinceLastSettle: BN;
|
|
467
|
+
maxRevenueWithdrawPerPeriod: BN;
|
|
468
|
+
lastRevenueWithdrawTs: BN;
|
|
469
|
+
quoteSettledInsurance: BN;
|
|
470
|
+
quoteMaxInsurance: BN;
|
|
384
471
|
};
|
|
385
|
-
export declare type
|
|
386
|
-
|
|
472
|
+
export declare type SpotMarketAccount = {
|
|
473
|
+
marketIndex: BN;
|
|
387
474
|
pubkey: PublicKey;
|
|
388
475
|
mint: PublicKey;
|
|
389
476
|
vault: PublicKey;
|
|
390
|
-
vaultAuthority: PublicKey;
|
|
391
|
-
vaultAuthorityNonce: number;
|
|
392
477
|
insuranceFundVault: PublicKey;
|
|
393
|
-
insuranceFundVaultAuthority: PublicKey;
|
|
394
|
-
insuranceFundVaultAuthorityNonce: number;
|
|
395
478
|
insuranceWithdrawEscrowPeriod: BN;
|
|
396
479
|
revenuePool: PoolBalance;
|
|
397
480
|
totalIfShares: BN;
|
|
@@ -420,6 +503,12 @@ export declare type BankAccount = {
|
|
|
420
503
|
depositTokenTwap: BN;
|
|
421
504
|
borrowTokenTwap: BN;
|
|
422
505
|
utilizationTwap: BN;
|
|
506
|
+
orderStepSize: BN;
|
|
507
|
+
nextFillRecordId: BN;
|
|
508
|
+
spotFeePool: {
|
|
509
|
+
balance: BN;
|
|
510
|
+
};
|
|
511
|
+
totalSpotFee: BN;
|
|
423
512
|
};
|
|
424
513
|
export declare type PoolBalance = {
|
|
425
514
|
balance: BN;
|
|
@@ -455,6 +544,7 @@ export declare type AMM = {
|
|
|
455
544
|
cumulativeFeePerLp: BN;
|
|
456
545
|
cumulativeNetBaseAssetAmountPerLp: BN;
|
|
457
546
|
userLpShares: BN;
|
|
547
|
+
netUnsettledLpBaseAssetAmount: BN;
|
|
458
548
|
minimumQuoteAssetTradeSize: BN;
|
|
459
549
|
baseAssetAmountStepSize: BN;
|
|
460
550
|
maxBaseAssetAmountRatio: number;
|
|
@@ -471,19 +561,22 @@ export declare type AMM = {
|
|
|
471
561
|
totalMmFee: BN;
|
|
472
562
|
netRevenueSinceLastFunding: BN;
|
|
473
563
|
lastUpdateSlot: BN;
|
|
564
|
+
lastOracleValid: boolean;
|
|
474
565
|
lastBidPriceTwap: BN;
|
|
475
566
|
lastAskPriceTwap: BN;
|
|
476
567
|
longSpread: BN;
|
|
477
568
|
shortSpread: BN;
|
|
478
569
|
maxSpread: number;
|
|
479
|
-
marketPosition:
|
|
480
|
-
marketPositionPerLp:
|
|
570
|
+
marketPosition: PerpPosition;
|
|
571
|
+
marketPositionPerLp: PerpPosition;
|
|
481
572
|
ammJitIntensity: number;
|
|
482
573
|
maxBaseAssetReserve: BN;
|
|
483
574
|
minBaseAssetReserve: BN;
|
|
575
|
+
cumulativeSocialLoss: BN;
|
|
484
576
|
};
|
|
485
|
-
export declare type
|
|
577
|
+
export declare type PerpPosition = {
|
|
486
578
|
baseAssetAmount: BN;
|
|
579
|
+
remainderBaseAssetAmount: BN;
|
|
487
580
|
lastCumulativeFundingRate: BN;
|
|
488
581
|
marketIndex: BN;
|
|
489
582
|
quoteAssetAmount: BN;
|
|
@@ -521,22 +614,26 @@ export declare type UserAccount = {
|
|
|
521
614
|
authority: PublicKey;
|
|
522
615
|
name: number[];
|
|
523
616
|
userId: number;
|
|
524
|
-
|
|
525
|
-
|
|
617
|
+
spotPositions: SpotPosition[];
|
|
618
|
+
perpPositions: PerpPosition[];
|
|
526
619
|
orders: Order[];
|
|
527
620
|
beingLiquidated: boolean;
|
|
528
621
|
bankrupt: boolean;
|
|
529
622
|
nextLiquidationId: number;
|
|
530
623
|
nextOrderId: BN;
|
|
531
624
|
};
|
|
532
|
-
export declare type
|
|
533
|
-
|
|
534
|
-
balanceType:
|
|
625
|
+
export declare type SpotPosition = {
|
|
626
|
+
marketIndex: BN;
|
|
627
|
+
balanceType: SpotBalanceType;
|
|
535
628
|
balance: BN;
|
|
629
|
+
openOrders: number;
|
|
630
|
+
openBids: BN;
|
|
631
|
+
openAsks: BN;
|
|
536
632
|
};
|
|
537
633
|
export declare type Order = {
|
|
538
634
|
status: OrderStatus;
|
|
539
635
|
orderType: OrderType;
|
|
636
|
+
marketType: MarketType;
|
|
540
637
|
ts: BN;
|
|
541
638
|
slot: BN;
|
|
542
639
|
orderId: BN;
|
|
@@ -563,6 +660,7 @@ export declare type Order = {
|
|
|
563
660
|
};
|
|
564
661
|
export declare type OrderParams = {
|
|
565
662
|
orderType: OrderType;
|
|
663
|
+
marketType: MarketType;
|
|
566
664
|
userOrderId: number;
|
|
567
665
|
direction: PositionDirection;
|
|
568
666
|
baseAssetAmount: BN;
|
|
@@ -577,10 +675,6 @@ export declare type OrderParams = {
|
|
|
577
675
|
oraclePriceOffset: BN;
|
|
578
676
|
padding0: boolean;
|
|
579
677
|
padding1: BN;
|
|
580
|
-
optionalAccounts: {
|
|
581
|
-
discountToken: boolean;
|
|
582
|
-
referrer: boolean;
|
|
583
|
-
};
|
|
584
678
|
};
|
|
585
679
|
export declare type NecessaryOrderParams = {
|
|
586
680
|
orderType: OrderType;
|
|
@@ -595,6 +689,9 @@ export declare const DefaultOrderParams: {
|
|
|
595
689
|
orderType: {
|
|
596
690
|
market: {};
|
|
597
691
|
};
|
|
692
|
+
marketType: {
|
|
693
|
+
perp: {};
|
|
694
|
+
};
|
|
598
695
|
userOrderId: number;
|
|
599
696
|
direction: {
|
|
600
697
|
long: {};
|
|
@@ -613,10 +710,6 @@ export declare const DefaultOrderParams: {
|
|
|
613
710
|
oraclePriceOffset: BN;
|
|
614
711
|
padding0: BN;
|
|
615
712
|
padding1: BN;
|
|
616
|
-
optionalAccounts: {
|
|
617
|
-
discountToken: boolean;
|
|
618
|
-
referrer: boolean;
|
|
619
|
-
};
|
|
620
713
|
};
|
|
621
714
|
export declare type MakerInfo = {
|
|
622
715
|
maker: PublicKey;
|
|
@@ -693,7 +786,7 @@ export declare type OrderFillerRewardStructure = {
|
|
|
693
786
|
};
|
|
694
787
|
export declare type MarginCategory = 'Initial' | 'Maintenance';
|
|
695
788
|
export declare type InsuranceFundStake = {
|
|
696
|
-
|
|
789
|
+
marketIndex: BN;
|
|
697
790
|
authority: PublicKey;
|
|
698
791
|
ifShares: BN;
|
|
699
792
|
ifBase: BN;
|
|
@@ -701,3 +794,19 @@ export declare type InsuranceFundStake = {
|
|
|
701
794
|
lastWithdrawRequestValue: BN;
|
|
702
795
|
lastWithdrawRequestTs: BN;
|
|
703
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.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
|
}
|
|
@@ -94,6 +119,12 @@ var TradeSide;
|
|
|
94
119
|
TradeSide[TradeSide["Buy"] = 1] = "Buy";
|
|
95
120
|
TradeSide[TradeSide["Sell"] = 2] = "Sell";
|
|
96
121
|
})(TradeSide = exports.TradeSide || (exports.TradeSide = {}));
|
|
122
|
+
class LPAction {
|
|
123
|
+
}
|
|
124
|
+
exports.LPAction = LPAction;
|
|
125
|
+
LPAction.ADD_LIQUIDITY = { addLiquidity: {} };
|
|
126
|
+
LPAction.REMOVE_LIQUIDITY = { removeLiquidity: {} };
|
|
127
|
+
LPAction.SETTLE_LIQUIDITY = { settleLiquidity: {} };
|
|
97
128
|
class LiquidationType {
|
|
98
129
|
}
|
|
99
130
|
exports.LiquidationType = LiquidationType;
|
|
@@ -113,6 +144,7 @@ LiquidationType.BORROW_BANKRUPTCY = {
|
|
|
113
144
|
};
|
|
114
145
|
exports.DefaultOrderParams = {
|
|
115
146
|
orderType: OrderType.MARKET,
|
|
147
|
+
marketType: MarketType.PERP,
|
|
116
148
|
userOrderId: 0,
|
|
117
149
|
direction: PositionDirection.LONG,
|
|
118
150
|
baseAssetAmount: _1.ZERO,
|
|
@@ -127,8 +159,4 @@ exports.DefaultOrderParams = {
|
|
|
127
159
|
oraclePriceOffset: _1.ZERO,
|
|
128
160
|
padding0: _1.ZERO,
|
|
129
161
|
padding1: _1.ZERO,
|
|
130
|
-
optionalAccounts: {
|
|
131
|
-
discountToken: false,
|
|
132
|
-
referrer: false,
|
|
133
|
-
},
|
|
134
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
|
+
}
|