@drift-labs/sdk 0.2.0-master.26 → 0.2.0-master.28
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/pollingClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
- package/lib/accounts/types.d.ts +2 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
- package/lib/admin.d.ts +5 -2
- package/lib/admin.js +30 -4
- package/lib/clearingHouse.d.ts +3 -0
- package/lib/clearingHouse.js +19 -1
- package/lib/clearingHouseUser.d.ts +11 -9
- package/lib/clearingHouseUser.js +184 -108
- package/lib/config.js +1 -1
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +557 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/idl/clearing_house.json +123 -203
- package/lib/index.d.ts +5 -0
- package/lib/index.js +5 -0
- package/lib/math/market.d.ts +3 -2
- package/lib/math/market.js +17 -12
- package/lib/math/orders.d.ts +3 -3
- package/lib/math/orders.js +31 -16
- package/lib/math/position.d.ts +3 -3
- package/lib/math/position.js +23 -16
- package/lib/math/repeg.js +8 -0
- package/lib/math/spotBalance.d.ts +3 -0
- package/lib/math/spotBalance.js +18 -1
- package/lib/math/spotPosition.d.ts +5 -1
- package/lib/math/spotPosition.js +16 -1
- package/lib/types.d.ts +25 -24
- package/lib/types.js +9 -3
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/package.json +6 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
- package/src/accounts/types.ts +1 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
- package/src/admin.ts +50 -8
- package/src/clearingHouse.ts +30 -1
- package/src/clearingHouseUser.ts +366 -167
- package/src/config.ts +1 -1
- package/src/dlob/DLOB.ts +884 -0
- package/src/dlob/DLOBNode.ts +163 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/idl/clearing_house.json +123 -203
- package/src/index.ts +5 -0
- package/src/math/market.ts +22 -13
- package/src/math/orders.ts +29 -21
- package/src/math/position.ts +36 -22
- package/src/math/repeg.ts +9 -0
- package/src/math/spotBalance.ts +26 -0
- package/src/math/spotPosition.ts +42 -1
- package/src/types.ts +26 -24
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/tests/bn/test.ts +2 -3
- package/tests/dlob/helpers.ts +322 -0
- package/tests/dlob/test.ts +2865 -0
- package/my-script/.env +0 -7
- package/my-script/getUserStats.ts +0 -106
- package/my-script/multiConnections.ts +0 -119
- package/my-script/test-regex.ts +0 -11
- package/my-script/utils.ts +0 -52
- package/src/accounts/bulkAccountLoader.js +0 -249
- package/src/accounts/bulkUserStatsSubscription.js +0 -75
- package/src/accounts/bulkUserSubscription.js +0 -75
- package/src/accounts/fetch.js +0 -92
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
- package/src/accounts/pollingOracleSubscriber.js +0 -156
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
- package/src/accounts/pollingUserAccountSubscriber.js +0 -208
- package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
- package/src/accounts/types.js +0 -28
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -138
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
- package/src/addresses/pda.js +0 -186
- package/src/admin.js +0 -1284
- package/src/assert/assert.js +0 -9
- package/src/clearingHouse.js +0 -3433
- package/src/clearingHouseConfig.js +0 -2
- package/src/clearingHouseUser.js +0 -874
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/clearingHouseUserStats.js +0 -115
- package/src/clearingHouseUserStatsConfig.js +0 -2
- package/src/config.js +0 -80
- package/src/constants/numericConstants.js +0 -48
- package/src/constants/perpMarkets.js +0 -42
- package/src/constants/spotMarkets.js +0 -51
- package/src/events/eventList.js +0 -120
- package/src/events/eventSubscriber.js +0 -202
- package/src/events/fetchLogs.js +0 -117
- package/src/events/pollingLogProvider.js +0 -113
- package/src/events/sort.js +0 -41
- package/src/events/txEventCache.js +0 -74
- package/src/events/types.js +0 -25
- package/src/events/webSocketLogProvider.js +0 -76
- package/src/factory/bigNum.js +0 -393
- package/src/factory/oracleClient.js +0 -20
- package/src/index.js +0 -75
- package/src/math/amm.js +0 -422
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -12
- package/src/math/funding.js +0 -296
- package/src/math/insurance.js +0 -27
- package/src/math/margin.js +0 -77
- package/src/math/market.js +0 -105
- package/src/math/oracles.js +0 -56
- package/src/math/orders.js +0 -153
- package/src/math/position.js +0 -172
- package/src/math/repeg.js +0 -128
- package/src/math/spotBalance.js +0 -176
- package/src/math/spotMarket.js +0 -8
- package/src/math/spotPosition.js +0 -8
- package/src/math/trade.js +0 -260
- package/src/math/utils.js +0 -27
- package/src/oracles/oracleClientCache.js +0 -20
- package/src/oracles/pythClient.js +0 -81
- package/src/oracles/quoteAssetOracleClient.js +0 -63
- package/src/oracles/switchboardClient.js +0 -101
- package/src/oracles/types.js +0 -2
- package/src/orderParams.js +0 -28
- package/src/serum/serumSubscriber.js +0 -102
- package/src/serum/types.js +0 -2
- package/src/slot/SlotSubscriber.js +0 -86
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -323
- package/src/tx/retryTxSender.js +0 -280
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -18
- package/src/types.js +0 -216
- package/src/userName.js +0 -20
- package/src/util/computeUnits.js +0 -62
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -61
- package/src/wallet.js +0 -72
package/src/math/funding.js
DELETED
|
@@ -1,296 +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
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
exports.__esModule = true;
|
|
39
|
-
exports.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
|
|
40
|
-
var anchor_1 = require("@project-serum/anchor");
|
|
41
|
-
var numericConstants_1 = require("../constants/numericConstants");
|
|
42
|
-
var types_1 = require("../types");
|
|
43
|
-
var market_1 = require("./market");
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
* @param market
|
|
47
|
-
* @param oraclePriceData
|
|
48
|
-
* @param periodAdjustment
|
|
49
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
50
|
-
*/
|
|
51
|
-
function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment) {
|
|
52
|
-
if (periodAdjustment === void 0) { periodAdjustment = new anchor_1.BN(1); }
|
|
53
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
54
|
-
var secondsInHour, hoursInDay, ONE, payFreq, now, timeSinceLastUpdate, lastMarkTwapWithMantissa, lastMarkPriceTwapTs, timeSinceLastMarkChange, markTwapTimeSinceLastUpdate, baseAssetPriceWithMantissa, markTwapWithMantissa, lastOracleTwapWithMantissa, lastOraclePriceTwapTs, oracleInvalidDuration, timeSinceLastOracleTwapUpdate, oracleTwapTimeSinceLastUpdate, oracleTwapWithMantissa, oraclePrice, oracleLiveVsTwap, shrunkLastOracleTwapwithMantissa, twapSpread, twapSpreadPct, lowerboundEst, interpEst, interpRateQuote, feePoolSize, cappedAltEst, largerSide, smallerSide, feePoolTopOff;
|
|
55
|
-
return __generator(this, function (_a) {
|
|
56
|
-
secondsInHour = new anchor_1.BN(3600);
|
|
57
|
-
hoursInDay = new anchor_1.BN(24);
|
|
58
|
-
ONE = new anchor_1.BN(1);
|
|
59
|
-
if ((0, types_1.isVariant)(market.status, 'uninitialized')) {
|
|
60
|
-
return [2 /*return*/, [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO]];
|
|
61
|
-
}
|
|
62
|
-
payFreq = new anchor_1.BN(market.amm.fundingPeriod);
|
|
63
|
-
now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
|
|
64
|
-
timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
|
|
65
|
-
lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
|
|
66
|
-
lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
67
|
-
timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
68
|
-
markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
|
|
69
|
-
baseAssetPriceWithMantissa = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
70
|
-
markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
71
|
-
.mul(lastMarkTwapWithMantissa)
|
|
72
|
-
.add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
|
|
73
|
-
.div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
|
|
74
|
-
lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
75
|
-
lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
76
|
-
oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
77
|
-
timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
78
|
-
oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
|
|
79
|
-
oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
80
|
-
// if passing live oracle data, improve predicted calc estimate
|
|
81
|
-
if (oraclePriceData) {
|
|
82
|
-
oraclePrice = oraclePriceData.price;
|
|
83
|
-
oracleLiveVsTwap = oraclePrice
|
|
84
|
-
.sub(lastOracleTwapWithMantissa)
|
|
85
|
-
.abs()
|
|
86
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
87
|
-
.mul(new anchor_1.BN(100))
|
|
88
|
-
.div(lastOracleTwapWithMantissa);
|
|
89
|
-
// verify pyth live input is within 10% of last twap for live update
|
|
90
|
-
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
91
|
-
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
92
|
-
.mul(lastOracleTwapWithMantissa)
|
|
93
|
-
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
94
|
-
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
98
|
-
.mul(lastOracleTwapWithMantissa)
|
|
99
|
-
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
100
|
-
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
101
|
-
twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
|
|
102
|
-
twapSpreadPct = twapSpread
|
|
103
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
104
|
-
.mul(new anchor_1.BN(100))
|
|
105
|
-
.div(shrunkLastOracleTwapwithMantissa);
|
|
106
|
-
lowerboundEst = twapSpreadPct
|
|
107
|
-
.mul(payFreq)
|
|
108
|
-
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
|
109
|
-
.mul(periodAdjustment)
|
|
110
|
-
.div(secondsInHour)
|
|
111
|
-
.div(secondsInHour)
|
|
112
|
-
.div(hoursInDay);
|
|
113
|
-
interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
|
|
114
|
-
interpRateQuote = twapSpreadPct
|
|
115
|
-
.mul(periodAdjustment)
|
|
116
|
-
.div(hoursInDay)
|
|
117
|
-
.div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
|
|
118
|
-
feePoolSize = calculateFundingPool(market);
|
|
119
|
-
if (interpRateQuote.lt(new anchor_1.BN(0))) {
|
|
120
|
-
feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
|
|
121
|
-
}
|
|
122
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
123
|
-
largerSide = market.baseAssetAmountLong.abs();
|
|
124
|
-
smallerSide = market.baseAssetAmountShort.abs();
|
|
125
|
-
if (twapSpread.gt(new anchor_1.BN(0))) {
|
|
126
|
-
return [2 /*return*/, [
|
|
127
|
-
markTwapWithMantissa,
|
|
128
|
-
oracleTwapWithMantissa,
|
|
129
|
-
lowerboundEst,
|
|
130
|
-
interpEst,
|
|
131
|
-
interpEst,
|
|
132
|
-
]];
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
136
|
-
largerSide = market.baseAssetAmountShort.abs();
|
|
137
|
-
smallerSide = market.baseAssetAmountLong.abs();
|
|
138
|
-
if (twapSpread.lt(new anchor_1.BN(0))) {
|
|
139
|
-
return [2 /*return*/, [
|
|
140
|
-
markTwapWithMantissa,
|
|
141
|
-
oracleTwapWithMantissa,
|
|
142
|
-
lowerboundEst,
|
|
143
|
-
interpEst,
|
|
144
|
-
interpEst,
|
|
145
|
-
]];
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
return [2 /*return*/, [
|
|
150
|
-
markTwapWithMantissa,
|
|
151
|
-
oracleTwapWithMantissa,
|
|
152
|
-
lowerboundEst,
|
|
153
|
-
interpEst,
|
|
154
|
-
interpEst,
|
|
155
|
-
]];
|
|
156
|
-
}
|
|
157
|
-
if (largerSide.gt(numericConstants_1.ZERO)) {
|
|
158
|
-
// funding smaller flow
|
|
159
|
-
cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
|
|
160
|
-
feePoolTopOff = feePoolSize
|
|
161
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
|
|
162
|
-
.mul(numericConstants_1.AMM_RESERVE_PRECISION);
|
|
163
|
-
cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
|
|
164
|
-
cappedAltEst = cappedAltEst
|
|
165
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
166
|
-
.mul(new anchor_1.BN(100))
|
|
167
|
-
.div(oracleTwapWithMantissa)
|
|
168
|
-
.mul(periodAdjustment);
|
|
169
|
-
if (cappedAltEst.abs().gte(interpEst.abs())) {
|
|
170
|
-
cappedAltEst = interpEst;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
cappedAltEst = interpEst;
|
|
175
|
-
}
|
|
176
|
-
return [2 /*return*/, [
|
|
177
|
-
markTwapWithMantissa,
|
|
178
|
-
oracleTwapWithMantissa,
|
|
179
|
-
lowerboundEst,
|
|
180
|
-
cappedAltEst,
|
|
181
|
-
interpEst,
|
|
182
|
-
]];
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
|
|
187
|
-
/**
|
|
188
|
-
*
|
|
189
|
-
* @param market
|
|
190
|
-
* @param oraclePriceData
|
|
191
|
-
* @param periodAdjustment
|
|
192
|
-
* @param estimationMethod
|
|
193
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
194
|
-
*/
|
|
195
|
-
function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment, estimationMethod) {
|
|
196
|
-
if (periodAdjustment === void 0) { periodAdjustment = new anchor_1.BN(1); }
|
|
197
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
198
|
-
var _a, _1, _2, lowerboundEst, cappedAltEst, interpEst;
|
|
199
|
-
return __generator(this, function (_b) {
|
|
200
|
-
switch (_b.label) {
|
|
201
|
-
case 0: return [4 /*yield*/, calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment)];
|
|
202
|
-
case 1:
|
|
203
|
-
_a = _b.sent(), _1 = _a[0], _2 = _a[1], lowerboundEst = _a[2], cappedAltEst = _a[3], interpEst = _a[4];
|
|
204
|
-
if (estimationMethod == 'lowerbound') {
|
|
205
|
-
//assuming remaining funding period has no gap
|
|
206
|
-
return [2 /*return*/, lowerboundEst];
|
|
207
|
-
}
|
|
208
|
-
else if (estimationMethod == 'capped') {
|
|
209
|
-
return [2 /*return*/, cappedAltEst];
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
return [2 /*return*/, interpEst];
|
|
213
|
-
}
|
|
214
|
-
return [2 /*return*/];
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
|
|
220
|
-
/**
|
|
221
|
-
*
|
|
222
|
-
* @param market
|
|
223
|
-
* @param oraclePriceData
|
|
224
|
-
* @param periodAdjustment
|
|
225
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
226
|
-
*/
|
|
227
|
-
function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment) {
|
|
228
|
-
if (periodAdjustment === void 0) { periodAdjustment = new anchor_1.BN(1); }
|
|
229
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
230
|
-
var _a, _1, _2, _, cappedAltEst, interpEst;
|
|
231
|
-
return __generator(this, function (_b) {
|
|
232
|
-
switch (_b.label) {
|
|
233
|
-
case 0: return [4 /*yield*/, calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment)];
|
|
234
|
-
case 1:
|
|
235
|
-
_a = _b.sent(), _1 = _a[0], _2 = _a[1], _ = _a[2], cappedAltEst = _a[3], interpEst = _a[4];
|
|
236
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
|
|
237
|
-
return [2 /*return*/, [cappedAltEst, interpEst]];
|
|
238
|
-
}
|
|
239
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
|
|
240
|
-
return [2 /*return*/, [interpEst, cappedAltEst]];
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
243
|
-
return [2 /*return*/, [interpEst, interpEst]];
|
|
244
|
-
}
|
|
245
|
-
return [2 /*return*/];
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
251
|
-
/**
|
|
252
|
-
*
|
|
253
|
-
* @param market
|
|
254
|
-
* @param oraclePriceData
|
|
255
|
-
* @param periodAdjustment
|
|
256
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
257
|
-
*/
|
|
258
|
-
function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment) {
|
|
259
|
-
if (periodAdjustment === void 0) { periodAdjustment = new anchor_1.BN(1); }
|
|
260
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
261
|
-
var _a, markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst;
|
|
262
|
-
return __generator(this, function (_b) {
|
|
263
|
-
switch (_b.label) {
|
|
264
|
-
case 0: return [4 /*yield*/, calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment)];
|
|
265
|
-
case 1:
|
|
266
|
-
_a = _b.sent(), markTwapLive = _a[0], oracleTwapLive = _a[1], _2 = _a[2], cappedAltEst = _a[3], interpEst = _a[4];
|
|
267
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
268
|
-
return [2 /*return*/, [markTwapLive, oracleTwapLive, cappedAltEst, interpEst]];
|
|
269
|
-
}
|
|
270
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
271
|
-
return [2 /*return*/, [markTwapLive, oracleTwapLive, interpEst, cappedAltEst]];
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
return [2 /*return*/, [markTwapLive, oracleTwapLive, interpEst, interpEst]];
|
|
275
|
-
}
|
|
276
|
-
return [2 /*return*/];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRateAndLiveTwaps;
|
|
282
|
-
/**
|
|
283
|
-
*
|
|
284
|
-
* @param market
|
|
285
|
-
* @returns Estimated fee pool size
|
|
286
|
-
*/
|
|
287
|
-
function calculateFundingPool(market) {
|
|
288
|
-
// todo
|
|
289
|
-
var totalFeeLB = market.amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
290
|
-
var feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
291
|
-
.sub(totalFeeLB)
|
|
292
|
-
.mul(new anchor_1.BN(1))
|
|
293
|
-
.div(new anchor_1.BN(3)));
|
|
294
|
-
return feePool;
|
|
295
|
-
}
|
|
296
|
-
exports.calculateFundingPool = calculateFundingPool;
|
package/src/math/insurance.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
exports.__esModule = true;
|
|
3
|
-
exports.unstakeSharesToAmount = exports.stakeAmountToShares = void 0;
|
|
4
|
-
var numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
var index_1 = require("../index");
|
|
6
|
-
function stakeAmountToShares(amount, totalIfShares, insuranceFundVaultBalance) {
|
|
7
|
-
var nShares;
|
|
8
|
-
if (insuranceFundVaultBalance.gt(numericConstants_1.ZERO)) {
|
|
9
|
-
nShares = amount.mul(totalIfShares).div(insuranceFundVaultBalance);
|
|
10
|
-
}
|
|
11
|
-
else {
|
|
12
|
-
nShares = amount;
|
|
13
|
-
}
|
|
14
|
-
return nShares;
|
|
15
|
-
}
|
|
16
|
-
exports.stakeAmountToShares = stakeAmountToShares;
|
|
17
|
-
function unstakeSharesToAmount(nShares, totalIfShares, insuranceFundVaultBalance) {
|
|
18
|
-
var amount;
|
|
19
|
-
if (totalIfShares.gt(numericConstants_1.ZERO)) {
|
|
20
|
-
amount = index_1.BN.max(numericConstants_1.ZERO, nShares.mul(insuranceFundVaultBalance).div(totalIfShares).sub(numericConstants_1.ONE));
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
amount = numericConstants_1.ZERO;
|
|
24
|
-
}
|
|
25
|
-
return amount;
|
|
26
|
-
}
|
|
27
|
-
exports.unstakeSharesToAmount = unstakeSharesToAmount;
|
package/src/math/margin.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
exports.__esModule = true;
|
|
3
|
-
exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
4
|
-
var utils_1 = require("./utils");
|
|
5
|
-
var numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
var anchor_1 = require("@project-serum/anchor");
|
|
7
|
-
var assert_1 = require("../assert/assert");
|
|
8
|
-
function calculateSizePremiumLiabilityWeight(size, // AMM_RESERVE_PRECISION
|
|
9
|
-
imfFactor, liabilityWeight, precision) {
|
|
10
|
-
if (imfFactor.eq(numericConstants_1.ZERO)) {
|
|
11
|
-
return liabilityWeight;
|
|
12
|
-
}
|
|
13
|
-
var sizeSqrt = (0, utils_1.squareRootBN)(size.div(new anchor_1.BN(1000)).add(new anchor_1.BN(1))); //1e13 -> 1e10 -> 1e5
|
|
14
|
-
var denom0 = anchor_1.BN.max(new anchor_1.BN(1), numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(imfFactor));
|
|
15
|
-
(0, assert_1.assert)(denom0.gt(numericConstants_1.ZERO));
|
|
16
|
-
var liabilityWeightNumerator = liabilityWeight.sub(liabilityWeight.div(anchor_1.BN.max(new anchor_1.BN(1), numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(imfFactor))));
|
|
17
|
-
var denom = new anchor_1.BN(100000).mul(numericConstants_1.SPOT_MARKET_IMF_PRECISION).div(precision);
|
|
18
|
-
(0, assert_1.assert)(denom.gt(numericConstants_1.ZERO));
|
|
19
|
-
var sizePremiumLiabilityWeight = liabilityWeightNumerator.add(sizeSqrt // 1e5
|
|
20
|
-
.mul(imfFactor)
|
|
21
|
-
.div(denom) // 1e5
|
|
22
|
-
);
|
|
23
|
-
var maxLiabilityWeight = anchor_1.BN.max(liabilityWeight, sizePremiumLiabilityWeight);
|
|
24
|
-
return maxLiabilityWeight;
|
|
25
|
-
}
|
|
26
|
-
exports.calculateSizePremiumLiabilityWeight = calculateSizePremiumLiabilityWeight;
|
|
27
|
-
function calculateSizeDiscountAssetWeight(size, // AMM_RESERVE_PRECISION
|
|
28
|
-
imfFactor, assetWeight) {
|
|
29
|
-
if (imfFactor.eq(numericConstants_1.ZERO)) {
|
|
30
|
-
return assetWeight;
|
|
31
|
-
}
|
|
32
|
-
var sizeSqrt = (0, utils_1.squareRootBN)(size.div(new anchor_1.BN(1000)).add(new anchor_1.BN(1))); //1e13 -> 1e10 -> 1e5
|
|
33
|
-
var imfNumerator = numericConstants_1.SPOT_MARKET_IMF_PRECISION.add(numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(new anchor_1.BN(10)));
|
|
34
|
-
var sizeDiscountAssetWeight = imfNumerator
|
|
35
|
-
.mul(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
|
36
|
-
.div(numericConstants_1.SPOT_MARKET_IMF_PRECISION.add(sizeSqrt // 1e5
|
|
37
|
-
.mul(imfFactor)
|
|
38
|
-
.div(new anchor_1.BN(100000)) // 1e5
|
|
39
|
-
));
|
|
40
|
-
var minAssetWeight = anchor_1.BN.min(assetWeight, sizeDiscountAssetWeight);
|
|
41
|
-
return minAssetWeight;
|
|
42
|
-
}
|
|
43
|
-
exports.calculateSizeDiscountAssetWeight = calculateSizeDiscountAssetWeight;
|
|
44
|
-
function calculateOraclePriceForPerpMargin(perpPosition, market, oraclePriceData) {
|
|
45
|
-
var oraclePriceOffset = anchor_1.BN.min(new anchor_1.BN(market.amm.maxSpread)
|
|
46
|
-
.mul(oraclePriceData.price)
|
|
47
|
-
.div(numericConstants_1.BID_ASK_SPREAD_PRECISION), oraclePriceData.confidence.add(new anchor_1.BN(market.amm.baseSpread)
|
|
48
|
-
.mul(oraclePriceData.price)
|
|
49
|
-
.div(numericConstants_1.BID_ASK_SPREAD_PRECISION)));
|
|
50
|
-
var marginPrice;
|
|
51
|
-
if (perpPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
52
|
-
marginPrice = oraclePriceData.price.sub(oraclePriceOffset);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
marginPrice = oraclePriceData.price.add(oraclePriceOffset);
|
|
56
|
-
}
|
|
57
|
-
return marginPrice;
|
|
58
|
-
}
|
|
59
|
-
exports.calculateOraclePriceForPerpMargin = calculateOraclePriceForPerpMargin;
|
|
60
|
-
function calculateBaseAssetValueWithOracle(market, perpPosition, oraclePriceData) {
|
|
61
|
-
return perpPosition.baseAssetAmount
|
|
62
|
-
.abs()
|
|
63
|
-
.mul(oraclePriceData.price)
|
|
64
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
65
|
-
}
|
|
66
|
-
exports.calculateBaseAssetValueWithOracle = calculateBaseAssetValueWithOracle;
|
|
67
|
-
function calculateWorstCaseBaseAssetAmount(perpPosition) {
|
|
68
|
-
var allBids = perpPosition.baseAssetAmount.add(perpPosition.openBids);
|
|
69
|
-
var allAsks = perpPosition.baseAssetAmount.add(perpPosition.openAsks);
|
|
70
|
-
if (allBids.abs().gt(allAsks.abs())) {
|
|
71
|
-
return allBids;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
return allAsks;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.calculateWorstCaseBaseAssetAmount = calculateWorstCaseBaseAssetAmount;
|
package/src/math/market.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
exports.__esModule = true;
|
|
3
|
-
exports.calculateNetUserImbalance = exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.calculateMarkOracleSpread = exports.calculateNewMarketAfterTrade = exports.calculateAskPrice = exports.calculateBidPrice = exports.calculateMarkPrice = void 0;
|
|
4
|
-
var anchor_1 = require("@project-serum/anchor");
|
|
5
|
-
var types_1 = require("../types");
|
|
6
|
-
var amm_1 = require("./amm");
|
|
7
|
-
var margin_1 = require("./margin");
|
|
8
|
-
var numericConstants_1 = require("../constants/numericConstants");
|
|
9
|
-
var spotBalance_1 = require("./spotBalance");
|
|
10
|
-
/**
|
|
11
|
-
* Calculates market mark price
|
|
12
|
-
*
|
|
13
|
-
* @param market
|
|
14
|
-
* @return markPrice : Precision MARK_PRICE_PRECISION
|
|
15
|
-
*/
|
|
16
|
-
function calculateMarkPrice(market, oraclePriceData) {
|
|
17
|
-
var newAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
18
|
-
return (0, amm_1.calculatePrice)(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
|
|
19
|
-
}
|
|
20
|
-
exports.calculateMarkPrice = calculateMarkPrice;
|
|
21
|
-
/**
|
|
22
|
-
* Calculates market bid price
|
|
23
|
-
*
|
|
24
|
-
* @param market
|
|
25
|
-
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
26
|
-
*/
|
|
27
|
-
function calculateBidPrice(market, oraclePriceData) {
|
|
28
|
-
var _a = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.SHORT, oraclePriceData), baseAssetReserve = _a.baseAssetReserve, quoteAssetReserve = _a.quoteAssetReserve, newPeg = _a.newPeg;
|
|
29
|
-
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
30
|
-
}
|
|
31
|
-
exports.calculateBidPrice = calculateBidPrice;
|
|
32
|
-
/**
|
|
33
|
-
* Calculates market ask price
|
|
34
|
-
*
|
|
35
|
-
* @param market
|
|
36
|
-
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
37
|
-
*/
|
|
38
|
-
function calculateAskPrice(market, oraclePriceData) {
|
|
39
|
-
var _a = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.LONG, oraclePriceData), baseAssetReserve = _a.baseAssetReserve, quoteAssetReserve = _a.quoteAssetReserve, newPeg = _a.newPeg;
|
|
40
|
-
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
41
|
-
}
|
|
42
|
-
exports.calculateAskPrice = calculateAskPrice;
|
|
43
|
-
function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
44
|
-
var _a = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'base', baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', direction)), newQuoteAssetReserve = _a[0], newBaseAssetReserve = _a[1];
|
|
45
|
-
var newAmm = Object.assign({}, market.amm);
|
|
46
|
-
var newMarket = Object.assign({}, market);
|
|
47
|
-
newMarket.amm = newAmm;
|
|
48
|
-
newMarket.amm.quoteAssetReserve = newQuoteAssetReserve;
|
|
49
|
-
newMarket.amm.baseAssetReserve = newBaseAssetReserve;
|
|
50
|
-
return newMarket;
|
|
51
|
-
}
|
|
52
|
-
exports.calculateNewMarketAfterTrade = calculateNewMarketAfterTrade;
|
|
53
|
-
function calculateMarkOracleSpread(market, oraclePriceData) {
|
|
54
|
-
var markPrice = calculateMarkPrice(market, oraclePriceData);
|
|
55
|
-
return calculateOracleSpread(markPrice, oraclePriceData);
|
|
56
|
-
}
|
|
57
|
-
exports.calculateMarkOracleSpread = calculateMarkOracleSpread;
|
|
58
|
-
function calculateOracleSpread(price, oraclePriceData) {
|
|
59
|
-
return price.sub(oraclePriceData.price);
|
|
60
|
-
}
|
|
61
|
-
exports.calculateOracleSpread = calculateOracleSpread;
|
|
62
|
-
function calculateMarketMarginRatio(market, size, marginCategory) {
|
|
63
|
-
var marginRatio;
|
|
64
|
-
switch (marginCategory) {
|
|
65
|
-
case 'Initial':
|
|
66
|
-
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, market.imfFactor, new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
67
|
-
break;
|
|
68
|
-
case 'Maintenance':
|
|
69
|
-
marginRatio = market.marginRatioMaintenance;
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
return marginRatio;
|
|
73
|
-
}
|
|
74
|
-
exports.calculateMarketMarginRatio = calculateMarketMarginRatio;
|
|
75
|
-
function calculateUnrealizedAssetWeight(market, unrealizedPnl, marginCategory) {
|
|
76
|
-
var assetWeight;
|
|
77
|
-
switch (marginCategory) {
|
|
78
|
-
case 'Initial':
|
|
79
|
-
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(unrealizedPnl, market.unrealizedImfFactor, new anchor_1.BN(market.unrealizedInitialAssetWeight));
|
|
80
|
-
break;
|
|
81
|
-
case 'Maintenance':
|
|
82
|
-
assetWeight = new anchor_1.BN(market.unrealizedMaintenanceAssetWeight);
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
return assetWeight;
|
|
86
|
-
}
|
|
87
|
-
exports.calculateUnrealizedAssetWeight = calculateUnrealizedAssetWeight;
|
|
88
|
-
function calculateMarketAvailablePNL(perpMarket, spotMarket) {
|
|
89
|
-
return (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.balance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
90
|
-
}
|
|
91
|
-
exports.calculateMarketAvailablePNL = calculateMarketAvailablePNL;
|
|
92
|
-
function calculateNetUserImbalance(market, bank, oraclePriceData) {
|
|
93
|
-
var netUserPositionValue = market.amm.netBaseAssetAmount
|
|
94
|
-
.mul(oraclePriceData.price)
|
|
95
|
-
.div(numericConstants_1.BASE_PRECISION)
|
|
96
|
-
.div(numericConstants_1.PRICE_TO_QUOTE_PRECISION);
|
|
97
|
-
var netUserCostBasis = market.amm.quoteAssetAmountLong
|
|
98
|
-
.add(market.amm.quoteAssetAmountShort)
|
|
99
|
-
.sub(market.amm.cumulativeSocialLoss);
|
|
100
|
-
var userEntitledPnl = netUserPositionValue.add(netUserCostBasis);
|
|
101
|
-
var pnlPool = (0, spotBalance_1.getTokenAmount)(market.pnlPool.balance, bank, types_1.SpotBalanceType.DEPOSIT);
|
|
102
|
-
var imbalance = userEntitledPnl.sub(pnlPool);
|
|
103
|
-
return imbalance;
|
|
104
|
-
}
|
|
105
|
-
exports.calculateNetUserImbalance = calculateNetUserImbalance;
|
package/src/math/oracles.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
exports.__esModule = true;
|
|
3
|
-
exports.isOracleTooDivergent = exports.isOracleValid = exports.oraclePriceBands = void 0;
|
|
4
|
-
var numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
var index_1 = require("../index");
|
|
6
|
-
var assert_1 = require("../assert/assert");
|
|
7
|
-
function oraclePriceBands(market, oraclePriceData) {
|
|
8
|
-
var maxPercentDiff = market.marginRatioInitial - market.marginRatioMaintenance;
|
|
9
|
-
var offset = oraclePriceData.price
|
|
10
|
-
.mul(new index_1.BN(maxPercentDiff))
|
|
11
|
-
.div(numericConstants_1.MARGIN_PRECISION);
|
|
12
|
-
(0, assert_1.assert)(offset.gt(numericConstants_1.ZERO));
|
|
13
|
-
return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
|
|
14
|
-
}
|
|
15
|
-
exports.oraclePriceBands = oraclePriceBands;
|
|
16
|
-
function isOracleValid(amm, oraclePriceData, oracleGuardRails, slot) {
|
|
17
|
-
var isOraclePriceNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
|
|
18
|
-
var isOraclePriceTooVolatile = oraclePriceData.price
|
|
19
|
-
.div(index_1.BN.max(numericConstants_1.ONE, amm.lastOraclePriceTwap))
|
|
20
|
-
.gt(oracleGuardRails.validity.tooVolatileRatio) ||
|
|
21
|
-
amm.lastOraclePriceTwap
|
|
22
|
-
.div(index_1.BN.max(numericConstants_1.ONE, oraclePriceData.price))
|
|
23
|
-
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
24
|
-
var isConfidenceTooLarge = new index_1.BN(amm.baseSpread)
|
|
25
|
-
.add(index_1.BN.max(numericConstants_1.ONE, oraclePriceData.confidence))
|
|
26
|
-
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
27
|
-
.div(oraclePriceData.price)
|
|
28
|
-
.gt(new index_1.BN(amm.maxSpread));
|
|
29
|
-
var oracleIsStale = oraclePriceData.slot
|
|
30
|
-
.sub(new index_1.BN(slot))
|
|
31
|
-
.gt(oracleGuardRails.validity.slotsBeforeStale);
|
|
32
|
-
return !(!oraclePriceData.hasSufficientNumberOfDataPoints ||
|
|
33
|
-
oracleIsStale ||
|
|
34
|
-
isOraclePriceNonPositive ||
|
|
35
|
-
isOraclePriceTooVolatile ||
|
|
36
|
-
isConfidenceTooLarge);
|
|
37
|
-
}
|
|
38
|
-
exports.isOracleValid = isOracleValid;
|
|
39
|
-
function isOracleTooDivergent(amm, oraclePriceData, oracleGuardRails, now) {
|
|
40
|
-
var sinceLastUpdate = now.sub(amm.lastOraclePriceTwapTs);
|
|
41
|
-
var sinceStart = index_1.BN.max(numericConstants_1.ZERO, new index_1.BN(60 * 5).sub(sinceLastUpdate));
|
|
42
|
-
var oracleTwap5min = amm.lastOraclePriceTwap5min
|
|
43
|
-
.mul(sinceStart)
|
|
44
|
-
.add(oraclePriceData.price)
|
|
45
|
-
.mul(sinceLastUpdate)
|
|
46
|
-
.div(sinceStart.add(sinceLastUpdate));
|
|
47
|
-
var oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
48
|
-
var oracleSpreadPct = oracleSpread
|
|
49
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
50
|
-
.div(oracleTwap5min);
|
|
51
|
-
var tooDivergent = oracleSpreadPct
|
|
52
|
-
.abs()
|
|
53
|
-
.gte(numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(oracleGuardRails.priceDivergence.markOracleDivergenceNumerator).div(oracleGuardRails.priceDivergence.markOracleDivergenceDenominator));
|
|
54
|
-
return tooDivergent;
|
|
55
|
-
}
|
|
56
|
-
exports.isOracleTooDivergent = isOracleTooDivergent;
|