@drift-labs/sdk 0.2.0-master.13 → 0.2.0-master.14
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/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -1
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +2 -0
- package/lib/admin.js +18 -0
- package/lib/clearingHouse.d.ts +17 -1
- package/lib/clearingHouse.js +195 -14
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +5 -0
- package/lib/clearingHouseUser.js +97 -3
- package/lib/clearingHouseUserStats.d.ts +17 -0
- package/lib/clearingHouseUserStats.js +36 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/{src/clearingHouseConfig.js → lib/clearingHouseUserStatsConfig.js} +0 -0
- package/lib/config.js +1 -1
- package/lib/idl/clearing_house.json +614 -64
- package/lib/math/bankBalance.d.ts +4 -0
- package/lib/math/bankBalance.js +23 -1
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/position.js +2 -1
- package/lib/math/trade.js +2 -2
- package/lib/types.d.ts +55 -8
- package/lib/types.js +6 -0
- package/package.json +1 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +29 -1
- package/src/clearingHouse.ts +318 -15
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +113 -10
- package/src/clearingHouseUserStats.ts +53 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/idl/clearing_house.json +614 -64
- package/src/math/bankBalance.ts +49 -0
- package/src/math/oracles.ts +42 -5
- package/src/math/position.ts +2 -1
- package/src/math/trade.ts +2 -2
- package/src/types.ts +59 -8
- package/src/accounts/bulkAccountLoader.js +0 -197
- package/src/accounts/bulkUserSubscription.js +0 -33
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
- package/src/accounts/pollingOracleSubscriber.js +0 -93
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
- package/src/accounts/pollingUserAccountSubscriber.js +0 -132
- package/src/accounts/types.js +0 -10
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -93
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/index.js +0 -69
- package/src/mockUSDCFaucet.js +0 -280
package/src/math/bankBalance.ts
CHANGED
|
@@ -207,3 +207,52 @@ export function calculateInterestAccumulated(
|
|
|
207
207
|
|
|
208
208
|
return { borrowInterest, depositInterest };
|
|
209
209
|
}
|
|
210
|
+
|
|
211
|
+
export function calculateWithdrawLimit(
|
|
212
|
+
bank: BankAccount,
|
|
213
|
+
now: BN
|
|
214
|
+
): { borrowLimit: BN; withdrawLimit: BN } {
|
|
215
|
+
const bankDepositTokenAmount = getTokenAmount(
|
|
216
|
+
bank.depositBalance,
|
|
217
|
+
bank,
|
|
218
|
+
BankBalanceType.DEPOSIT
|
|
219
|
+
);
|
|
220
|
+
const bankBorrowTokenAmount = getTokenAmount(
|
|
221
|
+
bank.borrowBalance,
|
|
222
|
+
bank,
|
|
223
|
+
BankBalanceType.BORROW
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
const twentyFourHours = new BN(60 * 60 * 24);
|
|
227
|
+
const sinceLast = now.sub(bank.lastUpdated);
|
|
228
|
+
const sinceStart = BN.max(ZERO, twentyFourHours.sub(sinceLast));
|
|
229
|
+
const borrowTokenTwapLive = bank.borrowTokenTwap
|
|
230
|
+
.mul(sinceStart)
|
|
231
|
+
.add(bankBorrowTokenAmount.mul(sinceLast))
|
|
232
|
+
.div(sinceLast.add(sinceLast));
|
|
233
|
+
|
|
234
|
+
const depositTokenTwapLive = bank.depositTokenTwap
|
|
235
|
+
.mul(sinceStart)
|
|
236
|
+
.add(bankDepositTokenAmount.mul(sinceLast))
|
|
237
|
+
.div(sinceLast.add(sinceLast));
|
|
238
|
+
|
|
239
|
+
const maxBorrowTokens = BN.min(
|
|
240
|
+
BN.max(
|
|
241
|
+
bankDepositTokenAmount.div(new BN(6)),
|
|
242
|
+
borrowTokenTwapLive.add(borrowTokenTwapLive.div(new BN(5)))
|
|
243
|
+
),
|
|
244
|
+
bankDepositTokenAmount.sub(bankDepositTokenAmount.div(new BN(10)))
|
|
245
|
+
); // between ~15-90% utilization with friction on twap
|
|
246
|
+
|
|
247
|
+
const minDepositTokens = depositTokenTwapLive.sub(
|
|
248
|
+
BN.min(
|
|
249
|
+
BN.max(depositTokenTwapLive.div(new BN(5)), bank.withdrawGuardThreshold),
|
|
250
|
+
depositTokenTwapLive
|
|
251
|
+
)
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
borrowLimit: maxBorrowTokens.sub(bankBorrowTokenAmount),
|
|
256
|
+
withdrawLimit: bankDepositTokenAmount.sub(minDepositTokens),
|
|
257
|
+
};
|
|
258
|
+
}
|
package/src/math/oracles.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { AMM, OracleGuardRails } from '../types';
|
|
2
2
|
import { OraclePriceData } from '../oracles/types';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
BID_ASK_SPREAD_PRECISION,
|
|
5
|
+
MARK_PRICE_PRECISION,
|
|
6
|
+
ONE,
|
|
7
|
+
ZERO,
|
|
8
|
+
} from '../constants/numericConstants';
|
|
4
9
|
import { BN } from '../index';
|
|
5
10
|
|
|
6
11
|
export function isOracleValid(
|
|
@@ -9,7 +14,7 @@ export function isOracleValid(
|
|
|
9
14
|
oracleGuardRails: OracleGuardRails,
|
|
10
15
|
slot: number
|
|
11
16
|
): boolean {
|
|
12
|
-
const isOraclePriceNonPositive = oraclePriceData.price.
|
|
17
|
+
const isOraclePriceNonPositive = oraclePriceData.price.lte(ZERO);
|
|
13
18
|
const isOraclePriceTooVolatile =
|
|
14
19
|
oraclePriceData.price
|
|
15
20
|
.div(BN.max(ONE, amm.lastOraclePriceTwap))
|
|
@@ -18,9 +23,11 @@ export function isOracleValid(
|
|
|
18
23
|
.div(BN.max(ONE, oraclePriceData.price))
|
|
19
24
|
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
20
25
|
|
|
21
|
-
const isConfidenceTooLarge =
|
|
22
|
-
.
|
|
23
|
-
.
|
|
26
|
+
const isConfidenceTooLarge = new BN(amm.baseSpread)
|
|
27
|
+
.add(BN.max(ONE, oraclePriceData.confidence))
|
|
28
|
+
.mul(BID_ASK_SPREAD_PRECISION)
|
|
29
|
+
.div(oraclePriceData.price)
|
|
30
|
+
.gt(new BN(amm.maxSpread));
|
|
24
31
|
|
|
25
32
|
const oracleIsStale = oraclePriceData.slot
|
|
26
33
|
.sub(new BN(slot))
|
|
@@ -34,3 +41,33 @@ export function isOracleValid(
|
|
|
34
41
|
isConfidenceTooLarge
|
|
35
42
|
);
|
|
36
43
|
}
|
|
44
|
+
|
|
45
|
+
export function isOracleTooDivergent(
|
|
46
|
+
amm: AMM,
|
|
47
|
+
oraclePriceData: OraclePriceData,
|
|
48
|
+
oracleGuardRails: OracleGuardRails,
|
|
49
|
+
now: BN
|
|
50
|
+
): boolean {
|
|
51
|
+
const sinceLastUpdate = now.sub(amm.lastOraclePriceTwapTs);
|
|
52
|
+
const sinceStart = BN.max(ZERO, new BN(60 * 5).sub(sinceLastUpdate));
|
|
53
|
+
const oracleTwap5min = amm.lastOraclePriceTwap5min
|
|
54
|
+
.mul(sinceStart)
|
|
55
|
+
.add(oraclePriceData.price)
|
|
56
|
+
.mul(sinceLastUpdate)
|
|
57
|
+
.div(sinceStart.add(sinceLastUpdate));
|
|
58
|
+
|
|
59
|
+
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
60
|
+
const oracleSpreadPct = oracleSpread
|
|
61
|
+
.mul(MARK_PRICE_PRECISION)
|
|
62
|
+
.div(oracleTwap5min);
|
|
63
|
+
|
|
64
|
+
const tooDivergent = oracleSpreadPct
|
|
65
|
+
.abs()
|
|
66
|
+
.gte(
|
|
67
|
+
BID_ASK_SPREAD_PRECISION.mul(
|
|
68
|
+
oracleGuardRails.priceDivergence.markOracleDivergenceNumerator
|
|
69
|
+
).div(oracleGuardRails.priceDivergence.markOracleDivergenceDenominator)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return tooDivergent;
|
|
73
|
+
}
|
package/src/math/position.ts
CHANGED
|
@@ -189,7 +189,8 @@ export function positionIsAvailable(position: UserPosition): boolean {
|
|
|
189
189
|
return (
|
|
190
190
|
position.baseAssetAmount.eq(ZERO) &&
|
|
191
191
|
position.openOrders.eq(ZERO) &&
|
|
192
|
-
position.quoteAssetAmount.eq(ZERO)
|
|
192
|
+
position.quoteAssetAmount.eq(ZERO) &&
|
|
193
|
+
position.lpShares.eq(ZERO)
|
|
193
194
|
);
|
|
194
195
|
}
|
|
195
196
|
|
package/src/math/trade.ts
CHANGED
|
@@ -177,13 +177,13 @@ export function calculateTradeAcquiredAmounts(
|
|
|
177
177
|
|
|
178
178
|
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
179
179
|
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
180
|
-
const
|
|
180
|
+
const acquiredQuoteAssetAmount = calculateQuoteAssetAmountSwapped(
|
|
181
181
|
acquiredQuote.abs(),
|
|
182
182
|
amm.pegMultiplier,
|
|
183
183
|
swapDirection
|
|
184
184
|
);
|
|
185
185
|
|
|
186
|
-
return [acquiredBase, acquiredQuote,
|
|
186
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetAmount];
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
/**
|
package/src/types.ts
CHANGED
|
@@ -177,6 +177,8 @@ export type LiquidationRecord = {
|
|
|
177
177
|
liquidateBorrow: LiquidateBorrowRecord;
|
|
178
178
|
liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
|
|
179
179
|
liquidatePerpPnlForDeposit: LiquidatePerpPnlForDepositRecord;
|
|
180
|
+
perpBankruptcy: PerpBankruptcyRecord;
|
|
181
|
+
borrowBankruptcy: BorrowBankruptcyRecord;
|
|
180
182
|
};
|
|
181
183
|
|
|
182
184
|
export class LiquidationType {
|
|
@@ -188,6 +190,12 @@ export class LiquidationType {
|
|
|
188
190
|
static readonly LIQUIDATE_PERP_PNL_FOR_DEPOSIT = {
|
|
189
191
|
liquidatePerpPnlForDeposit: {},
|
|
190
192
|
};
|
|
193
|
+
static readonly PERP_BANKRUPTCY = {
|
|
194
|
+
perpBankruptcy: {},
|
|
195
|
+
};
|
|
196
|
+
static readonly BORROW_BANKRUPTCY = {
|
|
197
|
+
borrowBankruptcy: {},
|
|
198
|
+
};
|
|
191
199
|
}
|
|
192
200
|
|
|
193
201
|
export type LiquidatePerpRecord = {
|
|
@@ -231,14 +239,27 @@ export type LiquidatePerpPnlForDepositRecord = {
|
|
|
231
239
|
assetTransfer: BN;
|
|
232
240
|
};
|
|
233
241
|
|
|
242
|
+
export type PerpBankruptcyRecord = {
|
|
243
|
+
marketIndex: BN;
|
|
244
|
+
pnl: BN;
|
|
245
|
+
cumulativeFundingRateDelta: BN;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export type BorrowBankruptcyRecord = {
|
|
249
|
+
bankIndex: BN;
|
|
250
|
+
borrowAmount: BN;
|
|
251
|
+
cumulativeDepositInterestDelta: BN;
|
|
252
|
+
};
|
|
253
|
+
|
|
234
254
|
export type SettlePnlRecord = {
|
|
235
255
|
ts: BN;
|
|
256
|
+
user: PublicKey;
|
|
236
257
|
marketIndex: BN;
|
|
237
258
|
pnl: BN;
|
|
238
259
|
baseAssetAmount: BN;
|
|
239
260
|
quoteAssetAmountAfter: BN;
|
|
240
261
|
quoteEntryamount: BN;
|
|
241
|
-
|
|
262
|
+
settlePrice: BN;
|
|
242
263
|
};
|
|
243
264
|
|
|
244
265
|
export type OrderRecord = {
|
|
@@ -337,6 +358,11 @@ export type BankAccount = {
|
|
|
337
358
|
maintenanceLiabilityWeight: BN;
|
|
338
359
|
liquidationFee: BN;
|
|
339
360
|
imfFactor: BN;
|
|
361
|
+
|
|
362
|
+
withdrawGuardThreshold: BN;
|
|
363
|
+
depositTokenTwap: BN;
|
|
364
|
+
borrowTokenTwap: BN;
|
|
365
|
+
utilizationTwap: BN;
|
|
340
366
|
};
|
|
341
367
|
|
|
342
368
|
export type PoolBalance = {
|
|
@@ -350,8 +376,10 @@ export type AMM = {
|
|
|
350
376
|
lastFundingRate: BN;
|
|
351
377
|
lastFundingRateTs: BN;
|
|
352
378
|
lastMarkPriceTwap: BN;
|
|
379
|
+
lastMarkPriceTwap5min: BN;
|
|
353
380
|
lastMarkPriceTwapTs: BN;
|
|
354
381
|
lastOraclePriceTwap: BN;
|
|
382
|
+
lastOraclePriceTwap5min: BN;
|
|
355
383
|
lastOraclePriceTwapTs: BN;
|
|
356
384
|
lastOracleMarkSpreadPct: BN;
|
|
357
385
|
lastOracleConfPct: BN;
|
|
@@ -362,11 +390,16 @@ export type AMM = {
|
|
|
362
390
|
pegMultiplier: BN;
|
|
363
391
|
cumulativeFundingRateLong: BN;
|
|
364
392
|
cumulativeFundingRateShort: BN;
|
|
393
|
+
cumulativeFundingRateLp: BN;
|
|
365
394
|
cumulativeRepegRebateLong: BN;
|
|
366
395
|
cumulativeRepegRebateShort: BN;
|
|
367
396
|
totalFeeMinusDistributions: BN;
|
|
368
397
|
totalFeeWithdrawn: BN;
|
|
369
398
|
totalFee: BN;
|
|
399
|
+
cumulativeFundingPaymentPerLp: BN;
|
|
400
|
+
cumulativeFeePerLp: BN;
|
|
401
|
+
cumulativeNetBaseAssetAmountPerLp: BN;
|
|
402
|
+
userLpShares: BN;
|
|
370
403
|
minimumQuoteAssetTradeSize: BN;
|
|
371
404
|
baseAssetAmountStepSize: BN;
|
|
372
405
|
maxBaseAssetAmountRatio: number;
|
|
@@ -388,6 +421,8 @@ export type AMM = {
|
|
|
388
421
|
longSpread: BN;
|
|
389
422
|
shortSpread: BN;
|
|
390
423
|
maxSpread: number;
|
|
424
|
+
marketPosition: UserPosition;
|
|
425
|
+
marketPositionPerLp: UserPosition;
|
|
391
426
|
};
|
|
392
427
|
|
|
393
428
|
// # User Account Types
|
|
@@ -400,15 +435,21 @@ export type UserPosition = {
|
|
|
400
435
|
openOrders: BN;
|
|
401
436
|
openBids: BN;
|
|
402
437
|
openAsks: BN;
|
|
438
|
+
realizedPnl: BN;
|
|
439
|
+
lpShares: BN;
|
|
440
|
+
lastFeePerLp: BN;
|
|
441
|
+
lastNetBaseAssetAmountPerLp: BN;
|
|
442
|
+
lastNetQuoteAssetAmountPerLp: BN;
|
|
403
443
|
};
|
|
404
444
|
|
|
405
|
-
export type
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
445
|
+
export type UserStatsAccount = {
|
|
446
|
+
numberOfUsers: number;
|
|
447
|
+
makerVolume30D: BN;
|
|
448
|
+
takerVolume30D: BN;
|
|
449
|
+
fillerVolume30D: BN;
|
|
450
|
+
lastMakerVolume30DTs: BN;
|
|
451
|
+
lastTakerVolume30DTs: BN;
|
|
452
|
+
lastFillerVolume30DTs: BN;
|
|
412
453
|
fees: {
|
|
413
454
|
totalFeePaid: BN;
|
|
414
455
|
totalFeeRebate: BN;
|
|
@@ -416,9 +457,17 @@ export type UserAccount = {
|
|
|
416
457
|
totalReferralReward: BN;
|
|
417
458
|
totalRefereeDiscount: BN;
|
|
418
459
|
};
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
export type UserAccount = {
|
|
463
|
+
authority: PublicKey;
|
|
464
|
+
name: number[];
|
|
465
|
+
userId: number;
|
|
466
|
+
bankBalances: UserBankBalance[];
|
|
419
467
|
positions: UserPosition[];
|
|
420
468
|
orders: Order[];
|
|
421
469
|
beingLiquidated: boolean;
|
|
470
|
+
bankrupt: boolean;
|
|
422
471
|
nextLiquidationId: number;
|
|
423
472
|
};
|
|
424
473
|
|
|
@@ -515,11 +564,13 @@ export const DefaultOrderParams = {
|
|
|
515
564
|
|
|
516
565
|
export type MakerInfo = {
|
|
517
566
|
maker: PublicKey;
|
|
567
|
+
makerStats: PublicKey;
|
|
518
568
|
order: Order;
|
|
519
569
|
};
|
|
520
570
|
|
|
521
571
|
export type TakerInfo = {
|
|
522
572
|
taker: PublicKey;
|
|
573
|
+
takerStats: PublicKey;
|
|
523
574
|
order: Order;
|
|
524
575
|
};
|
|
525
576
|
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.BulkAccountLoader = void 0;
|
|
13
|
-
const uuid_1 = require("uuid");
|
|
14
|
-
const promiseTimeout_1 = require("../util/promiseTimeout");
|
|
15
|
-
const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
|
|
16
|
-
const oneMinute = 60 * 1000;
|
|
17
|
-
class BulkAccountLoader {
|
|
18
|
-
constructor(connection, commitment, pollingFrequency) {
|
|
19
|
-
this.accountsToLoad = new Map();
|
|
20
|
-
this.bufferAndSlotMap = new Map();
|
|
21
|
-
this.errorCallbacks = new Map();
|
|
22
|
-
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
23
|
-
this.mostRecentSlot = 0;
|
|
24
|
-
this.connection = connection;
|
|
25
|
-
this.commitment = commitment;
|
|
26
|
-
this.pollingFrequency = pollingFrequency;
|
|
27
|
-
}
|
|
28
|
-
addAccount(publicKey, callback) {
|
|
29
|
-
const existingSize = this.accountsToLoad.size;
|
|
30
|
-
const callbackId = uuid_1.v4();
|
|
31
|
-
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
32
|
-
if (existingAccountToLoad) {
|
|
33
|
-
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
const callbacks = new Map();
|
|
37
|
-
callbacks.set(callbackId, callback);
|
|
38
|
-
const newAccountToLoad = {
|
|
39
|
-
publicKey,
|
|
40
|
-
callbacks,
|
|
41
|
-
};
|
|
42
|
-
this.accountsToLoad.set(publicKey.toString(), newAccountToLoad);
|
|
43
|
-
}
|
|
44
|
-
if (existingSize === 0) {
|
|
45
|
-
this.startPolling();
|
|
46
|
-
}
|
|
47
|
-
// if a new account needs to be polled, remove the cached loadPromise in case client calls load immediately after
|
|
48
|
-
this.loadPromise = undefined;
|
|
49
|
-
return callbackId;
|
|
50
|
-
}
|
|
51
|
-
removeAccount(publicKey, callbackId) {
|
|
52
|
-
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
53
|
-
if (existingAccountToLoad) {
|
|
54
|
-
existingAccountToLoad.callbacks.delete(callbackId);
|
|
55
|
-
if (existingAccountToLoad.callbacks.size === 0) {
|
|
56
|
-
this.accountsToLoad.delete(existingAccountToLoad.publicKey.toString());
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (this.accountsToLoad.size === 0) {
|
|
60
|
-
this.stopPolling();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
addErrorCallbacks(callback) {
|
|
64
|
-
const callbackId = uuid_1.v4();
|
|
65
|
-
this.errorCallbacks.set(callbackId, callback);
|
|
66
|
-
return callbackId;
|
|
67
|
-
}
|
|
68
|
-
removeErrorCallbacks(callbackId) {
|
|
69
|
-
this.errorCallbacks.delete(callbackId);
|
|
70
|
-
}
|
|
71
|
-
chunks(array, size) {
|
|
72
|
-
return new Array(Math.ceil(array.length / size))
|
|
73
|
-
.fill(null)
|
|
74
|
-
.map((_, index) => index * size)
|
|
75
|
-
.map((begin) => array.slice(begin, begin + size));
|
|
76
|
-
}
|
|
77
|
-
load() {
|
|
78
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
if (this.loadPromise) {
|
|
80
|
-
const now = Date.now();
|
|
81
|
-
if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
|
|
82
|
-
this.loadPromise = undefined;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
return this.loadPromise;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
this.loadPromise = new Promise((resolver) => {
|
|
89
|
-
this.loadPromiseResolver = resolver;
|
|
90
|
-
});
|
|
91
|
-
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
92
|
-
try {
|
|
93
|
-
const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
|
|
94
|
-
yield Promise.all(chunks.map((chunk) => {
|
|
95
|
-
return this.loadChunk(chunk);
|
|
96
|
-
}));
|
|
97
|
-
}
|
|
98
|
-
catch (e) {
|
|
99
|
-
console.error(`Error in bulkAccountLoader.load()`);
|
|
100
|
-
console.error(e);
|
|
101
|
-
for (const [_, callback] of this.errorCallbacks) {
|
|
102
|
-
callback(e);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
finally {
|
|
106
|
-
this.loadPromiseResolver();
|
|
107
|
-
this.loadPromise = undefined;
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
loadChunk(accountsToLoad) {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
if (accountsToLoad.length === 0) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const args = [
|
|
117
|
-
accountsToLoad.map((accountToLoad) => {
|
|
118
|
-
return accountToLoad.publicKey.toBase58();
|
|
119
|
-
}),
|
|
120
|
-
{ commitment: this.commitment },
|
|
121
|
-
];
|
|
122
|
-
const rpcResponse = yield promiseTimeout_1.promiseTimeout(
|
|
123
|
-
// @ts-ignore
|
|
124
|
-
this.connection._rpcRequest('getMultipleAccounts', args), 10 * 1000 // 30 second timeout
|
|
125
|
-
);
|
|
126
|
-
if (rpcResponse === null) {
|
|
127
|
-
this.log('request to rpc timed out');
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
const newSlot = rpcResponse.result.context.slot;
|
|
131
|
-
if (newSlot > this.mostRecentSlot) {
|
|
132
|
-
this.mostRecentSlot = newSlot;
|
|
133
|
-
}
|
|
134
|
-
for (const i in accountsToLoad) {
|
|
135
|
-
const accountToLoad = accountsToLoad[i];
|
|
136
|
-
const key = accountToLoad.publicKey.toString();
|
|
137
|
-
const oldRPCResponse = this.bufferAndSlotMap.get(key);
|
|
138
|
-
let newBuffer = undefined;
|
|
139
|
-
if (rpcResponse.result.value[i]) {
|
|
140
|
-
const raw = rpcResponse.result.value[i].data[0];
|
|
141
|
-
const dataType = rpcResponse.result.value[i].data[1];
|
|
142
|
-
newBuffer = Buffer.from(raw, dataType);
|
|
143
|
-
}
|
|
144
|
-
if (!oldRPCResponse) {
|
|
145
|
-
this.bufferAndSlotMap.set(key, {
|
|
146
|
-
slot: newSlot,
|
|
147
|
-
buffer: newBuffer,
|
|
148
|
-
});
|
|
149
|
-
this.handleAccountCallbacks(accountToLoad, newBuffer, newSlot);
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
if (newSlot <= oldRPCResponse.slot) {
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
const oldBuffer = oldRPCResponse.buffer;
|
|
156
|
-
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
157
|
-
this.bufferAndSlotMap.set(key, {
|
|
158
|
-
slot: newSlot,
|
|
159
|
-
buffer: newBuffer,
|
|
160
|
-
});
|
|
161
|
-
this.handleAccountCallbacks(accountToLoad, newBuffer, newSlot);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
handleAccountCallbacks(accountToLoad, buffer, slot) {
|
|
167
|
-
for (const [_, callback] of accountToLoad.callbacks) {
|
|
168
|
-
callback(buffer, slot);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
getBufferAndSlot(publicKey) {
|
|
172
|
-
return this.bufferAndSlotMap.get(publicKey.toString());
|
|
173
|
-
}
|
|
174
|
-
startPolling() {
|
|
175
|
-
if (this.intervalId) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
179
|
-
}
|
|
180
|
-
stopPolling() {
|
|
181
|
-
if (this.intervalId) {
|
|
182
|
-
clearInterval(this.intervalId);
|
|
183
|
-
this.intervalId = undefined;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
log(msg) {
|
|
187
|
-
console.log(msg);
|
|
188
|
-
}
|
|
189
|
-
updatePollingFrequency(pollingFrequency) {
|
|
190
|
-
this.stopPolling();
|
|
191
|
-
this.pollingFrequency = pollingFrequency;
|
|
192
|
-
if (this.accountsToLoad.size > 0) {
|
|
193
|
-
this.startPolling();
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
exports.BulkAccountLoader = BulkAccountLoader;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.bulkPollingUserSubscribe = void 0;
|
|
13
|
-
/**
|
|
14
|
-
* @param users
|
|
15
|
-
* @param accountLoader
|
|
16
|
-
*/
|
|
17
|
-
function bulkPollingUserSubscribe(users, accountLoader) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
if (users.length === 0) {
|
|
20
|
-
yield accountLoader.load();
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
yield Promise.all(users.map((user) => {
|
|
24
|
-
// Pull the keys from the authority map so we can skip fetching them in addToAccountLoader
|
|
25
|
-
return user.accountSubscriber.addToAccountLoader();
|
|
26
|
-
}));
|
|
27
|
-
yield accountLoader.load();
|
|
28
|
-
yield Promise.all(users.map((user) => __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
return user.subscribe();
|
|
30
|
-
})));
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
exports.bulkPollingUserSubscribe = bulkPollingUserSubscribe;
|