@drift-labs/sdk 0.1.18-orders.1 → 0.1.18
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/defaultClearingHouseAccountSubscriber.d.ts +4 -7
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/defaultClearingHouseAccountSubscriber.js +1 -26
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -3
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/defaultUserAccountSubscriber.js +4 -13
- package/lib/accounts/types.d.ts +6 -32
- package/lib/accounts/types.d.ts.map +1 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -0
- package/lib/addresses.d.ts +1 -4
- package/lib/addresses.d.ts.map +1 -0
- package/lib/addresses.js +1 -28
- package/lib/admin.d.ts +6 -10
- package/lib/admin.d.ts.map +1 -0
- package/lib/admin.js +44 -50
- package/lib/assert/assert.d.ts +1 -0
- package/lib/assert/assert.d.ts.map +1 -0
- package/lib/clearingHouse.d.ts +3 -22
- package/lib/clearingHouse.d.ts.map +1 -0
- package/lib/clearingHouse.js +7 -238
- package/lib/clearingHouseUser.d.ts +17 -10
- package/lib/clearingHouseUser.d.ts.map +1 -0
- package/lib/clearingHouseUser.js +84 -98
- package/lib/config.d.ts +1 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -1
- package/lib/constants/markets.d.ts +1 -0
- package/lib/constants/markets.d.ts.map +1 -0
- package/lib/constants/markets.js +16 -0
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.d.ts.map +1 -0
- package/lib/examples/makeTradeExample.d.ts +1 -0
- package/lib/examples/makeTradeExample.d.ts.map +1 -0
- package/lib/idl/clearing_house.json +233 -911
- package/lib/index.d.ts +1 -4
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +0 -4
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.d.ts.map +1 -0
- package/lib/math/amm.js +8 -31
- package/lib/math/conversion.d.ts +1 -0
- package/lib/math/conversion.d.ts.map +1 -0
- package/lib/math/funding.d.ts +1 -0
- package/lib/math/funding.d.ts.map +1 -0
- package/lib/math/funding.js +4 -1
- package/lib/math/insuranceFund.d.ts +1 -0
- package/lib/math/insuranceFund.d.ts.map +1 -0
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.d.ts.map +1 -0
- package/lib/math/market.js +1 -11
- package/lib/math/position.d.ts +2 -4
- package/lib/math/position.d.ts.map +1 -0
- package/lib/math/position.js +4 -18
- package/lib/math/trade.d.ts +1 -0
- package/lib/math/trade.d.ts.map +1 -0
- package/lib/math/utils.d.ts +1 -0
- package/lib/math/utils.d.ts.map +1 -0
- package/lib/mockUSDCFaucet.d.ts +1 -0
- package/lib/mockUSDCFaucet.d.ts.map +1 -0
- package/lib/pythClient.d.ts +1 -0
- package/lib/pythClient.d.ts.map +1 -0
- package/lib/tx/defaultTxSender.d.ts +1 -0
- package/lib/tx/defaultTxSender.d.ts.map +1 -0
- package/lib/tx/types.d.ts +1 -0
- package/lib/tx/types.d.ts.map +1 -0
- package/lib/tx/utils.d.ts +1 -0
- package/lib/tx/utils.d.ts.map +1 -0
- package/lib/types.d.ts +8 -140
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +1 -36
- package/lib/util/computeUnits.d.ts +1 -0
- package/lib/util/computeUnits.d.ts.map +1 -0
- package/lib/util/tps.d.ts +1 -0
- package/lib/util/tps.d.ts.map +1 -0
- package/lib/wallet.d.ts +1 -0
- package/lib/wallet.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/accounts/defaultClearingHouseAccountSubscriber.ts +5 -52
- package/src/accounts/defaultUserAccountSubscriber.ts +6 -31
- package/src/accounts/types.ts +5 -42
- package/src/addresses.ts +0 -35
- package/src/admin.ts +52 -86
- package/src/clearingHouse.ts +7 -340
- package/src/clearingHouseUser.ts +102 -154
- package/src/config.ts +1 -1
- package/src/constants/markets.ts +16 -0
- package/src/idl/clearing_house.json +233 -911
- package/src/index.ts +0 -4
- package/src/math/amm.ts +14 -47
- package/src/math/funding.ts +5 -1
- package/src/math/market.ts +2 -28
- package/src/math/position.ts +3 -23
- package/src/types.ts +7 -124
- package/tsconfig.json +1 -0
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/lib/math/orders.d.ts +0 -3
- package/lib/math/orders.js +0 -30
- package/lib/orderParams.d.ts +0 -7
- package/lib/orderParams.js +0 -88
- package/lib/orders.d.ts +0 -5
- package/lib/orders.js +0 -136
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
- package/src/math/orders.ts +0 -39
- package/src/orderParams.ts +0 -128
- package/src/orders.ts +0 -230
package/src/index.ts
CHANGED
|
@@ -4,7 +4,6 @@ export * from './mockUSDCFaucet';
|
|
|
4
4
|
export * from './pythClient';
|
|
5
5
|
export * from './types';
|
|
6
6
|
export * from './constants/markets';
|
|
7
|
-
export * from './accounts/defaultHistoryAccountSubscriber';
|
|
8
7
|
export * from './accounts/defaultClearingHouseAccountSubscriber';
|
|
9
8
|
export * from './accounts/types';
|
|
10
9
|
export * from './addresses';
|
|
@@ -18,9 +17,6 @@ export * from './math/market';
|
|
|
18
17
|
export * from './math/position';
|
|
19
18
|
export * from './math/amm';
|
|
20
19
|
export * from './math/trade';
|
|
21
|
-
export * from './math/orders';
|
|
22
|
-
export * from './orders';
|
|
23
|
-
export * from './orderParams';
|
|
24
20
|
export * from './wallet';
|
|
25
21
|
export * from './types';
|
|
26
22
|
export * from './math/utils';
|
package/src/math/amm.ts
CHANGED
|
@@ -6,20 +6,9 @@ import {
|
|
|
6
6
|
ZERO,
|
|
7
7
|
} from '../constants/numericConstants';
|
|
8
8
|
import { calculateBaseAssetValue } from './position';
|
|
9
|
-
import {
|
|
10
|
-
AMM,
|
|
11
|
-
PositionDirection,
|
|
12
|
-
SwapDirection,
|
|
13
|
-
Market,
|
|
14
|
-
isVariant,
|
|
15
|
-
} from '../types';
|
|
9
|
+
import { AMM, PositionDirection, SwapDirection, Market } from '../types';
|
|
16
10
|
import { assert } from '../assert/assert';
|
|
17
|
-
import {
|
|
18
|
-
calculatePositionPNL,
|
|
19
|
-
calculateMarkPrice,
|
|
20
|
-
convertToNumber,
|
|
21
|
-
squareRootBN,
|
|
22
|
-
} from '..';
|
|
11
|
+
import { calculatePositionPNL, calculateMarkPrice, convertToNumber } from '..';
|
|
23
12
|
|
|
24
13
|
/**
|
|
25
14
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
@@ -125,11 +114,17 @@ export function getSwapDirection(
|
|
|
125
114
|
inputAssetType: AssetType,
|
|
126
115
|
positionDirection: PositionDirection
|
|
127
116
|
): SwapDirection {
|
|
128
|
-
if (
|
|
117
|
+
if (
|
|
118
|
+
positionDirection === PositionDirection.LONG &&
|
|
119
|
+
inputAssetType === 'base'
|
|
120
|
+
) {
|
|
129
121
|
return SwapDirection.REMOVE;
|
|
130
122
|
}
|
|
131
123
|
|
|
132
|
-
if (
|
|
124
|
+
if (
|
|
125
|
+
positionDirection === PositionDirection.SHORT &&
|
|
126
|
+
inputAssetType === 'quote'
|
|
127
|
+
) {
|
|
133
128
|
return SwapDirection.REMOVE;
|
|
134
129
|
}
|
|
135
130
|
|
|
@@ -155,7 +150,6 @@ export function calculateAdjustKCost(
|
|
|
155
150
|
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
156
151
|
marketIndex: new BN(marketIndex),
|
|
157
152
|
quoteAssetAmount: new BN(0),
|
|
158
|
-
openOrders: new BN(0),
|
|
159
153
|
};
|
|
160
154
|
|
|
161
155
|
const currentValue = calculateBaseAssetValue(market, netUserPosition);
|
|
@@ -196,7 +190,6 @@ export function calculateRepegCost(
|
|
|
196
190
|
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
197
191
|
marketIndex: new BN(marketIndex),
|
|
198
192
|
quoteAssetAmount: new BN(0),
|
|
199
|
-
openOrders: new BN(0),
|
|
200
193
|
};
|
|
201
194
|
|
|
202
195
|
const currentValue = calculateBaseAssetValue(market, netUserPosition);
|
|
@@ -227,6 +220,10 @@ export function calculateRepegCost(
|
|
|
227
220
|
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
228
221
|
*/
|
|
229
222
|
export function calculateTerminalPrice(market: Market) {
|
|
223
|
+
if (!market.initialized) {
|
|
224
|
+
return new BN(0);
|
|
225
|
+
}
|
|
226
|
+
|
|
230
227
|
const directionToClose = market.baseAssetAmount.gt(ZERO)
|
|
231
228
|
? PositionDirection.SHORT
|
|
232
229
|
: PositionDirection.LONG;
|
|
@@ -246,33 +243,3 @@ export function calculateTerminalPrice(market: Market) {
|
|
|
246
243
|
|
|
247
244
|
return terminalPrice;
|
|
248
245
|
}
|
|
249
|
-
|
|
250
|
-
export function calculateMaxBaseAssetAmountToTrade(
|
|
251
|
-
amm: AMM,
|
|
252
|
-
limit_price: BN
|
|
253
|
-
): [BN, PositionDirection] {
|
|
254
|
-
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
255
|
-
|
|
256
|
-
const newBaseAssetReserveSquared = invariant
|
|
257
|
-
.mul(MARK_PRICE_PRECISION)
|
|
258
|
-
.mul(amm.pegMultiplier)
|
|
259
|
-
.div(limit_price)
|
|
260
|
-
.div(PEG_PRECISION);
|
|
261
|
-
|
|
262
|
-
const newBaseAssetReserve = squareRootBN(newBaseAssetReserveSquared);
|
|
263
|
-
|
|
264
|
-
if (newBaseAssetReserve.gt(amm.baseAssetReserve)) {
|
|
265
|
-
return [
|
|
266
|
-
newBaseAssetReserve.sub(amm.baseAssetReserve),
|
|
267
|
-
PositionDirection.SHORT,
|
|
268
|
-
];
|
|
269
|
-
} else if (newBaseAssetReserve.lt(amm.baseAssetReserve)) {
|
|
270
|
-
return [
|
|
271
|
-
amm.baseAssetReserve.sub(newBaseAssetReserve),
|
|
272
|
-
PositionDirection.LONG,
|
|
273
|
-
];
|
|
274
|
-
} else {
|
|
275
|
-
console.log('tradeSize Too Small');
|
|
276
|
-
return [new BN(0), PositionDirection.LONG];
|
|
277
|
-
}
|
|
278
|
-
}
|
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/market.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
|
-
import { Market
|
|
3
|
-
import {
|
|
4
|
-
calculateAmmReservesAfterSwap,
|
|
5
|
-
calculatePrice,
|
|
6
|
-
getSwapDirection,
|
|
7
|
-
} from './amm';
|
|
2
|
+
import { Market } from '../types';
|
|
3
|
+
import { calculatePrice } from './amm';
|
|
8
4
|
|
|
9
5
|
/**
|
|
10
6
|
* Calculates market mark price
|
|
@@ -19,25 +15,3 @@ export function calculateMarkPrice(market: Market): BN {
|
|
|
19
15
|
market.amm.pegMultiplier
|
|
20
16
|
);
|
|
21
17
|
}
|
|
22
|
-
|
|
23
|
-
export function calculateNewMarketAfterTrade(
|
|
24
|
-
baseAssetAmount: BN,
|
|
25
|
-
direction: PositionDirection,
|
|
26
|
-
market: Market
|
|
27
|
-
): Market {
|
|
28
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] =
|
|
29
|
-
calculateAmmReservesAfterSwap(
|
|
30
|
-
market.amm,
|
|
31
|
-
'base',
|
|
32
|
-
baseAssetAmount.abs(),
|
|
33
|
-
getSwapDirection('base', direction)
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const newAmm = Object.assign({}, market.amm);
|
|
37
|
-
const newMarket = Object.assign({}, market);
|
|
38
|
-
newMarket.amm = newAmm;
|
|
39
|
-
newMarket.amm.quoteAssetReserve = newQuoteAssetReserve;
|
|
40
|
-
newMarket.amm.baseAssetReserve = newBaseAssetReserve;
|
|
41
|
-
|
|
42
|
-
return newMarket;
|
|
43
|
-
}
|
package/src/math/position.ts
CHANGED
|
@@ -27,7 +27,9 @@ export function calculateBaseAssetValue(
|
|
|
27
27
|
return ZERO;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const directionToClose =
|
|
30
|
+
const directionToClose = userPosition.baseAssetAmount.gt(ZERO)
|
|
31
|
+
? PositionDirection.SHORT
|
|
32
|
+
: PositionDirection.LONG;
|
|
31
33
|
|
|
32
34
|
const [newQuoteAssetReserve, _] = calculateAmmReservesAfterSwap(
|
|
33
35
|
market.amm,
|
|
@@ -136,25 +138,3 @@ export function calculateEntryPrice(userPosition: UserPosition): BN {
|
|
|
136
138
|
.div(userPosition.baseAssetAmount)
|
|
137
139
|
.abs();
|
|
138
140
|
}
|
|
139
|
-
|
|
140
|
-
export function findDirectionToClose(
|
|
141
|
-
userPosition: UserPosition
|
|
142
|
-
): PositionDirection {
|
|
143
|
-
return userPosition.baseAssetAmount.gt(ZERO)
|
|
144
|
-
? PositionDirection.SHORT
|
|
145
|
-
: PositionDirection.LONG;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export function positionCurrentDirection(
|
|
149
|
-
userPosition: UserPosition
|
|
150
|
-
): PositionDirection {
|
|
151
|
-
return userPosition.baseAssetAmount.gte(ZERO)
|
|
152
|
-
? PositionDirection.LONG
|
|
153
|
-
: PositionDirection.SHORT;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function isEmptyPosition(userPosition: UserPosition): boolean {
|
|
157
|
-
return (
|
|
158
|
-
userPosition.baseAssetAmount.eq(ZERO) && userPosition.openOrders.eq(ZERO)
|
|
159
|
-
);
|
|
160
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -6,7 +6,6 @@ export class SwapDirection {
|
|
|
6
6
|
static readonly ADD = { add: {} };
|
|
7
7
|
static readonly REMOVE = { remove: {} };
|
|
8
8
|
}
|
|
9
|
-
|
|
10
9
|
export class PositionDirection {
|
|
11
10
|
static readonly LONG = { long: {} };
|
|
12
11
|
static readonly SHORT = { short: {} };
|
|
@@ -17,41 +16,6 @@ export class OracleSource {
|
|
|
17
16
|
static readonly SWITCHBOARD = { switchboard: {} };
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
export class OrderType {
|
|
21
|
-
static readonly LIMIT = { limit: {} };
|
|
22
|
-
static readonly STOP = { stop: {} };
|
|
23
|
-
static readonly STOP_LIMIT = { stopLimit: {} };
|
|
24
|
-
static readonly MARKET = { market: {} };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class OrderStatus {
|
|
28
|
-
static readonly INIT = { init: {} };
|
|
29
|
-
static readonly OPEN = { open: {} };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class OrderDiscountTier {
|
|
33
|
-
static readonly NONE = { none: {} };
|
|
34
|
-
static readonly FIRST = { first: {} };
|
|
35
|
-
static readonly SECOND = { second: {} };
|
|
36
|
-
static readonly THIRD = { third: {} };
|
|
37
|
-
static readonly FOURTH = { fourth: {} };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class OrderAction {
|
|
41
|
-
static readonly PLACE = { place: {} };
|
|
42
|
-
static readonly CANCEL = { cancel: {} };
|
|
43
|
-
static readonly FILL = { fill: {} };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export class OrderTriggerCondition {
|
|
47
|
-
static readonly ABOVE = { above: {} };
|
|
48
|
-
static readonly BELOW = { below: {} };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function isVariant(object: unknown, type: string) {
|
|
52
|
-
return object.hasOwnProperty(type);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
19
|
export enum TradeSide {
|
|
56
20
|
None = 0,
|
|
57
21
|
Buy = 1,
|
|
@@ -79,9 +43,9 @@ export type DepositHistoryAccount = {
|
|
|
79
43
|
depositRecords: DepositRecord[];
|
|
80
44
|
};
|
|
81
45
|
|
|
82
|
-
export type
|
|
46
|
+
export type ExtendedCurveHistoryAccount = {
|
|
83
47
|
head: BN;
|
|
84
|
-
curveRecords:
|
|
48
|
+
curveRecords: ExtendedCurveRecord[];
|
|
85
49
|
};
|
|
86
50
|
|
|
87
51
|
export type FundingRateHistoryAccount = {
|
|
@@ -99,12 +63,6 @@ export type LiquidationHistoryAccount = {
|
|
|
99
63
|
liquidationRecords: LiquidationRecord[];
|
|
100
64
|
};
|
|
101
65
|
|
|
102
|
-
export type OrderHistoryAccount = {
|
|
103
|
-
head: BN;
|
|
104
|
-
lastOrderId: BN;
|
|
105
|
-
orderRecords: OrderRecord[];
|
|
106
|
-
};
|
|
107
|
-
|
|
108
66
|
export type DepositRecord = {
|
|
109
67
|
ts: BN;
|
|
110
68
|
recordId: BN;
|
|
@@ -119,7 +77,7 @@ export type DepositRecord = {
|
|
|
119
77
|
amount: BN;
|
|
120
78
|
};
|
|
121
79
|
|
|
122
|
-
export type
|
|
80
|
+
export type ExtendedCurveRecord = {
|
|
123
81
|
ts: BN;
|
|
124
82
|
recordId: BN;
|
|
125
83
|
marketIndex: BN;
|
|
@@ -135,6 +93,8 @@ export type CurveRecord = {
|
|
|
135
93
|
baseAssetAmountShort: BN;
|
|
136
94
|
baseAssetAmount: BN;
|
|
137
95
|
openInterest: BN;
|
|
96
|
+
oraclePrice: BN;
|
|
97
|
+
tradeId: BN;
|
|
138
98
|
};
|
|
139
99
|
|
|
140
100
|
export type TradeRecord = {
|
|
@@ -202,21 +162,6 @@ export type LiquidationRecord = {
|
|
|
202
162
|
marginRatio: BN;
|
|
203
163
|
};
|
|
204
164
|
|
|
205
|
-
export type OrderRecord = {
|
|
206
|
-
ts: BN;
|
|
207
|
-
recordId: BN;
|
|
208
|
-
order: Order;
|
|
209
|
-
user: PublicKey;
|
|
210
|
-
authority: PublicKey;
|
|
211
|
-
action: OrderAction;
|
|
212
|
-
filler: PublicKey;
|
|
213
|
-
baseAssetAmountFilled: BN;
|
|
214
|
-
quoteAssetAmountFilled: BN;
|
|
215
|
-
fee: BN;
|
|
216
|
-
fillerReward: BN;
|
|
217
|
-
tradeRecordId: BN;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
165
|
export type StateAccount = {
|
|
221
166
|
admin: PublicKey;
|
|
222
167
|
fundingPaused: boolean;
|
|
@@ -254,13 +199,7 @@ export type StateAccount = {
|
|
|
254
199
|
discountMint: PublicKey;
|
|
255
200
|
oracleGuardRails: OracleGuardRails;
|
|
256
201
|
maxDeposit: BN;
|
|
257
|
-
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
export type OrderStateAccount = {
|
|
261
|
-
orderHistory: PublicKey;
|
|
262
|
-
orderFillerRewardStructure: OrderFillerRewardStructure;
|
|
263
|
-
minOrderQuoteAssetAmount: BN;
|
|
202
|
+
extendedCurveHistory: PublicKey;
|
|
264
203
|
};
|
|
265
204
|
|
|
266
205
|
export type MarketsAccount = {
|
|
@@ -299,8 +238,7 @@ export type AMM = {
|
|
|
299
238
|
totalFeeMinusDistributions: BN;
|
|
300
239
|
totalFeeWithdrawn: BN;
|
|
301
240
|
totalFee: BN;
|
|
302
|
-
|
|
303
|
-
minimumBaseAssetTradeSize: BN;
|
|
241
|
+
minimumTradeSize: BN;
|
|
304
242
|
};
|
|
305
243
|
|
|
306
244
|
// # User Account Types
|
|
@@ -309,7 +247,6 @@ export type UserPosition = {
|
|
|
309
247
|
lastCumulativeFundingRate: BN;
|
|
310
248
|
marketIndex: BN;
|
|
311
249
|
quoteAssetAmount: BN;
|
|
312
|
-
openOrders: BN;
|
|
313
250
|
};
|
|
314
251
|
|
|
315
252
|
export type UserPositionsAccount = {
|
|
@@ -323,54 +260,6 @@ export type UserAccount = {
|
|
|
323
260
|
cumulativeDeposits: BN;
|
|
324
261
|
positions: PublicKey;
|
|
325
262
|
totalFeePaid: BN;
|
|
326
|
-
totalTokenDiscount: BN;
|
|
327
|
-
totalReferralReward: BN;
|
|
328
|
-
totalRefereeDiscount: BN;
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
export type UserOrdersAccount = {
|
|
332
|
-
orders: Order[];
|
|
333
|
-
user: PublicKey;
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
export type Order = {
|
|
337
|
-
status: OrderStatus;
|
|
338
|
-
orderType: OrderType;
|
|
339
|
-
ts: BN;
|
|
340
|
-
orderId: BN;
|
|
341
|
-
marketIndex: BN;
|
|
342
|
-
price: BN;
|
|
343
|
-
baseAssetAmount: BN;
|
|
344
|
-
baseAssetAmountFilled: BN;
|
|
345
|
-
quoteAssetAmount: BN;
|
|
346
|
-
quoteAssetAmountFilled: BN;
|
|
347
|
-
fee: BN;
|
|
348
|
-
direction: PositionDirection;
|
|
349
|
-
reduceOnly: boolean;
|
|
350
|
-
triggerPrice: BN;
|
|
351
|
-
triggerCondition: OrderTriggerCondition;
|
|
352
|
-
discountTier: OrderDiscountTier;
|
|
353
|
-
referrer: PublicKey;
|
|
354
|
-
postOnly: boolean;
|
|
355
|
-
immediateOrCancel: boolean;
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
export type OrderParams = {
|
|
359
|
-
orderType: OrderType;
|
|
360
|
-
direction: PositionDirection;
|
|
361
|
-
quoteAssetAmount: BN;
|
|
362
|
-
baseAssetAmount: BN;
|
|
363
|
-
price: BN;
|
|
364
|
-
marketIndex: BN;
|
|
365
|
-
reduceOnly: boolean;
|
|
366
|
-
postOnly: boolean;
|
|
367
|
-
immediateOrCancel: boolean;
|
|
368
|
-
triggerPrice: BN;
|
|
369
|
-
triggerCondition: OrderTriggerCondition;
|
|
370
|
-
optionalAccounts: {
|
|
371
|
-
discountToken: boolean;
|
|
372
|
-
referrer: boolean;
|
|
373
|
-
};
|
|
374
263
|
};
|
|
375
264
|
|
|
376
265
|
// # Misc Types
|
|
@@ -425,9 +314,3 @@ export type OracleGuardRails = {
|
|
|
425
314
|
};
|
|
426
315
|
useForLiquidations: boolean;
|
|
427
316
|
};
|
|
428
|
-
|
|
429
|
-
export type OrderFillerRewardStructure = {
|
|
430
|
-
rewardNumerator: BN;
|
|
431
|
-
rewardDenominator: BN;
|
|
432
|
-
timeBasedRewardLowerBound: BN;
|
|
433
|
-
};
|
package/tsconfig.json
CHANGED
|
@@ -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;
|
package/lib/math/orders.d.ts
DELETED
package/lib/math/orders.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isOrderRiskIncreasing = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
function isOrderRiskIncreasing(user, order) {
|
|
7
|
-
if (types_1.isVariant(order.status, 'init')) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
const position = user.getUserPosition(order.marketIndex) ||
|
|
11
|
-
user.getEmptyPosition(order.marketIndex);
|
|
12
|
-
// if no position exists, it's risk increasing
|
|
13
|
-
if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
// if position is long and order is long
|
|
17
|
-
if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && types_1.isVariant(order.direction, 'long')) {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
// if position is short and order is short
|
|
21
|
-
if (position.baseAssetAmount.lt(numericConstants_1.ZERO) &&
|
|
22
|
-
types_1.isVariant(order.direction, 'short')) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
// if order will flip position
|
|
26
|
-
if (position.baseAssetAmount.abs().gt(order.baseAssetAmountFilled)) {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.isOrderRiskIncreasing = isOrderRiskIncreasing;
|
package/lib/orderParams.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { OrderParams, OrderTriggerCondition, PositionDirection } from './types';
|
|
3
|
-
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
|
|
5
|
-
export declare function getStopOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
|
|
6
|
-
export declare function getStopLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
|
|
7
|
-
export declare function getMarketOrderParams(marketIndex: BN, direction: PositionDirection, quoteAssetAmount: BN, baseAssetAmount: BN, reduceOnly: boolean, price?: BN, discountToken?: boolean, referrer?: boolean): OrderParams;
|