@drift-labs/sdk 0.2.0-master.20 → 0.2.0-master.23
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/addresses/pda.d.ts +3 -0
- package/lib/addresses/pda.js +23 -1
- package/lib/admin.d.ts +3 -0
- package/lib/admin.js +31 -0
- package/lib/clearingHouse.d.ts +11 -4
- package/lib/clearingHouse.js +157 -23
- package/lib/clearingHouseUser.d.ts +9 -1
- package/lib/clearingHouseUser.js +62 -25
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -0
- package/lib/constants/banks.js +9 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +4 -1
- package/lib/events/sort.js +7 -10
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/idl/clearing_house.json +1008 -147
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/amm.js +2 -2
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +1 -1
- package/lib/math/margin.js +5 -7
- package/lib/math/orders.d.ts +2 -1
- package/lib/math/orders.js +14 -5
- package/lib/math/position.js +1 -1
- package/lib/types.d.ts +59 -20
- package/lib/types.js +0 -3
- package/package.json +1 -1
- package/src/addresses/pda.ts +47 -0
- package/src/admin.ts +65 -0
- package/src/clearingHouse.ts +244 -32
- package/src/clearingHouseUser.ts +77 -48
- package/src/config.ts +1 -1
- package/src/constants/banks.ts +16 -0
- package/src/constants/numericConstants.ts +4 -0
- package/src/events/sort.ts +10 -14
- package/src/events/types.ts +3 -0
- package/src/idl/clearing_house.json +1008 -147
- package/src/index.ts +1 -0
- package/src/math/amm.ts +2 -2
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +3 -10
- package/src/math/orders.ts +26 -9
- package/src/math/position.ts +2 -2
- package/src/types.ts +67 -20
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/webSocketLogProvider.js +0 -41
package/src/index.ts
CHANGED
|
@@ -41,6 +41,7 @@ export * from './math/trade';
|
|
|
41
41
|
export * from './math/orders';
|
|
42
42
|
export * from './math/repeg';
|
|
43
43
|
export * from './math/margin';
|
|
44
|
+
export * from './math/insurance';
|
|
44
45
|
export * from './orderParams';
|
|
45
46
|
export * from './slot/SlotSubscriber';
|
|
46
47
|
export * from './wallet';
|
package/src/math/amm.ts
CHANGED
|
@@ -635,12 +635,12 @@ export function calculateMaxBaseAssetAmountFillable(
|
|
|
635
635
|
if (isVariant(orderDirection, 'long')) {
|
|
636
636
|
maxBaseAssetAmountOnSide = BN.max(
|
|
637
637
|
ZERO,
|
|
638
|
-
amm.
|
|
638
|
+
amm.baseAssetReserve.sub(amm.minBaseAssetReserve)
|
|
639
639
|
);
|
|
640
640
|
} else {
|
|
641
641
|
maxBaseAssetAmountOnSide = BN.max(
|
|
642
642
|
ZERO,
|
|
643
|
-
amm.
|
|
643
|
+
amm.maxBaseAssetReserve.sub(amm.baseAssetReserve)
|
|
644
644
|
);
|
|
645
645
|
}
|
|
646
646
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ONE, ZERO } from '../constants/numericConstants';
|
|
2
|
+
import { BN } from '../index';
|
|
3
|
+
|
|
4
|
+
export function stakeAmountToShares(
|
|
5
|
+
amount: BN,
|
|
6
|
+
totalIfShares: BN,
|
|
7
|
+
insuranceFundVaultBalance: BN
|
|
8
|
+
): BN {
|
|
9
|
+
let nShares: BN;
|
|
10
|
+
if (insuranceFundVaultBalance.gt(ZERO)) {
|
|
11
|
+
nShares = amount.mul(totalIfShares).div(insuranceFundVaultBalance);
|
|
12
|
+
} else {
|
|
13
|
+
nShares = amount;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return nShares;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function unstakeSharesToAmount(
|
|
20
|
+
nShares: BN,
|
|
21
|
+
totalIfShares: BN,
|
|
22
|
+
insuranceFundVaultBalance: BN
|
|
23
|
+
): BN {
|
|
24
|
+
let amount: BN;
|
|
25
|
+
if (totalIfShares.gt(ZERO)) {
|
|
26
|
+
amount = BN.max(
|
|
27
|
+
ZERO,
|
|
28
|
+
nShares.mul(insuranceFundVaultBalance).div(totalIfShares).sub(ONE)
|
|
29
|
+
);
|
|
30
|
+
} else {
|
|
31
|
+
amount = ZERO;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return amount;
|
|
35
|
+
}
|
package/src/math/margin.ts
CHANGED
|
@@ -92,22 +92,15 @@ export function calculateOraclePriceForPerpMargin(
|
|
|
92
92
|
return marginPrice;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
export function
|
|
95
|
+
export function calculateBaseAssetValueWithOracle(
|
|
96
96
|
market: MarketAccount,
|
|
97
97
|
marketPosition: UserPosition,
|
|
98
98
|
oraclePriceData: OraclePriceData
|
|
99
99
|
): BN {
|
|
100
|
-
|
|
101
|
-
marketPosition,
|
|
102
|
-
market,
|
|
103
|
-
oraclePriceData
|
|
104
|
-
);
|
|
105
|
-
const baseAssetValue = marketPosition.baseAssetAmount
|
|
100
|
+
return marketPosition.baseAssetAmount
|
|
106
101
|
.abs()
|
|
107
|
-
.mul(
|
|
102
|
+
.mul(oraclePriceData.price)
|
|
108
103
|
.div(AMM_TO_QUOTE_PRECISION_RATIO.mul(MARK_PRICE_PRECISION));
|
|
109
|
-
|
|
110
|
-
return baseAssetValue;
|
|
111
104
|
}
|
|
112
105
|
|
|
113
106
|
export function calculateWorstCaseBaseAssetAmount(
|
package/src/math/orders.ts
CHANGED
|
@@ -139,7 +139,7 @@ export function getLimitPrice(
|
|
|
139
139
|
if (!order.oraclePriceOffset.eq(ZERO)) {
|
|
140
140
|
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
141
141
|
} else if (isOneOfVariant(order.orderType, ['market', 'triggerMarket'])) {
|
|
142
|
-
if (isAuctionComplete(order, slot)) {
|
|
142
|
+
if (!isAuctionComplete(order, slot)) {
|
|
143
143
|
limitPrice = getAuctionPrice(order, slot);
|
|
144
144
|
} else if (!order.price.eq(ZERO)) {
|
|
145
145
|
limitPrice = order.price;
|
|
@@ -163,16 +163,18 @@ export function isFillableByVAMM(
|
|
|
163
163
|
order: Order,
|
|
164
164
|
market: MarketAccount,
|
|
165
165
|
oraclePriceData: OraclePriceData,
|
|
166
|
-
slot: number
|
|
166
|
+
slot: number,
|
|
167
|
+
maxAuctionDuration: number
|
|
167
168
|
): boolean {
|
|
168
169
|
return (
|
|
169
|
-
isAuctionComplete(order, slot) &&
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
170
|
+
(isAuctionComplete(order, slot) &&
|
|
171
|
+
!calculateBaseAssetAmountForAmmToFulfill(
|
|
172
|
+
order,
|
|
173
|
+
market,
|
|
174
|
+
oraclePriceData,
|
|
175
|
+
slot
|
|
176
|
+
).eq(ZERO)) ||
|
|
177
|
+
isOrderExpired(order, slot, maxAuctionDuration)
|
|
176
178
|
);
|
|
177
179
|
}
|
|
178
180
|
|
|
@@ -243,3 +245,18 @@ function isSameDirection(
|
|
|
243
245
|
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
244
246
|
);
|
|
245
247
|
}
|
|
248
|
+
|
|
249
|
+
export function isOrderExpired(
|
|
250
|
+
order: Order,
|
|
251
|
+
slot: number,
|
|
252
|
+
maxAuctionDuration: number
|
|
253
|
+
): boolean {
|
|
254
|
+
if (
|
|
255
|
+
!isVariant(order.orderType, 'market') ||
|
|
256
|
+
!isVariant(order.status, 'open')
|
|
257
|
+
) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return new BN(slot).sub(order.slot).gt(new BN(maxAuctionDuration));
|
|
262
|
+
}
|
package/src/math/position.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
getSwapDirection,
|
|
19
19
|
} from './amm';
|
|
20
20
|
|
|
21
|
-
import {
|
|
21
|
+
import { calculateBaseAssetValueWithOracle } from './margin';
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* calculateBaseAssetValue
|
|
@@ -99,7 +99,7 @@ export function calculatePositionPNL(
|
|
|
99
99
|
return marketPosition.quoteAssetAmount;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
const baseAssetValue =
|
|
102
|
+
const baseAssetValue = calculateBaseAssetValueWithOracle(
|
|
103
103
|
market,
|
|
104
104
|
marketPosition,
|
|
105
105
|
oraclePriceData
|
package/src/types.ts
CHANGED
|
@@ -58,9 +58,6 @@ export class OrderAction {
|
|
|
58
58
|
|
|
59
59
|
export class OrderActionExplanation {
|
|
60
60
|
static readonly NONE = { none: {} };
|
|
61
|
-
static readonly BREACHED_MARGIN_REQUIREMENT = {
|
|
62
|
-
breachedMarginRequirement: {},
|
|
63
|
-
};
|
|
64
61
|
static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
|
|
65
62
|
oraclePriceBreachedLimitPrice: {},
|
|
66
63
|
};
|
|
@@ -156,10 +153,15 @@ export type FundingRateRecord = {
|
|
|
156
153
|
recordId: BN;
|
|
157
154
|
marketIndex: BN;
|
|
158
155
|
fundingRate: BN;
|
|
156
|
+
fundingRateLong: BN;
|
|
157
|
+
fundingRateShort: BN;
|
|
159
158
|
cumulativeFundingRateLong: BN;
|
|
160
159
|
cumulativeFundingRateShort: BN;
|
|
161
160
|
oraclePriceTwap: BN;
|
|
162
161
|
markPriceTwap: BN;
|
|
162
|
+
periodRevenue: BN;
|
|
163
|
+
netBaseAssetAmount: BN;
|
|
164
|
+
netUnsettledLpBaseAssetAmount: BN;
|
|
163
165
|
};
|
|
164
166
|
|
|
165
167
|
export type FundingPaymentRecord = {
|
|
@@ -274,27 +276,41 @@ export type SettlePnlRecord = {
|
|
|
274
276
|
|
|
275
277
|
export type OrderRecord = {
|
|
276
278
|
ts: BN;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
279
|
+
user: PublicKey;
|
|
280
|
+
order: Order;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
export type OrderActionRecord = {
|
|
284
|
+
ts: BN;
|
|
283
285
|
action: OrderAction;
|
|
284
286
|
actionExplanation: OrderActionExplanation;
|
|
285
|
-
filler: PublicKey;
|
|
286
|
-
fillRecordId: BN;
|
|
287
287
|
marketIndex: BN;
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
288
|
+
filler: PublicKey | null;
|
|
289
|
+
fillerReward: BN | null;
|
|
290
|
+
fillRecordId: BN | null;
|
|
291
|
+
referrer: PublicKey | null;
|
|
292
|
+
baseAssetAmountFilled: BN | null;
|
|
293
|
+
quoteAssetAmountFilled: BN | null;
|
|
294
|
+
takerPnl: BN | null;
|
|
295
|
+
makerPnl: BN | null;
|
|
296
|
+
takerFee: BN | null;
|
|
297
|
+
makerRebate: BN | null;
|
|
298
|
+
referrerReward: BN | null;
|
|
299
|
+
refereeDiscount: BN | null;
|
|
300
|
+
quoteAssetAmountSurplus: BN | null;
|
|
301
|
+
taker: PublicKey | null;
|
|
302
|
+
takerOrderId: BN | null;
|
|
303
|
+
takerOrderBaseAssetAmount: BN | null;
|
|
304
|
+
takerOrderBaseAssetAmountFilled: BN | null;
|
|
305
|
+
takerOrderQuoteAssetAmountFilled: BN | null;
|
|
306
|
+
takerOrderFee: BN | null;
|
|
307
|
+
maker: PublicKey | null;
|
|
308
|
+
makerOrderId: BN | null;
|
|
309
|
+
makerOrderBaseAssetAmount: BN | null;
|
|
310
|
+
makerOrderBaseAssetAmountFilled: BN | null;
|
|
311
|
+
makerOrderQuoteAssetAmountFilled: BN | null;
|
|
312
|
+
makerOrderFee: BN | null;
|
|
294
313
|
oraclePrice: BN;
|
|
295
|
-
referrer: PublicKey;
|
|
296
|
-
referrerReward: BN;
|
|
297
|
-
refereeDiscount: BN;
|
|
298
314
|
};
|
|
299
315
|
|
|
300
316
|
export type StateAccount = {
|
|
@@ -326,6 +342,8 @@ export type StateAccount = {
|
|
|
326
342
|
numberOfMarkets: BN;
|
|
327
343
|
numberOfBanks: BN;
|
|
328
344
|
minOrderQuoteAssetAmount: BN;
|
|
345
|
+
maxAuctionDuration: number;
|
|
346
|
+
minAuctionDuration: number;
|
|
329
347
|
};
|
|
330
348
|
|
|
331
349
|
export type MarketAccount = {
|
|
@@ -355,6 +373,20 @@ export type BankAccount = {
|
|
|
355
373
|
vault: PublicKey;
|
|
356
374
|
vaultAuthority: PublicKey;
|
|
357
375
|
vaultAuthorityNonce: number;
|
|
376
|
+
|
|
377
|
+
insuranceFundVault: PublicKey;
|
|
378
|
+
insuranceFundVaultAuthority: PublicKey;
|
|
379
|
+
insuranceFundVaultAuthorityNonce: number;
|
|
380
|
+
insuranceWithdrawEscrowPeriod: BN;
|
|
381
|
+
revenuePool: PoolBalance;
|
|
382
|
+
|
|
383
|
+
totalIfShares: BN;
|
|
384
|
+
userIfShares: BN;
|
|
385
|
+
|
|
386
|
+
userIfFactor: BN;
|
|
387
|
+
totalIfFactor: BN;
|
|
388
|
+
liquidationIfFactor: BN;
|
|
389
|
+
|
|
358
390
|
decimals: number;
|
|
359
391
|
optimalUtilization: BN;
|
|
360
392
|
optimalBorrowRate: BN;
|
|
@@ -437,6 +469,7 @@ export type AMM = {
|
|
|
437
469
|
maxSpread: number;
|
|
438
470
|
marketPosition: UserPosition;
|
|
439
471
|
marketPositionPerLp: UserPosition;
|
|
472
|
+
ammJitIntensity: number;
|
|
440
473
|
maxBaseAssetReserve: BN;
|
|
441
474
|
minBaseAssetReserve: BN;
|
|
442
475
|
};
|
|
@@ -476,6 +509,7 @@ export type UserStatsAccount = {
|
|
|
476
509
|
isReferrer: boolean;
|
|
477
510
|
totalReferrerReward: BN;
|
|
478
511
|
authority: PublicKey;
|
|
512
|
+
quoteAssetInsuranceFundStake: BN;
|
|
479
513
|
};
|
|
480
514
|
|
|
481
515
|
export type UserAccount = {
|
|
@@ -589,6 +623,7 @@ export type MakerInfo = {
|
|
|
589
623
|
export type TakerInfo = {
|
|
590
624
|
taker: PublicKey;
|
|
591
625
|
takerStats: PublicKey;
|
|
626
|
+
takerUserAccount: UserAccount;
|
|
592
627
|
order: Order;
|
|
593
628
|
};
|
|
594
629
|
|
|
@@ -661,3 +696,15 @@ export type OrderFillerRewardStructure = {
|
|
|
661
696
|
};
|
|
662
697
|
|
|
663
698
|
export type MarginCategory = 'Initial' | 'Maintenance';
|
|
699
|
+
|
|
700
|
+
export type InsuranceFundStake = {
|
|
701
|
+
bankIndex: BN;
|
|
702
|
+
authority: PublicKey;
|
|
703
|
+
|
|
704
|
+
ifShares: BN;
|
|
705
|
+
ifBase: BN;
|
|
706
|
+
|
|
707
|
+
lastWithdrawRequestShares: BN;
|
|
708
|
+
lastWithdrawRequestValue: BN;
|
|
709
|
+
lastWithdrawRequestTs: BN;
|
|
710
|
+
};
|
|
@@ -1,139 +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.EventSubscriber = void 0;
|
|
13
|
-
const types_1 = require("./types");
|
|
14
|
-
const txEventCache_1 = require("./txEventCache");
|
|
15
|
-
const eventList_1 = require("./eventList");
|
|
16
|
-
const pollingLogProvider_1 = require("./pollingLogProvider");
|
|
17
|
-
const fetchLogs_1 = require("./fetchLogs");
|
|
18
|
-
const webSocketLogProvider_1 = require("./webSocketLogProvider");
|
|
19
|
-
const events_1 = require("events");
|
|
20
|
-
const sort_1 = require("./sort");
|
|
21
|
-
class EventSubscriber {
|
|
22
|
-
constructor(connection, program, options = types_1.DefaultEventSubscriptionOptions) {
|
|
23
|
-
this.connection = connection;
|
|
24
|
-
this.program = program;
|
|
25
|
-
this.options = options;
|
|
26
|
-
this.awaitTxPromises = new Map();
|
|
27
|
-
this.awaitTxResolver = new Map();
|
|
28
|
-
this.options = Object.assign({}, types_1.DefaultEventSubscriptionOptions, options);
|
|
29
|
-
this.txEventCache = new txEventCache_1.TxEventCache(this.options.maxTx);
|
|
30
|
-
this.eventListMap = new Map();
|
|
31
|
-
for (const eventType of this.options.eventTypes) {
|
|
32
|
-
this.eventListMap.set(eventType, new eventList_1.EventList(eventType, this.options.maxEventsPerType, sort_1.getSortFn(this.options.orderBy, this.options.orderDir, eventType), this.options.orderDir));
|
|
33
|
-
}
|
|
34
|
-
this.eventEmitter = new events_1.EventEmitter();
|
|
35
|
-
if (this.options.logProviderConfig.type === 'websocket') {
|
|
36
|
-
this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(this.connection, this.program.programId, this.options.commitment);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.logProvider = new pollingLogProvider_1.PollingLogProvider(this.connection, this.program.programId, options.commitment, this.options.logProviderConfig.frequency);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
subscribe() {
|
|
43
|
-
if (this.logProvider.isSubscribed()) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
this.fetchPreviousTx().catch((e) => {
|
|
47
|
-
console.error('Error fetching previous txs in event subscriber');
|
|
48
|
-
console.error(e);
|
|
49
|
-
});
|
|
50
|
-
return this.logProvider.subscribe((txSig, slot, logs) => {
|
|
51
|
-
this.handleTxLogs(txSig, slot, logs);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
handleTxLogs(txSig, slot, logs) {
|
|
55
|
-
if (this.txEventCache.has(txSig)) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const wrappedEvents = this.parseEventsFromLogs(txSig, slot, logs);
|
|
59
|
-
for (const wrappedEvent of wrappedEvents) {
|
|
60
|
-
this.eventListMap.get(wrappedEvent.eventType).insert(wrappedEvent);
|
|
61
|
-
this.eventEmitter.emit('newEvent', wrappedEvent);
|
|
62
|
-
}
|
|
63
|
-
if (this.awaitTxPromises.has(txSig)) {
|
|
64
|
-
this.awaitTxPromises.delete(txSig);
|
|
65
|
-
this.awaitTxResolver.get(txSig)();
|
|
66
|
-
this.awaitTxResolver.delete(txSig);
|
|
67
|
-
}
|
|
68
|
-
this.txEventCache.add(txSig, wrappedEvents);
|
|
69
|
-
}
|
|
70
|
-
fetchPreviousTx() {
|
|
71
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
if (!this.options.untilTx) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
let txFetched = 0;
|
|
76
|
-
let beforeTx = undefined;
|
|
77
|
-
const untilTx = this.options.untilTx;
|
|
78
|
-
while (txFetched < this.options.maxTx) {
|
|
79
|
-
const response = yield fetchLogs_1.fetchLogs(this.connection, this.program.programId, this.options.commitment === 'finalized' ? 'finalized' : 'confirmed', beforeTx, untilTx);
|
|
80
|
-
if (response === undefined) {
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
txFetched += response.transactionLogs.length;
|
|
84
|
-
beforeTx = response.earliestTx;
|
|
85
|
-
for (const { txSig, slot, logs } of response.transactionLogs) {
|
|
86
|
-
this.handleTxLogs(txSig, slot, logs);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
unsubscribe() {
|
|
92
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
return yield this.logProvider.unsubscribe();
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
parseEventsFromLogs(txSig, slot, logs) {
|
|
97
|
-
const records = [];
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
this.program._events._eventParser.parseLogs(logs, (event) => {
|
|
100
|
-
const expectRecordType = this.eventListMap.has(event.name);
|
|
101
|
-
if (expectRecordType) {
|
|
102
|
-
event.data.txSig = txSig;
|
|
103
|
-
event.data.slot = slot;
|
|
104
|
-
event.data.eventType = event.name;
|
|
105
|
-
records.push(event.data);
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
return records;
|
|
109
|
-
}
|
|
110
|
-
awaitTx(txSig) {
|
|
111
|
-
if (this.awaitTxPromises.has(txSig)) {
|
|
112
|
-
return this.awaitTxPromises.get(txSig);
|
|
113
|
-
}
|
|
114
|
-
if (this.txEventCache.has(txSig)) {
|
|
115
|
-
return Promise.resolve();
|
|
116
|
-
}
|
|
117
|
-
const promise = new Promise((resolve) => {
|
|
118
|
-
this.awaitTxResolver.set(txSig, resolve);
|
|
119
|
-
});
|
|
120
|
-
this.awaitTxPromises.set(txSig, promise);
|
|
121
|
-
return promise;
|
|
122
|
-
}
|
|
123
|
-
getEventList(eventType) {
|
|
124
|
-
return this.eventListMap.get(eventType);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* This requires the EventList be cast to an array, which requires reallocation of memory.
|
|
128
|
-
* Would bias to using getEventList over getEvents
|
|
129
|
-
*
|
|
130
|
-
* @param eventType
|
|
131
|
-
*/
|
|
132
|
-
getEventsArray(eventType) {
|
|
133
|
-
return this.eventListMap.get(eventType).toArray();
|
|
134
|
-
}
|
|
135
|
-
getEventsByTx(txSig) {
|
|
136
|
-
return this.txEventCache.get(txSig);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
exports.EventSubscriber = EventSubscriber;
|
package/src/events/fetchLogs.js
DELETED
|
@@ -1,50 +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.fetchLogs = void 0;
|
|
13
|
-
function fetchLogs(connection, programId, finality, beforeTx, untilTx) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
const signatures = yield connection.getSignaturesForAddress(programId, {
|
|
16
|
-
before: beforeTx,
|
|
17
|
-
until: untilTx,
|
|
18
|
-
}, finality);
|
|
19
|
-
const sortedSignatures = signatures.sort((a, b) => a.slot < b.slot ? -1 : 1);
|
|
20
|
-
const filteredSignatures = sortedSignatures.filter((signature) => !signature.err);
|
|
21
|
-
if (filteredSignatures.length === 0) {
|
|
22
|
-
return undefined;
|
|
23
|
-
}
|
|
24
|
-
const chunkedSignatures = chunk(filteredSignatures, 100);
|
|
25
|
-
const transactionLogs = (yield Promise.all(chunkedSignatures.map((chunk) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const transactions = yield connection.getTransactions(chunk.map((confirmedSignature) => confirmedSignature.signature), finality);
|
|
27
|
-
return transactions.map((transaction) => {
|
|
28
|
-
return {
|
|
29
|
-
txSig: transaction.transaction.signatures[0],
|
|
30
|
-
slot: transaction.slot,
|
|
31
|
-
logs: transaction.meta.logMessages,
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
})))).flat();
|
|
35
|
-
const earliestTx = filteredSignatures[0].signature;
|
|
36
|
-
const mostRecentTx = filteredSignatures[filteredSignatures.length - 1].signature;
|
|
37
|
-
return {
|
|
38
|
-
transactionLogs: transactionLogs,
|
|
39
|
-
earliestTx: earliestTx,
|
|
40
|
-
mostRecentTx: mostRecentTx,
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
exports.fetchLogs = fetchLogs;
|
|
45
|
-
function chunk(array, size) {
|
|
46
|
-
return new Array(Math.ceil(array.length / size))
|
|
47
|
-
.fill(null)
|
|
48
|
-
.map((_, index) => index * size)
|
|
49
|
-
.map((begin) => array.slice(begin, begin + size));
|
|
50
|
-
}
|
|
@@ -1,64 +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.PollingLogProvider = void 0;
|
|
13
|
-
const fetchLogs_1 = require("./fetchLogs");
|
|
14
|
-
class PollingLogProvider {
|
|
15
|
-
constructor(connection, programId, commitment, frequency = 15 * 1000) {
|
|
16
|
-
this.connection = connection;
|
|
17
|
-
this.programId = programId;
|
|
18
|
-
this.frequency = frequency;
|
|
19
|
-
this.finality = commitment === 'finalized' ? 'finalized' : 'confirmed';
|
|
20
|
-
}
|
|
21
|
-
subscribe(callback) {
|
|
22
|
-
if (this.intervalId) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
if (this.mutex === 1) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
this.mutex = 1;
|
|
30
|
-
try {
|
|
31
|
-
const response = yield fetchLogs_1.fetchLogs(this.connection, this.programId, this.finality, undefined, this.mostRecentSeenTx);
|
|
32
|
-
if (response === undefined) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const { mostRecentTx, transactionLogs } = response;
|
|
36
|
-
for (const { txSig, slot, logs } of transactionLogs) {
|
|
37
|
-
callback(txSig, slot, logs);
|
|
38
|
-
}
|
|
39
|
-
this.mostRecentSeenTx = mostRecentTx;
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
console.error('PollingLogProvider threw an Error');
|
|
43
|
-
console.error(e);
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
this.mutex = 0;
|
|
47
|
-
}
|
|
48
|
-
}), this.frequency);
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
isSubscribed() {
|
|
52
|
-
return this.intervalId !== undefined;
|
|
53
|
-
}
|
|
54
|
-
unsubscribe() {
|
|
55
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
if (this.intervalId !== undefined) {
|
|
57
|
-
clearInterval(this.intervalId);
|
|
58
|
-
this.intervalId = undefined;
|
|
59
|
-
}
|
|
60
|
-
return true;
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.PollingLogProvider = PollingLogProvider;
|
package/src/events/sort.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSortFn = void 0;
|
|
4
|
-
const index_1 = require("../index");
|
|
5
|
-
function clientSortAscFn() {
|
|
6
|
-
return 'less than';
|
|
7
|
-
}
|
|
8
|
-
function clientSortDescFn() {
|
|
9
|
-
return 'greater than';
|
|
10
|
-
}
|
|
11
|
-
function defaultBlockchainSortFn(currentEvent, newEvent) {
|
|
12
|
-
return currentEvent.slot <= newEvent.slot ? 'less than' : 'greater than';
|
|
13
|
-
}
|
|
14
|
-
function orderRecordSortFn(currentEvent, newEvent) {
|
|
15
|
-
const currentEventMarketIndex = !currentEvent.maker.equals(index_1.PublicKey.default)
|
|
16
|
-
? currentEvent.makerOrder.marketIndex
|
|
17
|
-
: currentEvent.takerOrder.marketIndex;
|
|
18
|
-
const newEventMarketIndex = !newEvent.maker.equals(index_1.PublicKey.default)
|
|
19
|
-
? newEvent.makerOrder.marketIndex
|
|
20
|
-
: newEvent.takerOrder.marketIndex;
|
|
21
|
-
if (!currentEventMarketIndex.eq(newEventMarketIndex)) {
|
|
22
|
-
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
23
|
-
}
|
|
24
|
-
if (currentEvent.fillRecordId.gt(index_1.ZERO) && newEvent.fillRecordId.gt(index_1.ZERO)) {
|
|
25
|
-
return currentEvent.fillRecordId.lte(newEvent.fillRecordId)
|
|
26
|
-
? 'less than'
|
|
27
|
-
: 'greater than';
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function getSortFn(orderBy, orderDir, eventType) {
|
|
34
|
-
if (orderBy === 'client') {
|
|
35
|
-
return orderDir === 'asc' ? clientSortAscFn : clientSortDescFn;
|
|
36
|
-
}
|
|
37
|
-
switch (eventType) {
|
|
38
|
-
case 'OrderRecord':
|
|
39
|
-
return orderRecordSortFn;
|
|
40
|
-
default:
|
|
41
|
-
return defaultBlockchainSortFn;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.getSortFn = getSortFn;
|
|
@@ -1,41 +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.WebSocketLogProvider = void 0;
|
|
13
|
-
class WebSocketLogProvider {
|
|
14
|
-
constructor(connection, programId, commitment) {
|
|
15
|
-
this.connection = connection;
|
|
16
|
-
this.programId = programId;
|
|
17
|
-
this.commitment = commitment;
|
|
18
|
-
}
|
|
19
|
-
subscribe(callback) {
|
|
20
|
-
if (this.subscriptionId) {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
this.subscriptionId = this.connection.onLogs(this.programId, (logs, ctx) => {
|
|
24
|
-
callback(logs.signature, ctx.slot, logs.logs);
|
|
25
|
-
}, this.commitment);
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
isSubscribed() {
|
|
29
|
-
return this.subscriptionId !== undefined;
|
|
30
|
-
}
|
|
31
|
-
unsubscribe() {
|
|
32
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
if (this.subscriptionId !== undefined) {
|
|
34
|
-
yield this.connection.removeOnLogsListener(this.subscriptionId);
|
|
35
|
-
this.subscriptionId = undefined;
|
|
36
|
-
}
|
|
37
|
-
return true;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
exports.WebSocketLogProvider = WebSocketLogProvider;
|