@drift-labs/sdk 0.1.18-orders.0 → 0.1.19-master.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/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 +2 -27
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -3
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/defaultUserAccountSubscriber.js +5 -14
- 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 +7 -10
- package/lib/admin.d.ts.map +1 -0
- package/lib/admin.js +64 -53
- 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 +15 -246
- package/lib/clearingHouseUser.d.ts +17 -10
- package/lib/clearingHouseUser.d.ts.map +1 -0
- package/lib/clearingHouseUser.js +97 -111
- 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/examples/makeTradeExample.js +6 -6
- package/lib/idl/clearing_house.json +233 -911
- package/lib/index.d.ts +1 -3
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +0 -3
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.d.ts.map +1 -0
- package/lib/math/amm.js +15 -38
- package/lib/math/conversion.d.ts +1 -0
- package/lib/math/conversion.d.ts.map +1 -0
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +1 -0
- package/lib/math/funding.d.ts.map +1 -0
- package/lib/math/funding.js +5 -2
- 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 +2 -12
- package/lib/math/position.d.ts +2 -4
- package/lib/math/position.d.ts.map +1 -0
- package/lib/math/position.js +5 -19
- package/lib/math/trade.d.ts +1 -0
- package/lib/math/trade.d.ts.map +1 -0
- package/lib/math/trade.js +16 -16
- 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/pythClient.js +1 -1
- 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 +9 -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 +71 -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 -3
- 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 +8 -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/lib/orderParams.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMarketOrderParams = exports.getStopLimitOrderParams = exports.getStopOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
|
-
const types_1 = require("./types");
|
|
5
|
-
const numericConstants_1 = require("./constants/numericConstants");
|
|
6
|
-
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false) {
|
|
7
|
-
return {
|
|
8
|
-
orderType: types_1.OrderType.LIMIT,
|
|
9
|
-
marketIndex,
|
|
10
|
-
direction,
|
|
11
|
-
quoteAssetAmount: numericConstants_1.ZERO,
|
|
12
|
-
baseAssetAmount,
|
|
13
|
-
price,
|
|
14
|
-
reduceOnly,
|
|
15
|
-
postOnly: false,
|
|
16
|
-
immediateOrCancel: false,
|
|
17
|
-
optionalAccounts: {
|
|
18
|
-
discountToken,
|
|
19
|
-
referrer,
|
|
20
|
-
},
|
|
21
|
-
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
22
|
-
triggerPrice: numericConstants_1.ZERO,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
exports.getLimitOrderParams = getLimitOrderParams;
|
|
26
|
-
function getStopOrderParams(marketIndex, direction, baseAssetAmount, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false) {
|
|
27
|
-
return {
|
|
28
|
-
orderType: types_1.OrderType.STOP,
|
|
29
|
-
marketIndex,
|
|
30
|
-
direction,
|
|
31
|
-
quoteAssetAmount: numericConstants_1.ZERO,
|
|
32
|
-
baseAssetAmount,
|
|
33
|
-
price: numericConstants_1.ZERO,
|
|
34
|
-
reduceOnly,
|
|
35
|
-
postOnly: false,
|
|
36
|
-
immediateOrCancel: false,
|
|
37
|
-
optionalAccounts: {
|
|
38
|
-
discountToken,
|
|
39
|
-
referrer,
|
|
40
|
-
},
|
|
41
|
-
triggerCondition,
|
|
42
|
-
triggerPrice,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
exports.getStopOrderParams = getStopOrderParams;
|
|
46
|
-
function getStopLimitOrderParams(marketIndex, direction, baseAssetAmount, price, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false) {
|
|
47
|
-
return {
|
|
48
|
-
orderType: types_1.OrderType.STOP_LIMIT,
|
|
49
|
-
marketIndex,
|
|
50
|
-
direction,
|
|
51
|
-
quoteAssetAmount: numericConstants_1.ZERO,
|
|
52
|
-
baseAssetAmount,
|
|
53
|
-
price,
|
|
54
|
-
reduceOnly,
|
|
55
|
-
postOnly: false,
|
|
56
|
-
immediateOrCancel: false,
|
|
57
|
-
optionalAccounts: {
|
|
58
|
-
discountToken,
|
|
59
|
-
referrer,
|
|
60
|
-
},
|
|
61
|
-
triggerCondition,
|
|
62
|
-
triggerPrice,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
exports.getStopLimitOrderParams = getStopLimitOrderParams;
|
|
66
|
-
function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAssetAmount, reduceOnly, price = numericConstants_1.ZERO, discountToken = false, referrer = false) {
|
|
67
|
-
if (baseAssetAmount.eq(numericConstants_1.ZERO) && quoteAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
68
|
-
throw Error('baseAssetAmount or quoteAssetAmount must be zero');
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
orderType: types_1.OrderType.MARKET,
|
|
72
|
-
marketIndex,
|
|
73
|
-
direction,
|
|
74
|
-
quoteAssetAmount,
|
|
75
|
-
baseAssetAmount,
|
|
76
|
-
price,
|
|
77
|
-
reduceOnly,
|
|
78
|
-
postOnly: false,
|
|
79
|
-
immediateOrCancel: false,
|
|
80
|
-
optionalAccounts: {
|
|
81
|
-
discountToken,
|
|
82
|
-
referrer,
|
|
83
|
-
},
|
|
84
|
-
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
85
|
-
triggerPrice: numericConstants_1.ZERO,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
exports.getMarketOrderParams = getMarketOrderParams;
|
package/lib/orders.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Market, Order, UserAccount, UserPosition } from './types';
|
|
2
|
-
import BN from 'bn.js';
|
|
3
|
-
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: Market, order: Order): [UserAccount, UserPosition, Market] | null;
|
|
4
|
-
export declare function calculateAmountToTradeForLimit(market: Market, order: Order): BN;
|
|
5
|
-
export declare function calculateAmountToTradeForStopLimit(market: Market, order: Order): BN;
|
package/lib/orders.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -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
|
-
}
|
package/src/math/orders.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
2
|
-
import { isVariant, Order } from '../types';
|
|
3
|
-
import { ZERO } from '../constants/numericConstants';
|
|
4
|
-
|
|
5
|
-
export function isOrderRiskIncreasing(
|
|
6
|
-
user: ClearingHouseUser,
|
|
7
|
-
order: Order
|
|
8
|
-
): boolean {
|
|
9
|
-
if (isVariant(order.status, 'init')) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const position =
|
|
14
|
-
user.getUserPosition(order.marketIndex) ||
|
|
15
|
-
user.getEmptyPosition(order.marketIndex);
|
|
16
|
-
|
|
17
|
-
// if no position exists, it's risk increasing
|
|
18
|
-
if (position.baseAssetAmount.eq(ZERO)) {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// if position is long and order is long
|
|
23
|
-
if (position.baseAssetAmount.gt(ZERO) && isVariant(order.direction, 'long')) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// if position is short and order is short
|
|
28
|
-
if (
|
|
29
|
-
position.baseAssetAmount.lt(ZERO) &&
|
|
30
|
-
isVariant(order.direction, 'short')
|
|
31
|
-
) {
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// if order will flip position
|
|
36
|
-
if (position.baseAssetAmount.abs().gt(order.baseAssetAmountFilled)) {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
}
|
package/src/orderParams.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
OrderParams,
|
|
3
|
-
OrderTriggerCondition,
|
|
4
|
-
OrderType,
|
|
5
|
-
PositionDirection,
|
|
6
|
-
} from './types';
|
|
7
|
-
import { BN } from '@project-serum/anchor';
|
|
8
|
-
import { ZERO } from './constants/numericConstants';
|
|
9
|
-
|
|
10
|
-
export function getLimitOrderParams(
|
|
11
|
-
marketIndex: BN,
|
|
12
|
-
direction: PositionDirection,
|
|
13
|
-
baseAssetAmount: BN,
|
|
14
|
-
price: BN,
|
|
15
|
-
reduceOnly: boolean,
|
|
16
|
-
discountToken = false,
|
|
17
|
-
referrer = false
|
|
18
|
-
): OrderParams {
|
|
19
|
-
return {
|
|
20
|
-
orderType: OrderType.LIMIT,
|
|
21
|
-
marketIndex,
|
|
22
|
-
direction,
|
|
23
|
-
quoteAssetAmount: ZERO,
|
|
24
|
-
baseAssetAmount,
|
|
25
|
-
price,
|
|
26
|
-
reduceOnly,
|
|
27
|
-
postOnly: false,
|
|
28
|
-
immediateOrCancel: false,
|
|
29
|
-
optionalAccounts: {
|
|
30
|
-
discountToken,
|
|
31
|
-
referrer,
|
|
32
|
-
},
|
|
33
|
-
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
34
|
-
triggerPrice: ZERO,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function getStopOrderParams(
|
|
39
|
-
marketIndex: BN,
|
|
40
|
-
direction: PositionDirection,
|
|
41
|
-
baseAssetAmount: BN,
|
|
42
|
-
triggerPrice: BN,
|
|
43
|
-
triggerCondition: OrderTriggerCondition,
|
|
44
|
-
reduceOnly: boolean,
|
|
45
|
-
discountToken = false,
|
|
46
|
-
referrer = false
|
|
47
|
-
): OrderParams {
|
|
48
|
-
return {
|
|
49
|
-
orderType: OrderType.STOP,
|
|
50
|
-
marketIndex,
|
|
51
|
-
direction,
|
|
52
|
-
quoteAssetAmount: ZERO,
|
|
53
|
-
baseAssetAmount,
|
|
54
|
-
price: ZERO,
|
|
55
|
-
reduceOnly,
|
|
56
|
-
postOnly: false,
|
|
57
|
-
immediateOrCancel: false,
|
|
58
|
-
optionalAccounts: {
|
|
59
|
-
discountToken,
|
|
60
|
-
referrer,
|
|
61
|
-
},
|
|
62
|
-
triggerCondition,
|
|
63
|
-
triggerPrice,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function getStopLimitOrderParams(
|
|
68
|
-
marketIndex: BN,
|
|
69
|
-
direction: PositionDirection,
|
|
70
|
-
baseAssetAmount: BN,
|
|
71
|
-
price: BN,
|
|
72
|
-
triggerPrice: BN,
|
|
73
|
-
triggerCondition: OrderTriggerCondition,
|
|
74
|
-
reduceOnly: boolean,
|
|
75
|
-
discountToken = false,
|
|
76
|
-
referrer = false
|
|
77
|
-
): OrderParams {
|
|
78
|
-
return {
|
|
79
|
-
orderType: OrderType.STOP_LIMIT,
|
|
80
|
-
marketIndex,
|
|
81
|
-
direction,
|
|
82
|
-
quoteAssetAmount: ZERO,
|
|
83
|
-
baseAssetAmount,
|
|
84
|
-
price,
|
|
85
|
-
reduceOnly,
|
|
86
|
-
postOnly: false,
|
|
87
|
-
immediateOrCancel: false,
|
|
88
|
-
optionalAccounts: {
|
|
89
|
-
discountToken,
|
|
90
|
-
referrer,
|
|
91
|
-
},
|
|
92
|
-
triggerCondition,
|
|
93
|
-
triggerPrice,
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function getMarketOrderParams(
|
|
98
|
-
marketIndex: BN,
|
|
99
|
-
direction: PositionDirection,
|
|
100
|
-
quoteAssetAmount: BN,
|
|
101
|
-
baseAssetAmount: BN,
|
|
102
|
-
reduceOnly: boolean,
|
|
103
|
-
price = ZERO,
|
|
104
|
-
discountToken = false,
|
|
105
|
-
referrer = false
|
|
106
|
-
): OrderParams {
|
|
107
|
-
if (baseAssetAmount.eq(ZERO) && quoteAssetAmount.eq(ZERO)) {
|
|
108
|
-
throw Error('baseAssetAmount or quoteAssetAmount must be zero');
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
orderType: OrderType.MARKET,
|
|
113
|
-
marketIndex,
|
|
114
|
-
direction,
|
|
115
|
-
quoteAssetAmount,
|
|
116
|
-
baseAssetAmount,
|
|
117
|
-
price,
|
|
118
|
-
reduceOnly,
|
|
119
|
-
postOnly: false,
|
|
120
|
-
immediateOrCancel: false,
|
|
121
|
-
optionalAccounts: {
|
|
122
|
-
discountToken,
|
|
123
|
-
referrer,
|
|
124
|
-
},
|
|
125
|
-
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
126
|
-
triggerPrice: ZERO,
|
|
127
|
-
};
|
|
128
|
-
}
|