@reyaxyz/common 0.364.1 → 0.364.3
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 +1 -1
- package/dist/commands/exposure/command.js +2 -340
- package/dist/commands/exposure/command.js.map +1 -1
- package/dist/commands/exposure/types.js.map +1 -1
- package/dist/trading-api-types.js.map +1 -1
- package/dist/types/commands/exposure/command.d.ts +1 -44
- package/dist/types/commands/exposure/command.d.ts.map +1 -1
- package/dist/types/commands/exposure/types.d.ts +0 -10
- package/dist/types/commands/exposure/types.d.ts.map +1 -1
- package/dist/types/trading-api-types.d.ts +2 -51
- package/dist/types/trading-api-types.d.ts.map +1 -1
- package/dist/types/types.d.ts +0 -8
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/calculate.d.ts +0 -125
- package/dist/types/utils/calculate.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/calculate.js +1 -324
- package/dist/utils/calculate.js.map +1 -1
- package/package.json +3 -5
- package/src/commands/exposure/command.ts +0 -550
- package/src/commands/exposure/types.ts +0 -12
- package/src/trading-api-types.ts +1 -66
- package/src/types.ts +0 -10
- package/src/utils/calculate.ts +0 -497
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
3
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
4
|
if (ar || !(i in from)) {
|
|
@@ -25,7 +14,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
15
|
exports.ExposureCommand = exports.clampFundingRate = exports.MIN_FUNDING_RATE = exports.MAX_FUNDING_RATE = void 0;
|
|
27
16
|
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
28
|
-
var lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
29
17
|
var utils_1 = require("../../utils");
|
|
30
18
|
exports.MAX_FUNDING_RATE = (0, bignumber_js_1.default)('0.07');
|
|
31
19
|
exports.MIN_FUNDING_RATE = (0, bignumber_js_1.default)('-0.07');
|
|
@@ -63,26 +51,6 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
63
51
|
this.mtmRpnlSum = mtmRpnlSum;
|
|
64
52
|
this.collateralAddressToExchangePrice = collateralAddressToExchangePrice;
|
|
65
53
|
}
|
|
66
|
-
ExposureCommand.prototype.getState = function () {
|
|
67
|
-
return {
|
|
68
|
-
accountId: this.accountId,
|
|
69
|
-
rootCollateralPoolId: this.rootCollateralPoolId,
|
|
70
|
-
oraclePricePerMarket: this.oraclePricePerMarket,
|
|
71
|
-
accountBalancePerAsset: this.accountBalancePerAsset,
|
|
72
|
-
groupedByCollateral: this.groupedByCollateral,
|
|
73
|
-
riskMultipliers: this.riskMultipliers,
|
|
74
|
-
riskMatrices: this.riskMatrices,
|
|
75
|
-
exchangeInfoPerAsset: this.exchangeInfoPerAsset,
|
|
76
|
-
positionInfoMarketConfiguration: this.positionInfoMarketConfiguration,
|
|
77
|
-
uniqueTokenAddresses: this.uniqueTokenAddresses,
|
|
78
|
-
uniqueQuoteCollaterals: this.uniqueQuoteCollaterals,
|
|
79
|
-
tokenMarginInfoPerAsset: this.tokenMarginInfoPerAsset,
|
|
80
|
-
realizedPnLSum: this.realizedPnLSum,
|
|
81
|
-
unrealizedPnLSum: this.unrealizedPnLSum,
|
|
82
|
-
mtmRpnlSum: this.mtmRpnlSum,
|
|
83
|
-
collateralAddressToExchangePrice: this.collateralAddressToExchangePrice,
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
54
|
Object.defineProperty(ExposureCommand.prototype, "getUsdNodeMarginInfo", {
|
|
87
55
|
get: function () {
|
|
88
56
|
return ExposureCommand.getUsdNodeMarginInfo(this.rootCollateralPoolId, this.uniqueTokenAddresses, this.exchangeInfoPerAsset, this.tokenMarginInfoPerAsset);
|
|
@@ -97,160 +65,6 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
97
65
|
enumerable: false,
|
|
98
66
|
configurable: true
|
|
99
67
|
});
|
|
100
|
-
Object.defineProperty(ExposureCommand.prototype, "exchangePricePerAsset", {
|
|
101
|
-
get: function () {
|
|
102
|
-
return this.exchangeInfoPerAsset;
|
|
103
|
-
},
|
|
104
|
-
enumerable: false,
|
|
105
|
-
configurable: true
|
|
106
|
-
});
|
|
107
|
-
ExposureCommand.prototype.getUsdNodeMarginInfoPostTrade = function (positionAmount, collateralAddress, marketConfiguration, riskBlockId) {
|
|
108
|
-
// perform deep copy of the object
|
|
109
|
-
var positionInfoMarketConfiguration = (0, lodash_clonedeep_1.default)(this.positionInfoMarketConfiguration);
|
|
110
|
-
// Check if the market_id already exists in the array
|
|
111
|
-
var existingConfigIndex = positionInfoMarketConfiguration.findIndex(function (config) {
|
|
112
|
-
return config.market_id ===
|
|
113
|
-
(0, bignumber_js_1.default)(String(marketConfiguration.market_id)).toNumber();
|
|
114
|
-
});
|
|
115
|
-
if (existingConfigIndex !== -1) {
|
|
116
|
-
// If it exists, update the amount
|
|
117
|
-
positionInfoMarketConfiguration[existingConfigIndex].base = (0, bignumber_js_1.default)(positionInfoMarketConfiguration[existingConfigIndex].base).plus(positionAmount);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
// If it doesn't exist, add a new element
|
|
121
|
-
positionInfoMarketConfiguration.push({
|
|
122
|
-
base: (0, bignumber_js_1.default)(positionAmount),
|
|
123
|
-
realized_pnl: (0, bignumber_js_1.default)(0),
|
|
124
|
-
realized_pnl_with_mtm: (0, bignumber_js_1.default)(0),
|
|
125
|
-
realized_pnl_latest_snapshot: (0, bignumber_js_1.default)(0),
|
|
126
|
-
latest_funding_cashflow: (0, bignumber_js_1.default)(0),
|
|
127
|
-
last_price: (0, bignumber_js_1.default)(0),
|
|
128
|
-
average_entry_price_off_chain_tracker: (0, bignumber_js_1.default)(0),
|
|
129
|
-
funding_value_off_chain_tracker: (0, bignumber_js_1.default)(0),
|
|
130
|
-
average_entry_price: (0, bignumber_js_1.default)(0),
|
|
131
|
-
last_timestamp: (0, bignumber_js_1.default)(0),
|
|
132
|
-
funding_value: (0, bignumber_js_1.default)(0),
|
|
133
|
-
base_multiplier: (0, bignumber_js_1.default)(0),
|
|
134
|
-
adl_unwind_price: (0, bignumber_js_1.default)(0),
|
|
135
|
-
market_id: (0, bignumber_js_1.default)(String(marketConfiguration.market_id)).toNumber(),
|
|
136
|
-
account_id: this.accountId,
|
|
137
|
-
market_configuration: marketConfiguration,
|
|
138
|
-
risk_block_id: riskBlockId,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
var uniqueQuoteCollaterals = new Set(this.uniqueQuoteCollaterals);
|
|
142
|
-
uniqueQuoteCollaterals.add(collateralAddress);
|
|
143
|
-
var tokenMarginInfoPerAsset = ExposureCommand.calculateTokenMarginInfoPerAsset(this.groupedByCollateral, this.rootCollateralPoolId, this.riskMatrices, this.riskMultipliers, uniqueQuoteCollaterals, this.realizedPnLSum, this.unrealizedPnLSum, positionInfoMarketConfiguration, this.oraclePricePerMarket);
|
|
144
|
-
var uniqueTokenAddresses = __spreadArray([], this.uniqueTokenAddresses, true);
|
|
145
|
-
if (!this.uniqueTokenAddresses.includes(collateralAddress)) {
|
|
146
|
-
uniqueTokenAddresses.push(collateralAddress);
|
|
147
|
-
}
|
|
148
|
-
return {
|
|
149
|
-
usdNodeMarginInfo: ExposureCommand.getUsdNodeMarginInfo(this.rootCollateralPoolId, uniqueTokenAddresses, this.exchangeInfoPerAsset, tokenMarginInfoPerAsset),
|
|
150
|
-
tokenMarginInfoPerAsset: tokenMarginInfoPerAsset,
|
|
151
|
-
positionInfoMarketConfiguration: positionInfoMarketConfiguration,
|
|
152
|
-
};
|
|
153
|
-
};
|
|
154
|
-
ExposureCommand.prototype.getEditCollateralActionsToCoverMargin = function (requiredMargin) {
|
|
155
|
-
/*
|
|
156
|
-
* Note, this function is implicitely making an assumption that required margin can be covered by
|
|
157
|
-
* the account without breaching account IMR
|
|
158
|
-
* */
|
|
159
|
-
var editCollateralActions = [];
|
|
160
|
-
var marginToCover = requiredMargin;
|
|
161
|
-
var uniqueQuoteCollaterals = new Set(this.uniqueQuoteCollaterals);
|
|
162
|
-
var tokenMarginInfoPerAsset = ExposureCommand.calculateTokenMarginInfoPerAsset(this.groupedByCollateral, this.rootCollateralPoolId, this.riskMatrices, this.riskMultipliers, uniqueQuoteCollaterals, this.realizedPnLSum, this.unrealizedPnLSum, this.positionInfoMarketConfiguration, this.oraclePricePerMarket);
|
|
163
|
-
var _loop_1 = function (tokenInfo) {
|
|
164
|
-
if (tokenInfo.marginBalance < 0) {
|
|
165
|
-
return "continue";
|
|
166
|
-
}
|
|
167
|
-
// todo: p1: haircut and exchange rate adjustment should not be done for rUSD
|
|
168
|
-
// does exchangeInfo currently return haircut as zero and price as 1?
|
|
169
|
-
// todo: p2: can just look at net deposits rather than margin balance for non-rusd as non pnl
|
|
170
|
-
var exchangeInfo = this_1.exchangeInfoPerAsset.find(function (exchangeInfo) {
|
|
171
|
-
return tokenInfo.assetAddress === exchangeInfo.tokenAddress;
|
|
172
|
-
});
|
|
173
|
-
if (!exchangeInfo) {
|
|
174
|
-
throw new Error('Unable to retrieve exchange info when calculating collaterals needed to cover margin');
|
|
175
|
-
}
|
|
176
|
-
var marginBalanceWithHaircutInRUSD = ExposureCommand.exchangeWithPriceHaircut(tokenInfo.marginBalance, exchangeInfo.price, exchangeInfo.priceHaircut);
|
|
177
|
-
if (marginBalanceWithHaircutInRUSD > marginToCover) {
|
|
178
|
-
var collateralDelta = -ExposureCommand.reverseExchangeWithPriceHaircut(marginToCover, exchangeInfo.price, exchangeInfo.priceHaircut);
|
|
179
|
-
editCollateralActions.push({
|
|
180
|
-
collateralAddress: tokenInfo.assetAddress,
|
|
181
|
-
collateralDelta: collateralDelta,
|
|
182
|
-
});
|
|
183
|
-
return "break";
|
|
184
|
-
}
|
|
185
|
-
else {
|
|
186
|
-
editCollateralActions.push({
|
|
187
|
-
collateralAddress: tokenInfo.assetAddress,
|
|
188
|
-
collateralDelta: -tokenInfo.marginBalance,
|
|
189
|
-
});
|
|
190
|
-
marginToCover = marginToCover - marginBalanceWithHaircutInRUSD;
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
var this_1 = this;
|
|
194
|
-
// todo: p2: consider prioritising rUSD when looping through token infos & document math in comments
|
|
195
|
-
for (var _i = 0, tokenMarginInfoPerAsset_1 = tokenMarginInfoPerAsset; _i < tokenMarginInfoPerAsset_1.length; _i++) {
|
|
196
|
-
var tokenInfo = tokenMarginInfoPerAsset_1[_i];
|
|
197
|
-
var state_1 = _loop_1(tokenInfo);
|
|
198
|
-
if (state_1 === "break")
|
|
199
|
-
break;
|
|
200
|
-
}
|
|
201
|
-
return editCollateralActions;
|
|
202
|
-
};
|
|
203
|
-
ExposureCommand.prototype.getUsdNodeMarginInfoPostEditCollaterals = function (editCollateralActions) {
|
|
204
|
-
// todo: check if there's a way to simplify this function & deprecate the single edit collateral sim below as it's a
|
|
205
|
-
// subset of this function
|
|
206
|
-
// perform deep copy of the object
|
|
207
|
-
var groupedByCollateral = (0, lodash_clonedeep_1.default)(this.groupedByCollateral);
|
|
208
|
-
var uniqueQuoteCollaterals = new Set(this.uniqueQuoteCollaterals);
|
|
209
|
-
var uniqueTokenAddresses = __spreadArray([], this.uniqueTokenAddresses, true);
|
|
210
|
-
// counterfactual update deep copy
|
|
211
|
-
for (var _i = 0, editCollateralActions_1 = editCollateralActions; _i < editCollateralActions_1.length; _i++) {
|
|
212
|
-
var action = editCollateralActions_1[_i];
|
|
213
|
-
if (groupedByCollateral[action.collateralAddress]) {
|
|
214
|
-
groupedByCollateral[action.collateralAddress] = __assign(__assign({}, groupedByCollateral[action.collateralAddress]), { amount: groupedByCollateral[action.collateralAddress].amount +
|
|
215
|
-
action.collateralDelta });
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
groupedByCollateral[action.collateralAddress] = {
|
|
219
|
-
accountId: this.accountId,
|
|
220
|
-
collateral: action.collateralAddress,
|
|
221
|
-
amount: action.collateralDelta,
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
uniqueQuoteCollaterals.add(action.collateralAddress);
|
|
225
|
-
if (!this.uniqueTokenAddresses.includes(action.collateralAddress)) {
|
|
226
|
-
uniqueTokenAddresses.push(action.collateralAddress);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
var tokenMarginInfoPerAsset = ExposureCommand.calculateTokenMarginInfoPerAsset(groupedByCollateral, this.rootCollateralPoolId, this.riskMatrices, this.riskMultipliers, uniqueQuoteCollaterals, this.realizedPnLSum, this.unrealizedPnLSum, this.positionInfoMarketConfiguration, this.oraclePricePerMarket);
|
|
230
|
-
return ExposureCommand.getUsdNodeMarginInfo(this.rootCollateralPoolId, uniqueTokenAddresses, this.exchangeInfoPerAsset, tokenMarginInfoPerAsset);
|
|
231
|
-
};
|
|
232
|
-
ExposureCommand.prototype.getUsdNodeMarginInfoPostEditCollateral = function (collateralDelta, collateralAddress) {
|
|
233
|
-
// perform deep copy of the object
|
|
234
|
-
var groupedByCollateral = (0, lodash_clonedeep_1.default)(this.groupedByCollateral);
|
|
235
|
-
// counterfactual update deep copy
|
|
236
|
-
if (groupedByCollateral[collateralAddress]) {
|
|
237
|
-
groupedByCollateral[collateralAddress] = __assign(__assign({}, groupedByCollateral[collateralAddress]), { amount: groupedByCollateral[collateralAddress].amount + collateralDelta });
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
groupedByCollateral[collateralAddress] = {
|
|
241
|
-
accountId: this.accountId,
|
|
242
|
-
collateral: collateralAddress,
|
|
243
|
-
amount: collateralDelta,
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
var uniqueQuoteCollaterals = new Set(this.uniqueQuoteCollaterals);
|
|
247
|
-
var tokenMarginInfoPerAsset = ExposureCommand.calculateTokenMarginInfoPerAsset(groupedByCollateral, this.rootCollateralPoolId, this.riskMatrices, this.riskMultipliers, uniqueQuoteCollaterals, this.realizedPnLSum, this.unrealizedPnLSum, this.positionInfoMarketConfiguration, this.oraclePricePerMarket);
|
|
248
|
-
var uniqueTokenAddresses = __spreadArray([], this.uniqueTokenAddresses, true);
|
|
249
|
-
if (!this.uniqueTokenAddresses.includes(collateralAddress)) {
|
|
250
|
-
uniqueTokenAddresses.push(collateralAddress);
|
|
251
|
-
}
|
|
252
|
-
return ExposureCommand.getUsdNodeMarginInfo(this.rootCollateralPoolId, uniqueTokenAddresses, this.exchangeInfoPerAsset, tokenMarginInfoPerAsset);
|
|
253
|
-
};
|
|
254
68
|
ExposureCommand.calculateTokenMarginInfoPerAsset = function (groupedByCollateral, rootCollateralPoolId, riskMatrices, riskMultipliers, uniqueQuoteCollaterals, realizedPnLSum, unrealizedPnLSum, positionInfoMarketConfiguration, oraclePricePerMarket) {
|
|
255
69
|
var _a;
|
|
256
70
|
var tokenMarginInfoPerAsset = [];
|
|
@@ -275,13 +89,6 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
275
89
|
.div(positionBase));
|
|
276
90
|
return bignumber_js_1.default.max(0, liquidationPrice);
|
|
277
91
|
};
|
|
278
|
-
ExposureCommand.calculateImpliedLeverage = function (notionalExposure, oldIMR, newIMR) {
|
|
279
|
-
var changeInImr = (0, bignumber_js_1.default)(newIMR).minus(oldIMR);
|
|
280
|
-
if (changeInImr.eq(0)) {
|
|
281
|
-
return 0;
|
|
282
|
-
}
|
|
283
|
-
return (0, bignumber_js_1.default)(notionalExposure).div(changeInImr).toNumber();
|
|
284
|
-
};
|
|
285
92
|
ExposureCommand.combineMarginInfo = function (parentMarginInfo, sonMarginInfo, sonParentExchangeInfo) {
|
|
286
93
|
return {
|
|
287
94
|
assetAddress: parentMarginInfo.assetAddress,
|
|
@@ -327,7 +134,7 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
327
134
|
initialBufferDelta: 0,
|
|
328
135
|
liquidationMarginRequirement: 0,
|
|
329
136
|
};
|
|
330
|
-
var
|
|
137
|
+
var _loop_1 = function (quoteToken) {
|
|
331
138
|
var exchangeInfo = exchangeInfoPerAsset.find(function (exchangeInfo) {
|
|
332
139
|
return quoteToken === exchangeInfo.tokenAddress;
|
|
333
140
|
});
|
|
@@ -341,7 +148,7 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
341
148
|
};
|
|
342
149
|
for (var _i = 0, quoteTokens_1 = quoteTokens; _i < quoteTokens_1.length; _i++) {
|
|
343
150
|
var quoteToken = quoteTokens_1[_i];
|
|
344
|
-
|
|
151
|
+
_loop_1(quoteToken);
|
|
345
152
|
}
|
|
346
153
|
return usdNodeMarginInfo;
|
|
347
154
|
};
|
|
@@ -494,22 +301,6 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
494
301
|
var haircutPrice = (0, bignumber_js_1.default)(price).multipliedBy(calHelper);
|
|
495
302
|
return haircutPrice.multipliedBy(quantity).toNumber();
|
|
496
303
|
};
|
|
497
|
-
ExposureCommand.reverseExchangeWithPriceHaircut = function (quantity, price, haircut) {
|
|
498
|
-
/*
|
|
499
|
-
* this operation takes quantity in rUSD terms, price between collateral token (e.g. weth) and rusd
|
|
500
|
-
* alongside the haircut between weth and rusd
|
|
501
|
-
* it then returns a quantity in collateral token terms (e.g. weth) which would return quantity if it were to be
|
|
502
|
-
* passed through the exchangeWithPriceHaircut function
|
|
503
|
-
* */
|
|
504
|
-
// todo: p2: consider abstracting haircut price calculation into separate function as duplciated
|
|
505
|
-
// For positive quantities, the haircut is `quantity * (1 - haircut)`
|
|
506
|
-
// For negative values, the haircut is `quantity / (1 - haircut)` because a negative value means the haircut should be applied from B to A.
|
|
507
|
-
var calHelper = (0, bignumber_js_1.default)(quantity).gt(0)
|
|
508
|
-
? (0, bignumber_js_1.default)(1).minus(haircut)
|
|
509
|
-
: (0, bignumber_js_1.default)(1).div((0, bignumber_js_1.default)(1).minus(haircut));
|
|
510
|
-
var haircutPrice = (0, bignumber_js_1.default)(price).multipliedBy(calHelper);
|
|
511
|
-
return (0, bignumber_js_1.default)(quantity).dividedBy(haircutPrice).toNumber();
|
|
512
|
-
};
|
|
513
304
|
ExposureCommand.getBalanceWithHaircut = function (exchangeInfoPerAsset, assetAddress, marginBalance) {
|
|
514
305
|
// todo: p1: haircut and exchange rate adjustment should not be done for rUSD
|
|
515
306
|
// does exchangeInfo currently return haircut as zero and price as 1?
|
|
@@ -522,135 +313,6 @@ var ExposureCommand = /** @class */ (function () {
|
|
|
522
313
|
}
|
|
523
314
|
return ExposureCommand.exchangeWithPriceHaircut(marginBalance, exchangeInfo.price, exchangeInfo.priceHaircut);
|
|
524
315
|
};
|
|
525
|
-
ExposureCommand.prototype.getMaxExposure = function (marketConfiguration, marketStorage, isPool) {
|
|
526
|
-
// todo: p2: cosider turning this into a hashmap vs. an array so that O(1) access
|
|
527
|
-
var riskMatrix = this.riskMatrices.find(function (riskMatrix) {
|
|
528
|
-
return (riskMatrix.risk_block_id ===
|
|
529
|
-
(0, bignumber_js_1.default)(String(marketStorage.risk_block_id)).toNumber());
|
|
530
|
-
});
|
|
531
|
-
if (!riskMatrix) {
|
|
532
|
-
throw new Error("RiskMatrix Doesn't exist");
|
|
533
|
-
}
|
|
534
|
-
var riskMatrixIndex = (0, bignumber_js_1.default)(String(marketConfiguration.risk_matrix_index)).toNumber();
|
|
535
|
-
var imrMultiplier;
|
|
536
|
-
if (isPool) {
|
|
537
|
-
imrMultiplier = utils_1.POOL_IMR;
|
|
538
|
-
}
|
|
539
|
-
else {
|
|
540
|
-
imrMultiplier = (0, utils_1.amountNormalizer)(String(this.riskMultipliers.im_multiplier)).toNumber();
|
|
541
|
-
}
|
|
542
|
-
var marginInfo = this.tokenMarginInfoPerAsset.find(function (marginInfo) {
|
|
543
|
-
return marginInfo.assetAddress === marketStorage.quote_collateral;
|
|
544
|
-
});
|
|
545
|
-
if (!marginInfo) {
|
|
546
|
-
throw new Error("marginInfo doesn't exist");
|
|
547
|
-
}
|
|
548
|
-
var exposures = ExposureCommand.getBlockExposures(this.positionInfoMarketConfiguration, this.oraclePricePerMarket, (0, bignumber_js_1.default)(String(marketStorage.risk_block_id)).toNumber());
|
|
549
|
-
var _a = ExposureCommand.computeMaxExposures(riskMatrix.matrix, exposures, marginInfo.liquidationMarginRequirement, marginInfo.marginBalance < 0 ? 0 : marginInfo.marginBalance, imrMultiplier, riskMatrixIndex), maxExposureShort = _a.maxExposureShort, maxExposureLong = _a.maxExposureLong;
|
|
550
|
-
return {
|
|
551
|
-
maxExposureShort: maxExposureShort,
|
|
552
|
-
maxExposureLong: maxExposureLong,
|
|
553
|
-
exposures: exposures,
|
|
554
|
-
riskMatrixIndex: riskMatrixIndex,
|
|
555
|
-
};
|
|
556
|
-
};
|
|
557
|
-
ExposureCommand.computeMaxExposures = function (riskMatrix, exposures, lmr, balance, imrMultiplier, exposureIndex) {
|
|
558
|
-
var b = (0, bignumber_js_1.default)(0);
|
|
559
|
-
for (var i = 0; i < exposures.length; i++) {
|
|
560
|
-
b = (0, bignumber_js_1.default)(b).plus((0, bignumber_js_1.default)(exposures[i]).multipliedBy((0, bignumber_js_1.default)(riskMatrix[exposureIndex][i]).plus(riskMatrix[i][exposureIndex])));
|
|
561
|
-
}
|
|
562
|
-
var _a = this.solveQuadraticEquation((0, bignumber_js_1.default)(riskMatrix[exposureIndex][exposureIndex]).toNumber(), // changes here
|
|
563
|
-
b.toNumber(), this.computeC(lmr, balance, imrMultiplier)), x1 = _a.x1, x2 = _a.x2;
|
|
564
|
-
var maxShortExposure = (0, bignumber_js_1.default)(x1).plus(exposures[exposureIndex]);
|
|
565
|
-
var maxLongExposure = (0, bignumber_js_1.default)(x2).plus(exposures[exposureIndex]);
|
|
566
|
-
var availableShortExposure = maxShortExposure.lt(0)
|
|
567
|
-
? maxShortExposure.negated().toNumber()
|
|
568
|
-
: 0;
|
|
569
|
-
var availableLongExposure = maxLongExposure.gt(0)
|
|
570
|
-
? maxLongExposure.toNumber()
|
|
571
|
-
: 0;
|
|
572
|
-
return {
|
|
573
|
-
maxExposureShort: availableShortExposure,
|
|
574
|
-
maxExposureLong: availableLongExposure,
|
|
575
|
-
};
|
|
576
|
-
};
|
|
577
|
-
ExposureCommand.solveQuadraticEquation = function (a, b, c) {
|
|
578
|
-
if ((0, bignumber_js_1.default)(a).eq(0)) {
|
|
579
|
-
throw new Error('ZeroQuadraticCoefficient');
|
|
580
|
-
}
|
|
581
|
-
var delta = (0, bignumber_js_1.default)(b)
|
|
582
|
-
.multipliedBy(b)
|
|
583
|
-
.minus((0, bignumber_js_1.default)(4).multipliedBy(a).multipliedBy(c));
|
|
584
|
-
if (delta.lt(0)) {
|
|
585
|
-
return {
|
|
586
|
-
x1: (0, bignumber_js_1.default)(0),
|
|
587
|
-
x2: (0, bignumber_js_1.default)(0),
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
var rootDelta = delta.sqrt();
|
|
591
|
-
var x1 = (0, bignumber_js_1.default)(b)
|
|
592
|
-
.multipliedBy(-1)
|
|
593
|
-
.minus(rootDelta)
|
|
594
|
-
.div((0, bignumber_js_1.default)(2).multipliedBy(a));
|
|
595
|
-
var x2 = (0, bignumber_js_1.default)(b)
|
|
596
|
-
.multipliedBy(-1)
|
|
597
|
-
.plus(rootDelta)
|
|
598
|
-
.div((0, bignumber_js_1.default)(2).multipliedBy(a));
|
|
599
|
-
return {
|
|
600
|
-
x1: x1,
|
|
601
|
-
x2: x2,
|
|
602
|
-
};
|
|
603
|
-
};
|
|
604
|
-
ExposureCommand.computeC = function (lmr, balance, imrMultiplier) {
|
|
605
|
-
var lmrSD = (0, bignumber_js_1.default)(lmr);
|
|
606
|
-
var lmrSquared = lmrSD.multipliedBy(lmrSD);
|
|
607
|
-
var balanceSD = (0, bignumber_js_1.default)(balance);
|
|
608
|
-
var balanceSquared = balanceSD.multipliedBy(balanceSD);
|
|
609
|
-
var imrMultiplierSD = (0, bignumber_js_1.default)(imrMultiplier);
|
|
610
|
-
var imrMultiplierSquared = imrMultiplierSD.multipliedBy(imrMultiplierSD);
|
|
611
|
-
return lmrSquared
|
|
612
|
-
.minus(balanceSquared.div(imrMultiplierSquared))
|
|
613
|
-
.toNumber();
|
|
614
|
-
};
|
|
615
|
-
ExposureCommand.calculateRebalancingOrderBase = function (_a) {
|
|
616
|
-
var poolBasePreTrade = _a.poolBasePreTrade, orderBase = _a.orderBase;
|
|
617
|
-
if (poolBasePreTrade > 0) {
|
|
618
|
-
if (orderBase < 0) {
|
|
619
|
-
return 0;
|
|
620
|
-
}
|
|
621
|
-
return Math.min(poolBasePreTrade, orderBase);
|
|
622
|
-
}
|
|
623
|
-
if (orderBase > 0) {
|
|
624
|
-
return 0;
|
|
625
|
-
}
|
|
626
|
-
return Math.max(poolBasePreTrade, orderBase);
|
|
627
|
-
};
|
|
628
|
-
ExposureCommand.calculateFee = function (_a) {
|
|
629
|
-
var oraclePrice = _a.oraclePrice, poolBasePreTrade = _a.poolBasePreTrade, orderBase = _a.orderBase, feeParameter = _a.feeParameter, rebalancingFeeParameter = _a.rebalancingFeeParameter;
|
|
630
|
-
var rebalancingOrderBase = this.calculateRebalancingOrderBase({
|
|
631
|
-
poolBasePreTrade: poolBasePreTrade,
|
|
632
|
-
orderBase: orderBase,
|
|
633
|
-
});
|
|
634
|
-
var imbalancingOrderBase = orderBase - rebalancingOrderBase;
|
|
635
|
-
var imbalancingFee = (0, bignumber_js_1.default)(oraclePrice)
|
|
636
|
-
.times(imbalancingOrderBase)
|
|
637
|
-
.times(feeParameter)
|
|
638
|
-
.abs()
|
|
639
|
-
.toNumber();
|
|
640
|
-
var rebalancingFee = (0, bignumber_js_1.default)(oraclePrice)
|
|
641
|
-
.times(rebalancingOrderBase)
|
|
642
|
-
.times(rebalancingFeeParameter)
|
|
643
|
-
.abs()
|
|
644
|
-
.toNumber();
|
|
645
|
-
return {
|
|
646
|
-
imbalancingFee: imbalancingFee,
|
|
647
|
-
rebalancingFee: rebalancingFee,
|
|
648
|
-
fee: imbalancingFee + rebalancingFee,
|
|
649
|
-
};
|
|
650
|
-
};
|
|
651
|
-
ExposureCommand.calculateEstimatedPrice = function (price, slippage) {
|
|
652
|
-
return (0, bignumber_js_1.default)(price).times((0, bignumber_js_1.default)(1).plus(slippage)).toNumber();
|
|
653
|
-
};
|
|
654
316
|
ExposureCommand.evaluateHealthStatus = function (marginRatioPercentage) {
|
|
655
317
|
if (marginRatioPercentage >= utils_1.MA_HEALTH_DANGER_THRESHOLD) {
|
|
656
318
|
return 'danger';
|