@drift-labs/sdk 0.1.12 → 0.1.13
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/types.d.ts +0 -21
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/clearingHouseUser.d.ts +27 -3
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +195 -47
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +7 -0
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.d.ts.map +1 -1
- package/lib/examples/makeTradeExample.js +14 -13
- package/lib/idl/clearing_house.json +94 -42
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/math/amm.d.ts +26 -1
- package/lib/math/amm.d.ts.map +1 -1
- package/lib/math/amm.js +84 -10
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.d.ts.map +1 -1
- package/lib/math/funding.js +41 -22
- package/lib/math/insuranceFund.d.ts +14 -0
- package/lib/math/insuranceFund.d.ts.map +1 -0
- package/lib/math/insuranceFund.js +35 -0
- package/lib/math/position.d.ts +1 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +15 -23
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.d.ts.map +1 -1
- package/lib/math/trade.js +9 -4
- package/lib/types.d.ts +0 -13
- package/lib/types.d.ts.map +1 -1
- package/lib/wallet.d.ts +10 -0
- package/lib/wallet.d.ts.map +1 -0
- package/lib/wallet.js +35 -0
- package/package.json +2 -2
- package/src/accounts/types.ts +0 -27
- package/src/clearingHouse.ts +2 -2
- package/src/clearingHouseUser.ts +282 -65
- package/src/constants/markets.ts +7 -0
- package/src/constants/numericConstants.ts +3 -0
- package/src/examples/makeTradeExample.ts +2 -1
- package/src/idl/clearing_house.json +94 -42
- package/src/index.ts +2 -1
- package/src/math/amm.ts +119 -12
- package/src/math/funding.ts +47 -25
- package/src/math/insuranceFund.ts +22 -0
- package/src/math/position.ts +15 -28
- package/src/math/trade.ts +9 -5
- package/src/types.ts +0 -14
- package/src/wallet.ts +22 -0
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -29
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -179
package/src/math/position.ts
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BN from 'bn.js';
|
|
2
2
|
import {
|
|
3
|
+
AMM_RESERVE_PRECISION, AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO,
|
|
3
4
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
4
|
-
|
|
5
|
+
FUNDING_PAYMENT_PRECISION,
|
|
6
|
+
MARK_PRICE_PRECISION, ONE,
|
|
5
7
|
PEG_PRECISION,
|
|
6
|
-
|
|
8
|
+
PRICE_TO_QUOTE_PRECISION,
|
|
7
9
|
ZERO,
|
|
8
10
|
} from '../constants/numericConstants';
|
|
9
|
-
import
|
|
11
|
+
import { Market, PositionDirection, UserPosition } from '../types';
|
|
10
12
|
import { calculateAmmReservesAfterSwap, getSwapDirection } from './amm';
|
|
11
|
-
import {
|
|
12
|
-
AMM_RESERVE_PRECISION,
|
|
13
|
-
FUNDING_PAYMENT_PRECISION,
|
|
14
|
-
PRICE_TO_QUOTE_PRECISION,
|
|
15
|
-
} from '../constants/numericConstants';
|
|
16
13
|
|
|
17
14
|
/**
|
|
18
15
|
* calculateBaseAssetValue
|
|
@@ -45,15 +42,13 @@ export function calculateBaseAssetValue(
|
|
|
45
42
|
return market.amm.quoteAssetReserve
|
|
46
43
|
.sub(newQuoteAssetReserve)
|
|
47
44
|
.mul(market.amm.pegMultiplier)
|
|
48
|
-
.div(
|
|
49
|
-
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
45
|
+
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
50
46
|
|
|
51
47
|
case PositionDirection.LONG:
|
|
52
48
|
return newQuoteAssetReserve
|
|
53
49
|
.sub(market.amm.quoteAssetReserve)
|
|
54
50
|
.mul(market.amm.pegMultiplier)
|
|
55
|
-
.div(
|
|
56
|
-
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
51
|
+
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
57
52
|
}
|
|
58
53
|
}
|
|
59
54
|
|
|
@@ -74,21 +69,13 @@ export function calculatePositionPNL(
|
|
|
74
69
|
return ZERO;
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
const directionToClose = marketPosition.baseAssetAmount.gt(ZERO)
|
|
78
|
-
? PositionDirection.SHORT
|
|
79
|
-
: PositionDirection.LONG;
|
|
80
|
-
|
|
81
72
|
const baseAssetValue = calculateBaseAssetValue(market, marketPosition);
|
|
82
|
-
let pnlAssetAmount;
|
|
83
73
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
case PositionDirection.LONG:
|
|
90
|
-
pnlAssetAmount = marketPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
91
|
-
break;
|
|
74
|
+
let pnl;
|
|
75
|
+
if (marketPosition.baseAssetAmount.gt(ZERO)) {
|
|
76
|
+
pnl = baseAssetValue.sub(marketPosition.quoteAssetAmount);
|
|
77
|
+
} else {
|
|
78
|
+
pnl = marketPosition.quoteAssetAmount.sub(baseAssetValue).sub(ONE);
|
|
92
79
|
}
|
|
93
80
|
|
|
94
81
|
if (withFunding) {
|
|
@@ -97,10 +84,10 @@ export function calculatePositionPNL(
|
|
|
97
84
|
marketPosition
|
|
98
85
|
).div(PRICE_TO_QUOTE_PRECISION);
|
|
99
86
|
|
|
100
|
-
|
|
87
|
+
pnl = pnl.add(fundingRatePnL);
|
|
101
88
|
}
|
|
102
89
|
|
|
103
|
-
return
|
|
90
|
+
return pnl;
|
|
104
91
|
}
|
|
105
92
|
|
|
106
93
|
/**
|
package/src/math/trade.ts
CHANGED
|
@@ -146,7 +146,8 @@ export function calculateTradeAcquiredAmounts(
|
|
|
146
146
|
export function calculateTargetPriceTrade(
|
|
147
147
|
market: Market,
|
|
148
148
|
targetPrice: BN,
|
|
149
|
-
pct: BN = MAXPCT
|
|
149
|
+
pct: BN = MAXPCT,
|
|
150
|
+
outputAssetType: AssetType = 'quote'
|
|
150
151
|
): [PositionDirection, BN, BN, BN] {
|
|
151
152
|
assert(market.amm.baseAssetReserve.gt(ZERO));
|
|
152
153
|
assert(targetPrice.gt(ZERO));
|
|
@@ -199,7 +200,7 @@ export function calculateTargetPriceTrade(
|
|
|
199
200
|
.mul(peg)
|
|
200
201
|
.div(PEG_PRECISION)
|
|
201
202
|
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
202
|
-
baseSize =
|
|
203
|
+
baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
|
|
203
204
|
} else if (markPriceBefore.lt(targetPrice)) {
|
|
204
205
|
// underestimate y2
|
|
205
206
|
baseAssetReserveAfter = squareRootBN(
|
|
@@ -221,7 +222,7 @@ export function calculateTargetPriceTrade(
|
|
|
221
222
|
.mul(peg)
|
|
222
223
|
.div(PEG_PRECISION)
|
|
223
224
|
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
224
|
-
baseSize =
|
|
225
|
+
baseSize = baseAssetReserveBefore.sub(baseAssetReserveAfter);
|
|
225
226
|
} else {
|
|
226
227
|
// no trade, market is at target
|
|
227
228
|
direction = PositionDirection.LONG;
|
|
@@ -254,6 +255,9 @@ export function calculateTargetPriceTrade(
|
|
|
254
255
|
'err: ' +
|
|
255
256
|
tp2.sub(tp1).abs().toString()
|
|
256
257
|
);
|
|
257
|
-
|
|
258
|
-
|
|
258
|
+
if (outputAssetType == 'quote') {
|
|
259
|
+
return [direction, tradeSize, entryPrice, targetPrice];
|
|
260
|
+
} else {
|
|
261
|
+
return [direction, baseSize, entryPrice, targetPrice];
|
|
262
|
+
}
|
|
259
263
|
}
|
package/src/types.ts
CHANGED
|
@@ -244,9 +244,6 @@ export type UserPosition = {
|
|
|
244
244
|
lastCumulativeFundingRate: BN;
|
|
245
245
|
marketIndex: BN;
|
|
246
246
|
quoteAssetAmount: BN;
|
|
247
|
-
unrealizedPnl?: BN;
|
|
248
|
-
unrealizedFundingPnl?: BN;
|
|
249
|
-
baseAssetValue?: BN;
|
|
250
247
|
};
|
|
251
248
|
|
|
252
249
|
export type UserPositionsAccount = {
|
|
@@ -262,17 +259,6 @@ export type UserAccount = {
|
|
|
262
259
|
totalFeePaid: BN;
|
|
263
260
|
};
|
|
264
261
|
|
|
265
|
-
export type UserSnapshotRecord = {
|
|
266
|
-
ts: BN;
|
|
267
|
-
userAuthority: PublicKey;
|
|
268
|
-
user: PublicKey;
|
|
269
|
-
userPositions: UserPosition[];
|
|
270
|
-
userTotalRealizedPnl: BN;
|
|
271
|
-
userTotalUnrealizedPnl: BN;
|
|
272
|
-
userTotalUnrealizedFundingPnl: BN;
|
|
273
|
-
userCollateral: BN;
|
|
274
|
-
};
|
|
275
|
-
|
|
276
262
|
// # Misc Types
|
|
277
263
|
export interface IWallet {
|
|
278
264
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
package/src/wallet.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {Keypair, PublicKey, Transaction} from "@solana/web3.js";
|
|
2
|
+
import {IWallet} from "./types";
|
|
3
|
+
|
|
4
|
+
export class Wallet implements IWallet {
|
|
5
|
+
constructor(readonly payer: Keypair) {}
|
|
6
|
+
|
|
7
|
+
async signTransaction(tx: Transaction): Promise<Transaction> {
|
|
8
|
+
tx.partialSign(this.payer);
|
|
9
|
+
return tx;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
|
|
13
|
+
return txs.map((t) => {
|
|
14
|
+
t.partialSign(this.payer);
|
|
15
|
+
return t;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get publicKey(): PublicKey {
|
|
20
|
+
return this.payer.publicKey;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -1,29 +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
|
-
}
|
|
29
|
-
//# sourceMappingURL=defaultHistoryAccountSubscriber.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"defaultHistoryAccountSubscriber.d.ts","sourceRoot":"","sources":["../../src/accounts/defaultHistoryAccountSubscriber.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAsB,MAAM,SAAS,CAAC;AAChE,OAAO,EACN,mBAAmB,EACnB,qBAAqB,EACrB,4BAA4B,EAC5B,yBAAyB,EACzB,yBAAyB,EAEzB,mBAAmB,EACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAItC,qBAAa,+BACZ,YAAW,wBAAwB;IAEnC,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAC;IAC3E,6BAA6B,CAAC,EAAE,iBAAiB,CAAC,mBAAmB,CAAC,CAAC;IACvE,+BAA+B,CAAC,EAAE,iBAAiB,CAAC,qBAAqB,CAAC,CAAC;IAC3E,sCAAsC,CAAC,EAAE,iBAAiB,CAAC,4BAA4B,CAAC,CAAC;IACzF,mCAAmC,CAAC,EAAE,iBAAiB,CAAC,yBAAyB,CAAC,CAAC;IACnF,6BAA6B,CAAC,EAAE,iBAAiB,CAAC,mBAAmB,CAAC,CAAC;IACvE,mCAAmC,CAAC,EAAE,iBAAiB,CAAC,yBAAyB,CAAC,CAAC;gBAEhE,OAAO,EAAE,OAAO;IAMtB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IA2F7B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAczC,kBAAkB,IAAI,IAAI;IAQnB,sBAAsB,IAAI,mBAAmB;IAK7C,wBAAwB,IAAI,qBAAqB;IAKjD,+BAA+B,IAAI,4BAA4B;IAK/D,4BAA4B,IAAI,yBAAyB;IAKzD,sBAAsB,IAAI,mBAAmB;IAK7C,4BAA4B,IAAI,yBAAyB;CAIhE"}
|
|
@@ -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,179 +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
|
-
StateAccount,
|
|
10
|
-
TradeHistoryAccount,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import { Program } from '@project-serum/anchor';
|
|
13
|
-
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
14
|
-
import { EventEmitter } from 'events';
|
|
15
|
-
import { getClearingHouseStateAccountPublicKey } from '../addresses';
|
|
16
|
-
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
17
|
-
|
|
18
|
-
export class DefaultHistoryAccountSubscriber
|
|
19
|
-
implements HistoryAccountSubscriber
|
|
20
|
-
{
|
|
21
|
-
isSubscribed: boolean;
|
|
22
|
-
program: Program;
|
|
23
|
-
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
24
|
-
tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
|
|
25
|
-
depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
|
|
26
|
-
fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
|
|
27
|
-
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
28
|
-
curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
|
|
29
|
-
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
30
|
-
|
|
31
|
-
public constructor(program: Program) {
|
|
32
|
-
this.isSubscribed = false;
|
|
33
|
-
this.program = program;
|
|
34
|
-
this.eventEmitter = new EventEmitter();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async subscribe(): Promise<boolean> {
|
|
38
|
-
if (this.isSubscribed) {
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const statePublicKey = await getClearingHouseStateAccountPublicKey(
|
|
43
|
-
this.program.programId
|
|
44
|
-
);
|
|
45
|
-
const state: StateAccount = await this.program.account.state.fetch(
|
|
46
|
-
statePublicKey
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
50
|
-
'tradeHistory',
|
|
51
|
-
this.program,
|
|
52
|
-
state.tradeHistory
|
|
53
|
-
);
|
|
54
|
-
await this.tradeHistoryAccountSubscriber.subscribe(
|
|
55
|
-
(data: TradeHistoryAccount) => {
|
|
56
|
-
this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
|
|
57
|
-
this.eventEmitter.emit('update');
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
this.depositHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
62
|
-
'depositHistory',
|
|
63
|
-
this.program,
|
|
64
|
-
state.depositHistory
|
|
65
|
-
);
|
|
66
|
-
await this.depositHistoryAccountSubscriber.subscribe(
|
|
67
|
-
(data: DepositHistoryAccount) => {
|
|
68
|
-
this.eventEmitter.emit('depositHistoryAccountUpdate', data);
|
|
69
|
-
this.eventEmitter.emit('update');
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
this.fundingPaymentHistoryAccountSubscriber =
|
|
74
|
-
new WebSocketAccountSubscriber(
|
|
75
|
-
'fundingPaymentHistory',
|
|
76
|
-
this.program,
|
|
77
|
-
state.fundingPaymentHistory
|
|
78
|
-
);
|
|
79
|
-
await this.fundingPaymentHistoryAccountSubscriber.subscribe(
|
|
80
|
-
(data: FundingPaymentHistoryAccount) => {
|
|
81
|
-
this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
|
|
82
|
-
this.eventEmitter.emit('update');
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
this.fundingRateHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
87
|
-
'fundingRateHistory',
|
|
88
|
-
this.program,
|
|
89
|
-
state.fundingRateHistory
|
|
90
|
-
);
|
|
91
|
-
await this.fundingRateHistoryAccountSubscriber.subscribe(
|
|
92
|
-
(data: FundingRateHistoryAccount) => {
|
|
93
|
-
this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
|
|
94
|
-
this.eventEmitter.emit('update');
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
this.liquidationHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
99
|
-
'liquidationHistory',
|
|
100
|
-
this.program,
|
|
101
|
-
state.liquidationHistory
|
|
102
|
-
);
|
|
103
|
-
await this.liquidationHistoryAccountSubscriber.subscribe(
|
|
104
|
-
(data: LiquidationHistoryAccount) => {
|
|
105
|
-
this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
|
|
106
|
-
this.eventEmitter.emit('update');
|
|
107
|
-
}
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
111
|
-
'curveHistory',
|
|
112
|
-
this.program,
|
|
113
|
-
state.curveHistory
|
|
114
|
-
);
|
|
115
|
-
await this.curveHistoryAccountSubscriber.subscribe(
|
|
116
|
-
(data: CurveHistoryAccount) => {
|
|
117
|
-
this.eventEmitter.emit('curveHistoryAccountUpdate', data);
|
|
118
|
-
this.eventEmitter.emit('update');
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
this.eventEmitter.emit('update');
|
|
123
|
-
|
|
124
|
-
this.isSubscribed = true;
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
public async unsubscribe(): Promise<void> {
|
|
129
|
-
if (!this.isSubscribed) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
await this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
134
|
-
await this.fundingRateHistoryAccountSubscriber.unsubscribe();
|
|
135
|
-
await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
|
|
136
|
-
await this.depositHistoryAccountSubscriber.unsubscribe();
|
|
137
|
-
await this.curveHistoryAccountSubscriber.unsubscribe();
|
|
138
|
-
await this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
139
|
-
this.isSubscribed = false;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
assertIsSubscribed(): void {
|
|
143
|
-
if (!this.isSubscribed) {
|
|
144
|
-
throw new NotSubscribedError(
|
|
145
|
-
'You must call `subscribe` before using this function'
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public getTradeHistoryAccount(): TradeHistoryAccount {
|
|
151
|
-
this.assertIsSubscribed();
|
|
152
|
-
return this.tradeHistoryAccountSubscriber.data;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
public getDepositHistoryAccount(): DepositHistoryAccount {
|
|
156
|
-
this.assertIsSubscribed();
|
|
157
|
-
return this.depositHistoryAccountSubscriber.data;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
public getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount {
|
|
161
|
-
this.assertIsSubscribed();
|
|
162
|
-
return this.fundingPaymentHistoryAccountSubscriber.data;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public getFundingRateHistoryAccount(): FundingRateHistoryAccount {
|
|
166
|
-
this.assertIsSubscribed();
|
|
167
|
-
return this.fundingRateHistoryAccountSubscriber.data;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
public getCurveHistoryAccount(): CurveHistoryAccount {
|
|
171
|
-
this.assertIsSubscribed();
|
|
172
|
-
return this.curveHistoryAccountSubscriber.data;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public getLiquidationHistoryAccount(): LiquidationHistoryAccount {
|
|
176
|
-
this.assertIsSubscribed();
|
|
177
|
-
return this.liquidationHistoryAccountSubscriber.data;
|
|
178
|
-
}
|
|
179
|
-
}
|