@drift-labs/sdk 0.1.18-master.0 → 0.1.18-orders.1
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/defaultClearingHouseAccountSubscriber.d.ts +7 -4
- package/lib/accounts/defaultClearingHouseAccountSubscriber.js +27 -2
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +28 -0
- package/lib/accounts/defaultHistoryAccountSubscriber.js +110 -0
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +3 -2
- package/lib/accounts/defaultUserAccountSubscriber.js +12 -1
- package/lib/accounts/types.d.ts +31 -5
- package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
- package/lib/addresses.d.ts +4 -1
- package/lib/addresses.js +28 -1
- package/lib/admin.d.ts +10 -5
- package/lib/admin.js +53 -30
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +22 -3
- package/lib/clearingHouse.js +246 -15
- package/lib/clearingHouseUser.d.ts +10 -17
- package/lib/clearingHouseUser.js +111 -97
- package/lib/config.d.ts +0 -1
- package/lib/config.js +1 -1
- package/lib/constants/markets.d.ts +0 -1
- package/lib/constants/numericConstants.d.ts +0 -1
- package/lib/examples/makeTradeExample.d.ts +0 -1
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/idl/clearing_house.json +895 -132
- package/lib/index.d.ts +4 -1
- package/lib/index.js +4 -0
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +38 -15
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +0 -1
- package/lib/math/funding.js +1 -1
- package/lib/math/insuranceFund.d.ts +0 -1
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.js +12 -2
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +30 -0
- package/lib/math/position.d.ts +4 -2
- package/lib/math/position.js +19 -5
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +0 -1
- package/lib/mockUSDCFaucet.d.ts +0 -1
- package/lib/orderParams.d.ts +7 -0
- package/lib/orderParams.js +88 -0
- package/lib/orders.d.ts +5 -0
- package/lib/orders.js +136 -0
- package/lib/pythClient.d.ts +0 -1
- package/lib/pythClient.js +1 -1
- package/lib/tx/defaultTxSender.d.ts +0 -1
- package/lib/tx/types.d.ts +0 -1
- package/lib/tx/utils.d.ts +0 -1
- package/lib/types.d.ts +140 -7
- package/lib/types.js +36 -1
- package/lib/util/computeUnits.d.ts +0 -1
- package/lib/util/tps.d.ts +0 -1
- package/lib/wallet.d.ts +0 -1
- package/package.json +1 -1
- package/src/accounts/defaultClearingHouseAccountSubscriber.ts +52 -5
- package/src/accounts/defaultHistoryAccountSubscriber.ts +176 -0
- package/src/accounts/defaultUserAccountSubscriber.ts +29 -2
- package/src/accounts/types.ts +41 -4
- package/src/addresses.ts +35 -0
- package/src/admin.ts +86 -33
- package/src/clearingHouse.ts +340 -7
- package/src/clearingHouseUser.ts +154 -102
- package/src/config.ts +1 -1
- package/src/idl/clearing_house.json +895 -132
- package/src/index.ts +4 -0
- package/src/math/amm.ts +47 -14
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +39 -0
- package/src/math/position.ts +23 -3
- package/src/orderParams.ts +128 -0
- package/src/orders.ts +230 -0
- package/src/types.ts +124 -6
- package/tsconfig.json +0 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/types.d.ts.map +0 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
- package/lib/addresses.d.ts.map +0 -1
- package/lib/admin.d.ts.map +0 -1
- package/lib/assert/assert.d.ts.map +0 -1
- package/lib/clearingHouse.d.ts.map +0 -1
- package/lib/clearingHouseUser.d.ts.map +0 -1
- package/lib/config.d.ts.map +0 -1
- package/lib/constants/markets.d.ts.map +0 -1
- package/lib/constants/numericConstants.d.ts.map +0 -1
- package/lib/examples/makeTradeExample.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/math/amm.d.ts.map +0 -1
- package/lib/math/conversion.d.ts.map +0 -1
- package/lib/math/funding.d.ts.map +0 -1
- package/lib/math/insuranceFund.d.ts.map +0 -1
- package/lib/math/market.d.ts.map +0 -1
- package/lib/math/position.d.ts.map +0 -1
- package/lib/math/trade.d.ts.map +0 -1
- package/lib/math/utils.d.ts.map +0 -1
- package/lib/mockUSDCFaucet.d.ts.map +0 -1
- package/lib/pythClient.d.ts.map +0 -1
- package/lib/tx/defaultTxSender.d.ts.map +0 -1
- package/lib/tx/types.d.ts.map +0 -1
- package/lib/tx/utils.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/util/computeUnits.d.ts.map +0 -1
- package/lib/util/tps.d.ts.map +0 -1
- package/lib/wallet.d.ts.map +0 -1
package/lib/orders.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateAmountToTradeForStopLimit = exports.calculateAmountToTradeForLimit = exports.calculateNewStateAfterOrder = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const market_1 = require("./math/market");
|
|
6
|
+
const numericConstants_1 = require("./constants/numericConstants");
|
|
7
|
+
const amm_1 = require("./math/amm");
|
|
8
|
+
const position_1 = require("./math/position");
|
|
9
|
+
function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
|
|
10
|
+
if (types_1.isVariant(order.status, 'init')) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
const baseAssetAmountToTrade = calculateAmountToTrade(market, order);
|
|
14
|
+
if (baseAssetAmountToTrade.lt(market.amm.minimumBaseAssetTradeSize)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const userAccountAfter = Object.assign({}, userAccount);
|
|
18
|
+
const userPositionAfter = Object.assign({}, userPosition);
|
|
19
|
+
const currentPositionDirection = position_1.positionCurrentDirection(userPosition);
|
|
20
|
+
const increasePosition = userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) ||
|
|
21
|
+
isSameDirection(order.direction, currentPositionDirection);
|
|
22
|
+
if (increasePosition) {
|
|
23
|
+
const marketAfter = market_1.calculateNewMarketAfterTrade(baseAssetAmountToTrade, order.direction, market);
|
|
24
|
+
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(market, marketAfter);
|
|
25
|
+
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
|
|
26
|
+
userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(quoteAssetAmountSwapped);
|
|
27
|
+
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const reversePosition = baseAssetAmountToTrade.gt(userPosition.baseAssetAmount.abs());
|
|
31
|
+
if (reversePosition) {
|
|
32
|
+
const intermediateMarket = market_1.calculateNewMarketAfterTrade(userPosition.baseAssetAmount, position_1.findDirectionToClose(userPosition), market);
|
|
33
|
+
const { quoteAssetAmountSwapped: baseAssetValue } = calculateAmountSwapped(market, intermediateMarket);
|
|
34
|
+
let pnl;
|
|
35
|
+
if (types_1.isVariant(currentPositionDirection, 'long')) {
|
|
36
|
+
pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
pnl = userPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
40
|
+
}
|
|
41
|
+
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
42
|
+
const baseAssetAmountLeft = baseAssetAmountToTrade.sub(userPosition.baseAssetAmount.abs());
|
|
43
|
+
const marketAfter = market_1.calculateNewMarketAfterTrade(baseAssetAmountLeft, order.direction, intermediateMarket);
|
|
44
|
+
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(intermediateMarket, marketAfter);
|
|
45
|
+
userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
|
|
46
|
+
userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
|
|
47
|
+
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const marketAfter = market_1.calculateNewMarketAfterTrade(baseAssetAmountToTrade, order.direction, market);
|
|
51
|
+
const { quoteAssetAmountSwapped: baseAssetValue, baseAssetAmountSwapped, } = calculateAmountSwapped(market, marketAfter);
|
|
52
|
+
const costBasisRealized = userPosition.quoteAssetAmount
|
|
53
|
+
.mul(baseAssetAmountSwapped.abs())
|
|
54
|
+
.div(userPosition.baseAssetAmount.abs());
|
|
55
|
+
let pnl;
|
|
56
|
+
if (types_1.isVariant(currentPositionDirection, 'long')) {
|
|
57
|
+
pnl = baseAssetValue.sub(costBasisRealized);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
pnl = costBasisRealized.sub(baseAssetValue);
|
|
61
|
+
}
|
|
62
|
+
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
63
|
+
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
|
|
64
|
+
userPositionAfter.quoteAssetAmount =
|
|
65
|
+
userPositionAfter.quoteAssetAmount.sub(costBasisRealized);
|
|
66
|
+
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.calculateNewStateAfterOrder = calculateNewStateAfterOrder;
|
|
71
|
+
function calculateAmountSwapped(marketBefore, marketAfter) {
|
|
72
|
+
return {
|
|
73
|
+
quoteAssetAmountSwapped: marketBefore.amm.quoteAssetReserve
|
|
74
|
+
.sub(marketAfter.amm.quoteAssetReserve)
|
|
75
|
+
.abs()
|
|
76
|
+
.mul(marketBefore.amm.pegMultiplier)
|
|
77
|
+
.div(numericConstants_1.PEG_PRECISION)
|
|
78
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO),
|
|
79
|
+
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(marketAfter.amm.baseAssetReserve),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function calculateAmountToTrade(market, order) {
|
|
83
|
+
if (types_1.isVariant(order.orderType, 'limit')) {
|
|
84
|
+
return calculateAmountToTradeForLimit(market, order);
|
|
85
|
+
}
|
|
86
|
+
else if (types_1.isVariant(order.orderType, 'stopLimit')) {
|
|
87
|
+
return calculateAmountToTradeForStopLimit(market, order);
|
|
88
|
+
}
|
|
89
|
+
else if (types_1.isVariant(order.orderType, 'market')) {
|
|
90
|
+
// should never be a market order queued
|
|
91
|
+
return numericConstants_1.ZERO;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return calculateAmountToTradeForStop(market, order);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function calculateAmountToTradeForLimit(market, order) {
|
|
98
|
+
const [maxAmountToTrade, direction] = amm_1.calculateMaxBaseAssetAmountToTrade(market.amm, order.price);
|
|
99
|
+
// Check that directions are the same
|
|
100
|
+
const sameDirection = isSameDirection(direction, order.direction);
|
|
101
|
+
if (!sameDirection) {
|
|
102
|
+
return numericConstants_1.ZERO;
|
|
103
|
+
}
|
|
104
|
+
return maxAmountToTrade.gt(order.baseAssetAmount)
|
|
105
|
+
? order.baseAssetAmount
|
|
106
|
+
: maxAmountToTrade;
|
|
107
|
+
}
|
|
108
|
+
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
109
|
+
function calculateAmountToTradeForStopLimit(market, order) {
|
|
110
|
+
if (order.baseAssetAmountFilled.eq(numericConstants_1.ZERO)) {
|
|
111
|
+
const baseAssetAmount = calculateAmountToTradeForStop(market, order);
|
|
112
|
+
if (baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
113
|
+
return numericConstants_1.ZERO;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return calculateAmountToTradeForLimit(market, order);
|
|
117
|
+
}
|
|
118
|
+
exports.calculateAmountToTradeForStopLimit = calculateAmountToTradeForStopLimit;
|
|
119
|
+
function isSameDirection(firstDirection, secondDirection) {
|
|
120
|
+
return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
|
|
121
|
+
(types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
|
|
122
|
+
}
|
|
123
|
+
function calculateAmountToTradeForStop(market, order) {
|
|
124
|
+
return isTriggerConditionSatisfied(market, order)
|
|
125
|
+
? order.baseAssetAmount
|
|
126
|
+
: numericConstants_1.ZERO;
|
|
127
|
+
}
|
|
128
|
+
function isTriggerConditionSatisfied(market, order) {
|
|
129
|
+
const markPrice = market_1.calculateMarkPrice(market);
|
|
130
|
+
if (types_1.isVariant(order.triggerCondition, 'above')) {
|
|
131
|
+
return markPrice.gt(order.triggerPrice);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return markPrice.lt(order.triggerPrice);
|
|
135
|
+
}
|
|
136
|
+
}
|
package/lib/pythClient.d.ts
CHANGED
package/lib/pythClient.js
CHANGED
|
@@ -18,7 +18,7 @@ class PythClient {
|
|
|
18
18
|
getPriceData(pricePublicKey) {
|
|
19
19
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
20
|
const account = yield this.connection.getAccountInfo(pricePublicKey);
|
|
21
|
-
return
|
|
21
|
+
return client_1.parsePriceData(account.data);
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
}
|
package/lib/tx/types.d.ts
CHANGED
package/lib/tx/utils.d.ts
CHANGED
package/lib/types.d.ts
CHANGED
|
@@ -24,6 +24,65 @@ export declare class OracleSource {
|
|
|
24
24
|
switchboard: {};
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
+
export declare class OrderType {
|
|
28
|
+
static readonly LIMIT: {
|
|
29
|
+
limit: {};
|
|
30
|
+
};
|
|
31
|
+
static readonly STOP: {
|
|
32
|
+
stop: {};
|
|
33
|
+
};
|
|
34
|
+
static readonly STOP_LIMIT: {
|
|
35
|
+
stopLimit: {};
|
|
36
|
+
};
|
|
37
|
+
static readonly MARKET: {
|
|
38
|
+
market: {};
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export declare class OrderStatus {
|
|
42
|
+
static readonly INIT: {
|
|
43
|
+
init: {};
|
|
44
|
+
};
|
|
45
|
+
static readonly OPEN: {
|
|
46
|
+
open: {};
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export declare class OrderDiscountTier {
|
|
50
|
+
static readonly NONE: {
|
|
51
|
+
none: {};
|
|
52
|
+
};
|
|
53
|
+
static readonly FIRST: {
|
|
54
|
+
first: {};
|
|
55
|
+
};
|
|
56
|
+
static readonly SECOND: {
|
|
57
|
+
second: {};
|
|
58
|
+
};
|
|
59
|
+
static readonly THIRD: {
|
|
60
|
+
third: {};
|
|
61
|
+
};
|
|
62
|
+
static readonly FOURTH: {
|
|
63
|
+
fourth: {};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export declare class OrderAction {
|
|
67
|
+
static readonly PLACE: {
|
|
68
|
+
place: {};
|
|
69
|
+
};
|
|
70
|
+
static readonly CANCEL: {
|
|
71
|
+
cancel: {};
|
|
72
|
+
};
|
|
73
|
+
static readonly FILL: {
|
|
74
|
+
fill: {};
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export declare class OrderTriggerCondition {
|
|
78
|
+
static readonly ABOVE: {
|
|
79
|
+
above: {};
|
|
80
|
+
};
|
|
81
|
+
static readonly BELOW: {
|
|
82
|
+
below: {};
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export declare function isVariant(object: unknown, type: string): boolean;
|
|
27
86
|
export declare enum TradeSide {
|
|
28
87
|
None = 0,
|
|
29
88
|
Buy = 1,
|
|
@@ -38,9 +97,9 @@ export declare type DepositHistoryAccount = {
|
|
|
38
97
|
head: BN;
|
|
39
98
|
depositRecords: DepositRecord[];
|
|
40
99
|
};
|
|
41
|
-
export declare type
|
|
100
|
+
export declare type CurveHistoryAccount = {
|
|
42
101
|
head: BN;
|
|
43
|
-
curveRecords:
|
|
102
|
+
curveRecords: CurveRecord[];
|
|
44
103
|
};
|
|
45
104
|
export declare type FundingRateHistoryAccount = {
|
|
46
105
|
head: BN;
|
|
@@ -54,6 +113,11 @@ export declare type LiquidationHistoryAccount = {
|
|
|
54
113
|
head: BN;
|
|
55
114
|
liquidationRecords: LiquidationRecord[];
|
|
56
115
|
};
|
|
116
|
+
export declare type OrderHistoryAccount = {
|
|
117
|
+
head: BN;
|
|
118
|
+
lastOrderId: BN;
|
|
119
|
+
orderRecords: OrderRecord[];
|
|
120
|
+
};
|
|
57
121
|
export declare type DepositRecord = {
|
|
58
122
|
ts: BN;
|
|
59
123
|
recordId: BN;
|
|
@@ -67,7 +131,7 @@ export declare type DepositRecord = {
|
|
|
67
131
|
cumulativeDepositsBefore: BN;
|
|
68
132
|
amount: BN;
|
|
69
133
|
};
|
|
70
|
-
export declare type
|
|
134
|
+
export declare type CurveRecord = {
|
|
71
135
|
ts: BN;
|
|
72
136
|
recordId: BN;
|
|
73
137
|
marketIndex: BN;
|
|
@@ -83,7 +147,6 @@ export declare type ExtendedCurveRecord = {
|
|
|
83
147
|
baseAssetAmountShort: BN;
|
|
84
148
|
baseAssetAmount: BN;
|
|
85
149
|
openInterest: BN;
|
|
86
|
-
oraclePrice: BN;
|
|
87
150
|
};
|
|
88
151
|
export declare type TradeRecord = {
|
|
89
152
|
ts: BN;
|
|
@@ -146,6 +209,20 @@ export declare type LiquidationRecord = {
|
|
|
146
209
|
unrealizedPnl: BN;
|
|
147
210
|
marginRatio: BN;
|
|
148
211
|
};
|
|
212
|
+
export declare type OrderRecord = {
|
|
213
|
+
ts: BN;
|
|
214
|
+
recordId: BN;
|
|
215
|
+
order: Order;
|
|
216
|
+
user: PublicKey;
|
|
217
|
+
authority: PublicKey;
|
|
218
|
+
action: OrderAction;
|
|
219
|
+
filler: PublicKey;
|
|
220
|
+
baseAssetAmountFilled: BN;
|
|
221
|
+
quoteAssetAmountFilled: BN;
|
|
222
|
+
fee: BN;
|
|
223
|
+
fillerReward: BN;
|
|
224
|
+
tradeRecordId: BN;
|
|
225
|
+
};
|
|
149
226
|
export declare type StateAccount = {
|
|
150
227
|
admin: PublicKey;
|
|
151
228
|
fundingPaused: boolean;
|
|
@@ -183,7 +260,12 @@ export declare type StateAccount = {
|
|
|
183
260
|
discountMint: PublicKey;
|
|
184
261
|
oracleGuardRails: OracleGuardRails;
|
|
185
262
|
maxDeposit: BN;
|
|
186
|
-
|
|
263
|
+
orderState: PublicKey;
|
|
264
|
+
};
|
|
265
|
+
export declare type OrderStateAccount = {
|
|
266
|
+
orderHistory: PublicKey;
|
|
267
|
+
orderFillerRewardStructure: OrderFillerRewardStructure;
|
|
268
|
+
minOrderQuoteAssetAmount: BN;
|
|
187
269
|
};
|
|
188
270
|
export declare type MarketsAccount = {
|
|
189
271
|
accountIndex: BN;
|
|
@@ -219,13 +301,15 @@ export declare type AMM = {
|
|
|
219
301
|
totalFeeMinusDistributions: BN;
|
|
220
302
|
totalFeeWithdrawn: BN;
|
|
221
303
|
totalFee: BN;
|
|
222
|
-
|
|
304
|
+
minimumQuoteAssetTradeSize: BN;
|
|
305
|
+
minimumBaseAssetTradeSize: BN;
|
|
223
306
|
};
|
|
224
307
|
export declare type UserPosition = {
|
|
225
308
|
baseAssetAmount: BN;
|
|
226
309
|
lastCumulativeFundingRate: BN;
|
|
227
310
|
marketIndex: BN;
|
|
228
311
|
quoteAssetAmount: BN;
|
|
312
|
+
openOrders: BN;
|
|
229
313
|
};
|
|
230
314
|
export declare type UserPositionsAccount = {
|
|
231
315
|
positions: UserPosition[];
|
|
@@ -237,6 +321,51 @@ export declare type UserAccount = {
|
|
|
237
321
|
cumulativeDeposits: BN;
|
|
238
322
|
positions: PublicKey;
|
|
239
323
|
totalFeePaid: BN;
|
|
324
|
+
totalTokenDiscount: BN;
|
|
325
|
+
totalReferralReward: BN;
|
|
326
|
+
totalRefereeDiscount: BN;
|
|
327
|
+
};
|
|
328
|
+
export declare type UserOrdersAccount = {
|
|
329
|
+
orders: Order[];
|
|
330
|
+
user: PublicKey;
|
|
331
|
+
};
|
|
332
|
+
export declare type Order = {
|
|
333
|
+
status: OrderStatus;
|
|
334
|
+
orderType: OrderType;
|
|
335
|
+
ts: BN;
|
|
336
|
+
orderId: BN;
|
|
337
|
+
marketIndex: BN;
|
|
338
|
+
price: BN;
|
|
339
|
+
baseAssetAmount: BN;
|
|
340
|
+
baseAssetAmountFilled: BN;
|
|
341
|
+
quoteAssetAmount: BN;
|
|
342
|
+
quoteAssetAmountFilled: BN;
|
|
343
|
+
fee: BN;
|
|
344
|
+
direction: PositionDirection;
|
|
345
|
+
reduceOnly: boolean;
|
|
346
|
+
triggerPrice: BN;
|
|
347
|
+
triggerCondition: OrderTriggerCondition;
|
|
348
|
+
discountTier: OrderDiscountTier;
|
|
349
|
+
referrer: PublicKey;
|
|
350
|
+
postOnly: boolean;
|
|
351
|
+
immediateOrCancel: boolean;
|
|
352
|
+
};
|
|
353
|
+
export declare type OrderParams = {
|
|
354
|
+
orderType: OrderType;
|
|
355
|
+
direction: PositionDirection;
|
|
356
|
+
quoteAssetAmount: BN;
|
|
357
|
+
baseAssetAmount: BN;
|
|
358
|
+
price: BN;
|
|
359
|
+
marketIndex: BN;
|
|
360
|
+
reduceOnly: boolean;
|
|
361
|
+
postOnly: boolean;
|
|
362
|
+
immediateOrCancel: boolean;
|
|
363
|
+
triggerPrice: BN;
|
|
364
|
+
triggerCondition: OrderTriggerCondition;
|
|
365
|
+
optionalAccounts: {
|
|
366
|
+
discountToken: boolean;
|
|
367
|
+
referrer: boolean;
|
|
368
|
+
};
|
|
240
369
|
};
|
|
241
370
|
export interface IWallet {
|
|
242
371
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
|
@@ -287,4 +416,8 @@ export declare type OracleGuardRails = {
|
|
|
287
416
|
};
|
|
288
417
|
useForLiquidations: boolean;
|
|
289
418
|
};
|
|
290
|
-
|
|
419
|
+
export declare type OrderFillerRewardStructure = {
|
|
420
|
+
rewardNumerator: BN;
|
|
421
|
+
rewardDenominator: BN;
|
|
422
|
+
timeBasedRewardLowerBound: BN;
|
|
423
|
+
};
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TradeSide = exports.OracleSource = exports.PositionDirection = exports.SwapDirection = void 0;
|
|
3
|
+
exports.TradeSide = exports.isVariant = exports.OrderTriggerCondition = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.SwapDirection = void 0;
|
|
4
4
|
// # Utility Types / Enums / Constants
|
|
5
5
|
class SwapDirection {
|
|
6
6
|
}
|
|
@@ -17,6 +17,41 @@ class OracleSource {
|
|
|
17
17
|
exports.OracleSource = OracleSource;
|
|
18
18
|
OracleSource.PYTH = { pyth: {} };
|
|
19
19
|
OracleSource.SWITCHBOARD = { switchboard: {} };
|
|
20
|
+
class OrderType {
|
|
21
|
+
}
|
|
22
|
+
exports.OrderType = OrderType;
|
|
23
|
+
OrderType.LIMIT = { limit: {} };
|
|
24
|
+
OrderType.STOP = { stop: {} };
|
|
25
|
+
OrderType.STOP_LIMIT = { stopLimit: {} };
|
|
26
|
+
OrderType.MARKET = { market: {} };
|
|
27
|
+
class OrderStatus {
|
|
28
|
+
}
|
|
29
|
+
exports.OrderStatus = OrderStatus;
|
|
30
|
+
OrderStatus.INIT = { init: {} };
|
|
31
|
+
OrderStatus.OPEN = { open: {} };
|
|
32
|
+
class OrderDiscountTier {
|
|
33
|
+
}
|
|
34
|
+
exports.OrderDiscountTier = OrderDiscountTier;
|
|
35
|
+
OrderDiscountTier.NONE = { none: {} };
|
|
36
|
+
OrderDiscountTier.FIRST = { first: {} };
|
|
37
|
+
OrderDiscountTier.SECOND = { second: {} };
|
|
38
|
+
OrderDiscountTier.THIRD = { third: {} };
|
|
39
|
+
OrderDiscountTier.FOURTH = { fourth: {} };
|
|
40
|
+
class OrderAction {
|
|
41
|
+
}
|
|
42
|
+
exports.OrderAction = OrderAction;
|
|
43
|
+
OrderAction.PLACE = { place: {} };
|
|
44
|
+
OrderAction.CANCEL = { cancel: {} };
|
|
45
|
+
OrderAction.FILL = { fill: {} };
|
|
46
|
+
class OrderTriggerCondition {
|
|
47
|
+
}
|
|
48
|
+
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
49
|
+
OrderTriggerCondition.ABOVE = { above: {} };
|
|
50
|
+
OrderTriggerCondition.BELOW = { below: {} };
|
|
51
|
+
function isVariant(object, type) {
|
|
52
|
+
return object.hasOwnProperty(type);
|
|
53
|
+
}
|
|
54
|
+
exports.isVariant = isVariant;
|
|
20
55
|
var TradeSide;
|
|
21
56
|
(function (TradeSide) {
|
|
22
57
|
TradeSide[TradeSide["None"] = 0] = "None";
|
package/lib/util/tps.d.ts
CHANGED
package/lib/wallet.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -5,12 +5,14 @@ import {
|
|
|
5
5
|
} from './types';
|
|
6
6
|
import { AccountSubscriber, NotSubscribedError } from './types';
|
|
7
7
|
import {
|
|
8
|
+
CurveHistoryAccount,
|
|
8
9
|
DepositHistoryAccount,
|
|
9
|
-
ExtendedCurveHistoryAccount,
|
|
10
10
|
FundingPaymentHistoryAccount,
|
|
11
11
|
FundingRateHistoryAccount,
|
|
12
12
|
LiquidationHistoryAccount,
|
|
13
13
|
MarketsAccount,
|
|
14
|
+
OrderHistoryAccount,
|
|
15
|
+
OrderStateAccount,
|
|
14
16
|
StateAccount,
|
|
15
17
|
TradeHistoryAccount,
|
|
16
18
|
} from '../types';
|
|
@@ -32,8 +34,10 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
32
34
|
depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
|
|
33
35
|
fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
|
|
34
36
|
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
35
|
-
curveHistoryAccountSubscriber?: AccountSubscriber<
|
|
37
|
+
curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
|
|
36
38
|
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
39
|
+
orderStateAccountSubscriber?: AccountSubscriber<OrderStateAccount>;
|
|
40
|
+
orderHistoryAccountSubscriber?: AccountSubscriber<OrderHistoryAccount>;
|
|
37
41
|
|
|
38
42
|
optionalExtraSubscriptions: ClearingHouseAccountTypes[] = [];
|
|
39
43
|
|
|
@@ -92,6 +96,21 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
92
96
|
this.eventEmitter.emit('update');
|
|
93
97
|
});
|
|
94
98
|
|
|
99
|
+
this.orderStateAccountSubscriber = new WebSocketAccountSubscriber(
|
|
100
|
+
'orderState',
|
|
101
|
+
this.program,
|
|
102
|
+
state.orderState
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
await this.orderStateAccountSubscriber.subscribe(
|
|
106
|
+
(data: OrderStateAccount) => {
|
|
107
|
+
this.eventEmitter.emit('orderStateAccountUpdate', data);
|
|
108
|
+
this.eventEmitter.emit('update');
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const orderState = this.orderStateAccountSubscriber.data;
|
|
113
|
+
|
|
95
114
|
// create subscribers for other state accounts
|
|
96
115
|
|
|
97
116
|
this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
@@ -126,9 +145,15 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
126
145
|
);
|
|
127
146
|
|
|
128
147
|
this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
129
|
-
'
|
|
148
|
+
'curveHistory',
|
|
130
149
|
this.program,
|
|
131
|
-
state.
|
|
150
|
+
state.curveHistory
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
this.orderHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
154
|
+
'orderHistory',
|
|
155
|
+
this.program,
|
|
156
|
+
orderState.orderHistory
|
|
132
157
|
);
|
|
133
158
|
|
|
134
159
|
const extraSusbcribersToUse: {
|
|
@@ -172,6 +197,12 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
172
197
|
eventType: 'curveHistoryAccountUpdate',
|
|
173
198
|
});
|
|
174
199
|
|
|
200
|
+
if (optionalSubscriptions?.includes('orderHistoryAccount'))
|
|
201
|
+
extraSusbcribersToUse.push({
|
|
202
|
+
subscriber: this.orderHistoryAccountSubscriber,
|
|
203
|
+
eventType: 'orderHistoryAccountUpdate',
|
|
204
|
+
});
|
|
205
|
+
|
|
175
206
|
this.optionalExtraSubscriptions = optionalSubscriptions ?? [];
|
|
176
207
|
|
|
177
208
|
// await all subcriptions in parallel to boost performance
|
|
@@ -219,6 +250,7 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
219
250
|
|
|
220
251
|
await this.stateAccountSubscriber.unsubscribe();
|
|
221
252
|
await this.marketsAccountSubscriber.unsubscribe();
|
|
253
|
+
await this.orderStateAccountSubscriber.unsubscribe();
|
|
222
254
|
|
|
223
255
|
if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
|
|
224
256
|
await this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
@@ -246,6 +278,10 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
246
278
|
await this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
247
279
|
}
|
|
248
280
|
|
|
281
|
+
if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
|
|
282
|
+
await this.orderHistoryAccountSubscriber.unsubscribe();
|
|
283
|
+
}
|
|
284
|
+
|
|
249
285
|
this.isSubscribed = false;
|
|
250
286
|
}
|
|
251
287
|
|
|
@@ -307,7 +343,7 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
307
343
|
return this.fundingRateHistoryAccountSubscriber.data;
|
|
308
344
|
}
|
|
309
345
|
|
|
310
|
-
public getCurveHistoryAccount():
|
|
346
|
+
public getCurveHistoryAccount(): CurveHistoryAccount {
|
|
311
347
|
this.assertIsSubscribed();
|
|
312
348
|
this.assertOptionalIsSubscribed('curveHistoryAccount');
|
|
313
349
|
return this.curveHistoryAccountSubscriber.data;
|
|
@@ -318,4 +354,15 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
318
354
|
this.assertOptionalIsSubscribed('liquidationHistoryAccount');
|
|
319
355
|
return this.liquidationHistoryAccountSubscriber.data;
|
|
320
356
|
}
|
|
357
|
+
|
|
358
|
+
public getOrderHistoryAccount(): OrderHistoryAccount {
|
|
359
|
+
this.assertIsSubscribed();
|
|
360
|
+
this.assertOptionalIsSubscribed('orderHistoryAccount');
|
|
361
|
+
return this.orderHistoryAccountSubscriber.data;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
public getOrderStateAccount(): OrderStateAccount {
|
|
365
|
+
this.assertIsSubscribed();
|
|
366
|
+
return this.orderStateAccountSubscriber.data;
|
|
367
|
+
}
|
|
321
368
|
}
|