@reyaxyz/api-sdk 0.26.0 → 0.28.0
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/dist/clients/api-client.js +19 -0
- package/dist/clients/api-client.js.map +1 -1
- package/dist/clients/modules/{account.js → account/index.js} +2 -2
- package/dist/clients/modules/account/index.js.map +1 -0
- package/dist/clients/{helpers/trade.simulation.types.js → modules/account/types.js} +1 -1
- package/dist/clients/modules/account/types.js.map +1 -0
- package/dist/clients/modules/{lp.js → lp/index.js} +2 -2
- package/dist/clients/modules/lp/index.js.map +1 -0
- package/dist/clients/modules/lp/types.js +13 -0
- package/dist/clients/modules/lp/types.js.map +1 -0
- package/dist/clients/modules/{markets.js → markets/index.js} +2 -2
- package/dist/clients/modules/markets/index.js.map +1 -0
- package/dist/clients/modules/markets/types.js +3 -0
- package/dist/clients/modules/markets/types.js.map +1 -0
- package/dist/clients/modules/{rest.js → rest/index.js} +4 -4
- package/dist/clients/modules/rest/index.js.map +1 -0
- package/dist/clients/modules/tokens/index.js +75 -0
- package/dist/clients/modules/tokens/index.js.map +1 -0
- package/dist/clients/modules/tokens/types.js +3 -0
- package/dist/clients/modules/tokens/types.js.map +1 -0
- package/dist/clients/modules/{trade.simulation.js → trade.simulation/index.js} +10 -10
- package/dist/clients/modules/trade.simulation/index.js.map +1 -0
- package/dist/clients/modules/trade.simulation/types.js +3 -0
- package/dist/clients/modules/trade.simulation/types.js.map +1 -0
- package/dist/clients/types.js +4 -36
- package/dist/clients/types.js.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/types/clients/api-client.d.ts +13 -0
- package/dist/types/clients/api-client.d.ts.map +1 -1
- package/dist/types/clients/modules/{account.d.ts → account/index.d.ts} +4 -4
- package/dist/types/clients/modules/account/index.d.ts.map +1 -0
- package/dist/types/clients/modules/account/types.d.ts +47 -0
- package/dist/types/clients/modules/account/types.d.ts.map +1 -0
- package/dist/types/clients/modules/{lp.d.ts → lp/index.d.ts} +4 -3
- package/dist/types/clients/modules/lp/index.d.ts.map +1 -0
- package/dist/types/clients/modules/lp/types.d.ts +46 -0
- package/dist/types/clients/modules/lp/types.d.ts.map +1 -0
- package/dist/types/clients/modules/{markets.d.ts → markets/index.d.ts} +3 -3
- package/dist/types/clients/modules/markets/index.d.ts.map +1 -0
- package/dist/types/clients/modules/markets/types.d.ts +23 -0
- package/dist/types/clients/modules/markets/types.d.ts.map +1 -0
- package/dist/types/clients/modules/{rest.d.ts → rest/index.d.ts} +2 -2
- package/dist/types/clients/modules/rest/index.d.ts.map +1 -0
- package/dist/types/clients/modules/tokens/index.d.ts +6 -0
- package/dist/types/clients/modules/tokens/index.d.ts.map +1 -0
- package/dist/types/clients/modules/tokens/types.d.ts +8 -0
- package/dist/types/clients/modules/tokens/types.d.ts.map +1 -0
- package/dist/types/clients/modules/{trade.simulation.d.ts → trade.simulation/index.d.ts} +3 -3
- package/dist/types/clients/modules/trade.simulation/index.d.ts.map +1 -0
- package/dist/types/clients/modules/trade.simulation/types.d.ts +19 -0
- package/dist/types/clients/modules/trade.simulation/types.d.ts.map +1 -0
- package/dist/types/clients/types.d.ts +1 -300
- package/dist/types/clients/types.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +6 -7
- package/src/clients/api-client.ts +17 -0
- package/src/clients/modules/{account.ts → account/index.ts} +3 -3
- package/src/clients/modules/account/types.ts +74 -0
- package/src/clients/modules/{lp.ts → lp/index.ts} +5 -3
- package/src/clients/modules/lp/types.ts +62 -0
- package/src/clients/modules/{markets.ts → markets/index.ts} +2 -2
- package/src/clients/modules/markets/types.ts +33 -0
- package/src/clients/modules/{rest.ts → rest/index.ts} +8 -3
- package/src/clients/modules/tokens/index.ts +11 -0
- package/src/clients/modules/tokens/types.ts +9 -0
- package/src/clients/modules/{trade.simulation.ts → trade.simulation/index.ts} +3 -4
- package/src/clients/modules/trade.simulation/types.ts +21 -0
- package/src/clients/types.ts +16 -388
- package/src/index.ts +0 -2
- package/dist/clients/helpers/exposure.calculator.js +0 -448
- package/dist/clients/helpers/exposure.calculator.js.map +0 -1
- package/dist/clients/helpers/number.js +0 -13
- package/dist/clients/helpers/number.js.map +0 -1
- package/dist/clients/helpers/trade.simulation.types.js.map +0 -1
- package/dist/clients/modules/account.js.map +0 -1
- package/dist/clients/modules/lp.js.map +0 -1
- package/dist/clients/modules/markets.js.map +0 -1
- package/dist/clients/modules/rest.js.map +0 -1
- package/dist/clients/modules/trade.simulation.js.map +0 -1
- package/dist/types/clients/helpers/exposure.calculator.d.ts +0 -58
- package/dist/types/clients/helpers/exposure.calculator.d.ts.map +0 -1
- package/dist/types/clients/helpers/number.d.ts +0 -3
- package/dist/types/clients/helpers/number.d.ts.map +0 -1
- package/dist/types/clients/helpers/trade.simulation.types.d.ts +0 -113
- package/dist/types/clients/helpers/trade.simulation.types.d.ts.map +0 -1
- package/dist/types/clients/modules/account.d.ts.map +0 -1
- package/dist/types/clients/modules/lp.d.ts.map +0 -1
- package/dist/types/clients/modules/markets.d.ts.map +0 -1
- package/dist/types/clients/modules/rest.d.ts.map +0 -1
- package/dist/types/clients/modules/trade.simulation.d.ts.map +0 -1
- package/src/clients/helpers/exposure.calculator.ts +0 -799
- package/src/clients/helpers/number.ts +0 -8
- package/src/clients/helpers/trade.simulation.types.ts +0 -125
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
-
if (ar || !(i in from)) {
|
|
5
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
-
ar[i] = from[i];
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.ExposureCommand = void 0;
|
|
16
|
-
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
|
-
var lodash_1 = __importDefault(require("lodash"));
|
|
18
|
-
var number_1 = require("./number");
|
|
19
|
-
var ExposureCommand = /** @class */ (function () {
|
|
20
|
-
function ExposureCommand(rootCollateralPoolId, oraclePricePerMarket, accountBalancePerAsset, groupedByCollateral, riskMultipliers, riskMatrices, exchangeInfoPerAsset, positionInfoMarketConfiguration, uniqueTokenAddresses, uniqueQuoteCollaterals, tokenMarginInfoPerAsset, realizedPnLSum, unrealizedPnLSum) {
|
|
21
|
-
this.rootCollateralPoolId = rootCollateralPoolId;
|
|
22
|
-
this.oraclePricePerMarket = oraclePricePerMarket;
|
|
23
|
-
this.accountBalancePerAsset = accountBalancePerAsset;
|
|
24
|
-
this.groupedByCollateral = groupedByCollateral;
|
|
25
|
-
this.riskMultipliers = riskMultipliers;
|
|
26
|
-
this.riskMatrices = riskMatrices;
|
|
27
|
-
this.exchangeInfoPerAsset = exchangeInfoPerAsset;
|
|
28
|
-
this.positionInfoMarketConfiguration = positionInfoMarketConfiguration;
|
|
29
|
-
this.uniqueTokenAddresses = uniqueTokenAddresses;
|
|
30
|
-
this.uniqueQuoteCollaterals = uniqueQuoteCollaterals;
|
|
31
|
-
this.tokenMarginInfoPerAsset = tokenMarginInfoPerAsset;
|
|
32
|
-
this.realizedPnLSum = realizedPnLSum;
|
|
33
|
-
this.unrealizedPnLSum = unrealizedPnLSum;
|
|
34
|
-
}
|
|
35
|
-
ExposureCommand.prototype.getState = function () {
|
|
36
|
-
return {
|
|
37
|
-
rootCollateralPoolId: this.rootCollateralPoolId,
|
|
38
|
-
oraclePricePerMarket: this.oraclePricePerMarket,
|
|
39
|
-
accountBalancePerAsset: this.accountBalancePerAsset,
|
|
40
|
-
groupedByCollateral: this.groupedByCollateral,
|
|
41
|
-
riskMultipliers: this.riskMultipliers,
|
|
42
|
-
riskMatrices: this.riskMatrices,
|
|
43
|
-
exchangeInfoPerAsset: this.exchangeInfoPerAsset,
|
|
44
|
-
positionInfoMarketConfiguration: this.positionInfoMarketConfiguration,
|
|
45
|
-
uniqueTokenAddresses: this.uniqueTokenAddresses,
|
|
46
|
-
uniqueQuoteCollaterals: this.uniqueQuoteCollaterals,
|
|
47
|
-
tokenMarginInfoPerAsset: this.tokenMarginInfoPerAsset,
|
|
48
|
-
realizedPnLSum: this.realizedPnLSum,
|
|
49
|
-
unrealizedPnLSum: this.unrealizedPnLSum,
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
Object.defineProperty(ExposureCommand.prototype, "getUsdNodeMarginInfo", {
|
|
53
|
-
get: function () {
|
|
54
|
-
return ExposureCommand.getUsdNodeMarginInfo(this.rootCollateralPoolId, this.uniqueTokenAddresses, this.exchangeInfoPerAsset, this.tokenMarginInfoPerAsset);
|
|
55
|
-
},
|
|
56
|
-
enumerable: false,
|
|
57
|
-
configurable: true
|
|
58
|
-
});
|
|
59
|
-
Object.defineProperty(ExposureCommand.prototype, "balancePerAsset", {
|
|
60
|
-
get: function () {
|
|
61
|
-
return this.tokenMarginInfoPerAsset;
|
|
62
|
-
},
|
|
63
|
-
enumerable: false,
|
|
64
|
-
configurable: true
|
|
65
|
-
});
|
|
66
|
-
Object.defineProperty(ExposureCommand.prototype, "exchangePricePerAsset", {
|
|
67
|
-
get: function () {
|
|
68
|
-
return this.exchangeInfoPerAsset;
|
|
69
|
-
},
|
|
70
|
-
enumerable: false,
|
|
71
|
-
configurable: true
|
|
72
|
-
});
|
|
73
|
-
ExposureCommand.prototype.getUsdNodeMarginInfoPostTrade = function (positionAmount, collateralAddress, marketConfiguration) {
|
|
74
|
-
// perform deep copy of the object
|
|
75
|
-
var positionInfoMarketConfiguration = lodash_1.default.cloneDeep(this.positionInfoMarketConfiguration);
|
|
76
|
-
// Check if the market_id already exists in the array
|
|
77
|
-
var existingConfigIndex = positionInfoMarketConfiguration.findIndex(function (config) {
|
|
78
|
-
return config.market_id ===
|
|
79
|
-
(0, bignumber_js_1.default)(String(marketConfiguration.market_id)).toNumber();
|
|
80
|
-
});
|
|
81
|
-
if (existingConfigIndex !== -1) {
|
|
82
|
-
// If it exists, update the amount
|
|
83
|
-
positionInfoMarketConfiguration[existingConfigIndex].base = (0, bignumber_js_1.default)(positionInfoMarketConfiguration[existingConfigIndex].base).plus(positionAmount);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
// If it doesn't exist, add a new element
|
|
87
|
-
positionInfoMarketConfiguration.push({
|
|
88
|
-
base: (0, bignumber_js_1.default)(positionAmount),
|
|
89
|
-
realized_pnl: (0, bignumber_js_1.default)(0),
|
|
90
|
-
last_price: (0, bignumber_js_1.default)(0),
|
|
91
|
-
last_timestamp: (0, bignumber_js_1.default)(0),
|
|
92
|
-
funding_value: (0, bignumber_js_1.default)(0),
|
|
93
|
-
base_multiplier: (0, bignumber_js_1.default)(0),
|
|
94
|
-
adl_unwind_price: (0, bignumber_js_1.default)(0),
|
|
95
|
-
market_id: (0, bignumber_js_1.default)(String(marketConfiguration.market_id)).toNumber(),
|
|
96
|
-
market_configuration: marketConfiguration,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
var uniqueQuoteCollaterals = new Set(this.uniqueQuoteCollaterals);
|
|
100
|
-
uniqueQuoteCollaterals.add(collateralAddress);
|
|
101
|
-
var tokenMarginInfoPerAsset = ExposureCommand.calculateTokenMarginInfoPerAsset(this.groupedByCollateral, this.rootCollateralPoolId, this.riskMatrices, this.riskMultipliers, uniqueQuoteCollaterals, this.realizedPnLSum, this.unrealizedPnLSum, positionInfoMarketConfiguration, this.oraclePricePerMarket);
|
|
102
|
-
var uniqueTokenAddresses = __spreadArray([], this.uniqueTokenAddresses, true);
|
|
103
|
-
if (!this.uniqueTokenAddresses.includes(collateralAddress)) {
|
|
104
|
-
uniqueTokenAddresses.push(collateralAddress);
|
|
105
|
-
}
|
|
106
|
-
return ExposureCommand.getUsdNodeMarginInfo(this.rootCollateralPoolId, uniqueTokenAddresses, this.exchangeInfoPerAsset, tokenMarginInfoPerAsset);
|
|
107
|
-
};
|
|
108
|
-
ExposureCommand.calculateTokenMarginInfoPerAsset = function (groupedByCollateral, rootCollateralPoolId, riskMatrices, riskMultipliers, uniqueQuoteCollaterals, realizedPnLSum, unrealizedPnLSum, positionInfoMarketConfiguration, oraclePricePerMarket) {
|
|
109
|
-
var _a;
|
|
110
|
-
var tokenMarginInfoPerAsset = [];
|
|
111
|
-
var uniqueQuoteTokens = Array.from(uniqueQuoteCollaterals);
|
|
112
|
-
var tokenUnion = new Set(__spreadArray(__spreadArray([], Object.keys(groupedByCollateral), true), uniqueQuoteTokens, true)); // get unique union of those arrays
|
|
113
|
-
var uniqueTokenAddresses = Array.from(tokenUnion);
|
|
114
|
-
for (var _i = 0, uniqueTokenAddresses_1 = uniqueTokenAddresses; _i < uniqueTokenAddresses_1.length; _i++) {
|
|
115
|
-
var token = uniqueTokenAddresses_1[_i];
|
|
116
|
-
tokenMarginInfoPerAsset.push(ExposureCommand.getTokenMarginInfo(rootCollateralPoolId, riskMatrices, riskMultipliers, ExposureCommand.getCollateralInfo(token, uniqueQuoteCollaterals.has(token) ? realizedPnLSum : (0, bignumber_js_1.default)(0), uniqueQuoteCollaterals.has(token) ? unrealizedPnLSum : (0, bignumber_js_1.default)(0), ((_a = groupedByCollateral[token]) === null || _a === void 0 ? void 0 : _a.amount) || 0), token, positionInfoMarketConfiguration, oraclePricePerMarket, uniqueQuoteTokens));
|
|
117
|
-
}
|
|
118
|
-
return tokenMarginInfoPerAsset;
|
|
119
|
-
};
|
|
120
|
-
ExposureCommand.calculateLiquidation = function (globalMarginInfo, oraclePrice, positionBase) {
|
|
121
|
-
var liquidationPrice = (0, bignumber_js_1.default)(oraclePrice).minus((0, bignumber_js_1.default)(globalMarginInfo.marginBalance)
|
|
122
|
-
.minus(globalMarginInfo.liquidationMarginRequirement)
|
|
123
|
-
.div(positionBase));
|
|
124
|
-
return bignumber_js_1.default.max(0, liquidationPrice);
|
|
125
|
-
};
|
|
126
|
-
ExposureCommand.calculateImpliedLeverage = function (notionalExposure, oldIMR, newIMR) {
|
|
127
|
-
var changeInImr = (0, bignumber_js_1.default)(newIMR).minus(oldIMR);
|
|
128
|
-
if (changeInImr.eq(0)) {
|
|
129
|
-
return 0;
|
|
130
|
-
}
|
|
131
|
-
return (0, bignumber_js_1.default)(notionalExposure).div(changeInImr).toNumber();
|
|
132
|
-
};
|
|
133
|
-
ExposureCommand.combineMarginInfo = function (parentMarginInfo, sonMarginInfo, sonParentExchangeInfo) {
|
|
134
|
-
return {
|
|
135
|
-
assetAddress: parentMarginInfo.assetAddress,
|
|
136
|
-
marginBalance: (0, bignumber_js_1.default)(parentMarginInfo.marginBalance)
|
|
137
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(sonMarginInfo.marginBalance, sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
138
|
-
.toNumber(),
|
|
139
|
-
realBalance: (0, bignumber_js_1.default)(parentMarginInfo.realBalance)
|
|
140
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(sonMarginInfo.realBalance, sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
141
|
-
.toNumber(),
|
|
142
|
-
initialDelta: (0, bignumber_js_1.default)(parentMarginInfo.initialDelta)
|
|
143
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(bignumber_js_1.default.min(sonMarginInfo.realBalance, sonMarginInfo.initialDelta).toNumber(), sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
144
|
-
.toNumber(),
|
|
145
|
-
maintenanceDelta: (0, bignumber_js_1.default)(parentMarginInfo.maintenanceDelta)
|
|
146
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(bignumber_js_1.default.min(sonMarginInfo.maintenanceDelta, sonMarginInfo.realBalance).toNumber(), sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
147
|
-
.toNumber(),
|
|
148
|
-
liquidationDelta: (0, bignumber_js_1.default)(parentMarginInfo.liquidationDelta)
|
|
149
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(bignumber_js_1.default.min(sonMarginInfo.liquidationDelta, sonMarginInfo.realBalance).toNumber(), sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
150
|
-
.toNumber(),
|
|
151
|
-
dutchDelta: (0, bignumber_js_1.default)(parentMarginInfo.dutchDelta)
|
|
152
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(bignumber_js_1.default.min(sonMarginInfo.dutchDelta, sonMarginInfo.realBalance).toNumber(), sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
153
|
-
.toNumber(),
|
|
154
|
-
adlDelta: (0, bignumber_js_1.default)(parentMarginInfo.adlDelta)
|
|
155
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(bignumber_js_1.default.min(sonMarginInfo.adlDelta, sonMarginInfo.realBalance).toNumber(), sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
156
|
-
.toNumber(),
|
|
157
|
-
initialBufferDelta: (0, bignumber_js_1.default)(parentMarginInfo.initialBufferDelta)
|
|
158
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(bignumber_js_1.default.min(sonMarginInfo.initialBufferDelta, sonMarginInfo.realBalance).toNumber(), sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
159
|
-
.toNumber(),
|
|
160
|
-
liquidationMarginRequirement: (0, bignumber_js_1.default)(parentMarginInfo.liquidationMarginRequirement)
|
|
161
|
-
.plus(ExposureCommand.exchangeWithPriceHaircut(sonMarginInfo.liquidationMarginRequirement, sonParentExchangeInfo.price, sonParentExchangeInfo.priceHaircut))
|
|
162
|
-
.toNumber(),
|
|
163
|
-
};
|
|
164
|
-
};
|
|
165
|
-
ExposureCommand.getUsdNodeMarginInfo = function (accountCollateralPoolId, quoteTokens, exchangeInfoPerAsset, marginInfoPerToken) {
|
|
166
|
-
var usdNodeMarginInfo = {
|
|
167
|
-
assetAddress: '',
|
|
168
|
-
marginBalance: 0,
|
|
169
|
-
realBalance: 0,
|
|
170
|
-
initialDelta: 0,
|
|
171
|
-
maintenanceDelta: 0,
|
|
172
|
-
liquidationDelta: 0,
|
|
173
|
-
dutchDelta: 0,
|
|
174
|
-
adlDelta: 0,
|
|
175
|
-
initialBufferDelta: 0,
|
|
176
|
-
liquidationMarginRequirement: 0,
|
|
177
|
-
};
|
|
178
|
-
var _loop_1 = function (quoteToken) {
|
|
179
|
-
var exchangeInfo = exchangeInfoPerAsset.find(function (exchangeInfo) {
|
|
180
|
-
return quoteToken === exchangeInfo.tokenAddress;
|
|
181
|
-
});
|
|
182
|
-
var marginInfo = marginInfoPerToken.find(function (marginInfo) {
|
|
183
|
-
return quoteToken === marginInfo.assetAddress;
|
|
184
|
-
});
|
|
185
|
-
if (!exchangeInfo || !marginInfo) {
|
|
186
|
-
throw Error('Missing exchangeInfo/marginInfo');
|
|
187
|
-
}
|
|
188
|
-
usdNodeMarginInfo = ExposureCommand.combineMarginInfo(usdNodeMarginInfo, marginInfo, exchangeInfo);
|
|
189
|
-
};
|
|
190
|
-
for (var _i = 0, quoteTokens_1 = quoteTokens; _i < quoteTokens_1.length; _i++) {
|
|
191
|
-
var quoteToken = quoteTokens_1[_i];
|
|
192
|
-
_loop_1(quoteToken);
|
|
193
|
-
}
|
|
194
|
-
return usdNodeMarginInfo;
|
|
195
|
-
};
|
|
196
|
-
ExposureCommand.getCollateralInfo = function (collateralAddress, realisedPnl, unrealizedPnL, netDeposits) {
|
|
197
|
-
return {
|
|
198
|
-
netDeposits: netDeposits,
|
|
199
|
-
marginBalance: (0, bignumber_js_1.default)(netDeposits)
|
|
200
|
-
.plus(realisedPnl)
|
|
201
|
-
.plus(unrealizedPnL)
|
|
202
|
-
.toNumber(),
|
|
203
|
-
realBalance: (0, bignumber_js_1.default)(netDeposits).plus(realisedPnl).toNumber(),
|
|
204
|
-
};
|
|
205
|
-
};
|
|
206
|
-
ExposureCommand.getTokenMarginInfo = function (rootCollateralPoolId, riskMatrices, riskMultipliers, collateralInfo, collateralAddress, positions, oraclePricePerMarket, uniqueQuoteTokens) {
|
|
207
|
-
var marginRequirements = {
|
|
208
|
-
liquidationMarginRequirement: 0,
|
|
209
|
-
initialMarginRequirement: 0,
|
|
210
|
-
maintenanceMarginRequirement: 0,
|
|
211
|
-
dutchMarginRequirement: 0,
|
|
212
|
-
adlMarginRequirement: 0,
|
|
213
|
-
initialBufferMarginRequirement: 0,
|
|
214
|
-
};
|
|
215
|
-
if (uniqueQuoteTokens.includes(collateralAddress)) {
|
|
216
|
-
// uniqueQuoteTokens is list is active markets tokens
|
|
217
|
-
for (var _i = 0, riskMatrices_1 = riskMatrices; _i < riskMatrices_1.length; _i++) {
|
|
218
|
-
var riskMatrix = riskMatrices_1[_i];
|
|
219
|
-
if (Number(riskMatrix.risk_block_id) === 1)
|
|
220
|
-
continue; // @todo remove and implement correct logic!
|
|
221
|
-
var filledExposures = ExposureCommand.getBlockExposures(positions, oraclePricePerMarket);
|
|
222
|
-
marginRequirements.liquidationMarginRequirement = (0, bignumber_js_1.default)(marginRequirements.liquidationMarginRequirement)
|
|
223
|
-
.plus(ExposureCommand.computeLiquidationMarginRequirement(riskMatrix.matrix, filledExposures))
|
|
224
|
-
.toNumber();
|
|
225
|
-
}
|
|
226
|
-
// Get the initial margin requirement
|
|
227
|
-
marginRequirements.initialMarginRequirement = (0, number_1.amountNormalizer)(String(riskMultipliers.im_multiplier))
|
|
228
|
-
.multipliedBy(marginRequirements.liquidationMarginRequirement)
|
|
229
|
-
.toNumber();
|
|
230
|
-
// Get the maintenance margin requirement
|
|
231
|
-
marginRequirements.maintenanceMarginRequirement = (0, number_1.amountNormalizer)(String(riskMultipliers.mmr_multiplier))
|
|
232
|
-
.multipliedBy(marginRequirements.liquidationMarginRequirement)
|
|
233
|
-
.toNumber();
|
|
234
|
-
// Get the dutch margin requirement
|
|
235
|
-
marginRequirements.dutchMarginRequirement = (0, number_1.amountNormalizer)(String(riskMultipliers.dutch_multiplier))
|
|
236
|
-
.multipliedBy(marginRequirements.liquidationMarginRequirement)
|
|
237
|
-
.toNumber();
|
|
238
|
-
// Get the adl margin requirement
|
|
239
|
-
marginRequirements.adlMarginRequirement = (0, number_1.amountNormalizer)(String(riskMultipliers.adl_multiplier))
|
|
240
|
-
.multipliedBy(marginRequirements.liquidationMarginRequirement)
|
|
241
|
-
.toNumber();
|
|
242
|
-
// Get the initial buffer margin requirement
|
|
243
|
-
marginRequirements.initialBufferMarginRequirement = (0, number_1.amountNormalizer)(String(riskMultipliers.im_buffer_multiplier))
|
|
244
|
-
.multipliedBy(marginRequirements.liquidationMarginRequirement)
|
|
245
|
-
.toNumber();
|
|
246
|
-
}
|
|
247
|
-
return {
|
|
248
|
-
assetAddress: collateralAddress,
|
|
249
|
-
marginBalance: collateralInfo.marginBalance,
|
|
250
|
-
realBalance: collateralInfo.realBalance,
|
|
251
|
-
initialDelta: (0, bignumber_js_1.default)(collateralInfo.marginBalance)
|
|
252
|
-
.minus(marginRequirements.initialMarginRequirement)
|
|
253
|
-
.toNumber(),
|
|
254
|
-
maintenanceDelta: (0, bignumber_js_1.default)(collateralInfo.marginBalance)
|
|
255
|
-
.minus(marginRequirements.maintenanceMarginRequirement)
|
|
256
|
-
.toNumber(),
|
|
257
|
-
liquidationDelta: (0, bignumber_js_1.default)(collateralInfo.marginBalance)
|
|
258
|
-
.minus(marginRequirements.liquidationMarginRequirement)
|
|
259
|
-
.toNumber(),
|
|
260
|
-
dutchDelta: (0, bignumber_js_1.default)(collateralInfo.marginBalance)
|
|
261
|
-
.minus(marginRequirements.dutchMarginRequirement)
|
|
262
|
-
.toNumber(),
|
|
263
|
-
adlDelta: (0, bignumber_js_1.default)(collateralInfo.marginBalance)
|
|
264
|
-
.minus(marginRequirements.adlMarginRequirement)
|
|
265
|
-
.toNumber(),
|
|
266
|
-
initialBufferDelta: (0, bignumber_js_1.default)(collateralInfo.marginBalance)
|
|
267
|
-
.minus(marginRequirements.initialBufferMarginRequirement)
|
|
268
|
-
.toNumber(),
|
|
269
|
-
liquidationMarginRequirement: marginRequirements.liquidationMarginRequirement,
|
|
270
|
-
};
|
|
271
|
-
};
|
|
272
|
-
ExposureCommand.computeLiquidationMarginRequirement = function (matrix, filledExposures) {
|
|
273
|
-
var lmrFilledSquared = 0;
|
|
274
|
-
for (var i = 0; i < filledExposures.length; i++) {
|
|
275
|
-
if ((0, bignumber_js_1.default)(filledExposures[i]).eq(0)) {
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
for (var j = 0; j < filledExposures.length; j++) {
|
|
279
|
-
var riskParam = matrix[i][j];
|
|
280
|
-
if ((0, bignumber_js_1.default)(filledExposures[j]).eq(0) || (0, bignumber_js_1.default)(riskParam).eq(0)) {
|
|
281
|
-
continue;
|
|
282
|
-
}
|
|
283
|
-
lmrFilledSquared = (0, bignumber_js_1.default)(lmrFilledSquared)
|
|
284
|
-
.plus((0, bignumber_js_1.default)(filledExposures[i])
|
|
285
|
-
.multipliedBy(filledExposures[j])
|
|
286
|
-
.multipliedBy(riskParam))
|
|
287
|
-
.toNumber();
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return (0, bignumber_js_1.default)(lmrFilledSquared).sqrt().toNumber();
|
|
291
|
-
};
|
|
292
|
-
ExposureCommand.getBlockExposures = function (positions, oraclePricePerMarket) {
|
|
293
|
-
var filledExposures = [];
|
|
294
|
-
for (var _i = 0, positions_1 = positions; _i < positions_1.length; _i++) {
|
|
295
|
-
var position = positions_1[_i];
|
|
296
|
-
var marketFilledExposure = ExposureCommand.getAccountFilledExposures(position, position.market_configuration, oraclePricePerMarket[position.market_id]);
|
|
297
|
-
filledExposures[marketFilledExposure.riskMatrixIndex] = (0, bignumber_js_1.default)(filledExposures[marketFilledExposure.riskMatrixIndex] || 0)
|
|
298
|
-
.plus(marketFilledExposure.exposure)
|
|
299
|
-
.toNumber();
|
|
300
|
-
}
|
|
301
|
-
return filledExposures.map(function (num) { return (0, bignumber_js_1.default)(num); });
|
|
302
|
-
};
|
|
303
|
-
ExposureCommand.getAccountFilledExposures = function (position, marketConfiguration, oraclePrice) {
|
|
304
|
-
var base = position.base;
|
|
305
|
-
return {
|
|
306
|
-
exposure: (0, bignumber_js_1.default)(oraclePrice).multipliedBy(base),
|
|
307
|
-
riskMatrixIndex: (0, bignumber_js_1.default)(String(marketConfiguration.risk_matrix_index)).toNumber(),
|
|
308
|
-
};
|
|
309
|
-
};
|
|
310
|
-
ExposureCommand.computePricePnL = function (openBase, openPrice, exitPrice) {
|
|
311
|
-
return (0, bignumber_js_1.default)((0, bignumber_js_1.default)(exitPrice).minus(openPrice).multipliedBy(openBase));
|
|
312
|
-
};
|
|
313
|
-
ExposureCommand.getMarginRatio = function (marginInfo) {
|
|
314
|
-
if (marginInfo.liquidationMarginRequirement === 0) {
|
|
315
|
-
return 0;
|
|
316
|
-
}
|
|
317
|
-
if (marginInfo.marginBalance <= 0) {
|
|
318
|
-
return 1;
|
|
319
|
-
}
|
|
320
|
-
var health = (0, bignumber_js_1.default)(marginInfo.liquidationMarginRequirement).div(marginInfo.marginBalance);
|
|
321
|
-
if (health.gt(1)) {
|
|
322
|
-
return 1;
|
|
323
|
-
}
|
|
324
|
-
return health.toNumber();
|
|
325
|
-
};
|
|
326
|
-
ExposureCommand.exchangeWithPriceHaircut = function (quantity, price, haircut) {
|
|
327
|
-
// For positive quantities, the haircut is `quantity * (1 - haircut)`
|
|
328
|
-
// For negative values, the haircut is `quantity / (1 - haircut)` because a negative value means the haircut should be applied from B to A.
|
|
329
|
-
var calHelper = (0, bignumber_js_1.default)(quantity).gt(0)
|
|
330
|
-
? (0, bignumber_js_1.default)(1).minus(haircut)
|
|
331
|
-
: (0, bignumber_js_1.default)(1).div((0, bignumber_js_1.default)(1).minus(haircut));
|
|
332
|
-
var haircutPrice = (0, bignumber_js_1.default)(price).multipliedBy(calHelper);
|
|
333
|
-
return haircutPrice.multipliedBy(quantity).toNumber();
|
|
334
|
-
};
|
|
335
|
-
ExposureCommand.prototype.getSlippage = function (deltaBase, marketConfiguration, marketStorage) {
|
|
336
|
-
var deltaExposure = (0, bignumber_js_1.default)(this.oraclePricePerMarket[marketConfiguration.market_id])
|
|
337
|
-
.times(deltaBase)
|
|
338
|
-
.toNumber();
|
|
339
|
-
var riskMatrixIndex = (0, bignumber_js_1.default)(String(marketConfiguration.risk_matrix_index)).toNumber();
|
|
340
|
-
var _a = this.getMaxExposure(marketConfiguration, marketStorage), maxExposureShort = _a.maxExposureShort, maxExposureLong = _a.maxExposureLong, exposures = _a.exposures;
|
|
341
|
-
var netExposure = exposures[riskMatrixIndex].plus(deltaExposure);
|
|
342
|
-
var maxExposure = netExposure.lt(0) ? maxExposureShort : maxExposureLong;
|
|
343
|
-
return (0, bignumber_js_1.default)(netExposure)
|
|
344
|
-
.negated()
|
|
345
|
-
.div((0, bignumber_js_1.default)(maxExposure).plus(netExposure))
|
|
346
|
-
.toNumber();
|
|
347
|
-
};
|
|
348
|
-
ExposureCommand.prototype.getMaxExposure = function (marketConfiguration, marketStorage) {
|
|
349
|
-
var riskMatrix = this.riskMatrices.find(function (riskMatrix) {
|
|
350
|
-
return (riskMatrix.risk_block_id ===
|
|
351
|
-
(0, bignumber_js_1.default)(String(marketStorage.risk_block_id)).toNumber());
|
|
352
|
-
});
|
|
353
|
-
if (!riskMatrix) {
|
|
354
|
-
throw new Error("RiskMatrix Doesn't exist");
|
|
355
|
-
}
|
|
356
|
-
var riskMatrixIndex = (0, bignumber_js_1.default)(String(marketConfiguration.risk_matrix_index)).toNumber();
|
|
357
|
-
var imrMultiplier = (0, number_1.amountNormalizer)(String(this.riskMultipliers.im_multiplier)).toNumber();
|
|
358
|
-
var marginInfo = this.tokenMarginInfoPerAsset.find(function (marginInfo) {
|
|
359
|
-
return marginInfo.assetAddress === marketStorage.quote_collateral;
|
|
360
|
-
});
|
|
361
|
-
if (!marginInfo) {
|
|
362
|
-
throw new Error("marginInfo doesn't exist");
|
|
363
|
-
}
|
|
364
|
-
var exposures = ExposureCommand.getBlockExposures(this.positionInfoMarketConfiguration, this.oraclePricePerMarket);
|
|
365
|
-
var _a = ExposureCommand.computeMaxExposures(riskMatrix.matrix, exposures, marginInfo.liquidationMarginRequirement, marginInfo.marginBalance < 0 ? 0 : marginInfo.marginBalance, imrMultiplier, riskMatrixIndex), maxExposureShort = _a.maxExposureShort, maxExposureLong = _a.maxExposureLong;
|
|
366
|
-
return {
|
|
367
|
-
maxExposureShort: maxExposureShort,
|
|
368
|
-
maxExposureLong: maxExposureLong,
|
|
369
|
-
exposures: exposures,
|
|
370
|
-
};
|
|
371
|
-
};
|
|
372
|
-
ExposureCommand.computeMaxExposures = function (riskMatrix, exposures, lmr, balance, imrMultiplier, exposureIndex) {
|
|
373
|
-
var b = (0, bignumber_js_1.default)(0);
|
|
374
|
-
for (var i = 0; i < exposures.length; i++) {
|
|
375
|
-
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])));
|
|
376
|
-
}
|
|
377
|
-
var _a = this.solveQuadraticEquation((0, bignumber_js_1.default)(riskMatrix[exposureIndex][exposureIndex]).toNumber(), // changes here
|
|
378
|
-
b.toNumber(), this.computeC(lmr, balance, imrMultiplier)), x1 = _a.x1, x2 = _a.x2;
|
|
379
|
-
var maxShortExposure = (0, bignumber_js_1.default)(x1).plus(exposures[exposureIndex]);
|
|
380
|
-
var maxLongExposure = (0, bignumber_js_1.default)(x2).plus(exposures[exposureIndex]);
|
|
381
|
-
var availableShortExposure = maxShortExposure.lt(0)
|
|
382
|
-
? maxShortExposure.negated().toNumber()
|
|
383
|
-
: 0;
|
|
384
|
-
var availableLongExposure = maxLongExposure.gt(0)
|
|
385
|
-
? maxLongExposure.toNumber()
|
|
386
|
-
: 0;
|
|
387
|
-
return {
|
|
388
|
-
maxExposureShort: availableShortExposure,
|
|
389
|
-
maxExposureLong: availableLongExposure,
|
|
390
|
-
};
|
|
391
|
-
};
|
|
392
|
-
ExposureCommand.solveQuadraticEquation = function (a, b, c) {
|
|
393
|
-
if ((0, bignumber_js_1.default)(a).eq(0)) {
|
|
394
|
-
throw new Error('ZeroQuadraticCoefficient');
|
|
395
|
-
}
|
|
396
|
-
var delta = (0, bignumber_js_1.default)(b)
|
|
397
|
-
.multipliedBy(b)
|
|
398
|
-
.minus((0, bignumber_js_1.default)(4).multipliedBy(a).multipliedBy(c));
|
|
399
|
-
if (delta.lt(0)) {
|
|
400
|
-
throw new Error('ComplexQuadraticRoots(a, b, c)');
|
|
401
|
-
}
|
|
402
|
-
var rootDelta = delta.sqrt();
|
|
403
|
-
var x1 = (0, bignumber_js_1.default)(b)
|
|
404
|
-
.multipliedBy(-1)
|
|
405
|
-
.minus(rootDelta)
|
|
406
|
-
.div((0, bignumber_js_1.default)(2).multipliedBy(a));
|
|
407
|
-
var x2 = (0, bignumber_js_1.default)(b)
|
|
408
|
-
.multipliedBy(-1)
|
|
409
|
-
.plus(rootDelta)
|
|
410
|
-
.div((0, bignumber_js_1.default)(2).multipliedBy(a));
|
|
411
|
-
return {
|
|
412
|
-
x1: x1,
|
|
413
|
-
x2: x2,
|
|
414
|
-
};
|
|
415
|
-
};
|
|
416
|
-
ExposureCommand.computeC = function (lmr, balance, imrMultiplier) {
|
|
417
|
-
var lmrSD = (0, bignumber_js_1.default)(lmr);
|
|
418
|
-
var lmrSquared = lmrSD.multipliedBy(lmrSD);
|
|
419
|
-
var balanceSD = (0, bignumber_js_1.default)(balance);
|
|
420
|
-
var balanceSquared = balanceSD.multipliedBy(balanceSD);
|
|
421
|
-
var imrMultiplierSD = (0, bignumber_js_1.default)(imrMultiplier);
|
|
422
|
-
var imrMultiplierSquared = imrMultiplierSD.multipliedBy(imrMultiplierSD);
|
|
423
|
-
return lmrSquared
|
|
424
|
-
.minus(balanceSquared.div(imrMultiplierSquared))
|
|
425
|
-
.toNumber();
|
|
426
|
-
};
|
|
427
|
-
ExposureCommand.calculateFee = function (price, amount, feeParameter) {
|
|
428
|
-
return (0, bignumber_js_1.default)(price).times(amount).times(feeParameter).abs().toNumber(); // @todo abs value
|
|
429
|
-
};
|
|
430
|
-
ExposureCommand.calculateEstimatedPrice = function (price, slippage) {
|
|
431
|
-
return (0, bignumber_js_1.default)(price).times((0, bignumber_js_1.default)(1).plus(slippage)).toNumber();
|
|
432
|
-
};
|
|
433
|
-
ExposureCommand.evaluateHealthStatus = function (number) {
|
|
434
|
-
// todo update logic
|
|
435
|
-
if (number >= 67) {
|
|
436
|
-
return 'danger';
|
|
437
|
-
}
|
|
438
|
-
else if (number >= 34) {
|
|
439
|
-
return 'warning';
|
|
440
|
-
}
|
|
441
|
-
else {
|
|
442
|
-
return 'healthy';
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
return ExposureCommand;
|
|
446
|
-
}());
|
|
447
|
-
exports.ExposureCommand = ExposureCommand;
|
|
448
|
-
//# sourceMappingURL=exposure.calculator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"exposure.calculator.js","sourceRoot":"/","sources":["clients/helpers/exposure.calculator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,8DAAqC;AACrC,kDAAuB;AAevB,mCAA4C;AAE5C;IAcE,yBACE,oBAA4B,EAC5B,oBAA8C,EAC9C,sBAA6C,EAC7C,mBAAwD,EACxD,eAA6C,EAC7C,YAA0B,EAC1B,oBAAoC,EACpC,+BAAkE,EAClE,oBAA8B,EAC9B,sBAAgC,EAChC,uBAAqC,EACrC,cAAyB,EACzB,gBAA2B;QAE3B,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QACjD,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QACjD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C,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;IACxE,CAAC;IAEM,oCAAoB,GAA3B,UAA4B,MAAc;QACxC,oBAAoB;QACpB,IAAI,MAAM,IAAI,EAAE,EAAE,CAAC;YACjB,OAAO,QAAQ,CAAC;QAClB,CAAC;aAAM,IAAI,MAAM,IAAI,EAAE,EAAE,CAAC;YACxB,OAAO,SAAS,CAAC;QACnB,CAAC;aAAM,CAAC;YACN,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IACH,sBAAC;AAAD,CAAC,AA5wBD,IA4wBC;AA5wBY,0CAAe","sourcesContent":["import BigNumber from 'bignumber.js';\nimport _ from 'lodash';\nimport {\n AccountAssetBalance,\n CollateralInfo,\n ExchangeInfo,\n ExposureCommandState,\n MarginInfo,\n MarketConfiguration,\n MarketIdToOraclePriceMap,\n MarketStorage,\n PositionInfo,\n PositionInfoMarketConfiguration,\n RiskMatrix,\n RiskMultipliersConfiguration,\n} from './trade.simulation.types';\nimport { amountNormalizer } from './number';\n\nexport class ExposureCommand {\n rootCollateralPoolId: number;\n oraclePricePerMarket: MarketIdToOraclePriceMap;\n accountBalancePerAsset: AccountAssetBalance[];\n groupedByCollateral: Record<string, AccountAssetBalance>;\n riskMultipliers: RiskMultipliersConfiguration;\n riskMatrices: RiskMatrix[];\n exchangeInfoPerAsset: ExchangeInfo[];\n positionInfoMarketConfiguration: PositionInfoMarketConfiguration[];\n uniqueTokenAddresses: string[];\n uniqueQuoteCollaterals: string[];\n tokenMarginInfoPerAsset: MarginInfo[];\n realizedPnLSum: BigNumber;\n unrealizedPnLSum: BigNumber;\n constructor(\n rootCollateralPoolId: number,\n oraclePricePerMarket: MarketIdToOraclePriceMap,\n accountBalancePerAsset: AccountAssetBalance[],\n groupedByCollateral: Record<string, AccountAssetBalance>,\n riskMultipliers: RiskMultipliersConfiguration,\n riskMatrices: RiskMatrix[],\n exchangeInfoPerAsset: ExchangeInfo[],\n positionInfoMarketConfiguration: PositionInfoMarketConfiguration[],\n uniqueTokenAddresses: string[],\n uniqueQuoteCollaterals: string[],\n tokenMarginInfoPerAsset: MarginInfo[],\n realizedPnLSum: BigNumber,\n unrealizedPnLSum: BigNumber,\n ) {\n this.rootCollateralPoolId = rootCollateralPoolId;\n this.oraclePricePerMarket = oraclePricePerMarket;\n this.accountBalancePerAsset = accountBalancePerAsset;\n this.groupedByCollateral = groupedByCollateral;\n this.riskMultipliers = riskMultipliers;\n this.riskMatrices = riskMatrices;\n this.exchangeInfoPerAsset = exchangeInfoPerAsset;\n this.positionInfoMarketConfiguration = positionInfoMarketConfiguration;\n this.uniqueTokenAddresses = uniqueTokenAddresses;\n this.uniqueQuoteCollaterals = uniqueQuoteCollaterals;\n this.tokenMarginInfoPerAsset = tokenMarginInfoPerAsset;\n this.realizedPnLSum = realizedPnLSum;\n this.unrealizedPnLSum = unrealizedPnLSum;\n }\n\n getState(): ExposureCommandState {\n return {\n rootCollateralPoolId: this.rootCollateralPoolId,\n oraclePricePerMarket: this.oraclePricePerMarket,\n accountBalancePerAsset: this.accountBalancePerAsset,\n groupedByCollateral: this.groupedByCollateral,\n riskMultipliers: this.riskMultipliers,\n riskMatrices: this.riskMatrices,\n exchangeInfoPerAsset: this.exchangeInfoPerAsset,\n positionInfoMarketConfiguration: this.positionInfoMarketConfiguration,\n uniqueTokenAddresses: this.uniqueTokenAddresses,\n uniqueQuoteCollaterals: this.uniqueQuoteCollaterals,\n tokenMarginInfoPerAsset: this.tokenMarginInfoPerAsset,\n realizedPnLSum: this.realizedPnLSum,\n unrealizedPnLSum: this.unrealizedPnLSum,\n };\n }\n\n get getUsdNodeMarginInfo() {\n return ExposureCommand.getUsdNodeMarginInfo(\n this.rootCollateralPoolId,\n this.uniqueTokenAddresses,\n this.exchangeInfoPerAsset,\n this.tokenMarginInfoPerAsset,\n );\n }\n\n get balancePerAsset() {\n return this.tokenMarginInfoPerAsset;\n }\n get exchangePricePerAsset() {\n return this.exchangeInfoPerAsset;\n }\n\n getUsdNodeMarginInfoPostTrade(\n positionAmount: number,\n collateralAddress: string,\n marketConfiguration: MarketConfiguration,\n ) {\n // perform deep copy of the object\n const positionInfoMarketConfiguration: PositionInfoMarketConfiguration[] =\n _.cloneDeep(this.positionInfoMarketConfiguration);\n\n // Check if the market_id already exists in the array\n const existingConfigIndex = positionInfoMarketConfiguration.findIndex(\n (config) =>\n config.market_id ===\n BigNumber(String(marketConfiguration.market_id)).toNumber(),\n );\n\n if (existingConfigIndex !== -1) {\n // If it exists, update the amount\n positionInfoMarketConfiguration[existingConfigIndex].base = BigNumber(\n positionInfoMarketConfiguration[existingConfigIndex].base,\n ).plus(positionAmount);\n } else {\n // If it doesn't exist, add a new element\n positionInfoMarketConfiguration.push({\n base: BigNumber(positionAmount),\n realized_pnl: BigNumber(0),\n last_price: BigNumber(0),\n last_timestamp: BigNumber(0),\n funding_value: BigNumber(0),\n base_multiplier: BigNumber(0),\n adl_unwind_price: BigNumber(0),\n market_id: BigNumber(String(marketConfiguration.market_id)).toNumber(),\n market_configuration: marketConfiguration,\n });\n }\n\n const uniqueQuoteCollaterals = new Set(this.uniqueQuoteCollaterals);\n uniqueQuoteCollaterals.add(collateralAddress);\n\n const tokenMarginInfoPerAsset =\n ExposureCommand.calculateTokenMarginInfoPerAsset(\n this.groupedByCollateral,\n this.rootCollateralPoolId,\n this.riskMatrices,\n this.riskMultipliers,\n uniqueQuoteCollaterals,\n this.realizedPnLSum,\n this.unrealizedPnLSum,\n positionInfoMarketConfiguration,\n this.oraclePricePerMarket,\n );\n\n const uniqueTokenAddresses = [...this.uniqueTokenAddresses];\n if (!this.uniqueTokenAddresses.includes(collateralAddress)) {\n uniqueTokenAddresses.push(collateralAddress);\n }\n\n return ExposureCommand.getUsdNodeMarginInfo(\n this.rootCollateralPoolId,\n uniqueTokenAddresses,\n this.exchangeInfoPerAsset,\n tokenMarginInfoPerAsset,\n );\n }\n\n static calculateTokenMarginInfoPerAsset(\n groupedByCollateral: Record<string, AccountAssetBalance>,\n rootCollateralPoolId: number,\n riskMatrices: RiskMatrix[],\n riskMultipliers: RiskMultipliersConfiguration,\n uniqueQuoteCollaterals: Set<string>,\n realizedPnLSum: BigNumber,\n unrealizedPnLSum: BigNumber,\n positionInfoMarketConfiguration: PositionInfoMarketConfiguration[],\n oraclePricePerMarket: MarketIdToOraclePriceMap,\n ): MarginInfo[] {\n const tokenMarginInfoPerAsset: MarginInfo[] = [];\n\n const uniqueQuoteTokens: string[] = Array.from(uniqueQuoteCollaterals);\n\n const tokenUnion = new Set([\n ...Object.keys(groupedByCollateral),\n ...uniqueQuoteTokens,\n ]); // get unique union of those arrays\n const uniqueTokenAddresses: string[] = Array.from(tokenUnion);\n\n for (const token of uniqueTokenAddresses) {\n tokenMarginInfoPerAsset.push(\n ExposureCommand.getTokenMarginInfo(\n rootCollateralPoolId,\n riskMatrices,\n riskMultipliers,\n ExposureCommand.getCollateralInfo(\n token,\n uniqueQuoteCollaterals.has(token) ? realizedPnLSum : BigNumber(0),\n uniqueQuoteCollaterals.has(token) ? unrealizedPnLSum : BigNumber(0),\n groupedByCollateral[token]?.amount || 0,\n ),\n token,\n positionInfoMarketConfiguration,\n oraclePricePerMarket,\n uniqueQuoteTokens,\n ),\n );\n }\n\n return tokenMarginInfoPerAsset;\n }\n static calculateLiquidation(\n globalMarginInfo: MarginInfo,\n oraclePrice: number,\n positionBase: number,\n ): BigNumber {\n const liquidationPrice = BigNumber(oraclePrice).minus(\n BigNumber(globalMarginInfo.marginBalance)\n .minus(globalMarginInfo.liquidationMarginRequirement)\n .div(positionBase),\n );\n\n return BigNumber.max(0, liquidationPrice);\n }\n\n static calculateImpliedLeverage(\n notionalExposure: number,\n oldIMR: number,\n newIMR: number,\n ): number {\n const changeInImr = BigNumber(newIMR).minus(oldIMR);\n\n if (changeInImr.eq(0)) {\n return 0;\n }\n return BigNumber(notionalExposure).div(changeInImr).toNumber();\n }\n\n static combineMarginInfo(\n parentMarginInfo: MarginInfo,\n sonMarginInfo: MarginInfo,\n sonParentExchangeInfo: ExchangeInfo,\n ): MarginInfo {\n return {\n assetAddress: parentMarginInfo.assetAddress,\n marginBalance: BigNumber(parentMarginInfo.marginBalance)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n sonMarginInfo.marginBalance,\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n realBalance: BigNumber(parentMarginInfo.realBalance)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n sonMarginInfo.realBalance,\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n initialDelta: BigNumber(parentMarginInfo.initialDelta)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n BigNumber.min(\n sonMarginInfo.realBalance,\n sonMarginInfo.initialDelta,\n ).toNumber(),\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n maintenanceDelta: BigNumber(parentMarginInfo.maintenanceDelta)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n BigNumber.min(\n sonMarginInfo.maintenanceDelta,\n sonMarginInfo.realBalance,\n ).toNumber(),\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n liquidationDelta: BigNumber(parentMarginInfo.liquidationDelta)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n BigNumber.min(\n sonMarginInfo.liquidationDelta,\n sonMarginInfo.realBalance,\n ).toNumber(),\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n dutchDelta: BigNumber(parentMarginInfo.dutchDelta)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n BigNumber.min(\n sonMarginInfo.dutchDelta,\n sonMarginInfo.realBalance,\n ).toNumber(),\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n adlDelta: BigNumber(parentMarginInfo.adlDelta)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n BigNumber.min(\n sonMarginInfo.adlDelta,\n sonMarginInfo.realBalance,\n ).toNumber(),\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n\n initialBufferDelta: BigNumber(parentMarginInfo.initialBufferDelta)\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n BigNumber.min(\n sonMarginInfo.initialBufferDelta,\n sonMarginInfo.realBalance,\n ).toNumber(),\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n liquidationMarginRequirement: BigNumber(\n parentMarginInfo.liquidationMarginRequirement,\n )\n .plus(\n ExposureCommand.exchangeWithPriceHaircut(\n sonMarginInfo.liquidationMarginRequirement,\n sonParentExchangeInfo.price,\n sonParentExchangeInfo.priceHaircut,\n ),\n )\n .toNumber(),\n };\n }\n\n static getUsdNodeMarginInfo(\n accountCollateralPoolId: number,\n quoteTokens: string[],\n exchangeInfoPerAsset: ExchangeInfo[],\n marginInfoPerToken: MarginInfo[],\n ) {\n let usdNodeMarginInfo: MarginInfo = {\n assetAddress: '',\n marginBalance: 0,\n realBalance: 0,\n initialDelta: 0,\n maintenanceDelta: 0,\n liquidationDelta: 0,\n dutchDelta: 0,\n adlDelta: 0,\n initialBufferDelta: 0,\n liquidationMarginRequirement: 0,\n };\n for (const quoteToken of quoteTokens) {\n const exchangeInfo = exchangeInfoPerAsset.find((exchangeInfo) => {\n return quoteToken === exchangeInfo.tokenAddress;\n });\n\n const marginInfo = marginInfoPerToken.find((marginInfo) => {\n return quoteToken === marginInfo.assetAddress;\n });\n\n if (!exchangeInfo || !marginInfo) {\n throw Error('Missing exchangeInfo/marginInfo');\n }\n\n usdNodeMarginInfo = ExposureCommand.combineMarginInfo(\n usdNodeMarginInfo,\n marginInfo,\n exchangeInfo,\n );\n }\n\n return usdNodeMarginInfo;\n }\n static getCollateralInfo(\n collateralAddress: string,\n realisedPnl: BigNumber,\n unrealizedPnL: BigNumber,\n netDeposits: number,\n ): CollateralInfo {\n return {\n netDeposits: netDeposits,\n marginBalance: BigNumber(netDeposits)\n .plus(realisedPnl)\n .plus(unrealizedPnL)\n .toNumber(),\n realBalance: BigNumber(netDeposits).plus(realisedPnl).toNumber(),\n };\n }\n\n static getTokenMarginInfo(\n rootCollateralPoolId: number,\n riskMatrices: RiskMatrix[],\n riskMultipliers: RiskMultipliersConfiguration,\n collateralInfo: CollateralInfo,\n collateralAddress: string,\n positions: PositionInfoMarketConfiguration[],\n oraclePricePerMarket: MarketIdToOraclePriceMap,\n uniqueQuoteTokens: string[],\n ): MarginInfo {\n const marginRequirements = {\n liquidationMarginRequirement: 0,\n initialMarginRequirement: 0,\n maintenanceMarginRequirement: 0,\n dutchMarginRequirement: 0,\n adlMarginRequirement: 0,\n initialBufferMarginRequirement: 0,\n };\n if (uniqueQuoteTokens.includes(collateralAddress)) {\n // uniqueQuoteTokens is list is active markets tokens\n for (const riskMatrix of riskMatrices) {\n if (Number(riskMatrix.risk_block_id) === 1) continue; // @todo remove and implement correct logic!\n const filledExposures = ExposureCommand.getBlockExposures(\n positions,\n oraclePricePerMarket,\n );\n\n marginRequirements.liquidationMarginRequirement = BigNumber(\n marginRequirements.liquidationMarginRequirement,\n )\n .plus(\n ExposureCommand.computeLiquidationMarginRequirement(\n riskMatrix.matrix,\n filledExposures,\n ),\n )\n .toNumber();\n }\n\n // Get the initial margin requirement\n marginRequirements.initialMarginRequirement = amountNormalizer(\n String(riskMultipliers.im_multiplier),\n )\n .multipliedBy(marginRequirements.liquidationMarginRequirement)\n .toNumber();\n // Get the maintenance margin requirement\n marginRequirements.maintenanceMarginRequirement = amountNormalizer(\n String(riskMultipliers.mmr_multiplier),\n )\n .multipliedBy(marginRequirements.liquidationMarginRequirement)\n .toNumber();\n // Get the dutch margin requirement\n marginRequirements.dutchMarginRequirement = amountNormalizer(\n String(riskMultipliers.dutch_multiplier),\n )\n .multipliedBy(marginRequirements.liquidationMarginRequirement)\n .toNumber();\n\n // Get the adl margin requirement\n marginRequirements.adlMarginRequirement = amountNormalizer(\n String(riskMultipliers.adl_multiplier),\n )\n .multipliedBy(marginRequirements.liquidationMarginRequirement)\n .toNumber();\n\n // Get the initial buffer margin requirement\n marginRequirements.initialBufferMarginRequirement = amountNormalizer(\n String(riskMultipliers.im_buffer_multiplier),\n )\n .multipliedBy(marginRequirements.liquidationMarginRequirement)\n .toNumber();\n }\n return {\n assetAddress: collateralAddress,\n marginBalance: collateralInfo.marginBalance,\n realBalance: collateralInfo.realBalance,\n initialDelta: BigNumber(collateralInfo.marginBalance)\n .minus(marginRequirements.initialMarginRequirement)\n .toNumber(),\n maintenanceDelta: BigNumber(collateralInfo.marginBalance)\n .minus(marginRequirements.maintenanceMarginRequirement)\n .toNumber(),\n liquidationDelta: BigNumber(collateralInfo.marginBalance)\n .minus(marginRequirements.liquidationMarginRequirement)\n .toNumber(),\n dutchDelta: BigNumber(collateralInfo.marginBalance)\n .minus(marginRequirements.dutchMarginRequirement)\n .toNumber(),\n adlDelta: BigNumber(collateralInfo.marginBalance)\n .minus(marginRequirements.adlMarginRequirement)\n .toNumber(),\n initialBufferDelta: BigNumber(collateralInfo.marginBalance)\n .minus(marginRequirements.initialBufferMarginRequirement)\n .toNumber(),\n liquidationMarginRequirement:\n marginRequirements.liquidationMarginRequirement,\n };\n }\n\n static computeLiquidationMarginRequirement(\n matrix: BigNumber[][],\n filledExposures: BigNumber[],\n ): number {\n let lmrFilledSquared = 0;\n\n for (let i = 0; i < filledExposures.length; i++) {\n if (BigNumber(filledExposures[i]).eq(0)) {\n continue;\n }\n for (let j = 0; j < filledExposures.length; j++) {\n const riskParam = matrix[i][j];\n\n if (BigNumber(filledExposures[j]).eq(0) || BigNumber(riskParam).eq(0)) {\n continue;\n }\n\n lmrFilledSquared = BigNumber(lmrFilledSquared)\n .plus(\n BigNumber(filledExposures[i])\n .multipliedBy(filledExposures[j])\n .multipliedBy(riskParam),\n )\n .toNumber();\n }\n }\n return BigNumber(lmrFilledSquared).sqrt().toNumber();\n }\n\n static getBlockExposures(\n positions: PositionInfoMarketConfiguration[],\n oraclePricePerMarket: MarketIdToOraclePriceMap,\n ): BigNumber[] {\n const filledExposures: number[] = [];\n\n for (const position of positions) {\n const marketFilledExposure = ExposureCommand.getAccountFilledExposures(\n position,\n position.market_configuration,\n oraclePricePerMarket[position.market_id],\n );\n filledExposures[marketFilledExposure.riskMatrixIndex] = BigNumber(\n filledExposures[marketFilledExposure.riskMatrixIndex] || 0,\n )\n .plus(marketFilledExposure.exposure)\n .toNumber();\n }\n\n return filledExposures.map((num) => BigNumber(num));\n }\n\n static getAccountFilledExposures(\n position: PositionInfo,\n marketConfiguration: MarketConfiguration,\n oraclePrice: number,\n ) {\n const base = position.base;\n\n return {\n exposure: BigNumber(oraclePrice).multipliedBy(base),\n riskMatrixIndex: BigNumber(\n String(marketConfiguration.risk_matrix_index),\n ).toNumber(),\n };\n }\n\n static computePricePnL(\n openBase: BigNumber,\n openPrice: BigNumber,\n exitPrice: BigNumber,\n ) {\n return BigNumber(\n BigNumber(exitPrice).minus(openPrice).multipliedBy(openBase),\n );\n }\n\n static getMarginRatio(marginInfo: MarginInfo) {\n if (marginInfo.liquidationMarginRequirement === 0) {\n return 0;\n }\n\n if (marginInfo.marginBalance <= 0) {\n return 1;\n }\n\n const health = BigNumber(marginInfo.liquidationMarginRequirement).div(\n marginInfo.marginBalance,\n );\n\n if (health.gt(1)) {\n return 1;\n }\n return health.toNumber();\n }\n\n static exchangeWithPriceHaircut(\n quantity: number,\n price: number,\n haircut: number,\n ) {\n // For positive quantities, the haircut is `quantity * (1 - haircut)`\n // For negative values, the haircut is `quantity / (1 - haircut)` because a negative value means the haircut should be applied from B to A.\n const calHelper = BigNumber(quantity).gt(0)\n ? BigNumber(1).minus(haircut)\n : BigNumber(1).div(BigNumber(1).minus(haircut));\n const haircutPrice = BigNumber(price).multipliedBy(calHelper);\n\n return haircutPrice.multipliedBy(quantity).toNumber();\n }\n\n getSlippage(\n deltaBase: number,\n marketConfiguration: MarketConfiguration,\n marketStorage: MarketStorage,\n ): number {\n const deltaExposure = BigNumber(\n this.oraclePricePerMarket[marketConfiguration.market_id],\n )\n .times(deltaBase)\n .toNumber();\n\n const riskMatrixIndex = BigNumber(\n String(marketConfiguration.risk_matrix_index),\n ).toNumber();\n\n const { maxExposureShort, maxExposureLong, exposures } =\n this.getMaxExposure(marketConfiguration, marketStorage);\n\n const netExposure = exposures[riskMatrixIndex].plus(deltaExposure);\n const maxExposure = netExposure.lt(0) ? maxExposureShort : maxExposureLong;\n\n return BigNumber(netExposure)\n .negated()\n .div(BigNumber(maxExposure).plus(netExposure))\n .toNumber();\n }\n\n getMaxExposure(\n marketConfiguration: MarketConfiguration,\n marketStorage: MarketStorage,\n ) {\n const riskMatrix = this.riskMatrices.find((riskMatrix) => {\n return (\n riskMatrix.risk_block_id ===\n BigNumber(String(marketStorage.risk_block_id)).toNumber()\n );\n });\n\n if (!riskMatrix) {\n throw new Error(\"RiskMatrix Doesn't exist\");\n }\n\n const riskMatrixIndex = BigNumber(\n String(marketConfiguration.risk_matrix_index),\n ).toNumber();\n\n const imrMultiplier = amountNormalizer(\n String(this.riskMultipliers.im_multiplier),\n ).toNumber();\n\n const marginInfo = this.tokenMarginInfoPerAsset.find((marginInfo) => {\n return marginInfo.assetAddress === marketStorage.quote_collateral;\n });\n\n if (!marginInfo) {\n throw new Error(\"marginInfo doesn't exist\");\n }\n\n const exposures = ExposureCommand.getBlockExposures(\n this.positionInfoMarketConfiguration,\n this.oraclePricePerMarket,\n );\n\n const { maxExposureShort, maxExposureLong } =\n ExposureCommand.computeMaxExposures(\n riskMatrix.matrix,\n exposures,\n marginInfo.liquidationMarginRequirement,\n marginInfo.marginBalance < 0 ? 0 : marginInfo.marginBalance,\n imrMultiplier,\n riskMatrixIndex,\n );\n\n return {\n maxExposureShort,\n maxExposureLong,\n exposures,\n };\n }\n\n static computeMaxExposures(\n riskMatrix: BigNumber[][],\n exposures: BigNumber[],\n lmr: number,\n balance: number,\n imrMultiplier: number,\n exposureIndex: number,\n ) {\n let b = BigNumber(0);\n\n for (let i = 0; i < exposures.length; i++) {\n b = BigNumber(b).plus(\n BigNumber(exposures[i]).multipliedBy(\n BigNumber(riskMatrix[exposureIndex][i]).plus(\n riskMatrix[i][exposureIndex],\n ),\n ),\n );\n }\n const { x1, x2 } = this.solveQuadraticEquation(\n BigNumber(riskMatrix[exposureIndex][exposureIndex]).toNumber(), // changes here\n b.toNumber(),\n this.computeC(lmr, balance, imrMultiplier),\n );\n\n const maxShortExposure = BigNumber(x1).plus(exposures[exposureIndex]);\n const maxLongExposure = BigNumber(x2).plus(exposures[exposureIndex]);\n\n const availableShortExposure = maxShortExposure.lt(0)\n ? maxShortExposure.negated().toNumber()\n : 0;\n\n const availableLongExposure = maxLongExposure.gt(0)\n ? maxLongExposure.toNumber()\n : 0;\n\n return {\n maxExposureShort: availableShortExposure,\n maxExposureLong: availableLongExposure,\n };\n }\n\n static solveQuadraticEquation(a: number, b: number, c: number) {\n if (BigNumber(a).eq(0)) {\n throw new Error('ZeroQuadraticCoefficient');\n }\n\n const delta = BigNumber(b)\n .multipliedBy(b)\n .minus(BigNumber(4).multipliedBy(a).multipliedBy(c));\n\n if (delta.lt(0)) {\n throw new Error('ComplexQuadraticRoots(a, b, c)');\n }\n\n const rootDelta = delta.sqrt();\n\n const x1 = BigNumber(b)\n .multipliedBy(-1)\n .minus(rootDelta)\n .div(BigNumber(2).multipliedBy(a));\n\n const x2 = BigNumber(b)\n .multipliedBy(-1)\n .plus(rootDelta)\n .div(BigNumber(2).multipliedBy(a));\n\n return {\n x1,\n x2,\n };\n }\n\n static computeC(lmr: number, balance: number, imrMultiplier: number): number {\n const lmrSD = BigNumber(lmr);\n const lmrSquared = lmrSD.multipliedBy(lmrSD);\n\n const balanceSD = BigNumber(balance);\n const balanceSquared = balanceSD.multipliedBy(balanceSD);\n\n const imrMultiplierSD = BigNumber(imrMultiplier);\n const imrMultiplierSquared = imrMultiplierSD.multipliedBy(imrMultiplierSD);\n\n return lmrSquared\n .minus(balanceSquared.div(imrMultiplierSquared))\n .toNumber();\n }\n\n static calculateFee(\n price: number,\n amount: number,\n feeParameter: BigNumber,\n ): number {\n return BigNumber(price).times(amount).times(feeParameter).abs().toNumber(); // @todo abs value\n }\n\n static calculateEstimatedPrice(price: number, slippage: number): number {\n return BigNumber(price).times(BigNumber(1).plus(slippage)).toNumber();\n }\n\n static evaluateHealthStatus(number: number) {\n // todo update logic\n if (number >= 67) {\n return 'danger';\n } else if (number >= 34) {\n return 'warning';\n } else {\n return 'healthy';\n }\n }\n}\n"]}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.amountNormalizer = void 0;
|
|
7
|
-
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
-
function amountNormalizer(value, decimals) {
|
|
9
|
-
if (decimals === void 0) { decimals = 18; }
|
|
10
|
-
return (0, bignumber_js_1.default)(value).div((0, bignumber_js_1.default)(10).pow(decimals));
|
|
11
|
-
}
|
|
12
|
-
exports.amountNormalizer = amountNormalizer;
|
|
13
|
-
//# sourceMappingURL=number.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"number.js","sourceRoot":"/","sources":["clients/helpers/number.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAqC;AAErC,SAAgB,gBAAgB,CAC9B,KAAkC,EAClC,QAAqB;IAArB,yBAAA,EAAA,aAAqB;IAErB,OAAO,IAAA,sBAAS,EAAC,KAAK,CAAC,CAAC,GAAG,CAAC,IAAA,sBAAS,EAAC,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC3D,CAAC;AALD,4CAKC","sourcesContent":["import BigNumber from 'bignumber.js';\n\nexport function amountNormalizer(\n value: BigNumber | number | string,\n decimals: number = 18,\n): BigNumber {\n return BigNumber(value).div(BigNumber(10).pow(decimals));\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"trade.simulation.types.js","sourceRoot":"/","sources":["clients/helpers/trade.simulation.types.ts"],"names":[],"mappings":"","sourcesContent":["import BigNumber from 'bignumber.js';\n\nexport interface MarketStorage {\n market_id: number;\n quote_collateral: string;\n instrument_address: string;\n name: string;\n risk_block_id: number;\n collateral_pool_id: number;\n block_timestamp: number;\n block_number: number;\n}\n\nexport interface MarketConfiguration {\n market_id: number;\n risk_matrix_index: number;\n max_open_base: number;\n velocity_multiplier: number;\n minimum_order_base: number;\n base_spacing: number;\n price_spacing: number;\n oracle_node_id: string;\n mtm_window: number;\n dutch_config_lambda: number;\n dutch_config_min_base: number;\n slippage_params_phi: number;\n slippage_params_beta: number;\n block_timestamp: number;\n block_number: number;\n}\n\nexport type AccountAssetBalance = {\n accountId: number;\n collateral: string;\n amount: number;\n};\n\nexport interface RiskMultipliersConfiguration {\n collateral_pool_id: number;\n im_multiplier: number;\n mmr_multiplier: number;\n dutch_multiplier: number;\n adl_multiplier: number;\n im_buffer_multiplier: number;\n block_timestamp: number;\n block_number: number;\n}\n\nexport interface RiskMatrix {\n collateral_pool_id: number;\n risk_block_id: number;\n matrix: BigNumber[][];\n}\nexport type MarketIdToOraclePriceMap = {\n [marketId: number]: number;\n};\n\nexport type CollateralAddressToExchangePriceMap = {\n [address: string]: number;\n};\n\nexport interface ExchangeInfo {\n price: number;\n priceHaircut: number;\n autoExchangeDiscount: number;\n tokenAddress: string;\n}\n\nexport interface PositionInfo {\n base: BigNumber;\n realized_pnl: BigNumber;\n last_price: BigNumber;\n last_timestamp: BigNumber;\n funding_value: BigNumber;\n base_multiplier: BigNumber;\n adl_unwind_price: BigNumber;\n market_id: number;\n}\n\nexport type PositionInfoMarketConfiguration = PositionInfo & {\n market_configuration: MarketConfiguration;\n};\n\nexport interface MarginInfo {\n assetAddress: string;\n marginBalance: number;\n realBalance: number;\n initialDelta: number;\n maintenanceDelta: number;\n liquidationDelta: number;\n dutchDelta: number;\n adlDelta: number;\n initialBufferDelta: number;\n liquidationMarginRequirement: number;\n}\n\nexport interface CollateralInfo {\n netDeposits: number;\n marginBalance: number;\n realBalance: number;\n}\n\nexport type ExposureCommandState = {\n rootCollateralPoolId: number;\n oraclePricePerMarket: MarketIdToOraclePriceMap;\n accountBalancePerAsset: AccountAssetBalance[];\n groupedByCollateral: Record<string, AccountAssetBalance>;\n riskMultipliers: RiskMultipliersConfiguration;\n riskMatrices: RiskMatrix[];\n exchangeInfoPerAsset: ExchangeInfo[];\n positionInfoMarketConfiguration: PositionInfoMarketConfiguration[];\n uniqueTokenAddresses: string[];\n uniqueQuoteCollaterals: string[];\n tokenMarginInfoPerAsset: MarginInfo[];\n realizedPnLSum: BigNumber;\n unrealizedPnLSum: BigNumber;\n};\n\nexport type TradeSimulationState = {\n feeParameter: BigNumber;\n marketStorage: MarketStorage;\n marketConfiguration: MarketConfiguration;\n exposureDataAccount: ExposureCommandState;\n exposureDataPassivePool: ExposureCommandState;\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"account.js","sourceRoot":"/","sources":["clients/modules/account.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBA,gDAAgC;AAGhC;IAA2C,iCAAU;IAArD;;IA0FA,CAAC;IAzFC;;;;;;;;;;;SAWK;IAEC,yCAAiB,GAAvB,UACE,MAA+B;;;;gBAEzB,GAAG,GAAG,wBAAiB,MAAM,CAAC,OAAO,CAAE,CAAC;gBAC9C,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,C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AAG,wBAAiB,MAAM,CAAC,eAAe,yBAAsB,CAAC;gBAC1E,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,KAAK,EAAE,MAAM,CAAC,KAAK;qBACpB,CAAC,EAAC;;;KACJ;IAEK,uDAA+B,GAArC,UACE,MAA6C;;;;gBAEvC,GAAG,GAAG,wBAAiB,MAAM,CAAC,eAAe,yBAAsB,CAAC;gBAC1E,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,YAAY,EAAE,MAAM,CAAC,YAAY;qBAClC,CAAC,EAAC;;;KACJ;IACH,oBAAC;AAAD,CAAC,AA1FD,CAA2C,cAAU,GA0FpD","sourcesContent":["import {\n GetMarginAccountParams,\n GetMarginAccountResult,\n GetMarginAccountsParams,\n GetMarginAccountsResult,\n GetMarginAccountTransactionHistoryParams,\n GetMarginAccountTransactionHistoryResult,\n GetMaxOrderSizeAvailableParams,\n GetMaxOrderSizeAvailableResult,\n GetMaxWithdrawBalanceForAccountParams,\n GetMaxWithdrawBalanceForAccountResult,\n GetPositionsForMarginAccountParams,\n GetPositionsForMarginAccountResult,\n GetPositionsHistoryForMarginAccountParams,\n GetPositionsHistoryForMarginAccountResult,\n GetTransactionSimulationInitialDataParams,\n} from '../types';\nimport RestClient from './rest';\nimport { TradeSimulationState } from '../helpers/trade.simulation.types';\n\nexport default class AccountClient extends RestClient {\n /**\n * Asynchronously retrieves a list of margin accounts associated with a specific address.\n *\n * This method makes a request to the API endpoint to fetch collateral account data. The data is filtered\n * based on the provided Ethereum address. An optional limit can be specified to control the number of\n * collateral accounts returned in the response.\n *\n * @param {GetMarginAccountsParams} params\n * @returns {Promise<GetMarginAccountsResult>} A promise that resolves to the response containing the margin\n * account data.\n * @memberof account\n * */\n\n async getMarginAccounts(\n params: GetMarginAccountsParams,\n ): Promise<GetMarginAccountsResult> {\n const uri = `/api/accounts/${params.address}`;\n return this.get(uri, { limit: params.limit });\n }\n\n /**\n * Asynchronously retrieves details of a specific collateral account for a given Ethereum address.\n *\n * This method sends a request to the API to obtain detailed information about a specific collateral account\n * associated with the provided Ethereum address. The account is identified using the collateral account number.\n *\n * @param {GetMarginAccountParams} params\n * @returns {Promise<GetMarginAccountResult>} A promise that resolves to the response containing the detailed\n * information of the specified margin account.\n * @memberof account\n */\n\n async getMarginAccount(\n params: GetMarginAccountParams,\n ): Promise<GetMarginAccountResult> {\n const uri = `/api/accounts/${params.address}/marginAccount/${params.marginAccountId}`;\n return this.get(uri);\n }\n\n async getPositionsForMarginAccount(\n params: GetPositionsForMarginAccountParams,\n ): Promise<GetPositionsForMarginAccountResult> {\n const uri = `/api/accounts/${params.address}/marginAccount/${params.marginAccountId}/positions`;\n return this.get(uri, { limit: params.limit });\n }\n\n async getPositionsHistoryForMarginAccount(\n params: GetPositionsHistoryForMarginAccountParams,\n ): Promise<GetPositionsHistoryForMarginAccountResult> {\n const uri = `/api/accounts/${params.address}/marginAccount/${params.marginAccountId}/positions/history`;\n return this.get(uri, { limit: params.limit });\n }\n\n async getMaxOrderSizeAvailable(\n params: GetMaxOrderSizeAvailableParams,\n ): Promise<GetMaxOrderSizeAvailableResult> {\n const uri = `/api/accounts/${params.marginAccountId}/max-order-size`;\n return this.get(uri, {\n marketId: params.marketId,\n direction: params.direction,\n });\n }\n\n async getTransactionSimulationInitialData(\n params: GetTransactionSimulationInitialDataParams,\n ): Promise<TradeSimulationState> {\n const uri = `/api/accounts/${params.marginAccountId}/trade-simulation-data`;\n return this.get(uri, {\n marketId: params.marketId,\n });\n }\n\n async getMarginAccountTransactionHistory(\n params: GetMarginAccountTransactionHistoryParams,\n ): Promise<GetMarginAccountTransactionHistoryResult> {\n const uri = `/api/accounts/${params.marginAccountId}/transaction-history`;\n return this.get(uri, {\n limit: params.limit,\n });\n }\n\n async getMaxWithdrawBalanceForAccount(\n params: GetMaxWithdrawBalanceForAccountParams,\n ): Promise<GetMaxWithdrawBalanceForAccountResult> {\n const uri = `/api/accounts/${params.marginAccountId}/max-withdraw-amount`;\n return this.get(uri, {\n assetAddress: params.tokenAddress,\n });\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lp.js","sourceRoot":"/","sources":["clients/modules/lp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,gDAAgC;AAEhC;IAAsC,4BAAU;IAAhD;;IA2DA,CAAC;IA1DO,6BAAU,GAAhB;;;;gBACQ,GAAG,GAAG,eAAe,CAAC;gBAC5B,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;;;KACtB;IAEK,4BAAS,GAAf,UAAgB,MAAuB;;;;gBAC/B,GAAG,GAAG,wBAAiB,MAAM,CAAC,EAAE,CAAE,CAAC;gBACzC,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;;;KACtB;IAEK,8CAA2B,GAAjC,UACE,MAAyC;;;;gBAEnC,GAAG,GAAG,wBAAiB,MAAM,CAAC,EAAE,yBAAsB,CAAC;gBAC7D,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,KAAK,EAAE,MAAM,CAAC,KAAK;wBACnB,OAAO,EAAE,MAAM,CAAC,OAAO;qBACxB,CAAC,EAAC;;;KACJ;IAEK,iCAAc,GAApB,UACE,MAA4B;;;;gBAEtB,GAAG,GAAG,wBAAiB,MAAM,CAAC,EAAE,eAAY,CAAC;gBACnD,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,OAAO,EAAE,MAAM,CAAC,OAAO;qBACxB,CAAC,EAAC;;;KACJ;IAEK,2CAAwB,GAA9B,UACE,MAAsC;;;;gBAEhC,GAAG,GAAG,wBAAiB,MAAM,CAAC,EAAE,sBAAmB,CAAC;gBAC1D,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,OAAO,EAAE,MAAM,CAAC,OAAO;qBACxB,CAAC,EAAC;;;KACJ;IAEK,4CAAyB,GAA/B,UACE,MAAuC;;;;gBAEjC,GAAG,GAAG,wBAAiB,MAAM,CAAC,MAAM,wBAAqB,CAAC;gBAChE,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW;wBACvC,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW;qBACxC,CAAC,EAAC;;;KACJ;IAEK,gDAA6B,GAAnC,UACE,MAA2C;;;;gBAErC,GAAG,GAAG,wBAAiB,MAAM,CAAC,MAAM,4BAAyB,CAAC;gBACpE,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW;wBACvC,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW;wBACvC,OAAO,EAAE,MAAM,CAAC,OAAO;qBACxB,CAAC,EAAC;;;KACJ;IACH,eAAC;AAAD,CAAC,AA3DD,CAAsC,cAAU,GA2D/C","sourcesContent":["import {\n GetLpPoolParams,\n GetLpPositionsParams,\n GetLpPoolResult,\n GetLpPoolsResult,\n GetLpPoolTransactionHistoryParams,\n GetLpPoolTransactionHistoryResult,\n GetLpPositionsResult,\n GetLpPoolWithdrawBalanceParams,\n GetLpPoolWithdrawBalanceResult,\n GetLpPoolBalanceChartDataParams,\n GetLpPoolBalanceChartDataResult,\n GetLpPoolPerformanceChartDataParams,\n GetLpPoolPerformanceChartDataResult,\n} from '../types';\nimport RestClient from './rest';\n\nexport default class LpClient extends RestClient {\n async getLpPools(): Promise<GetLpPoolsResult> {\n const uri = '/api/lp-pools';\n return this.get(uri);\n }\n\n async getLpPool(params: GetLpPoolParams): Promise<GetLpPoolResult> {\n const uri = `/api/lp-pools/${params.id}`;\n return this.get(uri);\n }\n\n async getLpPoolTransactionHistory(\n params: GetLpPoolTransactionHistoryParams,\n ): Promise<GetLpPoolTransactionHistoryResult> {\n const uri = `/api/lp-pools/${params.id}/transaction-history`;\n return this.get(uri, {\n limit: params.limit,\n address: params.address,\n });\n }\n\n async getLpPositions(\n params: GetLpPositionsParams,\n ): Promise<GetLpPositionsResult> {\n const uri = `/api/lp-pools/${params.id}/positions`;\n return this.get(uri, {\n address: params.address,\n });\n }\n\n async getLpPoolWithdrawBalance(\n params: GetLpPoolWithdrawBalanceParams,\n ): Promise<GetLpPoolWithdrawBalanceResult> {\n const uri = `/api/lp-pools/${params.id}/withdraw-balance`;\n return this.get(uri, {\n address: params.address,\n });\n }\n\n async getLpPoolBalanceChartData(\n params: GetLpPoolBalanceChartDataParams,\n ): Promise<GetLpPoolBalanceChartDataResult> {\n const uri = `/api/lp-pools/${params.poolId}/balance-chart-data`;\n return this.get(uri, {\n timeframeMs: params.filters.timeframeMs,\n granularity: params.filters.granularity,\n });\n }\n\n async getLpPoolPerformanceChartData(\n params: GetLpPoolPerformanceChartDataParams,\n ): Promise<GetLpPoolPerformanceChartDataResult> {\n const uri = `/api/lp-pools/${params.poolId}/performance-chart-data`;\n return this.get(uri, {\n timeframeMs: params.filters.timeframeMs,\n granularity: params.filters.granularity,\n address: params.address,\n });\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"markets.js","sourceRoot":"/","sources":["clients/modules/markets.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,gDAAgC;AAEhC;IAA2C,iCAAU;IAArD;;IAgEA,CAAC;IA/DC;;;;;;;;;;;;;;;;OAgBG;IAEG,kCAAU,GAAhB;;;;gBACQ,GAAG,GAAG,cAAc,CAAC;gBAC3B,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;;;KACtB;IAEK,iCAAS,GAAf,UAAgB,MAAuB;;;;gBAC/B,GAAG,GAAG,uBAAgB,MAAM,CAAC,EAAE,CAAE,CAAC;gBACxC,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;;;KACtB;IAED;;;;;;;;;;;;OAYG;IAEG,wCAAgB,GAAtB,UACE,MAAwB;;;;gBAElB,GAAG,GAAG,+BAAwB,MAAM,CAAC,QAAQ,CAAE,CAAC;gBACtD,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,UAAU,EAAE,MAAM,CAAC,UAAU;wBAC7B,OAAO,EAAE,MAAM,CAAC,OAAO;wBACvB,KAAK,EAAE,MAAM,CAAC,KAAK;wBACnB,KAAK,EAAE,MAAM,CAAC,KAAK;qBACpB,CAAC,EAAC;;;KACJ;IAEK,+CAAuB,GAA7B,UACE,MAAqC;;;;gBAE/B,GAAG,GAAG,sBAAe,MAAM,CAAC,QAAQ,CAAE,CAAC;gBAC7C,sBAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;wBACnB,KAAK,EAAE,MAAM,CAAC,KAAK;wBACnB,4BAA4B,EAAE,MAAM,CAAC,4BAA4B;qBAClE,CAAC,EAAC;;;KACJ;IACH,oBAAC;AAAD,CAAC,AAhED,CAA2C,cAAU,GAgEpD","sourcesContent":["import {\n GetCandlesParams,\n GetMarketParams,\n GetMarketResult,\n GetMarketsResult,\n GetMarketTradingHistoryParams,\n GetTradingHistoryResult,\n MarketCandlesResponse,\n} from '../types';\nimport RestClient from './rest';\n\nexport default class MarketsClient extends RestClient {\n /**\n * Asynchronously retrieves market data from the API.\n *\n * This method makes a request to the API to fetch data for markets. If a specific market is provided as a parameter,\n * it fetches data for that particular market; otherwise, it fetches data for all available markets.\n *\n * @returns {Promise<GetMarketsResult>} A promise that resolves to the response containing market data.\n * @memberof MarketsClient\n *\n * @example\n * // Fetch data for all markets\n * const allMarketsData = await market.getMarkets();\n *\n * @example\n * // Fetch data for a specific market\n * const specificMarketData = await market.getMarkets('ETH-USDC');\n */\n\n async getMarkets(): Promise<GetMarketsResult> {\n const uri = '/api/markets';\n return this.get(uri);\n }\n\n async getMarket(params: GetMarketParams): Promise<GetMarketResult> {\n const uri = `/api/markets/${params.id}`;\n return this.get(uri);\n }\n\n /**\n * Retrieves candlestick data for a specified market.\n *\n * This method fetches historical candlestick (or OHLC - Open, High, Low, Close) data for a given market.\n * The data can be filtered by time range (fromISO and toISO) and resolution.\n *\n * @param {GetCandlesParams} params\n * @returns {Promise<MarketCandlesResponse>} A promise that resolves to the market candlestick data.\n * @memberof MarketsClient\n *\n * @example\n * const marketCandles = await market.getMarketCandles('BTC-USD', '1HR', '2023-01-01T00:00:00Z', '2023-01-02T00:00:00Z');\n */\n\n async getMarketCandles(\n params: GetCandlesParams,\n ): Promise<MarketCandlesResponse> {\n const uri = `/api/markets/candles/${params.marketId}`;\n return this.get(uri, {\n resolution: params.resolution,\n fromISO: params.fromISO,\n toISO: params.toISO,\n limit: params.limit,\n });\n }\n\n async getMarketTradingHistory(\n params: GetMarketTradingHistoryParams,\n ): Promise<GetTradingHistoryResult> {\n const uri = `/api/trades/${params.marketId}`;\n return this.get(uri, {\n limit: params.limit,\n fromTimestampMillisecondsUTC: params.fromTimestampMillisecondsUTC,\n });\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rest.js","sourceRoot":"/","sources":["clients/modules/rest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAmD;AACnD,wDAA4D;AAC5D,sCAA8E;AAC9E;IAIE,oBAAY,IAAY,EAAE,UAA0B;QAClD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,UAAU,IAAI,uBAAW,CAAC;IAC9C,CAAC;IAEK,wBAAG,GAAT,UACE,WAAmB,EACnB,MAAiC;QAAjC,uBAAA,EAAA,WAAiC;;;;;;wBAE3B,GAAG,GAAG,UAAG,IAAI,CAAC,IAAI,SAAG,IAAA,gCAAiB,EAAC,WAAW,EAAE,MAAM,CAAC,CAAE,CAAC;wBACnD,qBAAM,IAAA,eAAO,EAAC,GAAG,CAAC,EAAA;;wBAA7B,QAAQ,GAAG,SAAkB;wBACnC,sBAAO,QAAQ,CAAC,IAAI,EAAC;;;;KACtB;IAEK,yBAAI,GAAV,UACE,WAAmB,EACnB,MAAiC,EACjC,IAAqB,EACrB,OAAkC;QAFlC,uBAAA,EAAA,WAAiC;QAEjC,wBAAA,EAAA,YAAkC;;;;gBAE5B,GAAG,GAAG,UAAG,IAAI,CAAC,IAAI,SAAG,IAAA,gCAAiB,EAAC,WAAW,EAAE,MAAM,CAAC,CAAE,CAAC;gBACpE,sBAAO,IAAA,eAAO,EAAC,GAAG,EAAE,qBAAa,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAC;;;KACxD;IACH,iBAAC;AAAD,CAAC,AA3BD,IA2BC","sourcesContent":["import { API_TIMEOUT } from '../helpers/constants';\nimport { generateQueryPath } from '../helpers/query-helper';\nimport { RequestMethod, Response, request, ResponseData } from '../lib/axios';\nexport default class RestClient {\n readonly host: string;\n readonly apiTimeout: number;\n\n constructor(host: string, apiTimeout?: number | null) {\n this.host = host;\n this.apiTimeout = apiTimeout || API_TIMEOUT;\n }\n\n async get(\n requestPath: string,\n params: NonNullable<unknown> = {},\n ): Promise<ResponseData> {\n const url = `${this.host}${generateQueryPath(requestPath, params)}`;\n const response = await request(url);\n return response.data;\n }\n\n async post(\n requestPath: string,\n params: NonNullable<unknown> = {},\n body?: unknown | null,\n headers: NonNullable<unknown> = {},\n ): Promise<Response> {\n const url = `${this.host}${generateQueryPath(requestPath, params)}`;\n return request(url, RequestMethod.POST, body, headers);\n }\n}\n"]}
|