@reyaxyz/common 0.330.0 → 0.330.2
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/transactions/abis/PassivePerpProxy.json +295 -0
- package/dist/transactions/index.js +3 -1
- package/dist/transactions/index.js.map +1 -1
- package/dist/types/transactions/index.d.ts +1 -0
- package/dist/types/transactions/index.d.ts.map +1 -1
- package/dist/types/utils/calculate.d.ts +9 -2
- package/dist/types/utils/calculate.d.ts.map +1 -1
- package/dist/utils/calculate.js +2 -2
- package/dist/utils/calculate.js.map +1 -1
- package/package.json +2 -2
- package/src/transactions/abis/PassivePerpProxy.json +295 -0
- package/src/transactions/index.ts +1 -0
- package/src/utils/calculate.ts +11 -4
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
{
|
|
2
|
+
"abi": [
|
|
3
|
+
{
|
|
4
|
+
"type": "function",
|
|
5
|
+
"name": "getPoolMaxExposures",
|
|
6
|
+
"inputs": [
|
|
7
|
+
{
|
|
8
|
+
"name": "marketId",
|
|
9
|
+
"type": "uint128",
|
|
10
|
+
"internalType": "uint128"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
"outputs": [
|
|
14
|
+
{
|
|
15
|
+
"name": "maxShortExposure",
|
|
16
|
+
"type": "uint256",
|
|
17
|
+
"internalType": "UD60x18"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"name": "maxLongExposure",
|
|
21
|
+
"type": "uint256",
|
|
22
|
+
"internalType": "UD60x18"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"stateMutability": "view"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"type": "function",
|
|
29
|
+
"name": "getMarketConfiguration",
|
|
30
|
+
"inputs": [
|
|
31
|
+
{
|
|
32
|
+
"name": "marketId",
|
|
33
|
+
"type": "uint128",
|
|
34
|
+
"internalType": "uint128"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"outputs": [
|
|
38
|
+
{
|
|
39
|
+
"name": "config",
|
|
40
|
+
"type": "tuple",
|
|
41
|
+
"internalType": "struct MarketConfigurationData",
|
|
42
|
+
"components": [
|
|
43
|
+
{
|
|
44
|
+
"name": "riskMatrixIndex",
|
|
45
|
+
"type": "uint256",
|
|
46
|
+
"internalType": "uint256"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"name": "maxOpenBase",
|
|
50
|
+
"type": "uint256",
|
|
51
|
+
"internalType": "UD60x18"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "velocityMultiplier",
|
|
55
|
+
"type": "uint256",
|
|
56
|
+
"internalType": "UD60x18"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"name": "oracleNodeId",
|
|
60
|
+
"type": "bytes32",
|
|
61
|
+
"internalType": "bytes32"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "mtmWindow",
|
|
65
|
+
"type": "uint256",
|
|
66
|
+
"internalType": "uint256"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"name": "dutchConfig",
|
|
70
|
+
"type": "tuple",
|
|
71
|
+
"internalType": "struct DutchConfiguration",
|
|
72
|
+
"components": [
|
|
73
|
+
{
|
|
74
|
+
"name": "lambda",
|
|
75
|
+
"type": "uint256",
|
|
76
|
+
"internalType": "UD60x18"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"name": "minBase",
|
|
80
|
+
"type": "uint256",
|
|
81
|
+
"internalType": "UD60x18"
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"name": "slippageParams",
|
|
87
|
+
"type": "tuple",
|
|
88
|
+
"internalType": "struct SlippageParams",
|
|
89
|
+
"components": [
|
|
90
|
+
{
|
|
91
|
+
"name": "phi",
|
|
92
|
+
"type": "uint256",
|
|
93
|
+
"internalType": "UD60x18"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"name": "beta",
|
|
97
|
+
"type": "uint256",
|
|
98
|
+
"internalType": "UD60x18"
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"name": "minimumOrderBase",
|
|
104
|
+
"type": "uint256",
|
|
105
|
+
"internalType": "UD60x18"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"name": "baseSpacing",
|
|
109
|
+
"type": "uint256",
|
|
110
|
+
"internalType": "UD60x18"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"name": "priceSpacing",
|
|
114
|
+
"type": "uint256",
|
|
115
|
+
"internalType": "UD60x18"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"name": "depthFactor",
|
|
119
|
+
"type": "uint256",
|
|
120
|
+
"internalType": "UD60x18"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"name": "maxExposureFactor",
|
|
124
|
+
"type": "uint256",
|
|
125
|
+
"internalType": "UD60x18"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"name": "maxPSlippage",
|
|
129
|
+
"type": "uint256",
|
|
130
|
+
"internalType": "UD60x18"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"name": "marketOrderMaxStaleDuration",
|
|
134
|
+
"type": "uint256",
|
|
135
|
+
"internalType": "uint256"
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
"name": "priceSpread",
|
|
139
|
+
"type": "uint256",
|
|
140
|
+
"internalType": "UD60x18"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"name": "volatilityIndexMultiplier",
|
|
144
|
+
"type": "uint256",
|
|
145
|
+
"internalType": "UD60x18"
|
|
146
|
+
}
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
],
|
|
150
|
+
"stateMutability": "view"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"type": "function",
|
|
154
|
+
"name": "setMarketConfiguration",
|
|
155
|
+
"inputs": [
|
|
156
|
+
{
|
|
157
|
+
"name": "marketId",
|
|
158
|
+
"type": "uint128",
|
|
159
|
+
"internalType": "uint128"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"name": "config",
|
|
163
|
+
"type": "tuple",
|
|
164
|
+
"internalType": "struct MarketConfigurationData",
|
|
165
|
+
"components": [
|
|
166
|
+
{
|
|
167
|
+
"name": "riskMatrixIndex",
|
|
168
|
+
"type": "uint256",
|
|
169
|
+
"internalType": "uint256"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"name": "maxOpenBase",
|
|
173
|
+
"type": "uint256",
|
|
174
|
+
"internalType": "UD60x18"
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"name": "velocityMultiplier",
|
|
178
|
+
"type": "uint256",
|
|
179
|
+
"internalType": "UD60x18"
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
"name": "oracleNodeId",
|
|
183
|
+
"type": "bytes32",
|
|
184
|
+
"internalType": "bytes32"
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"name": "mtmWindow",
|
|
188
|
+
"type": "uint256",
|
|
189
|
+
"internalType": "uint256"
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
"name": "dutchConfig",
|
|
193
|
+
"type": "tuple",
|
|
194
|
+
"internalType": "struct DutchConfiguration",
|
|
195
|
+
"components": [
|
|
196
|
+
{
|
|
197
|
+
"name": "lambda",
|
|
198
|
+
"type": "uint256",
|
|
199
|
+
"internalType": "UD60x18"
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"name": "minBase",
|
|
203
|
+
"type": "uint256",
|
|
204
|
+
"internalType": "UD60x18"
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
"name": "slippageParams",
|
|
210
|
+
"type": "tuple",
|
|
211
|
+
"internalType": "struct SlippageParams",
|
|
212
|
+
"components": [
|
|
213
|
+
{
|
|
214
|
+
"name": "phi",
|
|
215
|
+
"type": "uint256",
|
|
216
|
+
"internalType": "UD60x18"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"name": "beta",
|
|
220
|
+
"type": "uint256",
|
|
221
|
+
"internalType": "UD60x18"
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
"name": "minimumOrderBase",
|
|
227
|
+
"type": "uint256",
|
|
228
|
+
"internalType": "UD60x18"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
"name": "baseSpacing",
|
|
232
|
+
"type": "uint256",
|
|
233
|
+
"internalType": "UD60x18"
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
"name": "priceSpacing",
|
|
237
|
+
"type": "uint256",
|
|
238
|
+
"internalType": "UD60x18"
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
"name": "depthFactor",
|
|
242
|
+
"type": "uint256",
|
|
243
|
+
"internalType": "UD60x18"
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"name": "maxExposureFactor",
|
|
247
|
+
"type": "uint256",
|
|
248
|
+
"internalType": "UD60x18"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"name": "maxPSlippage",
|
|
252
|
+
"type": "uint256",
|
|
253
|
+
"internalType": "UD60x18"
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
"name": "marketOrderMaxStaleDuration",
|
|
257
|
+
"type": "uint256",
|
|
258
|
+
"internalType": "uint256"
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
"name": "priceSpread",
|
|
262
|
+
"type": "uint256",
|
|
263
|
+
"internalType": "UD60x18"
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
"name": "volatilityIndexMultiplier",
|
|
267
|
+
"type": "uint256",
|
|
268
|
+
"internalType": "UD60x18"
|
|
269
|
+
}
|
|
270
|
+
]
|
|
271
|
+
}
|
|
272
|
+
],
|
|
273
|
+
"outputs": [],
|
|
274
|
+
"stateMutability": "nonpayable"
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
"type": "function",
|
|
278
|
+
"name": "setMarketConfigurationDepth",
|
|
279
|
+
"inputs": [
|
|
280
|
+
{
|
|
281
|
+
"name": "marketId",
|
|
282
|
+
"type": "uint128",
|
|
283
|
+
"internalType": "uint128"
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
"name": "depthFactor",
|
|
287
|
+
"type": "uint256",
|
|
288
|
+
"internalType": "UD60x18"
|
|
289
|
+
}
|
|
290
|
+
],
|
|
291
|
+
"outputs": [],
|
|
292
|
+
"stateMutability": "nonpayable"
|
|
293
|
+
}
|
|
294
|
+
]
|
|
295
|
+
}
|
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.OrdersGatewayAbi = exports.Socket_VaultWithPayloadAbi = exports.PeripheryAbi = exports.OracleAdaptersAbi = exports.MulticallAbi = exports.ErrorsAbi = exports.CoreAbi = void 0;
|
|
17
|
+
exports.PassivePerpAbi = exports.OrdersGatewayAbi = exports.Socket_VaultWithPayloadAbi = exports.PeripheryAbi = exports.OracleAdaptersAbi = exports.MulticallAbi = exports.ErrorsAbi = exports.CoreAbi = void 0;
|
|
18
18
|
__exportStar(require("./action"), exports);
|
|
19
19
|
var CoreProxy_json_1 = require("./abis/CoreProxy.json");
|
|
20
20
|
Object.defineProperty(exports, "CoreAbi", { enumerable: true, get: function () { return CoreProxy_json_1.abi; } });
|
|
@@ -30,6 +30,8 @@ var VaultWithPayload_json_1 = require("./abis/socket/VaultWithPayload.json");
|
|
|
30
30
|
Object.defineProperty(exports, "Socket_VaultWithPayloadAbi", { enumerable: true, get: function () { return VaultWithPayload_json_1.abi; } });
|
|
31
31
|
var OrdersGateway_json_1 = require("./abis/OrdersGateway.json");
|
|
32
32
|
Object.defineProperty(exports, "OrdersGatewayAbi", { enumerable: true, get: function () { return OrdersGateway_json_1.abi; } });
|
|
33
|
+
var PassivePerpProxy_json_1 = require("./abis/PassivePerpProxy.json");
|
|
34
|
+
Object.defineProperty(exports, "PassivePerpAbi", { enumerable: true, get: function () { return PassivePerpProxy_json_1.abi; } });
|
|
33
35
|
__exportStar(require("./contractAddresses"), exports);
|
|
34
36
|
__exportStar(require("./routerCommands"), exports);
|
|
35
37
|
__exportStar(require("./sign"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,wDAAuD;AAA9C,yGAAA,GAAG,OAAW;AACvB,kDAAsD;AAA7C,wGAAA,GAAG,OAAa;AACzB,sEAAmE;AAA1D,qHAAA,GAAG,OAAgB;AAC5B,4EAA2E;AAAlE,6HAAA,GAAG,OAAqB;AACjC,wDAA4D;AAAnD,8GAAA,GAAG,OAAgB;AAC5B,6EAAwF;AAA/E,mIAAA,GAAG,OAA8B;AAC1C,gEAAoE;AAA3D,sHAAA,GAAG,OAAoB;AAChC,sDAAoC;AACpC,mDAAiC;AACjC,yCAAuB;AACvB,0CAAwB;AACxB,8CAA4B;AAC5B,2CAAyB;AACzB,uDAAqC;AACrC,gDAA8B;AAC9B,qDAAmC;AACnC,wDAAsC","sourcesContent":["export * from './action';\nexport { abi as CoreAbi } from './abis/CoreProxy.json';\nexport { abi as ErrorsAbi } from './abis/Errors.json';\nexport { abi as MulticallAbi } from './abis/CustomMulticall3.json';\nexport { abi as OracleAdaptersAbi } from './abis/OracleAdaptersProxy.json';\nexport { abi as PeripheryAbi } from './abis/Periphery.json';\nexport { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';\nexport { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';\nexport * from './contractAddresses';\nexport * from './routerCommands';\nexport * from './sign';\nexport * from './trade';\nexport * from './txHelpers';\nexport * from './consts';\nexport * from './executeTransaction';\nexport * from './permissions';\nexport * from './buildMulticallTx';\nexport * from './encodeStorkUpdateTx';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,wDAAuD;AAA9C,yGAAA,GAAG,OAAW;AACvB,kDAAsD;AAA7C,wGAAA,GAAG,OAAa;AACzB,sEAAmE;AAA1D,qHAAA,GAAG,OAAgB;AAC5B,4EAA2E;AAAlE,6HAAA,GAAG,OAAqB;AACjC,wDAA4D;AAAnD,8GAAA,GAAG,OAAgB;AAC5B,6EAAwF;AAA/E,mIAAA,GAAG,OAA8B;AAC1C,gEAAoE;AAA3D,sHAAA,GAAG,OAAoB;AAChC,sEAAqE;AAA5D,uHAAA,GAAG,OAAkB;AAC9B,sDAAoC;AACpC,mDAAiC;AACjC,yCAAuB;AACvB,0CAAwB;AACxB,8CAA4B;AAC5B,2CAAyB;AACzB,uDAAqC;AACrC,gDAA8B;AAC9B,qDAAmC;AACnC,wDAAsC","sourcesContent":["export * from './action';\nexport { abi as CoreAbi } from './abis/CoreProxy.json';\nexport { abi as ErrorsAbi } from './abis/Errors.json';\nexport { abi as MulticallAbi } from './abis/CustomMulticall3.json';\nexport { abi as OracleAdaptersAbi } from './abis/OracleAdaptersProxy.json';\nexport { abi as PeripheryAbi } from './abis/Periphery.json';\nexport { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';\nexport { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';\nexport { abi as PassivePerpAbi } from './abis/PassivePerpProxy.json';\nexport * from './contractAddresses';\nexport * from './routerCommands';\nexport * from './sign';\nexport * from './trade';\nexport * from './txHelpers';\nexport * from './consts';\nexport * from './executeTransaction';\nexport * from './permissions';\nexport * from './buildMulticallTx';\nexport * from './encodeStorkUpdateTx';\n"]}
|
|
@@ -6,6 +6,7 @@ export { abi as OracleAdaptersAbi } from './abis/OracleAdaptersProxy.json';
|
|
|
6
6
|
export { abi as PeripheryAbi } from './abis/Periphery.json';
|
|
7
7
|
export { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';
|
|
8
8
|
export { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';
|
|
9
|
+
export { abi as PassivePerpAbi } from './abis/PassivePerpProxy.json';
|
|
9
10
|
export * from './contractAddresses';
|
|
10
11
|
export * from './routerCommands';
|
|
11
12
|
export * from './sign';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,GAAG,IAAI,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,GAAG,IAAI,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAAE,GAAG,IAAI,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,GAAG,IAAI,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EAAE,GAAG,IAAI,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AACpE,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,GAAG,IAAI,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,GAAG,IAAI,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAAE,GAAG,IAAI,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,GAAG,IAAI,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EAAE,GAAG,IAAI,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,GAAG,IAAI,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACrE,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC"}
|
|
@@ -11,9 +11,16 @@ export declare const calculateMaxWithdraw: ({ currentIMR, marginBalance, realBal
|
|
|
11
11
|
currentIMR: number;
|
|
12
12
|
/** Current margin balance of the account (expected to already include haircuts) in rUSD terms */
|
|
13
13
|
marginBalance: number;
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Current real balance of the token the user wants to withdraw in token terms.
|
|
16
|
+
* For rUSD: this equals net deposits (balanceDeprecated from v2 endpoints + realized PnL).
|
|
17
|
+
* For all other tokens: this equals balanceDeprecated (since realized PnL is 0 for them).
|
|
18
|
+
*/
|
|
15
19
|
realBalance: number;
|
|
16
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Haircut parameter of the token.
|
|
22
|
+
* Note: haircut for rUSD is always expected to be 0.
|
|
23
|
+
*/
|
|
17
24
|
haircut: number;
|
|
18
25
|
/** Price of the token in rUSD terms */
|
|
19
26
|
price: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calculate.d.ts","sourceRoot":"/","sources":["utils/calculate.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC,eAAO,MAAM,QAAQ,MAAM,CAAC;AAE5B,KAAK,QAAQ,GAAG,MAAM,CAAC;AACvB,KAAK,OAAO,GAAG,MAAM,CAAC;AAEtB;;;GAGG;AACH,eAAO,MAAM,oBAAoB;IAO/B,sEAAsE;gBAC1D,MAAM;IAClB,iGAAiG;mBAClF,MAAM;IACrB
|
|
1
|
+
{"version":3,"file":"calculate.d.ts","sourceRoot":"/","sources":["utils/calculate.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC,eAAO,MAAM,QAAQ,MAAM,CAAC;AAE5B,KAAK,QAAQ,GAAG,MAAM,CAAC;AACvB,KAAK,OAAO,GAAG,MAAM,CAAC;AAEtB;;;GAGG;AACH,eAAO,MAAM,oBAAoB;IAO/B,sEAAsE;gBAC1D,MAAM;IAClB,iGAAiG;mBAClF,MAAM;IACrB;;;;OAIG;iBACU,MAAM;IACnB;;;OAGG;aACM,MAAM;IACf,uCAAuC;WAChC,MAAM;MACX,MAiBH,CAAC;AAEF,eAAO,MAAM,oBAAoB;iBAKlB,MAAM;;gBAEP,MAAM;YACiC,CAAC;AAEtD,eAAO,MAAM,gCAAgC;eAQhC,MAAM;iBACJ,MAAM;mBACJ,MAAM;qBACJ,MAAM;uBACJ,MAAM;kBACX,MAAM;MAClB,MAWH,CAAC;AAEF,eAAO,MAAM,iBAAiB;aAInB,MAAM;mBACA,MAAM;YACgB,CA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mBAAmB,EAAE,MAAM,CAAC;QAC5B,mBAAmB,EAAE,MAAM,CAAC;QAC5B,iBAAiB,EAAE,MAAM,CAAC;KAC3B,CACF;eACU;QACT,cAAc,EAAE,MAAM,CAAC;QACvB,IAAI,EAAE,MAAM,CAAC;QACb,cAAc,EAAE,MAAM,CAAC;QACvB,YAAY,EAAE,MAAM,CAAC;QACrB,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,EAAE,MAAM,CAAC;KACrB,EAAE;;YA6DJ,CAAC;AAEF,eAAO,MAAM,mBAAmB;wBAMV,MAAM;0BACJ,MAAM;kCACE,MAAM;0BACd,MAAM;MAC1B,MAOH,CAAC;AAEF,eAAO,MAAM,iBAAiB;WAKrB,MAAM;2BACU,MAAM;0BACP,MAAM;MAC1B,MAEH,CAAC;AAEF,eAAO,MAAM,oBAAoB,gBAClB,SAAS,gBACR,SAAS,cAGxB,CAAC;AAEF,eAAO,MAAM,4BAA4B,YAC9B,SAAS,wBACI,SAAS,0BACP,SAAS,KAChC,SAEF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,uBAAuB;iCAQL,SAAS;iCACT,SAAS;kBACxB,SAAS;2BACA,SAAS;mCACD,SAAS;mCACT,SAAS;MACtC,SAqBH,CAAC"}
|
package/dist/utils/calculate.js
CHANGED
|
@@ -19,11 +19,11 @@ var calculateMaxWithdraw = function (_a) {
|
|
|
19
19
|
if (currentIMR === 0) {
|
|
20
20
|
return realBalance;
|
|
21
21
|
}
|
|
22
|
-
if (haircut ===
|
|
22
|
+
if (haircut === 1 || price === 0) {
|
|
23
23
|
return 0;
|
|
24
24
|
}
|
|
25
25
|
var availableMargin = marginBalance - currentIMR;
|
|
26
|
-
var maxFromMargin = availableMargin / (price * haircut);
|
|
26
|
+
var maxFromMargin = availableMargin / (price * (1 - haircut));
|
|
27
27
|
return Math.max(0, Math.min(realBalance, maxFromMargin));
|
|
28
28
|
};
|
|
29
29
|
exports.calculateM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,OAAO,OAAO,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACzE,CAAC,CAAC;AANW,QAAA,4BAA4B,gCAMvC;AAEF;;GAEG;AACI,IAAM,uBAAuB,GAAG,UAAC,EAcvC;QAbC,2BAA2B,iCAAA,EAC3B,2BAA2B,iCAAA,EAC3B,YAAY,kBAAA,EACZ,qBAAqB,2BAAA,EACrB,6BAA6B,mCAAA,EAC7B,6BAA6B,mCAAA;IAS7B,IAAM,qBAAqB,GAAG,2BAA2B;SACtD,KAAK,CAAC,6BAA6B,CAAC;SACpC,YAAY,CAAC,IAAI,CAAC;SAClB,GAAG,CAAC,6BAA6B,CAAC,CAAC;IAEtC,IAAM,YAAY,GAAG,qBAAqB;SACvC,KAAK,CACJ,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC,KAAK,CACnB,2BAA2B;SACxB,YAAY,CAAC,IAAI,CAAC;SAClB,GAAG,CAAC,6BAA6B,CAAC,CACtC,CACF;SACA,GAAG,CAAC,IAAI,CAAC,CAAC;IAEb,IAAM,MAAM,GAAG,YAAY;SACxB,KAAK,CAAC,qBAAqB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;SAChD,GAAG,CAAC,IAAI,CAAC,CAAC;IAEb,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAnCW,QAAA,uBAAuB,2BAmClC","sourcesContent":["import { mergeMap } from './struct';\nimport BigNumber from 'bignumber.js';\n\nexport const POOL_IMR = 9.1;\n\ntype MarketId = string;\ntype Address = string;\n\n/**\n * Calculates the maximum amount that can be withdrawn in terms of the token\n * (e.g., if rUSD then in rUSD terms, if WETH then in WETH terms)\n */\nexport const calculateMaxWithdraw = ({\n currentIMR,\n marginBalance,\n realBalance,\n haircut,\n price,\n}: {\n /** Current Initial Margin Requirement of the account in rUSD terms */\n currentIMR: number;\n /** Current margin balance of the account (expected to already include haircuts) in rUSD terms */\n marginBalance: number;\n /** Current real balance of the token the user wants to withdraw in token terms */\n realBalance: number;\n /** Haircut parameter of the token */\n haircut: number;\n /** Price of the token in rUSD terms */\n price: number;\n}): number => {\n if (currentIMR < 0 || price < 0 || haircut < 0) {\n throw new Error('currentIMR, price, and haircut cannot be negative');\n }\n\n if (currentIMR === 0) {\n return realBalance;\n }\n\n if (haircut === 0 || price === 0) {\n return 0;\n }\n\n const availableMargin = marginBalance - currentIMR;\n const maxFromMargin = availableMargin / (price * haircut);\n\n return Math.max(0, Math.min(realBalance, maxFromMargin));\n};\n\nexport const calculateMaxExposure = ({\n poolBalance,\n riskFactor,\n poolIMR = POOL_IMR,\n}: {\n poolBalance: number;\n poolIMR?: number;\n riskFactor: number;\n}) => poolBalance / (poolIMR * Math.sqrt(riskFactor));\n\nexport const calculateEstimatedExecutionPrice = ({\n poolPrice,\n oraclePrice,\n tradeBaseSize,\n poolMaxExposure,\n marketDepthFactor,\n marketSpread,\n}: {\n poolPrice: number;\n oraclePrice: number;\n tradeBaseSize: number;\n poolMaxExposure: number;\n marketDepthFactor: number;\n marketSpread: number;\n}): number => {\n if (tradeBaseSize === 0) {\n return poolPrice;\n }\n\n const poolDeviation =\n (tradeBaseSize * oraclePrice) / (marketDepthFactor * poolMaxExposure);\n const appliedSpread = tradeBaseSize > 0 ? marketSpread : -marketSpread;\n const estExecutionPrice = poolPrice * (1 + poolDeviation + appliedSpread);\n\n return Math.max(0, estExecutionPrice);\n};\n\nexport const calculateTradeFee = ({\n feeRate,\n tradeNotional,\n}: {\n feeRate: number;\n tradeNotional: number;\n}) => feeRate * Math.abs(tradeNotional);\n\nexport const calculateSpread = ({\n depthFactor,\n maxExposure,\n oraclePrice,\n poolPrice,\n tradeNotional,\n}: {\n depthFactor: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n tradeNotional: number;\n}) =>\n (poolPrice / oraclePrice) *\n (1 + tradeNotional / (depthFactor * maxExposure) - 1) *\n tradeNotional;\n\nexport const calculateEstimatedLiquidationPrice = ({\n LMR,\n accountBalance,\n isShort,\n notional,\n oraclePrice,\n}: {\n LMR: number;\n accountBalance: number;\n isShort: boolean;\n notional: number;\n oraclePrice: number;\n}) =>\n Math.max(\n 0,\n oraclePrice *\n (1 - ((accountBalance - LMR) / notional) * (isShort ? -1 : 1)),\n );\n\nexport const calculateLeverage = ({\n imrMultiplier,\n riskFactor,\n}: {\n imrMultiplier: number;\n riskFactor: number;\n}) => 1 / (imrMultiplier * Math.sqrt(riskFactor));\n\nexport const calculateRequiredMargin = ({\n currIMR,\n currentNotional,\n leverage,\n tradeNotional,\n}: {\n currIMR: number;\n currentNotional: number;\n leverage: number;\n tradeNotional: number;\n}) =>\n currIMR +\n (Math.abs(currentNotional + tradeNotional) - Math.abs(currentNotional)) /\n leverage;\n\nexport const calculateMaxOrderSize = ({\n currIMR,\n currNodeBalance,\n currNotional,\n depthFactor,\n extendingPosition,\n feeRate,\n isShortTrade,\n leverage,\n maxExposure,\n oraclePrice,\n poolPrice,\n}: {\n currIMR: number;\n currNodeBalance: number;\n currNotional: number;\n depthFactor: number;\n extendingPosition: boolean;\n feeRate: number;\n isShortTrade: boolean;\n leverage: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n}) => {\n const a = poolPrice / oraclePrice;\n const c = 1 / (depthFactor * maxExposure);\n const R = a - 1 - feeRate * (isShortTrade ? -1 : 1);\n const SN = currNotional < 0 ? -1 : 1;\n const Q = R - (SN / leverage) * (extendingPosition ? 1 : -1);\n return Math.abs(\n (-1 * Q +\n Math.sqrt(\n Math.pow(Q, 2) -\n 4 *\n a *\n c *\n (currNodeBalance -\n currIMR +\n (extendingPosition ? 0 : ((2 * SN) / leverage) * currNotional)),\n ) *\n (isShortTrade ? 1 : -1)) /\n (2 * a * c),\n );\n};\n\nexport const calculateIMR = ({\n marketLeverages,\n marketPrices,\n positions,\n}: {\n marketLeverages: Map<MarketId, number>;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n}) => {\n return Array.from(positions.entries()).reduce(\n (sum, [marketId, positionSize]) => {\n const leverage = marketLeverages.get(marketId);\n return leverage\n ? sum +\n (Math.abs(positionSize) * (marketPrices.get(marketId) ?? 0)) /\n leverage\n : sum;\n },\n 0,\n );\n};\n\nexport const calculateLMR = ({\n marketPrices,\n positions,\n riskFactors,\n marketId,\n tradeSize,\n}: {\n marketId?: string;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n riskFactors: Map<MarketId, number>;\n tradeSize?: number;\n}) =>\n Array.from(\n (marketId && tradeSize\n ? mergeMap(positions, [\n [marketId, (positions.get(marketId) ?? 0) + tradeSize],\n ])\n : positions\n ).entries(),\n ).reduce((sum, [marketId, positionSize]) => {\n const riskFactor = riskFactors.get(marketId);\n return riskFactor\n ? sum +\n Math.abs(positionSize) *\n (marketPrices.get(marketId) ?? 0) *\n Math.sqrt(riskFactor)\n : sum;\n }, 0);\n\n// if account balance is below LMR position is liquidable\n\nexport const calculateMarginRatio = ({\n LMR,\n totalBalance,\n tradeCost,\n}: {\n LMR: number;\n totalBalance: number;\n tradeCost: number;\n}) => {\n if (LMR === 0) {\n return 0;\n }\n if (tradeCost > totalBalance) {\n return 1;\n }\n if (totalBalance === tradeCost) {\n return Infinity;\n }\n return Math.min(1, LMR / (totalBalance - tradeCost));\n};\n\nexport const calculateTotalMargin = ({\n marketsTrackers,\n accountBalances,\n assetPrices,\n marketsTokenInfo,\n positions,\n rUSDAddress,\n}: {\n accountBalances: Map<Address, number>;\n assetPrices: Map<Address, number>;\n marketsTokenInfo: Map<\n MarketId,\n {\n quoteTokenId: string;\n underlyingAssetId: string;\n }\n >;\n marketsTrackers: Map<\n MarketId,\n {\n longADLUnwindPrice: number;\n longBaseMultiplier: number;\n longFundingValue: number;\n shortADLUnwindPrice: number;\n shortBaseMultiplier: number;\n shortFundingValue: number;\n }\n >;\n positions: {\n adlUnwindPrice: number;\n base: number;\n baseMultiplier: number;\n fundingValue: number;\n lastPrice: number;\n marketId: string;\n realizedPnl: number;\n }[];\n rUSDAddress?: string;\n}) => {\n const pnl = new Map<Address, number>();\n const rUSDPrice = (rUSDAddress && assetPrices.get(rUSDAddress)) ?? 1;\n positions.forEach((position) => {\n const marketTracker = marketsTrackers?.get(position.marketId);\n const tokenInfo = marketsTokenInfo.get(position.marketId);\n if (!tokenInfo) return;\n let fundingCashFlow = 0;\n let adlCashFlow = 0;\n\n if (marketTracker) {\n // fundingCashFlow\n const marketTrackerFundingValue =\n position.base < 0\n ? marketTracker.shortFundingValue\n : marketTracker.longFundingValue;\n const trackerDelta = marketTrackerFundingValue - position.fundingValue;\n fundingCashFlow =\n (trackerDelta / position.baseMultiplier) * -1 * position.base;\n\n // adlCashFlow\n const adlUnwindPrice =\n ((position.base < 0\n ? marketTracker.shortADLUnwindPrice\n : marketTracker.longADLUnwindPrice) -\n position.adlUnwindPrice) /\n position.baseMultiplier;\n const baseMultiplier =\n (position.base < 0\n ? marketTracker.shortBaseMultiplier\n : marketTracker.longBaseMultiplier) / position.baseMultiplier;\n const lastPriceCoefficient = 1 - baseMultiplier;\n const initialPrice = lastPriceCoefficient * position.lastPrice;\n adlCashFlow = (adlUnwindPrice - initialPrice) * position.base;\n }\n\n const unrealizedPnl =\n ((assetPrices.get(tokenInfo.quoteTokenId) ?? 0) - position.lastPrice) *\n position.base;\n\n pnl.set(\n tokenInfo.underlyingAssetId,\n (pnl.get(tokenInfo.underlyingAssetId) ?? 0) +\n position.realizedPnl +\n fundingCashFlow +\n adlCashFlow +\n unrealizedPnl,\n );\n });\n\n return Array.from(accountBalances.entries()).reduce(\n (sum, [address, balance]) =>\n sum +\n (balance + (pnl.get(address) ?? 0)) *\n (address === rUSDAddress\n ? 1\n : (assetPrices.get(address) ?? 0) / (rUSDPrice || 1)),\n 0,\n );\n};\n\nexport const calculateFundingPnl = ({\n marketFundingValue,\n marketBaseMultiplier,\n positionAvgEntryFundingValue,\n positionAdjustedBase,\n}: {\n marketFundingValue: number;\n marketBaseMultiplier: number;\n positionAvgEntryFundingValue: number;\n positionAdjustedBase: number;\n}): number => {\n return (\n -1 *\n (((marketFundingValue - positionAvgEntryFundingValue) *\n positionAdjustedBase) /\n marketBaseMultiplier)\n );\n};\n\nexport const calculatePricePnl = ({\n price,\n positionAvgEntryPrice,\n positionAdjustedBase,\n}: {\n price: number;\n positionAvgEntryPrice: number;\n positionAdjustedBase: number;\n}): number => {\n return (price - positionAvgEntryPrice) * positionAdjustedBase;\n};\n\nexport const calculateRealBalance = (\n netDeposits: BigNumber,\n realized_pnl: BigNumber,\n) => {\n return netDeposits.plus(realized_pnl);\n};\n\nexport const calculatePositionAdjustedQty = (\n origQty: BigNumber,\n marketBaseMultiplier: BigNumber,\n positionBaseMultiplier: BigNumber,\n): BigNumber => {\n return origQty.times(marketBaseMultiplier).div(positionBaseMultiplier);\n};\n\n/**\n * Note: This function assumes all the inputs are scaled by 10^18.\n */\nexport const calculatePositionAdlPnL = ({\n marketTrackerAdlUnwindPrice,\n marketTrackerBaseMultiplier,\n positionBase,\n positionAvgEntryPrice,\n positionTrackerBaseMultiplier,\n positionTrackerAdlUnwindPrice,\n}: {\n marketTrackerAdlUnwindPrice: BigNumber;\n marketTrackerBaseMultiplier: BigNumber;\n positionBase: BigNumber;\n positionAvgEntryPrice: BigNumber;\n positionTrackerBaseMultiplier: BigNumber;\n positionTrackerAdlUnwindPrice: BigNumber;\n}): BigNumber => {\n const currentAdlUnwindPrice = marketTrackerAdlUnwindPrice\n .minus(positionTrackerAdlUnwindPrice)\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier);\n\n const initialPrice = positionAvgEntryPrice\n .times(\n BigNumber(1e18).minus(\n marketTrackerBaseMultiplier\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier),\n ),\n )\n .div(1e18);\n\n const adlPnL = positionBase\n .times(currentAdlUnwindPrice.minus(initialPrice))\n .div(1e18);\n\n return adlPnL;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"calculate.js","sourceRoot":"/","sources":["utils/calculate.ts"],"names":[],"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CAAC,CAAC;AANW,QAAA,4BAA4B,gCAMvC;AAEF;;GAEG;AACI,IAAM,uBAAuB,GAAG,UAAC,EAcvC;QAbC,2BAA2B,iCAAA,EAC3B,2BAA2B,iCAAA,EAC3B,YAAY,kBAAA,EACZ,qBAAqB,2BAAA,EACrB,6BAA6B,mCAAA,EAC7B,6BAA6B,mCAAA;IAS7B,IAAM,qBAAqB,GAAG,2BAA2B;SACtD,KAAK,CAAC,6BAA6B,CAAC;SACpC,YAAY,CAAC,IAAI,CAAC;SAClB,GAAG,CAAC,6BAA6B,CAAC,CAAC;IAEtC,IAAM,YAAY,GAAG,qBAAqB;SACvC,KAAK,CACJ,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC,KAAK,CACnB,2BAA2B;SACxB,YAAY,CAAC,IAAI,CAAC;SAClB,GAAG,CAAC,6BAA6B,CAAC,CACtC,CACF;SACA,GAAG,CAAC,IAAI,CAAC,CAAC;IAEb,IAAM,MAAM,GAAG,YAAY;SACxB,KAAK,CAAC,qBAAqB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;SAChD,GAAG,CAAC,IAAI,CAAC,CAAC;IAEb,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAnCW,QAAA,uBAAuB,2BAmClC","sourcesContent":["import { mergeMap } from './struct';\nimport BigNumber from 'bignumber.js';\n\nexport const POOL_IMR = 9.1;\n\ntype MarketId = string;\ntype Address = string;\n\n/**\n * Calculates the maximum amount that can be withdrawn in terms of the token\n * (e.g., if rUSD then in rUSD terms, if WETH then in WETH terms)\n */\nexport const calculateMaxWithdraw = ({\n currentIMR,\n marginBalance,\n realBalance,\n haircut,\n price,\n}: {\n /** Current Initial Margin Requirement of the account in rUSD terms */\n currentIMR: number;\n /** Current margin balance of the account (expected to already include haircuts) in rUSD terms */\n marginBalance: number;\n /**\n * Current real balance of the token the user wants to withdraw in token terms.\n * For rUSD: this equals net deposits (balanceDeprecated from v2 endpoints + realized PnL).\n * For all other tokens: this equals balanceDeprecated (since realized PnL is 0 for them).\n */\n realBalance: number;\n /**\n * Haircut parameter of the token.\n * Note: haircut for rUSD is always expected to be 0.\n */\n haircut: number;\n /** Price of the token in rUSD terms */\n price: number;\n}): number => {\n if (currentIMR < 0 || price < 0 || haircut < 0) {\n throw new Error('currentIMR, price, and haircut cannot be negative');\n }\n\n if (currentIMR === 0) {\n return realBalance;\n }\n\n if (haircut === 1 || price === 0) {\n return 0;\n }\n\n const availableMargin = marginBalance - currentIMR;\n const maxFromMargin = availableMargin / (price * (1 - haircut));\n\n return Math.max(0, Math.min(realBalance, maxFromMargin));\n};\n\nexport const calculateMaxExposure = ({\n poolBalance,\n riskFactor,\n poolIMR = POOL_IMR,\n}: {\n poolBalance: number;\n poolIMR?: number;\n riskFactor: number;\n}) => poolBalance / (poolIMR * Math.sqrt(riskFactor));\n\nexport const calculateEstimatedExecutionPrice = ({\n poolPrice,\n oraclePrice,\n tradeBaseSize,\n poolMaxExposure,\n marketDepthFactor,\n marketSpread,\n}: {\n poolPrice: number;\n oraclePrice: number;\n tradeBaseSize: number;\n poolMaxExposure: number;\n marketDepthFactor: number;\n marketSpread: number;\n}): number => {\n if (tradeBaseSize === 0) {\n return poolPrice;\n }\n\n const poolDeviation =\n (tradeBaseSize * oraclePrice) / (marketDepthFactor * poolMaxExposure);\n const appliedSpread = tradeBaseSize > 0 ? marketSpread : -marketSpread;\n const estExecutionPrice = poolPrice * (1 + poolDeviation + appliedSpread);\n\n return Math.max(0, estExecutionPrice);\n};\n\nexport const calculateTradeFee = ({\n feeRate,\n tradeNotional,\n}: {\n feeRate: number;\n tradeNotional: number;\n}) => feeRate * Math.abs(tradeNotional);\n\nexport const calculateSpread = ({\n depthFactor,\n maxExposure,\n oraclePrice,\n poolPrice,\n tradeNotional,\n}: {\n depthFactor: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n tradeNotional: number;\n}) =>\n (poolPrice / oraclePrice) *\n (1 + tradeNotional / (depthFactor * maxExposure) - 1) *\n tradeNotional;\n\nexport const calculateEstimatedLiquidationPrice = ({\n LMR,\n accountBalance,\n isShort,\n notional,\n oraclePrice,\n}: {\n LMR: number;\n accountBalance: number;\n isShort: boolean;\n notional: number;\n oraclePrice: number;\n}) =>\n Math.max(\n 0,\n oraclePrice *\n (1 - ((accountBalance - LMR) / notional) * (isShort ? -1 : 1)),\n );\n\nexport const calculateLeverage = ({\n imrMultiplier,\n riskFactor,\n}: {\n imrMultiplier: number;\n riskFactor: number;\n}) => 1 / (imrMultiplier * Math.sqrt(riskFactor));\n\nexport const calculateRequiredMargin = ({\n currIMR,\n currentNotional,\n leverage,\n tradeNotional,\n}: {\n currIMR: number;\n currentNotional: number;\n leverage: number;\n tradeNotional: number;\n}) =>\n currIMR +\n (Math.abs(currentNotional + tradeNotional) - Math.abs(currentNotional)) /\n leverage;\n\nexport const calculateMaxOrderSize = ({\n currIMR,\n currNodeBalance,\n currNotional,\n depthFactor,\n extendingPosition,\n feeRate,\n isShortTrade,\n leverage,\n maxExposure,\n oraclePrice,\n poolPrice,\n}: {\n currIMR: number;\n currNodeBalance: number;\n currNotional: number;\n depthFactor: number;\n extendingPosition: boolean;\n feeRate: number;\n isShortTrade: boolean;\n leverage: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n}) => {\n const a = poolPrice / oraclePrice;\n const c = 1 / (depthFactor * maxExposure);\n const R = a - 1 - feeRate * (isShortTrade ? -1 : 1);\n const SN = currNotional < 0 ? -1 : 1;\n const Q = R - (SN / leverage) * (extendingPosition ? 1 : -1);\n return Math.abs(\n (-1 * Q +\n Math.sqrt(\n Math.pow(Q, 2) -\n 4 *\n a *\n c *\n (currNodeBalance -\n currIMR +\n (extendingPosition ? 0 : ((2 * SN) / leverage) * currNotional)),\n ) *\n (isShortTrade ? 1 : -1)) /\n (2 * a * c),\n );\n};\n\nexport const calculateIMR = ({\n marketLeverages,\n marketPrices,\n positions,\n}: {\n marketLeverages: Map<MarketId, number>;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n}) => {\n return Array.from(positions.entries()).reduce(\n (sum, [marketId, positionSize]) => {\n const leverage = marketLeverages.get(marketId);\n return leverage\n ? sum +\n (Math.abs(positionSize) * (marketPrices.get(marketId) ?? 0)) /\n leverage\n : sum;\n },\n 0,\n );\n};\n\nexport const calculateLMR = ({\n marketPrices,\n positions,\n riskFactors,\n marketId,\n tradeSize,\n}: {\n marketId?: string;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n riskFactors: Map<MarketId, number>;\n tradeSize?: number;\n}) =>\n Array.from(\n (marketId && tradeSize\n ? mergeMap(positions, [\n [marketId, (positions.get(marketId) ?? 0) + tradeSize],\n ])\n : positions\n ).entries(),\n ).reduce((sum, [marketId, positionSize]) => {\n const riskFactor = riskFactors.get(marketId);\n return riskFactor\n ? sum +\n Math.abs(positionSize) *\n (marketPrices.get(marketId) ?? 0) *\n Math.sqrt(riskFactor)\n : sum;\n }, 0);\n\n// if account balance is below LMR position is liquidable\n\nexport const calculateMarginRatio = ({\n LMR,\n totalBalance,\n tradeCost,\n}: {\n LMR: number;\n totalBalance: number;\n tradeCost: number;\n}) => {\n if (LMR === 0) {\n return 0;\n }\n if (tradeCost > totalBalance) {\n return 1;\n }\n if (totalBalance === tradeCost) {\n return Infinity;\n }\n return Math.min(1, LMR / (totalBalance - tradeCost));\n};\n\nexport const calculateTotalMargin = ({\n marketsTrackers,\n accountBalances,\n assetPrices,\n marketsTokenInfo,\n positions,\n rUSDAddress,\n}: {\n accountBalances: Map<Address, number>;\n assetPrices: Map<Address, number>;\n marketsTokenInfo: Map<\n MarketId,\n {\n quoteTokenId: string;\n underlyingAssetId: string;\n }\n >;\n marketsTrackers: Map<\n MarketId,\n {\n longADLUnwindPrice: number;\n longBaseMultiplier: number;\n longFundingValue: number;\n shortADLUnwindPrice: number;\n shortBaseMultiplier: number;\n shortFundingValue: number;\n }\n >;\n positions: {\n adlUnwindPrice: number;\n base: number;\n baseMultiplier: number;\n fundingValue: number;\n lastPrice: number;\n marketId: string;\n realizedPnl: number;\n }[];\n rUSDAddress?: string;\n}) => {\n const pnl = new Map<Address, number>();\n const rUSDPrice = (rUSDAddress && assetPrices.get(rUSDAddress)) ?? 1;\n positions.forEach((position) => {\n const marketTracker = marketsTrackers?.get(position.marketId);\n const tokenInfo = marketsTokenInfo.get(position.marketId);\n if (!tokenInfo) return;\n let fundingCashFlow = 0;\n let adlCashFlow = 0;\n\n if (marketTracker) {\n // fundingCashFlow\n const marketTrackerFundingValue =\n position.base < 0\n ? marketTracker.shortFundingValue\n : marketTracker.longFundingValue;\n const trackerDelta = marketTrackerFundingValue - position.fundingValue;\n fundingCashFlow =\n (trackerDelta / position.baseMultiplier) * -1 * position.base;\n\n // adlCashFlow\n const adlUnwindPrice =\n ((position.base < 0\n ? marketTracker.shortADLUnwindPrice\n : marketTracker.longADLUnwindPrice) -\n position.adlUnwindPrice) /\n position.baseMultiplier;\n const baseMultiplier =\n (position.base < 0\n ? marketTracker.shortBaseMultiplier\n : marketTracker.longBaseMultiplier) / position.baseMultiplier;\n const lastPriceCoefficient = 1 - baseMultiplier;\n const initialPrice = lastPriceCoefficient * position.lastPrice;\n adlCashFlow = (adlUnwindPrice - initialPrice) * position.base;\n }\n\n const unrealizedPnl =\n ((assetPrices.get(tokenInfo.quoteTokenId) ?? 0) - position.lastPrice) *\n position.base;\n\n pnl.set(\n tokenInfo.underlyingAssetId,\n (pnl.get(tokenInfo.underlyingAssetId) ?? 0) +\n position.realizedPnl +\n fundingCashFlow +\n adlCashFlow +\n unrealizedPnl,\n );\n });\n\n return Array.from(accountBalances.entries()).reduce(\n (sum, [address, balance]) =>\n sum +\n (balance + (pnl.get(address) ?? 0)) *\n (address === rUSDAddress\n ? 1\n : (assetPrices.get(address) ?? 0) / (rUSDPrice || 1)),\n 0,\n );\n};\n\nexport const calculateFundingPnl = ({\n marketFundingValue,\n marketBaseMultiplier,\n positionAvgEntryFundingValue,\n positionAdjustedBase,\n}: {\n marketFundingValue: number;\n marketBaseMultiplier: number;\n positionAvgEntryFundingValue: number;\n positionAdjustedBase: number;\n}): number => {\n return (\n -1 *\n (((marketFundingValue - positionAvgEntryFundingValue) *\n positionAdjustedBase) /\n marketBaseMultiplier)\n );\n};\n\nexport const calculatePricePnl = ({\n price,\n positionAvgEntryPrice,\n positionAdjustedBase,\n}: {\n price: number;\n positionAvgEntryPrice: number;\n positionAdjustedBase: number;\n}): number => {\n return (price - positionAvgEntryPrice) * positionAdjustedBase;\n};\n\nexport const calculateRealBalance = (\n netDeposits: BigNumber,\n realized_pnl: BigNumber,\n) => {\n return netDeposits.plus(realized_pnl);\n};\n\nexport const calculatePositionAdjustedQty = (\n origQty: BigNumber,\n marketBaseMultiplier: BigNumber,\n positionBaseMultiplier: BigNumber,\n): BigNumber => {\n return origQty.times(marketBaseMultiplier).div(positionBaseMultiplier);\n};\n\n/**\n * Note: This function assumes all the inputs are scaled by 10^18.\n */\nexport const calculatePositionAdlPnL = ({\n marketTrackerAdlUnwindPrice,\n marketTrackerBaseMultiplier,\n positionBase,\n positionAvgEntryPrice,\n positionTrackerBaseMultiplier,\n positionTrackerAdlUnwindPrice,\n}: {\n marketTrackerAdlUnwindPrice: BigNumber;\n marketTrackerBaseMultiplier: BigNumber;\n positionBase: BigNumber;\n positionAvgEntryPrice: BigNumber;\n positionTrackerBaseMultiplier: BigNumber;\n positionTrackerAdlUnwindPrice: BigNumber;\n}): BigNumber => {\n const currentAdlUnwindPrice = marketTrackerAdlUnwindPrice\n .minus(positionTrackerAdlUnwindPrice)\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier);\n\n const initialPrice = positionAvgEntryPrice\n .times(\n BigNumber(1e18).minus(\n marketTrackerBaseMultiplier\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier),\n ),\n )\n .div(1e18);\n\n const adlPnL = positionBase\n .times(currentAdlUnwindPrice.minus(initialPrice))\n .div(1e18);\n\n return adlPnL;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/common",
|
|
3
|
-
"version": "0.330.
|
|
3
|
+
"version": "0.330.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
45
45
|
},
|
|
46
46
|
"packageManager": "pnpm@8.3.1",
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "8ed4e282f479d80bb791fca2b28b1699eaefb0ee"
|
|
48
48
|
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
{
|
|
2
|
+
"abi": [
|
|
3
|
+
{
|
|
4
|
+
"type": "function",
|
|
5
|
+
"name": "getPoolMaxExposures",
|
|
6
|
+
"inputs": [
|
|
7
|
+
{
|
|
8
|
+
"name": "marketId",
|
|
9
|
+
"type": "uint128",
|
|
10
|
+
"internalType": "uint128"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
"outputs": [
|
|
14
|
+
{
|
|
15
|
+
"name": "maxShortExposure",
|
|
16
|
+
"type": "uint256",
|
|
17
|
+
"internalType": "UD60x18"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"name": "maxLongExposure",
|
|
21
|
+
"type": "uint256",
|
|
22
|
+
"internalType": "UD60x18"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"stateMutability": "view"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"type": "function",
|
|
29
|
+
"name": "getMarketConfiguration",
|
|
30
|
+
"inputs": [
|
|
31
|
+
{
|
|
32
|
+
"name": "marketId",
|
|
33
|
+
"type": "uint128",
|
|
34
|
+
"internalType": "uint128"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"outputs": [
|
|
38
|
+
{
|
|
39
|
+
"name": "config",
|
|
40
|
+
"type": "tuple",
|
|
41
|
+
"internalType": "struct MarketConfigurationData",
|
|
42
|
+
"components": [
|
|
43
|
+
{
|
|
44
|
+
"name": "riskMatrixIndex",
|
|
45
|
+
"type": "uint256",
|
|
46
|
+
"internalType": "uint256"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"name": "maxOpenBase",
|
|
50
|
+
"type": "uint256",
|
|
51
|
+
"internalType": "UD60x18"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "velocityMultiplier",
|
|
55
|
+
"type": "uint256",
|
|
56
|
+
"internalType": "UD60x18"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"name": "oracleNodeId",
|
|
60
|
+
"type": "bytes32",
|
|
61
|
+
"internalType": "bytes32"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "mtmWindow",
|
|
65
|
+
"type": "uint256",
|
|
66
|
+
"internalType": "uint256"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"name": "dutchConfig",
|
|
70
|
+
"type": "tuple",
|
|
71
|
+
"internalType": "struct DutchConfiguration",
|
|
72
|
+
"components": [
|
|
73
|
+
{
|
|
74
|
+
"name": "lambda",
|
|
75
|
+
"type": "uint256",
|
|
76
|
+
"internalType": "UD60x18"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"name": "minBase",
|
|
80
|
+
"type": "uint256",
|
|
81
|
+
"internalType": "UD60x18"
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"name": "slippageParams",
|
|
87
|
+
"type": "tuple",
|
|
88
|
+
"internalType": "struct SlippageParams",
|
|
89
|
+
"components": [
|
|
90
|
+
{
|
|
91
|
+
"name": "phi",
|
|
92
|
+
"type": "uint256",
|
|
93
|
+
"internalType": "UD60x18"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"name": "beta",
|
|
97
|
+
"type": "uint256",
|
|
98
|
+
"internalType": "UD60x18"
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"name": "minimumOrderBase",
|
|
104
|
+
"type": "uint256",
|
|
105
|
+
"internalType": "UD60x18"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"name": "baseSpacing",
|
|
109
|
+
"type": "uint256",
|
|
110
|
+
"internalType": "UD60x18"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"name": "priceSpacing",
|
|
114
|
+
"type": "uint256",
|
|
115
|
+
"internalType": "UD60x18"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"name": "depthFactor",
|
|
119
|
+
"type": "uint256",
|
|
120
|
+
"internalType": "UD60x18"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"name": "maxExposureFactor",
|
|
124
|
+
"type": "uint256",
|
|
125
|
+
"internalType": "UD60x18"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"name": "maxPSlippage",
|
|
129
|
+
"type": "uint256",
|
|
130
|
+
"internalType": "UD60x18"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"name": "marketOrderMaxStaleDuration",
|
|
134
|
+
"type": "uint256",
|
|
135
|
+
"internalType": "uint256"
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
"name": "priceSpread",
|
|
139
|
+
"type": "uint256",
|
|
140
|
+
"internalType": "UD60x18"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"name": "volatilityIndexMultiplier",
|
|
144
|
+
"type": "uint256",
|
|
145
|
+
"internalType": "UD60x18"
|
|
146
|
+
}
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
],
|
|
150
|
+
"stateMutability": "view"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"type": "function",
|
|
154
|
+
"name": "setMarketConfiguration",
|
|
155
|
+
"inputs": [
|
|
156
|
+
{
|
|
157
|
+
"name": "marketId",
|
|
158
|
+
"type": "uint128",
|
|
159
|
+
"internalType": "uint128"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"name": "config",
|
|
163
|
+
"type": "tuple",
|
|
164
|
+
"internalType": "struct MarketConfigurationData",
|
|
165
|
+
"components": [
|
|
166
|
+
{
|
|
167
|
+
"name": "riskMatrixIndex",
|
|
168
|
+
"type": "uint256",
|
|
169
|
+
"internalType": "uint256"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"name": "maxOpenBase",
|
|
173
|
+
"type": "uint256",
|
|
174
|
+
"internalType": "UD60x18"
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"name": "velocityMultiplier",
|
|
178
|
+
"type": "uint256",
|
|
179
|
+
"internalType": "UD60x18"
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
"name": "oracleNodeId",
|
|
183
|
+
"type": "bytes32",
|
|
184
|
+
"internalType": "bytes32"
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"name": "mtmWindow",
|
|
188
|
+
"type": "uint256",
|
|
189
|
+
"internalType": "uint256"
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
"name": "dutchConfig",
|
|
193
|
+
"type": "tuple",
|
|
194
|
+
"internalType": "struct DutchConfiguration",
|
|
195
|
+
"components": [
|
|
196
|
+
{
|
|
197
|
+
"name": "lambda",
|
|
198
|
+
"type": "uint256",
|
|
199
|
+
"internalType": "UD60x18"
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"name": "minBase",
|
|
203
|
+
"type": "uint256",
|
|
204
|
+
"internalType": "UD60x18"
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
"name": "slippageParams",
|
|
210
|
+
"type": "tuple",
|
|
211
|
+
"internalType": "struct SlippageParams",
|
|
212
|
+
"components": [
|
|
213
|
+
{
|
|
214
|
+
"name": "phi",
|
|
215
|
+
"type": "uint256",
|
|
216
|
+
"internalType": "UD60x18"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"name": "beta",
|
|
220
|
+
"type": "uint256",
|
|
221
|
+
"internalType": "UD60x18"
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
"name": "minimumOrderBase",
|
|
227
|
+
"type": "uint256",
|
|
228
|
+
"internalType": "UD60x18"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
"name": "baseSpacing",
|
|
232
|
+
"type": "uint256",
|
|
233
|
+
"internalType": "UD60x18"
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
"name": "priceSpacing",
|
|
237
|
+
"type": "uint256",
|
|
238
|
+
"internalType": "UD60x18"
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
"name": "depthFactor",
|
|
242
|
+
"type": "uint256",
|
|
243
|
+
"internalType": "UD60x18"
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"name": "maxExposureFactor",
|
|
247
|
+
"type": "uint256",
|
|
248
|
+
"internalType": "UD60x18"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"name": "maxPSlippage",
|
|
252
|
+
"type": "uint256",
|
|
253
|
+
"internalType": "UD60x18"
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
"name": "marketOrderMaxStaleDuration",
|
|
257
|
+
"type": "uint256",
|
|
258
|
+
"internalType": "uint256"
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
"name": "priceSpread",
|
|
262
|
+
"type": "uint256",
|
|
263
|
+
"internalType": "UD60x18"
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
"name": "volatilityIndexMultiplier",
|
|
267
|
+
"type": "uint256",
|
|
268
|
+
"internalType": "UD60x18"
|
|
269
|
+
}
|
|
270
|
+
]
|
|
271
|
+
}
|
|
272
|
+
],
|
|
273
|
+
"outputs": [],
|
|
274
|
+
"stateMutability": "nonpayable"
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
"type": "function",
|
|
278
|
+
"name": "setMarketConfigurationDepth",
|
|
279
|
+
"inputs": [
|
|
280
|
+
{
|
|
281
|
+
"name": "marketId",
|
|
282
|
+
"type": "uint128",
|
|
283
|
+
"internalType": "uint128"
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
"name": "depthFactor",
|
|
287
|
+
"type": "uint256",
|
|
288
|
+
"internalType": "UD60x18"
|
|
289
|
+
}
|
|
290
|
+
],
|
|
291
|
+
"outputs": [],
|
|
292
|
+
"stateMutability": "nonpayable"
|
|
293
|
+
}
|
|
294
|
+
]
|
|
295
|
+
}
|
|
@@ -6,6 +6,7 @@ export { abi as OracleAdaptersAbi } from './abis/OracleAdaptersProxy.json';
|
|
|
6
6
|
export { abi as PeripheryAbi } from './abis/Periphery.json';
|
|
7
7
|
export { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';
|
|
8
8
|
export { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';
|
|
9
|
+
export { abi as PassivePerpAbi } from './abis/PassivePerpProxy.json';
|
|
9
10
|
export * from './contractAddresses';
|
|
10
11
|
export * from './routerCommands';
|
|
11
12
|
export * from './sign';
|
package/src/utils/calculate.ts
CHANGED
|
@@ -21,9 +21,16 @@ export const calculateMaxWithdraw = ({
|
|
|
21
21
|
currentIMR: number;
|
|
22
22
|
/** Current margin balance of the account (expected to already include haircuts) in rUSD terms */
|
|
23
23
|
marginBalance: number;
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Current real balance of the token the user wants to withdraw in token terms.
|
|
26
|
+
* For rUSD: this equals net deposits (balanceDeprecated from v2 endpoints + realized PnL).
|
|
27
|
+
* For all other tokens: this equals balanceDeprecated (since realized PnL is 0 for them).
|
|
28
|
+
*/
|
|
25
29
|
realBalance: number;
|
|
26
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Haircut parameter of the token.
|
|
32
|
+
* Note: haircut for rUSD is always expected to be 0.
|
|
33
|
+
*/
|
|
27
34
|
haircut: number;
|
|
28
35
|
/** Price of the token in rUSD terms */
|
|
29
36
|
price: number;
|
|
@@ -36,12 +43,12 @@ export const calculateMaxWithdraw = ({
|
|
|
36
43
|
return realBalance;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
|
-
if (haircut ===
|
|
46
|
+
if (haircut === 1 || price === 0) {
|
|
40
47
|
return 0;
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
const availableMargin = marginBalance - currentIMR;
|
|
44
|
-
const maxFromMargin = availableMargin / (price * haircut);
|
|
51
|
+
const maxFromMargin = availableMargin / (price * (1 - haircut));
|
|
45
52
|
|
|
46
53
|
return Math.max(0, Math.min(realBalance, maxFromMargin));
|
|
47
54
|
};
|