@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/src/types.ts
CHANGED
|
@@ -2,12 +2,24 @@ import { PublicKey, Transaction } from '@solana/web3.js';
|
|
|
2
2
|
import { BN, ZERO } from '.';
|
|
3
3
|
|
|
4
4
|
// # Utility Types / Enums / Constants
|
|
5
|
+
|
|
6
|
+
export class MarketStatus {
|
|
7
|
+
static readonly INITIALIZED = { initialized: {} };
|
|
8
|
+
static readonly REDUCEONLY = { reduceonly: {} };
|
|
9
|
+
static readonly SETTLEMENT = { settlement: {} };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ContractType {
|
|
13
|
+
static readonly PERPETUAL = { perpetual: {} };
|
|
14
|
+
static readonly FUTURE = { future: {} };
|
|
15
|
+
}
|
|
16
|
+
|
|
5
17
|
export class SwapDirection {
|
|
6
18
|
static readonly ADD = { add: {} };
|
|
7
19
|
static readonly REMOVE = { remove: {} };
|
|
8
20
|
}
|
|
9
21
|
|
|
10
|
-
export class
|
|
22
|
+
export class SpotBalanceType {
|
|
11
23
|
static readonly DEPOSIT = { deposit: {} };
|
|
12
24
|
static readonly BORROW = { borrow: {} };
|
|
13
25
|
}
|
|
@@ -35,6 +47,11 @@ export class OrderType {
|
|
|
35
47
|
static readonly MARKET = { market: {} };
|
|
36
48
|
}
|
|
37
49
|
|
|
50
|
+
export class MarketType {
|
|
51
|
+
static readonly SPOT = { spot: {} };
|
|
52
|
+
static readonly PERP = { perp: {} };
|
|
53
|
+
}
|
|
54
|
+
|
|
38
55
|
export class OrderStatus {
|
|
39
56
|
static readonly INIT = { init: {} };
|
|
40
57
|
static readonly OPEN = { open: {} };
|
|
@@ -77,6 +94,15 @@ export class OrderTriggerCondition {
|
|
|
77
94
|
static readonly BELOW = { below: {} };
|
|
78
95
|
}
|
|
79
96
|
|
|
97
|
+
export class SpotFulfillmentType {
|
|
98
|
+
static readonly SERUM_v3 = { serumV3: {} };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class SpotFulfillmentStatus {
|
|
102
|
+
static readonly ENABLED = { enabled: {} };
|
|
103
|
+
static readonly DISABLED = { disabled: {} };
|
|
104
|
+
}
|
|
105
|
+
|
|
80
106
|
export function isVariant(object: unknown, type: string) {
|
|
81
107
|
return object.hasOwnProperty(type);
|
|
82
108
|
}
|
|
@@ -120,7 +146,7 @@ export type DepositRecord = {
|
|
|
120
146
|
deposit?: any;
|
|
121
147
|
withdraw?: any;
|
|
122
148
|
};
|
|
123
|
-
|
|
149
|
+
marketIndex: BN;
|
|
124
150
|
amount: BN;
|
|
125
151
|
oraclePrice: BN;
|
|
126
152
|
referrer: PublicKey;
|
|
@@ -148,6 +174,36 @@ export type CurveRecord = {
|
|
|
148
174
|
tradeId: BN;
|
|
149
175
|
};
|
|
150
176
|
|
|
177
|
+
export declare type InsuranceFundRecord = {
|
|
178
|
+
ts: BN;
|
|
179
|
+
bankIndex: BN;
|
|
180
|
+
marketIndex: BN;
|
|
181
|
+
userIfFactor: BN;
|
|
182
|
+
totalIfFactor: BN;
|
|
183
|
+
vaultAmountBefore: BN;
|
|
184
|
+
insuranceVaultAmountBefore: BN;
|
|
185
|
+
amount: BN;
|
|
186
|
+
totalIfSharesBefore: BN;
|
|
187
|
+
totalIfSharesAfter: BN;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export type LPRecord = {
|
|
191
|
+
ts: BN;
|
|
192
|
+
user: PublicKey;
|
|
193
|
+
action: LPAction;
|
|
194
|
+
nShares: BN;
|
|
195
|
+
marketIndex: BN;
|
|
196
|
+
deltaBaseAssetAmount: BN;
|
|
197
|
+
deltaQuoteAssetAmount: BN;
|
|
198
|
+
pnl: BN;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export class LPAction {
|
|
202
|
+
static readonly ADD_LIQUIDITY = { addLiquidity: {} };
|
|
203
|
+
static readonly REMOVE_LIQUIDITY = { removeLiquidity: {} };
|
|
204
|
+
static readonly SETTLE_LIQUIDITY = { settleLiquidity: {} };
|
|
205
|
+
}
|
|
206
|
+
|
|
151
207
|
export type FundingRateRecord = {
|
|
152
208
|
ts: BN;
|
|
153
209
|
recordId: BN;
|
|
@@ -216,6 +272,7 @@ export type LiquidatePerpRecord = {
|
|
|
216
272
|
oraclePrice: BN;
|
|
217
273
|
baseAssetAmount: BN;
|
|
218
274
|
quoteAssetAmount: BN;
|
|
275
|
+
lpShares: BN;
|
|
219
276
|
userPnl: BN;
|
|
220
277
|
liquidatorPnl: BN;
|
|
221
278
|
canceledOrdersFee: BN;
|
|
@@ -225,28 +282,28 @@ export type LiquidatePerpRecord = {
|
|
|
225
282
|
};
|
|
226
283
|
|
|
227
284
|
export type LiquidateBorrowRecord = {
|
|
228
|
-
|
|
285
|
+
assetMarketIndex: BN;
|
|
229
286
|
assetPrice: BN;
|
|
230
287
|
assetTransfer: BN;
|
|
231
|
-
|
|
288
|
+
liabilityMarketIndex: BN;
|
|
232
289
|
liabilityPrice: BN;
|
|
233
290
|
liabilityTransfer: BN;
|
|
234
291
|
};
|
|
235
292
|
|
|
236
293
|
export type LiquidateBorrowForPerpPnlRecord = {
|
|
237
|
-
|
|
294
|
+
perpMarketIndex: BN;
|
|
238
295
|
marketOraclePrice: BN;
|
|
239
296
|
pnlTransfer: BN;
|
|
240
|
-
|
|
297
|
+
liabilityMarketIndex: BN;
|
|
241
298
|
liabilityPrice: BN;
|
|
242
299
|
liabilityTransfer: BN;
|
|
243
300
|
};
|
|
244
301
|
|
|
245
302
|
export type LiquidatePerpPnlForDepositRecord = {
|
|
246
|
-
|
|
303
|
+
perpMarketIndex: BN;
|
|
247
304
|
marketOraclePrice: BN;
|
|
248
305
|
pnlTransfer: BN;
|
|
249
|
-
|
|
306
|
+
assetMarketIndex: BN;
|
|
250
307
|
assetPrice: BN;
|
|
251
308
|
assetTransfer: BN;
|
|
252
309
|
};
|
|
@@ -258,7 +315,7 @@ export type PerpBankruptcyRecord = {
|
|
|
258
315
|
};
|
|
259
316
|
|
|
260
317
|
export type BorrowBankruptcyRecord = {
|
|
261
|
-
|
|
318
|
+
marketIndex: BN;
|
|
262
319
|
borrowAmount: BN;
|
|
263
320
|
cumulativeDepositInterestDelta: BN;
|
|
264
321
|
};
|
|
@@ -270,7 +327,7 @@ export type SettlePnlRecord = {
|
|
|
270
327
|
pnl: BN;
|
|
271
328
|
baseAssetAmount: BN;
|
|
272
329
|
quoteAssetAmountAfter: BN;
|
|
273
|
-
|
|
330
|
+
quoteEntryAmount: BN;
|
|
274
331
|
settlePrice: BN;
|
|
275
332
|
};
|
|
276
333
|
|
|
@@ -300,12 +357,14 @@ export type OrderActionRecord = {
|
|
|
300
357
|
quoteAssetAmountSurplus: BN | null;
|
|
301
358
|
taker: PublicKey | null;
|
|
302
359
|
takerOrderId: BN | null;
|
|
360
|
+
takerOrderDirection: PositionDirection | null;
|
|
303
361
|
takerOrderBaseAssetAmount: BN | null;
|
|
304
362
|
takerOrderBaseAssetAmountFilled: BN | null;
|
|
305
363
|
takerOrderQuoteAssetAmountFilled: BN | null;
|
|
306
364
|
takerOrderFee: BN | null;
|
|
307
365
|
maker: PublicKey | null;
|
|
308
366
|
makerOrderId: BN | null;
|
|
367
|
+
makerOrderDirection: PositionDirection | null;
|
|
309
368
|
makerOrderBaseAssetAmount: BN | null;
|
|
310
369
|
makerOrderBaseAssetAmountFilled: BN | null;
|
|
311
370
|
makerOrderQuoteAssetAmountFilled: BN | null;
|
|
@@ -319,8 +378,6 @@ export type StateAccount = {
|
|
|
319
378
|
exchangePaused: boolean;
|
|
320
379
|
adminControlsPrices: boolean;
|
|
321
380
|
insuranceVault: PublicKey;
|
|
322
|
-
insuranceVaultAuthority: PublicKey;
|
|
323
|
-
insuranceVaultNonce: number;
|
|
324
381
|
marginRatioInitial: BN;
|
|
325
382
|
marginRatioMaintenance: BN;
|
|
326
383
|
marginRatioPartial: BN;
|
|
@@ -332,7 +389,8 @@ export type StateAccount = {
|
|
|
332
389
|
fullLiquidationPenaltyPercentageDenominator: BN;
|
|
333
390
|
partialLiquidationLiquidatorShareDenominator: BN;
|
|
334
391
|
fullLiquidationLiquidatorShareDenominator: BN;
|
|
335
|
-
|
|
392
|
+
perpFeeStructure: FeeStructure;
|
|
393
|
+
spotFeeStructure: FeeStructure;
|
|
336
394
|
totalFee: BN;
|
|
337
395
|
totalFeeWithdrawn: BN;
|
|
338
396
|
whitelistMint: PublicKey;
|
|
@@ -340,14 +398,20 @@ export type StateAccount = {
|
|
|
340
398
|
oracleGuardRails: OracleGuardRails;
|
|
341
399
|
maxDeposit: BN;
|
|
342
400
|
numberOfMarkets: BN;
|
|
343
|
-
|
|
401
|
+
numberOfSpotMarkets: BN;
|
|
344
402
|
minOrderQuoteAssetAmount: BN;
|
|
403
|
+
signer: PublicKey;
|
|
404
|
+
signerNonce: number;
|
|
345
405
|
maxAuctionDuration: number;
|
|
346
406
|
minAuctionDuration: number;
|
|
407
|
+
liquidationMarginBufferRatio: number;
|
|
347
408
|
};
|
|
348
409
|
|
|
349
|
-
export type
|
|
350
|
-
|
|
410
|
+
export type PerpMarketAccount = {
|
|
411
|
+
status: MarketStatus;
|
|
412
|
+
contractType: ContractType;
|
|
413
|
+
expiryTs: BN;
|
|
414
|
+
settlementPrice: BN;
|
|
351
415
|
marketIndex: BN;
|
|
352
416
|
pubkey: PublicKey;
|
|
353
417
|
amm: AMM;
|
|
@@ -362,21 +426,23 @@ export type MarketAccount = {
|
|
|
362
426
|
liquidationFee: BN;
|
|
363
427
|
imfFactor: BN;
|
|
364
428
|
unrealizedImfFactor: BN;
|
|
429
|
+
unrealizedMaxImbalance: BN;
|
|
365
430
|
unrealizedInitialAssetWeight: number;
|
|
366
431
|
unrealizedMaintenanceAssetWeight: number;
|
|
432
|
+
revenueWithdrawSinceLastSettle: BN;
|
|
433
|
+
maxRevenueWithdrawPerPeriod: BN;
|
|
434
|
+
lastRevenueWithdrawTs: BN;
|
|
435
|
+
quoteSettledInsurance: BN;
|
|
436
|
+
quoteMaxInsurance: BN;
|
|
367
437
|
};
|
|
368
438
|
|
|
369
|
-
export type
|
|
370
|
-
|
|
439
|
+
export type SpotMarketAccount = {
|
|
440
|
+
marketIndex: BN;
|
|
371
441
|
pubkey: PublicKey;
|
|
372
442
|
mint: PublicKey;
|
|
373
443
|
vault: PublicKey;
|
|
374
|
-
vaultAuthority: PublicKey;
|
|
375
|
-
vaultAuthorityNonce: number;
|
|
376
444
|
|
|
377
445
|
insuranceFundVault: PublicKey;
|
|
378
|
-
insuranceFundVaultAuthority: PublicKey;
|
|
379
|
-
insuranceFundVaultAuthorityNonce: number;
|
|
380
446
|
insuranceWithdrawEscrowPeriod: BN;
|
|
381
447
|
revenuePool: PoolBalance;
|
|
382
448
|
|
|
@@ -409,6 +475,13 @@ export type BankAccount = {
|
|
|
409
475
|
depositTokenTwap: BN;
|
|
410
476
|
borrowTokenTwap: BN;
|
|
411
477
|
utilizationTwap: BN;
|
|
478
|
+
|
|
479
|
+
orderStepSize: BN;
|
|
480
|
+
nextFillRecordId: BN;
|
|
481
|
+
spotFeePool: {
|
|
482
|
+
balance: BN;
|
|
483
|
+
};
|
|
484
|
+
totalSpotFee: BN;
|
|
412
485
|
};
|
|
413
486
|
|
|
414
487
|
export type PoolBalance = {
|
|
@@ -446,6 +519,7 @@ export type AMM = {
|
|
|
446
519
|
cumulativeFeePerLp: BN;
|
|
447
520
|
cumulativeNetBaseAssetAmountPerLp: BN;
|
|
448
521
|
userLpShares: BN;
|
|
522
|
+
netUnsettledLpBaseAssetAmount: BN;
|
|
449
523
|
minimumQuoteAssetTradeSize: BN;
|
|
450
524
|
baseAssetAmountStepSize: BN;
|
|
451
525
|
maxBaseAssetAmountRatio: number;
|
|
@@ -462,21 +536,24 @@ export type AMM = {
|
|
|
462
536
|
totalMmFee: BN;
|
|
463
537
|
netRevenueSinceLastFunding: BN;
|
|
464
538
|
lastUpdateSlot: BN;
|
|
539
|
+
lastOracleValid: boolean;
|
|
465
540
|
lastBidPriceTwap: BN;
|
|
466
541
|
lastAskPriceTwap: BN;
|
|
467
542
|
longSpread: BN;
|
|
468
543
|
shortSpread: BN;
|
|
469
544
|
maxSpread: number;
|
|
470
|
-
marketPosition:
|
|
471
|
-
marketPositionPerLp:
|
|
545
|
+
marketPosition: PerpPosition;
|
|
546
|
+
marketPositionPerLp: PerpPosition;
|
|
472
547
|
ammJitIntensity: number;
|
|
473
548
|
maxBaseAssetReserve: BN;
|
|
474
549
|
minBaseAssetReserve: BN;
|
|
550
|
+
cumulativeSocialLoss: BN;
|
|
475
551
|
};
|
|
476
552
|
|
|
477
553
|
// # User Account Types
|
|
478
|
-
export type
|
|
554
|
+
export type PerpPosition = {
|
|
479
555
|
baseAssetAmount: BN;
|
|
556
|
+
remainderBaseAssetAmount: BN;
|
|
480
557
|
lastCumulativeFundingRate: BN;
|
|
481
558
|
marketIndex: BN;
|
|
482
559
|
quoteAssetAmount: BN;
|
|
@@ -516,8 +593,8 @@ export type UserAccount = {
|
|
|
516
593
|
authority: PublicKey;
|
|
517
594
|
name: number[];
|
|
518
595
|
userId: number;
|
|
519
|
-
|
|
520
|
-
|
|
596
|
+
spotPositions: SpotPosition[];
|
|
597
|
+
perpPositions: PerpPosition[];
|
|
521
598
|
orders: Order[];
|
|
522
599
|
beingLiquidated: boolean;
|
|
523
600
|
bankrupt: boolean;
|
|
@@ -525,15 +602,19 @@ export type UserAccount = {
|
|
|
525
602
|
nextOrderId: BN;
|
|
526
603
|
};
|
|
527
604
|
|
|
528
|
-
export type
|
|
529
|
-
|
|
530
|
-
balanceType:
|
|
605
|
+
export type SpotPosition = {
|
|
606
|
+
marketIndex: BN;
|
|
607
|
+
balanceType: SpotBalanceType;
|
|
531
608
|
balance: BN;
|
|
609
|
+
openOrders: number;
|
|
610
|
+
openBids: BN;
|
|
611
|
+
openAsks: BN;
|
|
532
612
|
};
|
|
533
613
|
|
|
534
614
|
export type Order = {
|
|
535
615
|
status: OrderStatus;
|
|
536
616
|
orderType: OrderType;
|
|
617
|
+
marketType: MarketType;
|
|
537
618
|
ts: BN;
|
|
538
619
|
slot: BN;
|
|
539
620
|
orderId: BN;
|
|
@@ -561,6 +642,7 @@ export type Order = {
|
|
|
561
642
|
|
|
562
643
|
export type OrderParams = {
|
|
563
644
|
orderType: OrderType;
|
|
645
|
+
marketType: MarketType;
|
|
564
646
|
userOrderId: number;
|
|
565
647
|
direction: PositionDirection;
|
|
566
648
|
baseAssetAmount: BN;
|
|
@@ -575,10 +657,6 @@ export type OrderParams = {
|
|
|
575
657
|
oraclePriceOffset: BN;
|
|
576
658
|
padding0: boolean;
|
|
577
659
|
padding1: BN;
|
|
578
|
-
optionalAccounts: {
|
|
579
|
-
discountToken: boolean;
|
|
580
|
-
referrer: boolean;
|
|
581
|
-
};
|
|
582
660
|
};
|
|
583
661
|
|
|
584
662
|
export type NecessaryOrderParams = {
|
|
@@ -594,6 +672,7 @@ export type OptionalOrderParams = {
|
|
|
594
672
|
|
|
595
673
|
export const DefaultOrderParams = {
|
|
596
674
|
orderType: OrderType.MARKET,
|
|
675
|
+
marketType: MarketType.PERP,
|
|
597
676
|
userOrderId: 0,
|
|
598
677
|
direction: PositionDirection.LONG,
|
|
599
678
|
baseAssetAmount: ZERO,
|
|
@@ -608,10 +687,6 @@ export const DefaultOrderParams = {
|
|
|
608
687
|
oraclePriceOffset: ZERO,
|
|
609
688
|
padding0: ZERO,
|
|
610
689
|
padding1: ZERO,
|
|
611
|
-
optionalAccounts: {
|
|
612
|
-
discountToken: false,
|
|
613
|
-
referrer: false,
|
|
614
|
-
},
|
|
615
690
|
};
|
|
616
691
|
|
|
617
692
|
export type MakerInfo = {
|
|
@@ -698,7 +773,7 @@ export type OrderFillerRewardStructure = {
|
|
|
698
773
|
export type MarginCategory = 'Initial' | 'Maintenance';
|
|
699
774
|
|
|
700
775
|
export type InsuranceFundStake = {
|
|
701
|
-
|
|
776
|
+
marketIndex: BN;
|
|
702
777
|
authority: PublicKey;
|
|
703
778
|
|
|
704
779
|
ifShares: BN;
|
|
@@ -708,3 +783,20 @@ export type InsuranceFundStake = {
|
|
|
708
783
|
lastWithdrawRequestValue: BN;
|
|
709
784
|
lastWithdrawRequestTs: BN;
|
|
710
785
|
};
|
|
786
|
+
|
|
787
|
+
export type SerumV3FulfillmentConfigAccount = {
|
|
788
|
+
fulfillmentType: SpotFulfillmentType;
|
|
789
|
+
status: SpotFulfillmentStatus;
|
|
790
|
+
pubkey: PublicKey;
|
|
791
|
+
marketIndex: BN;
|
|
792
|
+
serumProgramId: PublicKey;
|
|
793
|
+
serumMarket: PublicKey;
|
|
794
|
+
serumRequestQueue: PublicKey;
|
|
795
|
+
serumEventQueue: PublicKey;
|
|
796
|
+
serumBids: PublicKey;
|
|
797
|
+
serumAsks: PublicKey;
|
|
798
|
+
serumBaseVault: PublicKey;
|
|
799
|
+
serumQuoteVault: PublicKey;
|
|
800
|
+
serumOpenOrders: PublicKey;
|
|
801
|
+
serumSignerNonce: BN;
|
|
802
|
+
};
|
package/src/userName.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.decodeName = exports.encodeName = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
|
|
4
4
|
exports.MAX_NAME_LENGTH = 32;
|
|
5
5
|
exports.DEFAULT_USER_NAME = 'Main Account';
|
|
6
6
|
function encodeName(name) {
|
|
7
7
|
if (name.length > exports.MAX_NAME_LENGTH) {
|
|
8
|
-
throw Error(
|
|
8
|
+
throw Error("User name (".concat(name, ") longer than 32 characters"));
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
var buffer = Buffer.alloc(32);
|
|
11
11
|
buffer.fill(name);
|
|
12
12
|
buffer.fill(' ', name.length);
|
|
13
|
-
return Array(
|
|
13
|
+
return Array.apply(void 0, buffer);
|
|
14
14
|
}
|
|
15
15
|
exports.encodeName = encodeName;
|
|
16
16
|
function decodeName(bytes) {
|
|
17
|
-
|
|
17
|
+
var buffer = Buffer.from(bytes);
|
|
18
18
|
return buffer.toString('utf8').trim();
|
|
19
19
|
}
|
|
20
20
|
exports.decodeName = decodeName;
|
package/src/util/computeUnits.js
CHANGED
|
@@ -8,20 +8,55 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
12
39
|
exports.findComputeUnitConsumption = void 0;
|
|
13
|
-
function findComputeUnitConsumption(programId, connection, txSignature, commitment
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
40
|
+
function findComputeUnitConsumption(programId, connection, txSignature, commitment) {
|
|
41
|
+
if (commitment === void 0) { commitment = 'confirmed'; }
|
|
42
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
43
|
+
var tx, computeUnits, regex;
|
|
44
|
+
return __generator(this, function (_a) {
|
|
45
|
+
switch (_a.label) {
|
|
46
|
+
case 0: return [4 /*yield*/, connection.getTransaction(txSignature, { commitment: commitment })];
|
|
47
|
+
case 1:
|
|
48
|
+
tx = _a.sent();
|
|
49
|
+
computeUnits = [];
|
|
50
|
+
regex = new RegExp("Program ".concat(programId.toString(), " consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units"));
|
|
51
|
+
tx.meta.logMessages.forEach(function (logMessage) {
|
|
52
|
+
var match = logMessage.match(regex);
|
|
53
|
+
if (match && match[1]) {
|
|
54
|
+
computeUnits.push(match[1]);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return [2 /*return*/, computeUnits];
|
|
22
58
|
}
|
|
23
59
|
});
|
|
24
|
-
return computeUnits;
|
|
25
60
|
});
|
|
26
61
|
}
|
|
27
62
|
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.promiseTimeout = void 0;
|
|
4
4
|
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
timeoutId = setTimeout(()
|
|
5
|
+
var timeoutId;
|
|
6
|
+
var timeoutPromise = new Promise(function (resolve) {
|
|
7
|
+
timeoutId = setTimeout(function () { return resolve(null); }, timeoutMs);
|
|
8
8
|
});
|
|
9
|
-
return Promise.race([promise, timeoutPromise]).then((result)
|
|
9
|
+
return Promise.race([promise, timeoutPromise]).then(function (result) {
|
|
10
10
|
clearTimeout(timeoutId);
|
|
11
11
|
return result;
|
|
12
12
|
});
|
package/src/util/tps.js
CHANGED
|
@@ -8,20 +8,54 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
12
39
|
exports.estimateTps = void 0;
|
|
13
40
|
function estimateTps(programId, connection, failed) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
41
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
42
|
+
var signatures, numberOfSignatures;
|
|
43
|
+
return __generator(this, function (_a) {
|
|
44
|
+
switch (_a.label) {
|
|
45
|
+
case 0: return [4 /*yield*/, connection.getSignaturesForAddress(programId, undefined, 'finalized')];
|
|
46
|
+
case 1:
|
|
47
|
+
signatures = _a.sent();
|
|
48
|
+
if (failed) {
|
|
49
|
+
signatures = signatures.filter(function (signature) { return signature.err; });
|
|
50
|
+
}
|
|
51
|
+
numberOfSignatures = signatures.length;
|
|
52
|
+
if (numberOfSignatures === 0) {
|
|
53
|
+
return [2 /*return*/, 0];
|
|
54
|
+
}
|
|
55
|
+
return [2 /*return*/, (numberOfSignatures /
|
|
56
|
+
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime))];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
25
59
|
});
|
|
26
60
|
}
|
|
27
61
|
exports.estimateTps = estimateTps;
|
package/src/wallet.js
CHANGED
|
@@ -8,28 +8,65 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
12
39
|
exports.Wallet = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
40
|
+
var Wallet = /** @class */ (function () {
|
|
41
|
+
function Wallet(payer) {
|
|
15
42
|
this.payer = payer;
|
|
16
43
|
}
|
|
17
|
-
signTransaction(tx) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function
|
|
19
|
-
|
|
20
|
-
|
|
44
|
+
Wallet.prototype.signTransaction = function (tx) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
46
|
+
return __generator(this, function (_a) {
|
|
47
|
+
tx.partialSign(this.payer);
|
|
48
|
+
return [2 /*return*/, tx];
|
|
49
|
+
});
|
|
21
50
|
});
|
|
22
|
-
}
|
|
23
|
-
signAllTransactions(txs) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return t
|
|
51
|
+
};
|
|
52
|
+
Wallet.prototype.signAllTransactions = function (txs) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
54
|
+
var _this = this;
|
|
55
|
+
return __generator(this, function (_a) {
|
|
56
|
+
return [2 /*return*/, txs.map(function (t) {
|
|
57
|
+
t.partialSign(_this.payer);
|
|
58
|
+
return t;
|
|
59
|
+
})];
|
|
28
60
|
});
|
|
29
61
|
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
62
|
+
};
|
|
63
|
+
Object.defineProperty(Wallet.prototype, "publicKey", {
|
|
64
|
+
get: function () {
|
|
65
|
+
return this.payer.publicKey;
|
|
66
|
+
},
|
|
67
|
+
enumerable: false,
|
|
68
|
+
configurable: true
|
|
69
|
+
});
|
|
70
|
+
return Wallet;
|
|
71
|
+
}());
|
|
35
72
|
exports.Wallet = Wallet;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { BankAccount, BankBalanceType, MarginCategory } from '../types';
|
|
3
|
-
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getBalance(tokenAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
|
|
5
|
-
export declare function getTokenAmount(balanceAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
|
|
6
|
-
export declare function calculateAssetWeight(balanceAmount: BN, bank: BankAccount, marginCategory: MarginCategory): BN;
|
|
7
|
-
export declare function calculateLiabilityWeight(balanceAmount: BN, bank: BankAccount, marginCategory: MarginCategory): BN;
|
|
8
|
-
export declare function calculateInterestAccumulated(bank: BankAccount, now: BN): {
|
|
9
|
-
borrowInterest: BN;
|
|
10
|
-
depositInterest: BN;
|
|
11
|
-
};
|
|
12
|
-
export declare function calculateWithdrawLimit(bank: BankAccount, now: BN): {
|
|
13
|
-
borrowLimit: BN;
|
|
14
|
-
withdrawLimit: BN;
|
|
15
|
-
};
|