@drift-labs/sdk 0.2.0-master.2 → 0.2.0-master.20
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/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -1
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +9 -5
- package/lib/admin.js +52 -11
- package/lib/clearingHouse.d.ts +52 -23
- package/lib/clearingHouse.js +727 -197
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +17 -17
- package/lib/clearingHouseUser.js +186 -101
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -2
- package/lib/constants/banks.js +12 -4
- package/lib/constants/numericConstants.d.ts +5 -0
- package/lib/constants/numericConstants.js +8 -3
- package/lib/events/eventList.js +3 -0
- package/lib/events/types.d.ts +3 -1
- package/lib/events/types.js +2 -0
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +1609 -388
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +9 -3
- package/lib/index.js +13 -3
- package/lib/math/amm.d.ts +1 -0
- package/lib/math/amm.js +22 -38
- package/lib/math/auction.js +4 -1
- package/lib/math/bankBalance.d.ts +7 -1
- package/lib/math/bankBalance.js +77 -2
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +72 -0
- package/lib/math/market.d.ts +4 -1
- package/lib/math/market.js +35 -1
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/orders.d.ts +5 -2
- package/lib/math/orders.js +53 -12
- package/lib/math/position.d.ts +8 -0
- package/lib/math/position.js +45 -12
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +7 -10
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.js +9 -2
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +233 -26
- package/lib/types.js +64 -1
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +3 -3
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +82 -15
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1224 -319
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +311 -148
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/banks.ts +14 -4
- package/src/constants/markets.js +42 -0
- package/src/constants/numericConstants.js +41 -0
- package/src/constants/numericConstants.ts +14 -2
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.ts +6 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +390 -0
- package/src/factory/bigNum.ts +42 -13
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +1609 -388
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +9 -3
- package/src/math/amm.ts +54 -55
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +5 -1
- package/src/math/bankBalance.ts +148 -2
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/margin.ts +124 -0
- package/src/math/market.ts +66 -1
- package/src/math/oracles.js +26 -0
- package/src/math/oracles.ts +42 -5
- package/src/math/orders.ts +112 -13
- package/src/math/position.ts +64 -9
- package/src/math/repeg.js +128 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +23 -25
- package/src/math/utils.js +0 -1
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/tokenFaucet.js +189 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +11 -3
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +236 -27
- package/src/userName.js +20 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +10 -0
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -142
- package/src/orders.ts +0 -251
- package/src/util/computeUnits.js.map +0 -1
|
@@ -0,0 +1,248 @@
|
|
|
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.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
|
|
13
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
15
|
+
const market_1 = require("./market");
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param market
|
|
19
|
+
* @param oraclePriceData
|
|
20
|
+
* @param periodAdjustment
|
|
21
|
+
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
22
|
+
*/
|
|
23
|
+
function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
// periodAdjustment
|
|
26
|
+
// 1: hourly
|
|
27
|
+
// 24: daily
|
|
28
|
+
// 24 * 365.25: annualized
|
|
29
|
+
const secondsInHour = new anchor_1.BN(3600);
|
|
30
|
+
const hoursInDay = new anchor_1.BN(24);
|
|
31
|
+
const ONE = new anchor_1.BN(1);
|
|
32
|
+
if (!market.initialized) {
|
|
33
|
+
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
34
|
+
}
|
|
35
|
+
const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
|
|
36
|
+
// todo: sufficiently differs from blockchain timestamp?
|
|
37
|
+
const now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
|
|
38
|
+
const timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
|
|
39
|
+
// calculate real-time mark twap
|
|
40
|
+
const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
|
|
41
|
+
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
42
|
+
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
43
|
+
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
|
|
44
|
+
const baseAssetPriceWithMantissa = market_1.calculateMarkPrice(market, oraclePriceData);
|
|
45
|
+
const markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
46
|
+
.mul(lastMarkTwapWithMantissa)
|
|
47
|
+
.add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
|
|
48
|
+
.div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
|
|
49
|
+
// calculate real-time (predicted) oracle twap
|
|
50
|
+
// note: oracle twap depends on `when the chord is struck` (market is trade)
|
|
51
|
+
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
52
|
+
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
53
|
+
const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
54
|
+
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
55
|
+
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
|
|
56
|
+
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
57
|
+
// if passing live oracle data, improve predicted calc estimate
|
|
58
|
+
if (oraclePriceData) {
|
|
59
|
+
const oraclePrice = oraclePriceData.price;
|
|
60
|
+
const oracleLiveVsTwap = oraclePrice
|
|
61
|
+
.sub(lastOracleTwapWithMantissa)
|
|
62
|
+
.abs()
|
|
63
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
64
|
+
.mul(new anchor_1.BN(100))
|
|
65
|
+
.div(lastOracleTwapWithMantissa);
|
|
66
|
+
// verify pyth live input is within 10% of last twap for live update
|
|
67
|
+
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
68
|
+
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
69
|
+
.mul(lastOracleTwapWithMantissa)
|
|
70
|
+
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
71
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
75
|
+
.mul(lastOracleTwapWithMantissa)
|
|
76
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
77
|
+
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
78
|
+
const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
|
|
79
|
+
const twapSpreadPct = twapSpread
|
|
80
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
81
|
+
.mul(new anchor_1.BN(100))
|
|
82
|
+
.div(shrunkLastOracleTwapwithMantissa);
|
|
83
|
+
const lowerboundEst = twapSpreadPct
|
|
84
|
+
.mul(payFreq)
|
|
85
|
+
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
|
86
|
+
.mul(periodAdjustment)
|
|
87
|
+
.div(secondsInHour)
|
|
88
|
+
.div(secondsInHour)
|
|
89
|
+
.div(hoursInDay);
|
|
90
|
+
const interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
|
|
91
|
+
const interpRateQuote = twapSpreadPct
|
|
92
|
+
.mul(periodAdjustment)
|
|
93
|
+
.div(hoursInDay)
|
|
94
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
|
|
95
|
+
let feePoolSize = calculateFundingPool(market);
|
|
96
|
+
if (interpRateQuote.lt(new anchor_1.BN(0))) {
|
|
97
|
+
feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
|
|
98
|
+
}
|
|
99
|
+
let cappedAltEst;
|
|
100
|
+
let largerSide;
|
|
101
|
+
let smallerSide;
|
|
102
|
+
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
103
|
+
largerSide = market.baseAssetAmountLong.abs();
|
|
104
|
+
smallerSide = market.baseAssetAmountShort.abs();
|
|
105
|
+
if (twapSpread.gt(new anchor_1.BN(0))) {
|
|
106
|
+
return [
|
|
107
|
+
markTwapWithMantissa,
|
|
108
|
+
oracleTwapWithMantissa,
|
|
109
|
+
lowerboundEst,
|
|
110
|
+
interpEst,
|
|
111
|
+
interpEst,
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
116
|
+
largerSide = market.baseAssetAmountShort.abs();
|
|
117
|
+
smallerSide = market.baseAssetAmountLong.abs();
|
|
118
|
+
if (twapSpread.lt(new anchor_1.BN(0))) {
|
|
119
|
+
return [
|
|
120
|
+
markTwapWithMantissa,
|
|
121
|
+
oracleTwapWithMantissa,
|
|
122
|
+
lowerboundEst,
|
|
123
|
+
interpEst,
|
|
124
|
+
interpEst,
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return [
|
|
130
|
+
markTwapWithMantissa,
|
|
131
|
+
oracleTwapWithMantissa,
|
|
132
|
+
lowerboundEst,
|
|
133
|
+
interpEst,
|
|
134
|
+
interpEst,
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
if (largerSide.gt(numericConstants_1.ZERO)) {
|
|
138
|
+
// funding smaller flow
|
|
139
|
+
cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
|
|
140
|
+
const feePoolTopOff = feePoolSize
|
|
141
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
|
|
142
|
+
.mul(numericConstants_1.AMM_RESERVE_PRECISION);
|
|
143
|
+
cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
|
|
144
|
+
cappedAltEst = cappedAltEst
|
|
145
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
146
|
+
.mul(new anchor_1.BN(100))
|
|
147
|
+
.div(oracleTwapWithMantissa)
|
|
148
|
+
.mul(periodAdjustment);
|
|
149
|
+
if (cappedAltEst.abs().gte(interpEst.abs())) {
|
|
150
|
+
cappedAltEst = interpEst;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
cappedAltEst = interpEst;
|
|
155
|
+
}
|
|
156
|
+
return [
|
|
157
|
+
markTwapWithMantissa,
|
|
158
|
+
oracleTwapWithMantissa,
|
|
159
|
+
lowerboundEst,
|
|
160
|
+
cappedAltEst,
|
|
161
|
+
interpEst,
|
|
162
|
+
];
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
* @param market
|
|
169
|
+
* @param oraclePriceData
|
|
170
|
+
* @param periodAdjustment
|
|
171
|
+
* @param estimationMethod
|
|
172
|
+
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
173
|
+
*/
|
|
174
|
+
function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1), estimationMethod) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
const [_1, _2, lowerboundEst, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
177
|
+
if (estimationMethod == 'lowerbound') {
|
|
178
|
+
//assuming remaining funding period has no gap
|
|
179
|
+
return lowerboundEst;
|
|
180
|
+
}
|
|
181
|
+
else if (estimationMethod == 'capped') {
|
|
182
|
+
return cappedAltEst;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
return interpEst;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
|
|
190
|
+
/**
|
|
191
|
+
*
|
|
192
|
+
* @param market
|
|
193
|
+
* @param oraclePriceData
|
|
194
|
+
* @param periodAdjustment
|
|
195
|
+
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
196
|
+
*/
|
|
197
|
+
function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
const [_1, _2, _, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
200
|
+
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
|
|
201
|
+
return [cappedAltEst, interpEst];
|
|
202
|
+
}
|
|
203
|
+
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
|
|
204
|
+
return [interpEst, cappedAltEst];
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
return [interpEst, interpEst];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
212
|
+
/**
|
|
213
|
+
*
|
|
214
|
+
* @param market
|
|
215
|
+
* @param oraclePriceData
|
|
216
|
+
* @param periodAdjustment
|
|
217
|
+
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
218
|
+
*/
|
|
219
|
+
function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
222
|
+
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
223
|
+
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
224
|
+
}
|
|
225
|
+
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
226
|
+
return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
return [markTwapLive, oracleTwapLive, interpEst, interpEst];
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRateAndLiveTwaps;
|
|
234
|
+
/**
|
|
235
|
+
*
|
|
236
|
+
* @param market
|
|
237
|
+
* @returns Estimated fee pool size
|
|
238
|
+
*/
|
|
239
|
+
function calculateFundingPool(market) {
|
|
240
|
+
// todo
|
|
241
|
+
const totalFeeLB = market.amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
242
|
+
const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
243
|
+
.sub(totalFeeLB)
|
|
244
|
+
.mul(new anchor_1.BN(1))
|
|
245
|
+
.div(new anchor_1.BN(3)));
|
|
246
|
+
return feePool;
|
|
247
|
+
}
|
|
248
|
+
exports.calculateFundingPool = calculateFundingPool;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { squareRootBN } from './utils';
|
|
2
|
+
import {
|
|
3
|
+
BANK_WEIGHT_PRECISION,
|
|
4
|
+
BANK_IMF_PRECISION,
|
|
5
|
+
ZERO,
|
|
6
|
+
BID_ASK_SPREAD_PRECISION,
|
|
7
|
+
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
8
|
+
MARK_PRICE_PRECISION,
|
|
9
|
+
} from '../constants/numericConstants';
|
|
10
|
+
import { BN } from '@project-serum/anchor';
|
|
11
|
+
import { OraclePriceData } from '../oracles/types';
|
|
12
|
+
import { MarketAccount, UserPosition } from '..';
|
|
13
|
+
|
|
14
|
+
export function calculateSizePremiumLiabilityWeight(
|
|
15
|
+
size: BN, // AMM_RESERVE_PRECISION
|
|
16
|
+
imfFactor: BN,
|
|
17
|
+
liabilityWeight: BN,
|
|
18
|
+
precision: BN
|
|
19
|
+
): BN {
|
|
20
|
+
if (imfFactor.eq(ZERO)) {
|
|
21
|
+
return liabilityWeight;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const sizeSqrt = squareRootBN(size.div(new BN(1000)).add(new BN(1))); //1e13 -> 1e10 -> 1e5
|
|
25
|
+
const liabilityWeightNumerator = liabilityWeight.sub(
|
|
26
|
+
liabilityWeight.div(BN.max(new BN(1), BANK_IMF_PRECISION.div(imfFactor)))
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const sizePremiumLiabilityWeight = liabilityWeightNumerator.add(
|
|
30
|
+
sizeSqrt // 1e5
|
|
31
|
+
.mul(imfFactor)
|
|
32
|
+
.div(new BN(100_000).mul(BANK_IMF_PRECISION).div(precision)) // 1e5
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const maxLiabilityWeight = BN.max(
|
|
36
|
+
liabilityWeight,
|
|
37
|
+
sizePremiumLiabilityWeight
|
|
38
|
+
);
|
|
39
|
+
return maxLiabilityWeight;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function calculateSizeDiscountAssetWeight(
|
|
43
|
+
size: BN, // AMM_RESERVE_PRECISION
|
|
44
|
+
imfFactor: BN,
|
|
45
|
+
assetWeight: BN
|
|
46
|
+
): BN {
|
|
47
|
+
if (imfFactor.eq(ZERO)) {
|
|
48
|
+
return assetWeight;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const sizeSqrt = squareRootBN(size.div(new BN(1000)).add(new BN(1))); //1e13 -> 1e10 -> 1e5
|
|
52
|
+
const imfNumerator = BANK_IMF_PRECISION.add(
|
|
53
|
+
BANK_IMF_PRECISION.div(new BN(10))
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const sizeDiscountAssetWeight = imfNumerator.mul(BANK_WEIGHT_PRECISION).div(
|
|
57
|
+
BANK_IMF_PRECISION.add(
|
|
58
|
+
sizeSqrt // 1e5
|
|
59
|
+
.mul(imfFactor)
|
|
60
|
+
.div(new BN(100_000)) // 1e5
|
|
61
|
+
)
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const minAssetWeight = BN.min(assetWeight, sizeDiscountAssetWeight);
|
|
65
|
+
|
|
66
|
+
return minAssetWeight;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function calculateOraclePriceForPerpMargin(
|
|
70
|
+
marketPosition: UserPosition,
|
|
71
|
+
market: MarketAccount,
|
|
72
|
+
oraclePriceData: OraclePriceData
|
|
73
|
+
): BN {
|
|
74
|
+
const oraclePriceOffset = BN.min(
|
|
75
|
+
new BN(market.amm.maxSpread)
|
|
76
|
+
.mul(oraclePriceData.price)
|
|
77
|
+
.div(BID_ASK_SPREAD_PRECISION),
|
|
78
|
+
oraclePriceData.confidence.add(
|
|
79
|
+
new BN(market.amm.baseSpread)
|
|
80
|
+
.mul(oraclePriceData.price)
|
|
81
|
+
.div(BID_ASK_SPREAD_PRECISION)
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
let marginPrice: BN;
|
|
86
|
+
if (marketPosition.baseAssetAmount.gt(ZERO)) {
|
|
87
|
+
marginPrice = oraclePriceData.price.sub(oraclePriceOffset);
|
|
88
|
+
} else {
|
|
89
|
+
marginPrice = oraclePriceData.price.add(oraclePriceOffset);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return marginPrice;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function calculateMarginBaseAssetValue(
|
|
96
|
+
market: MarketAccount,
|
|
97
|
+
marketPosition: UserPosition,
|
|
98
|
+
oraclePriceData: OraclePriceData
|
|
99
|
+
): BN {
|
|
100
|
+
const marginPrice = calculateOraclePriceForPerpMargin(
|
|
101
|
+
marketPosition,
|
|
102
|
+
market,
|
|
103
|
+
oraclePriceData
|
|
104
|
+
);
|
|
105
|
+
const baseAssetValue = marketPosition.baseAssetAmount
|
|
106
|
+
.abs()
|
|
107
|
+
.mul(marginPrice)
|
|
108
|
+
.div(AMM_TO_QUOTE_PRECISION_RATIO.mul(MARK_PRICE_PRECISION));
|
|
109
|
+
|
|
110
|
+
return baseAssetValue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function calculateWorstCaseBaseAssetAmount(
|
|
114
|
+
marketPosition: UserPosition
|
|
115
|
+
): BN {
|
|
116
|
+
const allBids = marketPosition.baseAssetAmount.add(marketPosition.openBids);
|
|
117
|
+
const allAsks = marketPosition.baseAssetAmount.add(marketPosition.openAsks);
|
|
118
|
+
|
|
119
|
+
if (allBids.abs().gt(allAsks.abs())) {
|
|
120
|
+
return allBids;
|
|
121
|
+
} else {
|
|
122
|
+
return allAsks;
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/math/market.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
MarketAccount,
|
|
4
|
+
PositionDirection,
|
|
5
|
+
MarginCategory,
|
|
6
|
+
BankAccount,
|
|
7
|
+
BankBalanceType,
|
|
8
|
+
} from '../types';
|
|
3
9
|
import {
|
|
4
10
|
calculateAmmReservesAfterSwap,
|
|
5
11
|
calculatePrice,
|
|
@@ -7,7 +13,13 @@ import {
|
|
|
7
13
|
getSwapDirection,
|
|
8
14
|
calculateUpdatedAMM,
|
|
9
15
|
} from './amm';
|
|
16
|
+
import {
|
|
17
|
+
calculateSizeDiscountAssetWeight,
|
|
18
|
+
calculateSizePremiumLiabilityWeight,
|
|
19
|
+
} from './margin';
|
|
10
20
|
import { OraclePriceData } from '../oracles/types';
|
|
21
|
+
import { MARGIN_PRECISION } from '../constants/numericConstants';
|
|
22
|
+
import { getTokenAmount } from './bankBalance';
|
|
11
23
|
|
|
12
24
|
/**
|
|
13
25
|
* Calculates market mark price
|
|
@@ -103,3 +115,56 @@ export function calculateOracleSpread(
|
|
|
103
115
|
): BN {
|
|
104
116
|
return price.sub(oraclePriceData.price);
|
|
105
117
|
}
|
|
118
|
+
|
|
119
|
+
export function calculateMarketMarginRatio(
|
|
120
|
+
market: MarketAccount,
|
|
121
|
+
size: BN,
|
|
122
|
+
marginCategory: MarginCategory
|
|
123
|
+
): number {
|
|
124
|
+
let marginRatio;
|
|
125
|
+
switch (marginCategory) {
|
|
126
|
+
case 'Initial':
|
|
127
|
+
marginRatio = calculateSizePremiumLiabilityWeight(
|
|
128
|
+
size,
|
|
129
|
+
market.imfFactor,
|
|
130
|
+
new BN(market.marginRatioInitial),
|
|
131
|
+
MARGIN_PRECISION
|
|
132
|
+
).toNumber();
|
|
133
|
+
break;
|
|
134
|
+
case 'Maintenance':
|
|
135
|
+
marginRatio = market.marginRatioMaintenance;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return marginRatio;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function calculateUnrealizedAssetWeight(
|
|
143
|
+
market: MarketAccount,
|
|
144
|
+
unrealizedPnl: BN,
|
|
145
|
+
marginCategory: MarginCategory
|
|
146
|
+
): BN {
|
|
147
|
+
let assetWeight: BN;
|
|
148
|
+
|
|
149
|
+
switch (marginCategory) {
|
|
150
|
+
case 'Initial':
|
|
151
|
+
assetWeight = calculateSizeDiscountAssetWeight(
|
|
152
|
+
unrealizedPnl,
|
|
153
|
+
market.unrealizedImfFactor,
|
|
154
|
+
new BN(market.unrealizedInitialAssetWeight)
|
|
155
|
+
);
|
|
156
|
+
break;
|
|
157
|
+
case 'Maintenance':
|
|
158
|
+
assetWeight = new BN(market.unrealizedMaintenanceAssetWeight);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return assetWeight;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function calculateMarketAvailablePNL(
|
|
166
|
+
market: MarketAccount,
|
|
167
|
+
bank: BankAccount
|
|
168
|
+
): BN {
|
|
169
|
+
return getTokenAmount(market.pnlPool.balance, bank, BankBalanceType.DEPOSIT);
|
|
170
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isOracleValid = void 0;
|
|
4
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
+
const index_1 = require("../index");
|
|
6
|
+
function isOracleValid(amm, oraclePriceData, oracleGuardRails, slot) {
|
|
7
|
+
const isOraclePriceNonPositive = oraclePriceData.price.lt(numericConstants_1.ZERO);
|
|
8
|
+
const isOraclePriceTooVolatile = oraclePriceData.price
|
|
9
|
+
.div(index_1.BN.max(numericConstants_1.ONE, amm.lastOraclePriceTwap))
|
|
10
|
+
.gt(oracleGuardRails.validity.tooVolatileRatio) ||
|
|
11
|
+
amm.lastOraclePriceTwap
|
|
12
|
+
.div(index_1.BN.max(numericConstants_1.ONE, oraclePriceData.price))
|
|
13
|
+
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
14
|
+
const isConfidenceTooLarge = oraclePriceData.price
|
|
15
|
+
.div(index_1.BN.max(numericConstants_1.ONE, oraclePriceData.confidence))
|
|
16
|
+
.lt(oracleGuardRails.validity.confidenceIntervalMaxSize);
|
|
17
|
+
const oracleIsStale = oraclePriceData.slot
|
|
18
|
+
.sub(new index_1.BN(slot))
|
|
19
|
+
.gt(oracleGuardRails.validity.slotsBeforeStale);
|
|
20
|
+
return !(!oraclePriceData.hasSufficientNumberOfDataPoints ||
|
|
21
|
+
oracleIsStale ||
|
|
22
|
+
isOraclePriceNonPositive ||
|
|
23
|
+
isOraclePriceTooVolatile ||
|
|
24
|
+
isConfidenceTooLarge);
|
|
25
|
+
}
|
|
26
|
+
exports.isOracleValid = isOracleValid;
|
package/src/math/oracles.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { AMM, OracleGuardRails } from '../types';
|
|
2
2
|
import { OraclePriceData } from '../oracles/types';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
BID_ASK_SPREAD_PRECISION,
|
|
5
|
+
MARK_PRICE_PRECISION,
|
|
6
|
+
ONE,
|
|
7
|
+
ZERO,
|
|
8
|
+
} from '../constants/numericConstants';
|
|
4
9
|
import { BN } from '../index';
|
|
5
10
|
|
|
6
11
|
export function isOracleValid(
|
|
@@ -9,7 +14,7 @@ export function isOracleValid(
|
|
|
9
14
|
oracleGuardRails: OracleGuardRails,
|
|
10
15
|
slot: number
|
|
11
16
|
): boolean {
|
|
12
|
-
const isOraclePriceNonPositive = oraclePriceData.price.
|
|
17
|
+
const isOraclePriceNonPositive = oraclePriceData.price.lte(ZERO);
|
|
13
18
|
const isOraclePriceTooVolatile =
|
|
14
19
|
oraclePriceData.price
|
|
15
20
|
.div(BN.max(ONE, amm.lastOraclePriceTwap))
|
|
@@ -18,9 +23,11 @@ export function isOracleValid(
|
|
|
18
23
|
.div(BN.max(ONE, oraclePriceData.price))
|
|
19
24
|
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
20
25
|
|
|
21
|
-
const isConfidenceTooLarge =
|
|
22
|
-
.
|
|
23
|
-
.
|
|
26
|
+
const isConfidenceTooLarge = new BN(amm.baseSpread)
|
|
27
|
+
.add(BN.max(ONE, oraclePriceData.confidence))
|
|
28
|
+
.mul(BID_ASK_SPREAD_PRECISION)
|
|
29
|
+
.div(oraclePriceData.price)
|
|
30
|
+
.gt(new BN(amm.maxSpread));
|
|
24
31
|
|
|
25
32
|
const oracleIsStale = oraclePriceData.slot
|
|
26
33
|
.sub(new BN(slot))
|
|
@@ -34,3 +41,33 @@ export function isOracleValid(
|
|
|
34
41
|
isConfidenceTooLarge
|
|
35
42
|
);
|
|
36
43
|
}
|
|
44
|
+
|
|
45
|
+
export function isOracleTooDivergent(
|
|
46
|
+
amm: AMM,
|
|
47
|
+
oraclePriceData: OraclePriceData,
|
|
48
|
+
oracleGuardRails: OracleGuardRails,
|
|
49
|
+
now: BN
|
|
50
|
+
): boolean {
|
|
51
|
+
const sinceLastUpdate = now.sub(amm.lastOraclePriceTwapTs);
|
|
52
|
+
const sinceStart = BN.max(ZERO, new BN(60 * 5).sub(sinceLastUpdate));
|
|
53
|
+
const oracleTwap5min = amm.lastOraclePriceTwap5min
|
|
54
|
+
.mul(sinceStart)
|
|
55
|
+
.add(oraclePriceData.price)
|
|
56
|
+
.mul(sinceLastUpdate)
|
|
57
|
+
.div(sinceStart.add(sinceLastUpdate));
|
|
58
|
+
|
|
59
|
+
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
60
|
+
const oracleSpreadPct = oracleSpread
|
|
61
|
+
.mul(MARK_PRICE_PRECISION)
|
|
62
|
+
.div(oracleTwap5min);
|
|
63
|
+
|
|
64
|
+
const tooDivergent = oracleSpreadPct
|
|
65
|
+
.abs()
|
|
66
|
+
.gte(
|
|
67
|
+
BID_ASK_SPREAD_PRECISION.mul(
|
|
68
|
+
oracleGuardRails.priceDivergence.markOracleDivergenceNumerator
|
|
69
|
+
).div(oracleGuardRails.priceDivergence.markOracleDivergenceDenominator)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return tooDivergent;
|
|
73
|
+
}
|
package/src/math/orders.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isOneOfVariant,
|
|
4
|
+
isVariant,
|
|
5
|
+
MarketAccount,
|
|
6
|
+
Order,
|
|
7
|
+
PositionDirection,
|
|
8
|
+
} from '../types';
|
|
3
9
|
import { ZERO, TWO } from '../constants/numericConstants';
|
|
4
10
|
import { BN } from '@project-serum/anchor';
|
|
5
11
|
import { OraclePriceData } from '../oracles/types';
|
|
6
|
-
import { getAuctionPrice } from './auction';
|
|
12
|
+
import { getAuctionPrice, isAuctionComplete } from './auction';
|
|
13
|
+
import { calculateAskPrice, calculateBidPrice } from './market';
|
|
14
|
+
import {
|
|
15
|
+
calculateMaxBaseAssetAmountFillable,
|
|
16
|
+
calculateMaxBaseAssetAmountToTrade,
|
|
17
|
+
} from './amm';
|
|
7
18
|
|
|
8
19
|
export function isOrderRiskIncreasing(
|
|
9
20
|
user: ClearingHouseUser,
|
|
@@ -120,27 +131,115 @@ export function standardizeBaseAssetAmount(
|
|
|
120
131
|
|
|
121
132
|
export function getLimitPrice(
|
|
122
133
|
order: Order,
|
|
134
|
+
market: MarketAccount,
|
|
123
135
|
oraclePriceData: OraclePriceData,
|
|
124
136
|
slot: number
|
|
125
137
|
): BN {
|
|
126
138
|
let limitPrice;
|
|
127
139
|
if (!order.oraclePriceOffset.eq(ZERO)) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
140
|
+
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
141
|
+
} else if (isOneOfVariant(order.orderType, ['market', 'triggerMarket'])) {
|
|
142
|
+
if (isAuctionComplete(order, slot)) {
|
|
143
|
+
limitPrice = getAuctionPrice(order, slot);
|
|
144
|
+
} else if (!order.price.eq(ZERO)) {
|
|
145
|
+
limitPrice = order.price;
|
|
146
|
+
} else if (isVariant(order.direction, 'long')) {
|
|
147
|
+
const askPrice = calculateAskPrice(market, oraclePriceData);
|
|
148
|
+
const delta = askPrice.div(new BN(market.amm.maxSlippageRatio));
|
|
149
|
+
limitPrice = askPrice.add(delta);
|
|
133
150
|
} else {
|
|
134
|
-
|
|
151
|
+
const bidPrice = calculateBidPrice(market, oraclePriceData);
|
|
152
|
+
const delta = bidPrice.div(new BN(market.amm.maxSlippageRatio));
|
|
153
|
+
limitPrice = bidPrice.sub(delta);
|
|
135
154
|
}
|
|
136
|
-
} else if (
|
|
137
|
-
isVariant(order.orderType, 'market') ||
|
|
138
|
-
isVariant(order.orderType, 'triggerMarket')
|
|
139
|
-
) {
|
|
140
|
-
limitPrice = getAuctionPrice(order, slot);
|
|
141
155
|
} else {
|
|
142
156
|
limitPrice = order.price;
|
|
143
157
|
}
|
|
144
158
|
|
|
145
159
|
return limitPrice;
|
|
146
160
|
}
|
|
161
|
+
|
|
162
|
+
export function isFillableByVAMM(
|
|
163
|
+
order: Order,
|
|
164
|
+
market: MarketAccount,
|
|
165
|
+
oraclePriceData: OraclePriceData,
|
|
166
|
+
slot: number
|
|
167
|
+
): boolean {
|
|
168
|
+
return (
|
|
169
|
+
isAuctionComplete(order, slot) &&
|
|
170
|
+
!calculateBaseAssetAmountForAmmToFulfill(
|
|
171
|
+
order,
|
|
172
|
+
market,
|
|
173
|
+
oraclePriceData,
|
|
174
|
+
slot
|
|
175
|
+
).eq(ZERO)
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function calculateBaseAssetAmountForAmmToFulfill(
|
|
180
|
+
order: Order,
|
|
181
|
+
market: MarketAccount,
|
|
182
|
+
oraclePriceData: OraclePriceData,
|
|
183
|
+
slot: number
|
|
184
|
+
): BN {
|
|
185
|
+
if (
|
|
186
|
+
isOneOfVariant(order.orderType, ['triggerMarket', 'triggerLimit']) &&
|
|
187
|
+
order.triggered === false
|
|
188
|
+
) {
|
|
189
|
+
return ZERO;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const limitPrice = getLimitPrice(order, market, oraclePriceData, slot);
|
|
193
|
+
const baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(
|
|
194
|
+
order,
|
|
195
|
+
market,
|
|
196
|
+
limitPrice,
|
|
197
|
+
oraclePriceData
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const maxBaseAssetAmount = calculateMaxBaseAssetAmountFillable(
|
|
201
|
+
market.amm,
|
|
202
|
+
order.direction
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
return BN.min(maxBaseAssetAmount, baseAssetAmount);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function calculateBaseAssetAmountToFillUpToLimitPrice(
|
|
209
|
+
order: Order,
|
|
210
|
+
market: MarketAccount,
|
|
211
|
+
limitPrice: BN,
|
|
212
|
+
oraclePriceData: OraclePriceData
|
|
213
|
+
): BN {
|
|
214
|
+
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
215
|
+
market.amm,
|
|
216
|
+
limitPrice,
|
|
217
|
+
order.direction,
|
|
218
|
+
oraclePriceData
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const baseAssetAmount = standardizeBaseAssetAmount(
|
|
222
|
+
maxAmountToTrade,
|
|
223
|
+
market.amm.baseAssetAmountStepSize
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
// Check that directions are the same
|
|
227
|
+
const sameDirection = isSameDirection(direction, order.direction);
|
|
228
|
+
if (!sameDirection) {
|
|
229
|
+
return ZERO;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
233
|
+
? order.baseAssetAmount
|
|
234
|
+
: baseAssetAmount;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isSameDirection(
|
|
238
|
+
firstDirection: PositionDirection,
|
|
239
|
+
secondDirection: PositionDirection
|
|
240
|
+
): boolean {
|
|
241
|
+
return (
|
|
242
|
+
(isVariant(firstDirection, 'long') && isVariant(secondDirection, 'long')) ||
|
|
243
|
+
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
244
|
+
);
|
|
245
|
+
}
|