@drift-labs/sdk 0.1.18-orders.1 → 0.1.18-orders.2
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/README.md +2 -1
- package/lib/accounts/bulkAccountLoader.d.ts +32 -0
- package/lib/accounts/bulkAccountLoader.js +156 -0
- package/lib/accounts/bulkUserSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +133 -0
- package/lib/accounts/types.d.ts +30 -25
- package/lib/accounts/utils.d.ts +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +6 -4
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +5 -4
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +3 -1
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +10 -7
- package/lib/addresses.d.ts +2 -2
- package/lib/addresses.js +4 -4
- package/lib/admin.d.ts +3 -0
- package/lib/admin.js +61 -13
- package/lib/clearingHouse.d.ts +16 -4
- package/lib/clearingHouse.js +50 -10
- package/lib/clearingHouseUser.d.ts +9 -1
- package/lib/clearingHouseUser.js +19 -5
- package/lib/config.js +1 -1
- package/lib/constants/markets.js +16 -0
- package/lib/factory/clearingHouse.d.ts +25 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +19 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/idl/clearing_house.json +360 -15
- package/lib/index.d.ts +9 -4
- package/lib/index.js +14 -5
- package/lib/math/funding.js +4 -1
- package/lib/math/utils.d.ts +2 -2
- package/lib/math/utils.js +6 -3
- package/lib/orderParams.d.ts +3 -3
- package/lib/orderParams.js +23 -3
- package/lib/token/index.d.ts +3 -0
- package/lib/token/index.js +38 -0
- package/lib/types.d.ts +13 -3
- package/package.json +3 -3
- package/src/accounts/bulkAccountLoader.ts +195 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +186 -0
- package/src/accounts/types.ts +35 -30
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +9 -6
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +10 -5
- package/src/addresses.ts +6 -4
- package/src/admin.ts +73 -20
- package/src/clearingHouse.ts +69 -25
- package/src/clearingHouseUser.ts +26 -6
- package/src/config.ts +1 -1
- package/src/constants/markets.ts +16 -0
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/idl/clearing_house.json +360 -15
- package/src/index.ts +9 -4
- package/src/math/funding.ts +5 -1
- package/src/math/utils.ts +1 -1
- package/src/orderParams.ts +26 -3
- package/src/token/index.ts +37 -0
- package/src/types.ts +13 -3
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
package/src/index.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
3
|
|
|
3
4
|
export * from './mockUSDCFaucet';
|
|
4
5
|
export * from './pythClient';
|
|
5
6
|
export * from './types';
|
|
6
7
|
export * from './constants/markets';
|
|
7
|
-
export * from './accounts/
|
|
8
|
-
export * from './accounts/
|
|
8
|
+
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
9
|
+
export * from './accounts/bulkAccountLoader';
|
|
10
|
+
export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
11
|
+
export * from './accounts/pollingTokenAccountSubscriber';
|
|
9
12
|
export * from './accounts/types';
|
|
10
13
|
export * from './addresses';
|
|
11
14
|
export * from './admin';
|
|
12
15
|
export * from './clearingHouseUser';
|
|
13
16
|
export * from './clearingHouse';
|
|
17
|
+
export * from './factory/clearingHouse';
|
|
18
|
+
export * from './factory/clearingHouseUser';
|
|
14
19
|
export * from './math/conversion';
|
|
15
20
|
export * from './math/funding';
|
|
16
21
|
export * from './math/insuranceFund';
|
|
@@ -29,4 +34,4 @@ export * from './constants/numericConstants';
|
|
|
29
34
|
export * from './util/computeUnits';
|
|
30
35
|
export * from './util/tps';
|
|
31
36
|
|
|
32
|
-
export { BN };
|
|
37
|
+
export { BN, PublicKey };
|
package/src/math/funding.ts
CHANGED
|
@@ -120,6 +120,7 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
120
120
|
.mul(periodAdjustment)
|
|
121
121
|
.div(hoursInDay)
|
|
122
122
|
.div(MARK_PRICE_PRECISION.div(QUOTE_PRECISION));
|
|
123
|
+
|
|
123
124
|
let feePoolSize = calculateFundingPool(market);
|
|
124
125
|
if (interpRateQuote.lt(new BN(0))) {
|
|
125
126
|
feePoolSize = feePoolSize.mul(new BN(-1));
|
|
@@ -289,7 +290,10 @@ export function calculateFundingPool(market: Market): BN {
|
|
|
289
290
|
const totalFeeLB = market.amm.totalFee.div(new BN(2));
|
|
290
291
|
const feePool = BN.max(
|
|
291
292
|
ZERO,
|
|
292
|
-
market.amm.totalFeeMinusDistributions
|
|
293
|
+
market.amm.totalFeeMinusDistributions
|
|
294
|
+
.sub(totalFeeLB)
|
|
295
|
+
.mul(new BN(2))
|
|
296
|
+
.div(new BN(3))
|
|
293
297
|
);
|
|
294
298
|
return feePool;
|
|
295
299
|
}
|
package/src/math/utils.ts
CHANGED
package/src/orderParams.ts
CHANGED
|
@@ -14,10 +14,12 @@ export function getLimitOrderParams(
|
|
|
14
14
|
price: BN,
|
|
15
15
|
reduceOnly: boolean,
|
|
16
16
|
discountToken = false,
|
|
17
|
-
referrer = false
|
|
17
|
+
referrer = false,
|
|
18
|
+
userOrderId = 0
|
|
18
19
|
): OrderParams {
|
|
19
20
|
return {
|
|
20
21
|
orderType: OrderType.LIMIT,
|
|
22
|
+
userOrderId,
|
|
21
23
|
marketIndex,
|
|
22
24
|
direction,
|
|
23
25
|
quoteAssetAmount: ZERO,
|
|
@@ -26,12 +28,16 @@ export function getLimitOrderParams(
|
|
|
26
28
|
reduceOnly,
|
|
27
29
|
postOnly: false,
|
|
28
30
|
immediateOrCancel: false,
|
|
31
|
+
positionLimit: ZERO,
|
|
32
|
+
padding0: true,
|
|
33
|
+
padding1: ZERO,
|
|
29
34
|
optionalAccounts: {
|
|
30
35
|
discountToken,
|
|
31
36
|
referrer,
|
|
32
37
|
},
|
|
33
38
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
34
39
|
triggerPrice: ZERO,
|
|
40
|
+
oraclePriceOffset: ZERO,
|
|
35
41
|
};
|
|
36
42
|
}
|
|
37
43
|
|
|
@@ -43,10 +49,12 @@ export function getStopOrderParams(
|
|
|
43
49
|
triggerCondition: OrderTriggerCondition,
|
|
44
50
|
reduceOnly: boolean,
|
|
45
51
|
discountToken = false,
|
|
46
|
-
referrer = false
|
|
52
|
+
referrer = false,
|
|
53
|
+
userOrderId = 0
|
|
47
54
|
): OrderParams {
|
|
48
55
|
return {
|
|
49
56
|
orderType: OrderType.STOP,
|
|
57
|
+
userOrderId,
|
|
50
58
|
marketIndex,
|
|
51
59
|
direction,
|
|
52
60
|
quoteAssetAmount: ZERO,
|
|
@@ -55,12 +63,16 @@ export function getStopOrderParams(
|
|
|
55
63
|
reduceOnly,
|
|
56
64
|
postOnly: false,
|
|
57
65
|
immediateOrCancel: false,
|
|
66
|
+
positionLimit: ZERO,
|
|
67
|
+
padding0: true,
|
|
68
|
+
padding1: ZERO,
|
|
58
69
|
optionalAccounts: {
|
|
59
70
|
discountToken,
|
|
60
71
|
referrer,
|
|
61
72
|
},
|
|
62
73
|
triggerCondition,
|
|
63
74
|
triggerPrice,
|
|
75
|
+
oraclePriceOffset: ZERO,
|
|
64
76
|
};
|
|
65
77
|
}
|
|
66
78
|
|
|
@@ -73,10 +85,12 @@ export function getStopLimitOrderParams(
|
|
|
73
85
|
triggerCondition: OrderTriggerCondition,
|
|
74
86
|
reduceOnly: boolean,
|
|
75
87
|
discountToken = false,
|
|
76
|
-
referrer = false
|
|
88
|
+
referrer = false,
|
|
89
|
+
userOrderId = 0
|
|
77
90
|
): OrderParams {
|
|
78
91
|
return {
|
|
79
92
|
orderType: OrderType.STOP_LIMIT,
|
|
93
|
+
userOrderId,
|
|
80
94
|
marketIndex,
|
|
81
95
|
direction,
|
|
82
96
|
quoteAssetAmount: ZERO,
|
|
@@ -85,12 +99,16 @@ export function getStopLimitOrderParams(
|
|
|
85
99
|
reduceOnly,
|
|
86
100
|
postOnly: false,
|
|
87
101
|
immediateOrCancel: false,
|
|
102
|
+
positionLimit: ZERO,
|
|
103
|
+
padding0: true,
|
|
104
|
+
padding1: ZERO,
|
|
88
105
|
optionalAccounts: {
|
|
89
106
|
discountToken,
|
|
90
107
|
referrer,
|
|
91
108
|
},
|
|
92
109
|
triggerCondition,
|
|
93
110
|
triggerPrice,
|
|
111
|
+
oraclePriceOffset: ZERO,
|
|
94
112
|
};
|
|
95
113
|
}
|
|
96
114
|
|
|
@@ -110,6 +128,7 @@ export function getMarketOrderParams(
|
|
|
110
128
|
|
|
111
129
|
return {
|
|
112
130
|
orderType: OrderType.MARKET,
|
|
131
|
+
userOrderId: 0,
|
|
113
132
|
marketIndex,
|
|
114
133
|
direction,
|
|
115
134
|
quoteAssetAmount,
|
|
@@ -118,11 +137,15 @@ export function getMarketOrderParams(
|
|
|
118
137
|
reduceOnly,
|
|
119
138
|
postOnly: false,
|
|
120
139
|
immediateOrCancel: false,
|
|
140
|
+
positionLimit: ZERO,
|
|
141
|
+
padding0: true,
|
|
142
|
+
padding1: ZERO,
|
|
121
143
|
optionalAccounts: {
|
|
122
144
|
discountToken,
|
|
123
145
|
referrer,
|
|
124
146
|
},
|
|
125
147
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
126
148
|
triggerPrice: ZERO,
|
|
149
|
+
oraclePriceOffset: ZERO,
|
|
127
150
|
};
|
|
128
151
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AccountInfo, AccountLayout, u64 } from '@solana/spl-token';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
export function parseTokenAccount(data: Buffer): AccountInfo {
|
|
5
|
+
const accountInfo = AccountLayout.decode(data);
|
|
6
|
+
accountInfo.mint = new PublicKey(accountInfo.mint);
|
|
7
|
+
accountInfo.owner = new PublicKey(accountInfo.owner);
|
|
8
|
+
accountInfo.amount = u64.fromBuffer(accountInfo.amount);
|
|
9
|
+
|
|
10
|
+
if (accountInfo.delegateOption === 0) {
|
|
11
|
+
accountInfo.delegate = null;
|
|
12
|
+
// eslint-disable-next-line new-cap
|
|
13
|
+
accountInfo.delegatedAmount = new u64(0);
|
|
14
|
+
} else {
|
|
15
|
+
accountInfo.delegate = new PublicKey(accountInfo.delegate);
|
|
16
|
+
accountInfo.delegatedAmount = u64.fromBuffer(accountInfo.delegatedAmount);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
20
|
+
accountInfo.isFrozen = accountInfo.state === 2;
|
|
21
|
+
|
|
22
|
+
if (accountInfo.isNativeOption === 1) {
|
|
23
|
+
accountInfo.rentExemptReserve = u64.fromBuffer(accountInfo.isNative);
|
|
24
|
+
accountInfo.isNative = true;
|
|
25
|
+
} else {
|
|
26
|
+
accountInfo.rentExemptReserve = null;
|
|
27
|
+
accountInfo.isNative = false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
+
accountInfo.closeAuthority = null;
|
|
32
|
+
} else {
|
|
33
|
+
accountInfo.closeAuthority = new PublicKey(accountInfo.closeAuthority);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return accountInfo;
|
|
37
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -79,9 +79,9 @@ export type DepositHistoryAccount = {
|
|
|
79
79
|
depositRecords: DepositRecord[];
|
|
80
80
|
};
|
|
81
81
|
|
|
82
|
-
export type
|
|
82
|
+
export type ExtendedCurveHistoryAccount = {
|
|
83
83
|
head: BN;
|
|
84
|
-
curveRecords:
|
|
84
|
+
curveRecords: ExtendedCurveRecord[];
|
|
85
85
|
};
|
|
86
86
|
|
|
87
87
|
export type FundingRateHistoryAccount = {
|
|
@@ -119,7 +119,7 @@ export type DepositRecord = {
|
|
|
119
119
|
amount: BN;
|
|
120
120
|
};
|
|
121
121
|
|
|
122
|
-
export type
|
|
122
|
+
export type ExtendedCurveRecord = {
|
|
123
123
|
ts: BN;
|
|
124
124
|
recordId: BN;
|
|
125
125
|
marketIndex: BN;
|
|
@@ -135,6 +135,8 @@ export type CurveRecord = {
|
|
|
135
135
|
baseAssetAmountShort: BN;
|
|
136
136
|
baseAssetAmount: BN;
|
|
137
137
|
openInterest: BN;
|
|
138
|
+
oraclePrice: BN;
|
|
139
|
+
tradeId: BN;
|
|
138
140
|
};
|
|
139
141
|
|
|
140
142
|
export type TradeRecord = {
|
|
@@ -255,6 +257,7 @@ export type StateAccount = {
|
|
|
255
257
|
oracleGuardRails: OracleGuardRails;
|
|
256
258
|
maxDeposit: BN;
|
|
257
259
|
orderState: PublicKey;
|
|
260
|
+
extendedCurveHistory: PublicKey;
|
|
258
261
|
};
|
|
259
262
|
|
|
260
263
|
export type OrderStateAccount = {
|
|
@@ -301,6 +304,7 @@ export type AMM = {
|
|
|
301
304
|
totalFee: BN;
|
|
302
305
|
minimumQuoteAssetTradeSize: BN;
|
|
303
306
|
minimumBaseAssetTradeSize: BN;
|
|
307
|
+
lastOraclePrice: BN;
|
|
304
308
|
};
|
|
305
309
|
|
|
306
310
|
// # User Account Types
|
|
@@ -338,6 +342,7 @@ export type Order = {
|
|
|
338
342
|
orderType: OrderType;
|
|
339
343
|
ts: BN;
|
|
340
344
|
orderId: BN;
|
|
345
|
+
userOrderId: number;
|
|
341
346
|
marketIndex: BN;
|
|
342
347
|
price: BN;
|
|
343
348
|
baseAssetAmount: BN;
|
|
@@ -357,6 +362,7 @@ export type Order = {
|
|
|
357
362
|
|
|
358
363
|
export type OrderParams = {
|
|
359
364
|
orderType: OrderType;
|
|
365
|
+
userOrderId: number;
|
|
360
366
|
direction: PositionDirection;
|
|
361
367
|
quoteAssetAmount: BN;
|
|
362
368
|
baseAssetAmount: BN;
|
|
@@ -367,6 +373,10 @@ export type OrderParams = {
|
|
|
367
373
|
immediateOrCancel: boolean;
|
|
368
374
|
triggerPrice: BN;
|
|
369
375
|
triggerCondition: OrderTriggerCondition;
|
|
376
|
+
positionLimit: BN;
|
|
377
|
+
oraclePriceOffset: BN;
|
|
378
|
+
padding0: boolean;
|
|
379
|
+
padding1: BN;
|
|
370
380
|
optionalAccounts: {
|
|
371
381
|
discountToken: boolean;
|
|
372
382
|
referrer: boolean;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { ClearingHouseAccountEvents, HistoryAccountSubscriber } from './types';
|
|
3
|
-
import { AccountSubscriber } from './types';
|
|
4
|
-
import { CurveHistoryAccount, DepositHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, TradeHistoryAccount } from '../types';
|
|
5
|
-
import { Program } from '@project-serum/anchor';
|
|
6
|
-
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
7
|
-
import { EventEmitter } from 'events';
|
|
8
|
-
export declare class DefaultHistoryAccountSubscriber implements HistoryAccountSubscriber {
|
|
9
|
-
isSubscribed: boolean;
|
|
10
|
-
program: Program;
|
|
11
|
-
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
12
|
-
tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
|
|
13
|
-
depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
|
|
14
|
-
fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
|
|
15
|
-
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
16
|
-
curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
|
|
17
|
-
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
18
|
-
constructor(program: Program);
|
|
19
|
-
subscribe(): Promise<boolean>;
|
|
20
|
-
unsubscribe(): Promise<void>;
|
|
21
|
-
assertIsSubscribed(): void;
|
|
22
|
-
getTradeHistoryAccount(): TradeHistoryAccount;
|
|
23
|
-
getDepositHistoryAccount(): DepositHistoryAccount;
|
|
24
|
-
getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
|
|
25
|
-
getFundingRateHistoryAccount(): FundingRateHistoryAccount;
|
|
26
|
-
getCurveHistoryAccount(): CurveHistoryAccount;
|
|
27
|
-
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
28
|
-
}
|
|
@@ -1,110 +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.DefaultHistoryAccountSubscriber = void 0;
|
|
13
|
-
const types_1 = require("./types");
|
|
14
|
-
const events_1 = require("events");
|
|
15
|
-
const addresses_1 = require("../addresses");
|
|
16
|
-
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
17
|
-
class DefaultHistoryAccountSubscriber {
|
|
18
|
-
constructor(program) {
|
|
19
|
-
this.isSubscribed = false;
|
|
20
|
-
this.program = program;
|
|
21
|
-
this.eventEmitter = new events_1.EventEmitter();
|
|
22
|
-
}
|
|
23
|
-
subscribe() {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
if (this.isSubscribed) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
29
|
-
const state = yield this.program.account.state.fetch(statePublicKey);
|
|
30
|
-
this.tradeHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('tradeHistory', this.program, state.tradeHistory);
|
|
31
|
-
yield this.tradeHistoryAccountSubscriber.subscribe((data) => {
|
|
32
|
-
this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
|
|
33
|
-
this.eventEmitter.emit('update');
|
|
34
|
-
});
|
|
35
|
-
this.depositHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('depositHistory', this.program, state.depositHistory);
|
|
36
|
-
yield this.depositHistoryAccountSubscriber.subscribe((data) => {
|
|
37
|
-
this.eventEmitter.emit('depositHistoryAccountUpdate', data);
|
|
38
|
-
this.eventEmitter.emit('update');
|
|
39
|
-
});
|
|
40
|
-
this.fundingPaymentHistoryAccountSubscriber =
|
|
41
|
-
new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingPaymentHistory', this.program, state.fundingPaymentHistory);
|
|
42
|
-
yield this.fundingPaymentHistoryAccountSubscriber.subscribe((data) => {
|
|
43
|
-
this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
|
|
44
|
-
this.eventEmitter.emit('update');
|
|
45
|
-
});
|
|
46
|
-
this.fundingRateHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingRateHistory', this.program, state.fundingRateHistory);
|
|
47
|
-
yield this.fundingRateHistoryAccountSubscriber.subscribe((data) => {
|
|
48
|
-
this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
|
|
49
|
-
this.eventEmitter.emit('update');
|
|
50
|
-
});
|
|
51
|
-
this.liquidationHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('liquidationHistory', this.program, state.liquidationHistory);
|
|
52
|
-
yield this.liquidationHistoryAccountSubscriber.subscribe((data) => {
|
|
53
|
-
this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
|
|
54
|
-
this.eventEmitter.emit('update');
|
|
55
|
-
});
|
|
56
|
-
this.curveHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('curveHistory', this.program, state.curveHistory);
|
|
57
|
-
yield this.curveHistoryAccountSubscriber.subscribe((data) => {
|
|
58
|
-
this.eventEmitter.emit('curveHistoryAccountUpdate', data);
|
|
59
|
-
this.eventEmitter.emit('update');
|
|
60
|
-
});
|
|
61
|
-
this.eventEmitter.emit('update');
|
|
62
|
-
this.isSubscribed = true;
|
|
63
|
-
return true;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
unsubscribe() {
|
|
67
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
if (!this.isSubscribed) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
yield this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
72
|
-
yield this.fundingRateHistoryAccountSubscriber.unsubscribe();
|
|
73
|
-
yield this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
|
|
74
|
-
yield this.depositHistoryAccountSubscriber.unsubscribe();
|
|
75
|
-
yield this.curveHistoryAccountSubscriber.unsubscribe();
|
|
76
|
-
yield this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
77
|
-
this.isSubscribed = false;
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
assertIsSubscribed() {
|
|
81
|
-
if (!this.isSubscribed) {
|
|
82
|
-
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
getTradeHistoryAccount() {
|
|
86
|
-
this.assertIsSubscribed();
|
|
87
|
-
return this.tradeHistoryAccountSubscriber.data;
|
|
88
|
-
}
|
|
89
|
-
getDepositHistoryAccount() {
|
|
90
|
-
this.assertIsSubscribed();
|
|
91
|
-
return this.depositHistoryAccountSubscriber.data;
|
|
92
|
-
}
|
|
93
|
-
getFundingPaymentHistoryAccount() {
|
|
94
|
-
this.assertIsSubscribed();
|
|
95
|
-
return this.fundingPaymentHistoryAccountSubscriber.data;
|
|
96
|
-
}
|
|
97
|
-
getFundingRateHistoryAccount() {
|
|
98
|
-
this.assertIsSubscribed();
|
|
99
|
-
return this.fundingRateHistoryAccountSubscriber.data;
|
|
100
|
-
}
|
|
101
|
-
getCurveHistoryAccount() {
|
|
102
|
-
this.assertIsSubscribed();
|
|
103
|
-
return this.curveHistoryAccountSubscriber.data;
|
|
104
|
-
}
|
|
105
|
-
getLiquidationHistoryAccount() {
|
|
106
|
-
this.assertIsSubscribed();
|
|
107
|
-
return this.liquidationHistoryAccountSubscriber.data;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
exports.DefaultHistoryAccountSubscriber = DefaultHistoryAccountSubscriber;
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { ClearingHouseAccountEvents, HistoryAccountSubscriber } from './types';
|
|
2
|
-
import { AccountSubscriber, NotSubscribedError } from './types';
|
|
3
|
-
import {
|
|
4
|
-
CurveHistoryAccount,
|
|
5
|
-
DepositHistoryAccount,
|
|
6
|
-
FundingPaymentHistoryAccount,
|
|
7
|
-
FundingRateHistoryAccount,
|
|
8
|
-
LiquidationHistoryAccount,
|
|
9
|
-
TradeHistoryAccount,
|
|
10
|
-
} from '../types';
|
|
11
|
-
import { Program } from '@project-serum/anchor';
|
|
12
|
-
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
13
|
-
import { EventEmitter } from 'events';
|
|
14
|
-
import { getClearingHouseStateAccountPublicKey } from '../addresses';
|
|
15
|
-
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
16
|
-
|
|
17
|
-
export class DefaultHistoryAccountSubscriber
|
|
18
|
-
implements HistoryAccountSubscriber
|
|
19
|
-
{
|
|
20
|
-
isSubscribed: boolean;
|
|
21
|
-
program: Program;
|
|
22
|
-
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
23
|
-
tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
|
|
24
|
-
depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
|
|
25
|
-
fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
|
|
26
|
-
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
27
|
-
curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
|
|
28
|
-
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
29
|
-
|
|
30
|
-
public constructor(program: Program) {
|
|
31
|
-
this.isSubscribed = false;
|
|
32
|
-
this.program = program;
|
|
33
|
-
this.eventEmitter = new EventEmitter();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public async subscribe(): Promise<boolean> {
|
|
37
|
-
if (this.isSubscribed) {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const statePublicKey = await getClearingHouseStateAccountPublicKey(
|
|
42
|
-
this.program.programId
|
|
43
|
-
);
|
|
44
|
-
const state: any = await this.program.account.state.fetch(statePublicKey);
|
|
45
|
-
|
|
46
|
-
this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
47
|
-
'tradeHistory',
|
|
48
|
-
this.program,
|
|
49
|
-
state.tradeHistory
|
|
50
|
-
);
|
|
51
|
-
await this.tradeHistoryAccountSubscriber.subscribe(
|
|
52
|
-
(data: TradeHistoryAccount) => {
|
|
53
|
-
this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
|
|
54
|
-
this.eventEmitter.emit('update');
|
|
55
|
-
}
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
this.depositHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
59
|
-
'depositHistory',
|
|
60
|
-
this.program,
|
|
61
|
-
state.depositHistory
|
|
62
|
-
);
|
|
63
|
-
await this.depositHistoryAccountSubscriber.subscribe(
|
|
64
|
-
(data: DepositHistoryAccount) => {
|
|
65
|
-
this.eventEmitter.emit('depositHistoryAccountUpdate', data);
|
|
66
|
-
this.eventEmitter.emit('update');
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
this.fundingPaymentHistoryAccountSubscriber =
|
|
71
|
-
new WebSocketAccountSubscriber(
|
|
72
|
-
'fundingPaymentHistory',
|
|
73
|
-
this.program,
|
|
74
|
-
state.fundingPaymentHistory
|
|
75
|
-
);
|
|
76
|
-
await this.fundingPaymentHistoryAccountSubscriber.subscribe(
|
|
77
|
-
(data: FundingPaymentHistoryAccount) => {
|
|
78
|
-
this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
|
|
79
|
-
this.eventEmitter.emit('update');
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
this.fundingRateHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
84
|
-
'fundingRateHistory',
|
|
85
|
-
this.program,
|
|
86
|
-
state.fundingRateHistory
|
|
87
|
-
);
|
|
88
|
-
await this.fundingRateHistoryAccountSubscriber.subscribe(
|
|
89
|
-
(data: FundingRateHistoryAccount) => {
|
|
90
|
-
this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
|
|
91
|
-
this.eventEmitter.emit('update');
|
|
92
|
-
}
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
this.liquidationHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
96
|
-
'liquidationHistory',
|
|
97
|
-
this.program,
|
|
98
|
-
state.liquidationHistory
|
|
99
|
-
);
|
|
100
|
-
await this.liquidationHistoryAccountSubscriber.subscribe(
|
|
101
|
-
(data: LiquidationHistoryAccount) => {
|
|
102
|
-
this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
|
|
103
|
-
this.eventEmitter.emit('update');
|
|
104
|
-
}
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
108
|
-
'curveHistory',
|
|
109
|
-
this.program,
|
|
110
|
-
state.curveHistory
|
|
111
|
-
);
|
|
112
|
-
await this.curveHistoryAccountSubscriber.subscribe(
|
|
113
|
-
(data: CurveHistoryAccount) => {
|
|
114
|
-
this.eventEmitter.emit('curveHistoryAccountUpdate', data);
|
|
115
|
-
this.eventEmitter.emit('update');
|
|
116
|
-
}
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
this.eventEmitter.emit('update');
|
|
120
|
-
|
|
121
|
-
this.isSubscribed = true;
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public async unsubscribe(): Promise<void> {
|
|
126
|
-
if (!this.isSubscribed) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
await this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
131
|
-
await this.fundingRateHistoryAccountSubscriber.unsubscribe();
|
|
132
|
-
await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
|
|
133
|
-
await this.depositHistoryAccountSubscriber.unsubscribe();
|
|
134
|
-
await this.curveHistoryAccountSubscriber.unsubscribe();
|
|
135
|
-
await this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
136
|
-
this.isSubscribed = false;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
assertIsSubscribed(): void {
|
|
140
|
-
if (!this.isSubscribed) {
|
|
141
|
-
throw new NotSubscribedError(
|
|
142
|
-
'You must call `subscribe` before using this function'
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
public getTradeHistoryAccount(): TradeHistoryAccount {
|
|
148
|
-
this.assertIsSubscribed();
|
|
149
|
-
return this.tradeHistoryAccountSubscriber.data;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public getDepositHistoryAccount(): DepositHistoryAccount {
|
|
153
|
-
this.assertIsSubscribed();
|
|
154
|
-
return this.depositHistoryAccountSubscriber.data;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount {
|
|
158
|
-
this.assertIsSubscribed();
|
|
159
|
-
return this.fundingPaymentHistoryAccountSubscriber.data;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
public getFundingRateHistoryAccount(): FundingRateHistoryAccount {
|
|
163
|
-
this.assertIsSubscribed();
|
|
164
|
-
return this.fundingRateHistoryAccountSubscriber.data;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public getCurveHistoryAccount(): CurveHistoryAccount {
|
|
168
|
-
this.assertIsSubscribed();
|
|
169
|
-
return this.curveHistoryAccountSubscriber.data;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
public getLiquidationHistoryAccount(): LiquidationHistoryAccount {
|
|
173
|
-
this.assertIsSubscribed();
|
|
174
|
-
return this.liquidationHistoryAccountSubscriber.data;
|
|
175
|
-
}
|
|
176
|
-
}
|