@nexusmutual/sdk 0.3.12 → 0.3.13
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/data/products.json +17 -17
- package/dist/index.d.mts +36 -35
- package/dist/index.d.ts +36 -35
- package/dist/index.js +250 -242
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +250 -242
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -105,235 +105,14 @@ __export(constants_exports, {
|
|
|
105
105
|
MAXIMUM_COVER_PERIOD: () => MAXIMUM_COVER_PERIOD,
|
|
106
106
|
MINIMUM_COVER_PERIOD: () => MINIMUM_COVER_PERIOD,
|
|
107
107
|
NEXUS_MUTUAL_DAO_TREASURY_ADDRESS: () => NEXUS_MUTUAL_DAO_TREASURY_ADDRESS,
|
|
108
|
-
SLIPPAGE_DENOMINATOR: () => SLIPPAGE_DENOMINATOR,
|
|
109
|
-
TARGET_PRICE_DENOMINATOR: () => TARGET_PRICE_DENOMINATOR
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// src/quote/index.ts
|
|
113
|
-
var quote_exports = {};
|
|
114
|
-
__export(quote_exports, {
|
|
115
|
-
getQuoteAndBuyCoverInputs: () => getQuoteAndBuyCoverInputs
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// src/quote/getQuoteAndBuyCoverInputs.ts
|
|
119
|
-
var import_axios = __toESM(require("axios"));
|
|
120
|
-
async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, coverAsset, coverBuyerAddress, slippage = DEFAULT_SLIPPAGE / SLIPPAGE_DENOMINATOR, ipfsCid = "", coverRouterUrl = "https://api.nexusmutual.io/v2") {
|
|
121
|
-
if (!Number.isInteger(productId) || productId <= 0) {
|
|
122
|
-
return { result: void 0, error: { message: "Invalid productId: must be a positive integer" } };
|
|
123
|
-
}
|
|
124
|
-
if (typeof coverAmount !== "string" || !/^\d+$/.test(coverAmount) || parseInt(coverAmount, 10) <= 0) {
|
|
125
|
-
return { result: void 0, error: { message: "Invalid coverAmount: must be a positive integer string" } };
|
|
126
|
-
}
|
|
127
|
-
if (!Number.isInteger(coverPeriod) || coverPeriod < MINIMUM_COVER_PERIOD || coverPeriod > MAXIMUM_COVER_PERIOD) {
|
|
128
|
-
return {
|
|
129
|
-
result: void 0,
|
|
130
|
-
error: {
|
|
131
|
-
message: `Invalid coverPeriod: must be between ${MINIMUM_COVER_PERIOD} and ${MAXIMUM_COVER_PERIOD} days`
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
const coverAssetsString = Object.keys(CoverAsset).filter((k) => isNaN(+k)).map((k) => `CoverAsset.${k}`).join(", ");
|
|
136
|
-
if (!Object.values(CoverAsset).includes(coverAsset)) {
|
|
137
|
-
return {
|
|
138
|
-
result: void 0,
|
|
139
|
-
error: {
|
|
140
|
-
message: `Invalid coverAsset: must be one of ${coverAssetsString}`
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
if (!/^0x[a-fA-F0-9]{40}$/.test(coverBuyerAddress)) {
|
|
145
|
-
return { result: void 0, error: { message: "Invalid coverBuyerAddress: must be a valid Ethereum address" } };
|
|
146
|
-
}
|
|
147
|
-
if (typeof slippage !== "number" || slippage < 0 || slippage > 1) {
|
|
148
|
-
return {
|
|
149
|
-
result: void 0,
|
|
150
|
-
error: { message: "Invalid slippage: must be a number between 0 and 1" }
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
if (typeof ipfsCid !== "string") {
|
|
154
|
-
return { result: void 0, error: { message: "Invalid ipfsCid: must be a valid IPFS CID" } };
|
|
155
|
-
}
|
|
156
|
-
slippage = slippage * SLIPPAGE_DENOMINATOR;
|
|
157
|
-
try {
|
|
158
|
-
const { quote } = await getQuote(productId, coverAmount, coverPeriod, coverAsset, coverRouterUrl);
|
|
159
|
-
const maxPremiumInAsset = calculatePremiumWithCommissionAndSlippage(
|
|
160
|
-
BigInt(quote.premiumInAsset),
|
|
161
|
-
DEFAULT_COMMISSION_RATIO,
|
|
162
|
-
slippage
|
|
163
|
-
);
|
|
164
|
-
const yearlyCostPerc = calculatePremiumWithCommissionAndSlippage(
|
|
165
|
-
BigInt(quote.annualPrice),
|
|
166
|
-
DEFAULT_COMMISSION_RATIO,
|
|
167
|
-
slippage
|
|
168
|
-
);
|
|
169
|
-
const result = {
|
|
170
|
-
displayInfo: {
|
|
171
|
-
premiumInAsset: maxPremiumInAsset.toString(),
|
|
172
|
-
coverAmount,
|
|
173
|
-
yearlyCostPerc: Number(yearlyCostPerc) / TARGET_PRICE_DENOMINATOR,
|
|
174
|
-
maxCapacity: await getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl) ?? ""
|
|
175
|
-
},
|
|
176
|
-
buyCoverInput: {
|
|
177
|
-
buyCoverParams: {
|
|
178
|
-
coverId: 0 /* BUY */,
|
|
179
|
-
owner: coverBuyerAddress,
|
|
180
|
-
productId,
|
|
181
|
-
coverAsset,
|
|
182
|
-
amount: coverAmount,
|
|
183
|
-
period: coverPeriod * 60 * 60 * 24,
|
|
184
|
-
// seconds
|
|
185
|
-
maxPremiumInAsset: maxPremiumInAsset.toString(),
|
|
186
|
-
paymentAsset: coverAsset,
|
|
187
|
-
commissionRatio: DEFAULT_COMMISSION_RATIO,
|
|
188
|
-
commissionDestination: NEXUS_MUTUAL_DAO_TREASURY_ADDRESS,
|
|
189
|
-
ipfsData: ipfsCid
|
|
190
|
-
},
|
|
191
|
-
poolAllocationRequests: quote.poolAllocationRequests
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
return { result, error: void 0 };
|
|
195
|
-
} catch (error) {
|
|
196
|
-
const errorResponse = await handleError(error, productId, coverPeriod, coverAsset, coverRouterUrl);
|
|
197
|
-
return errorResponse;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
async function getQuote(productId, coverAmount, coverPeriod, coverAsset, coverRouterUrl) {
|
|
201
|
-
const params = { productId, amount: coverAmount, period: coverPeriod, coverAsset };
|
|
202
|
-
const response = await import_axios.default.get(coverRouterUrl + "/quote", { params });
|
|
203
|
-
if (!response.data) {
|
|
204
|
-
throw new Error("Failed to fetch cover quote");
|
|
205
|
-
}
|
|
206
|
-
return response.data;
|
|
207
|
-
}
|
|
208
|
-
async function getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl) {
|
|
209
|
-
const params = { period: coverPeriod };
|
|
210
|
-
const capacityUrl = coverRouterUrl + `/capacity/${productId}`;
|
|
211
|
-
const response = await import_axios.default.get(capacityUrl, { params });
|
|
212
|
-
if (!response.data) {
|
|
213
|
-
throw new Error("Failed to fetch cover capacities");
|
|
214
|
-
}
|
|
215
|
-
return response.data.availableCapacity.find((av) => av.assetId === coverAsset)?.amount;
|
|
216
|
-
}
|
|
217
|
-
async function handleError(error, productId, coverPeriod, coverAsset, coverRouterUrl) {
|
|
218
|
-
const axiosError = error;
|
|
219
|
-
if (axiosError.isAxiosError) {
|
|
220
|
-
if (axiosError.response?.data?.error?.includes("Not enough capacity")) {
|
|
221
|
-
const maxCapacity = await getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl);
|
|
222
|
-
return {
|
|
223
|
-
result: void 0,
|
|
224
|
-
error: {
|
|
225
|
-
message: axiosError.response?.data.error,
|
|
226
|
-
data: maxCapacity ? { maxCapacity } : void 0
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
return {
|
|
232
|
-
result: void 0,
|
|
233
|
-
error: { message: error.message || "Something went wrong" }
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// src/swap/index.ts
|
|
238
|
-
var swap_exports = {};
|
|
239
|
-
__export(swap_exports, {
|
|
240
|
-
calculateEthForExactNxm: () => calculateEthForExactNxm,
|
|
241
|
-
calculateExactEthForNxm: () => calculateExactEthForNxm,
|
|
242
|
-
calculateExactNxmForEth: () => calculateExactNxmForEth,
|
|
243
|
-
calculateNxmForExactEth: () => calculateNxmForExactEth,
|
|
244
|
-
calculatePriceImpactA: () => calculatePriceImpactA,
|
|
245
|
-
calculatePriceImpactB: () => calculatePriceImpactB,
|
|
246
|
-
calculateSpotPrice: () => calculateSpotPrice
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
// src/swap/calculateExactNxmForEth.ts
|
|
250
|
-
var calculateExactNxmForEth = (ethIn, reserves) => {
|
|
251
|
-
if (ethIn <= 0n) {
|
|
252
|
-
throw new Error("ETH in value must be greater than 0");
|
|
253
|
-
}
|
|
254
|
-
const k = reserves.nxmA * reserves.ethReserve;
|
|
255
|
-
const ethReservesAfter = reserves.ethReserve + ethIn;
|
|
256
|
-
const nxmReservesAfter = k / ethReservesAfter;
|
|
257
|
-
const nxmOut = reserves.nxmA - nxmReservesAfter;
|
|
258
|
-
if (nxmOut < 0n) {
|
|
259
|
-
throw new Error("Cannot swap this amount");
|
|
260
|
-
}
|
|
261
|
-
return nxmOut;
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
// src/swap/calculateExactEthForNxm.ts
|
|
265
|
-
var calculateExactEthForNxm = (nxmIn, reserves) => {
|
|
266
|
-
if (nxmIn <= 0n) {
|
|
267
|
-
throw new Error("NXM in value must be greater than 0");
|
|
268
|
-
}
|
|
269
|
-
const k = reserves.nxmB * reserves.ethReserve;
|
|
270
|
-
const nxmReservesAfter = reserves.nxmB + nxmIn;
|
|
271
|
-
const ethReservesAfter = k / nxmReservesAfter;
|
|
272
|
-
const ethOut = reserves.ethReserve - ethReservesAfter;
|
|
273
|
-
if (ethOut < 0n) {
|
|
274
|
-
throw new Error("Cannot swap this amount");
|
|
275
|
-
}
|
|
276
|
-
return ethOut;
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
// src/swap/calculateNxmForExactEth.ts
|
|
280
|
-
var calculateNxmForExactEth = (ethOut, reserves) => {
|
|
281
|
-
if (ethOut <= 0n || ethOut >= reserves.ethReserve) {
|
|
282
|
-
throw new Error("ETH out value must be greater than 0 and less than the reserves");
|
|
283
|
-
}
|
|
284
|
-
const k = reserves.nxmB * reserves.ethReserve;
|
|
285
|
-
const ethReservesAfter = reserves.ethReserve - ethOut;
|
|
286
|
-
const nxmReservesAfter = k / ethReservesAfter;
|
|
287
|
-
const nxmIn = nxmReservesAfter - reserves.nxmB;
|
|
288
|
-
return nxmIn;
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
// src/swap/calculateEthForExactNxm.ts
|
|
292
|
-
var calculateEthForExactNxm = (nxmOut, reserves) => {
|
|
293
|
-
if (nxmOut <= 0n || nxmOut >= reserves.nxmA) {
|
|
294
|
-
throw new Error("NXM out value must be greater than 0 and less than the reserves");
|
|
295
|
-
}
|
|
296
|
-
const k = reserves.nxmA * reserves.ethReserve;
|
|
297
|
-
const nxmReservesAfter = reserves.nxmA - nxmOut;
|
|
298
|
-
const ethReservesAfter = k / nxmReservesAfter;
|
|
299
|
-
const ethIn = ethReservesAfter - reserves.ethReserve;
|
|
300
|
-
return ethIn;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
// src/swap/calculateSpotPrice.ts
|
|
304
|
-
var calculateSpotPrice = (reserves) => {
|
|
305
|
-
const oneEth = BigInt(1e18);
|
|
306
|
-
return {
|
|
307
|
-
spotPriceA: oneEth * reserves.ethReserve / reserves.nxmA,
|
|
308
|
-
spotPriceB: oneEth * reserves.ethReserve / reserves.nxmB
|
|
309
|
-
};
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
// src/swap/calculatePriceImpactA.ts
|
|
313
|
-
var calculatePriceImpactA = (ethIn, reserves) => {
|
|
314
|
-
const { spotPriceA } = calculateSpotPrice(reserves);
|
|
315
|
-
const nxmOut = calculateExactNxmForEth(ethIn, reserves);
|
|
316
|
-
const nxmOutAtSpotPrice = BigInt(1e18) * ethIn / spotPriceA;
|
|
317
|
-
return BigInt(1e6) - BigInt(1e6) * nxmOut / nxmOutAtSpotPrice;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
// src/swap/calculatePriceImpactB.ts
|
|
321
|
-
var calculatePriceImpactB = (nxmIn, reserves) => {
|
|
322
|
-
const { spotPriceB } = calculateSpotPrice(reserves);
|
|
323
|
-
const ethOut = calculateExactEthForNxm(nxmIn, reserves);
|
|
324
|
-
const ethOutAtSpotPrice = spotPriceB * nxmIn / BigInt(1e18);
|
|
325
|
-
return BigInt(1e6) - BigInt(1e6) * ethOut / ethOutAtSpotPrice;
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
// src/types/index.ts
|
|
329
|
-
var types_exports = {};
|
|
330
|
-
__export(types_exports, {
|
|
331
108
|
ProductCategoryEnum: () => ProductCategoryEnum,
|
|
109
|
+
SLIPPAGE_DENOMINATOR: () => SLIPPAGE_DENOMINATOR,
|
|
110
|
+
TARGET_PRICE_DENOMINATOR: () => TARGET_PRICE_DENOMINATOR,
|
|
332
111
|
categoryLabelByEnum: () => categoryLabelByEnum,
|
|
333
112
|
productCategoryMap: () => productCategoryMap
|
|
334
113
|
});
|
|
335
114
|
|
|
336
|
-
// src/
|
|
115
|
+
// src/constants/products.ts
|
|
337
116
|
var ProductCategoryEnum = /* @__PURE__ */ ((ProductCategoryEnum2) => {
|
|
338
117
|
ProductCategoryEnum2["All"] = "all";
|
|
339
118
|
ProductCategoryEnum2["Lending"] = "lending";
|
|
@@ -619,12 +398,241 @@ var productCategoryMap = {
|
|
|
619
398
|
// Etherfi (Zircuit) Pendle
|
|
620
399
|
161: "liquid-restaking" /* LiquidRestaking */,
|
|
621
400
|
// KelpDAO (Zircuit) Pendle
|
|
622
|
-
162: "liquid-restaking" /* LiquidRestaking
|
|
401
|
+
162: "liquid-restaking" /* LiquidRestaking */,
|
|
623
402
|
// Renzo (Zircuit) Pendle
|
|
624
403
|
// 163: ProductCategoryEnum.Uncategorized, // Pocket Universe
|
|
625
404
|
// 164: ProductCategoryEnum.Uncategorized, // Request Finance
|
|
405
|
+
165: "yield-optimizer" /* YieldOptimizer */,
|
|
406
|
+
// Etherfi Liquid Market-Neutral USD Vault
|
|
407
|
+
166: "yield-optimizer" /* YieldOptimizer */,
|
|
408
|
+
// Superform
|
|
409
|
+
167: "liquid-restaking" /* LiquidRestaking */,
|
|
410
|
+
// EigenLayer + Etherfi
|
|
411
|
+
168: "yield-optimizer" /* YieldOptimizer */,
|
|
412
|
+
// Beefy CLM + Uniswap v3
|
|
413
|
+
169: "eth-staking" /* ETHStaking */
|
|
414
|
+
// RockX
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
// src/quote/index.ts
|
|
418
|
+
var quote_exports = {};
|
|
419
|
+
__export(quote_exports, {
|
|
420
|
+
getQuoteAndBuyCoverInputs: () => getQuoteAndBuyCoverInputs
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// src/quote/getQuoteAndBuyCoverInputs.ts
|
|
424
|
+
var import_axios = __toESM(require("axios"));
|
|
425
|
+
async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, coverAsset, coverBuyerAddress, slippage = DEFAULT_SLIPPAGE / SLIPPAGE_DENOMINATOR, ipfsCid = "", coverRouterUrl = "https://api.nexusmutual.io/v2") {
|
|
426
|
+
if (!Number.isInteger(productId) || productId <= 0) {
|
|
427
|
+
return { result: void 0, error: { message: "Invalid productId: must be a positive integer" } };
|
|
428
|
+
}
|
|
429
|
+
if (typeof coverAmount !== "string" || !/^\d+$/.test(coverAmount) || parseInt(coverAmount, 10) <= 0) {
|
|
430
|
+
return { result: void 0, error: { message: "Invalid coverAmount: must be a positive integer string" } };
|
|
431
|
+
}
|
|
432
|
+
if (!Number.isInteger(coverPeriod) || coverPeriod < MINIMUM_COVER_PERIOD || coverPeriod > MAXIMUM_COVER_PERIOD) {
|
|
433
|
+
return {
|
|
434
|
+
result: void 0,
|
|
435
|
+
error: {
|
|
436
|
+
message: `Invalid coverPeriod: must be between ${MINIMUM_COVER_PERIOD} and ${MAXIMUM_COVER_PERIOD} days`
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
const coverAssetsString = Object.keys(CoverAsset).filter((k) => isNaN(+k)).map((k) => `CoverAsset.${k}`).join(", ");
|
|
441
|
+
if (!Object.values(CoverAsset).includes(coverAsset)) {
|
|
442
|
+
return {
|
|
443
|
+
result: void 0,
|
|
444
|
+
error: {
|
|
445
|
+
message: `Invalid coverAsset: must be one of ${coverAssetsString}`
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(coverBuyerAddress)) {
|
|
450
|
+
return { result: void 0, error: { message: "Invalid coverBuyerAddress: must be a valid Ethereum address" } };
|
|
451
|
+
}
|
|
452
|
+
if (typeof slippage !== "number" || slippage < 0 || slippage > 1) {
|
|
453
|
+
return {
|
|
454
|
+
result: void 0,
|
|
455
|
+
error: { message: "Invalid slippage: must be a number between 0 and 1" }
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
if (typeof ipfsCid !== "string") {
|
|
459
|
+
return { result: void 0, error: { message: "Invalid ipfsCid: must be a valid IPFS CID" } };
|
|
460
|
+
}
|
|
461
|
+
slippage = slippage * SLIPPAGE_DENOMINATOR;
|
|
462
|
+
try {
|
|
463
|
+
const { quote } = await getQuote(productId, coverAmount, coverPeriod, coverAsset, coverRouterUrl);
|
|
464
|
+
const maxPremiumInAsset = calculatePremiumWithCommissionAndSlippage(
|
|
465
|
+
BigInt(quote.premiumInAsset),
|
|
466
|
+
DEFAULT_COMMISSION_RATIO,
|
|
467
|
+
slippage
|
|
468
|
+
);
|
|
469
|
+
const yearlyCostPerc = calculatePremiumWithCommissionAndSlippage(
|
|
470
|
+
BigInt(quote.annualPrice),
|
|
471
|
+
DEFAULT_COMMISSION_RATIO,
|
|
472
|
+
slippage
|
|
473
|
+
);
|
|
474
|
+
const result = {
|
|
475
|
+
displayInfo: {
|
|
476
|
+
premiumInAsset: maxPremiumInAsset.toString(),
|
|
477
|
+
coverAmount,
|
|
478
|
+
yearlyCostPerc: Number(yearlyCostPerc) / TARGET_PRICE_DENOMINATOR,
|
|
479
|
+
maxCapacity: await getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl) ?? ""
|
|
480
|
+
},
|
|
481
|
+
buyCoverInput: {
|
|
482
|
+
buyCoverParams: {
|
|
483
|
+
coverId: 0 /* BUY */,
|
|
484
|
+
owner: coverBuyerAddress,
|
|
485
|
+
productId,
|
|
486
|
+
coverAsset,
|
|
487
|
+
amount: coverAmount,
|
|
488
|
+
period: coverPeriod * 60 * 60 * 24,
|
|
489
|
+
// seconds
|
|
490
|
+
maxPremiumInAsset: maxPremiumInAsset.toString(),
|
|
491
|
+
paymentAsset: coverAsset,
|
|
492
|
+
commissionRatio: DEFAULT_COMMISSION_RATIO,
|
|
493
|
+
commissionDestination: NEXUS_MUTUAL_DAO_TREASURY_ADDRESS,
|
|
494
|
+
ipfsData: ipfsCid
|
|
495
|
+
},
|
|
496
|
+
poolAllocationRequests: quote.poolAllocationRequests
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
return { result, error: void 0 };
|
|
500
|
+
} catch (error) {
|
|
501
|
+
const errorResponse = await handleError(error, productId, coverPeriod, coverAsset, coverRouterUrl);
|
|
502
|
+
return errorResponse;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
async function getQuote(productId, coverAmount, coverPeriod, coverAsset, coverRouterUrl) {
|
|
506
|
+
const params = { productId, amount: coverAmount, period: coverPeriod, coverAsset };
|
|
507
|
+
const response = await import_axios.default.get(coverRouterUrl + "/quote", { params });
|
|
508
|
+
if (!response.data) {
|
|
509
|
+
throw new Error("Failed to fetch cover quote");
|
|
510
|
+
}
|
|
511
|
+
return response.data;
|
|
512
|
+
}
|
|
513
|
+
async function getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl) {
|
|
514
|
+
const params = { period: coverPeriod };
|
|
515
|
+
const capacityUrl = coverRouterUrl + `/capacity/${productId}`;
|
|
516
|
+
const response = await import_axios.default.get(capacityUrl, { params });
|
|
517
|
+
if (!response.data) {
|
|
518
|
+
throw new Error("Failed to fetch cover capacities");
|
|
519
|
+
}
|
|
520
|
+
return response.data.availableCapacity.find((av) => av.assetId === coverAsset)?.amount;
|
|
521
|
+
}
|
|
522
|
+
async function handleError(error, productId, coverPeriod, coverAsset, coverRouterUrl) {
|
|
523
|
+
const axiosError = error;
|
|
524
|
+
if (axiosError.isAxiosError) {
|
|
525
|
+
if (axiosError.response?.data?.error?.includes("Not enough capacity")) {
|
|
526
|
+
const maxCapacity = await getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl);
|
|
527
|
+
return {
|
|
528
|
+
result: void 0,
|
|
529
|
+
error: {
|
|
530
|
+
message: axiosError.response?.data.error,
|
|
531
|
+
data: maxCapacity ? { maxCapacity } : void 0
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return {
|
|
537
|
+
result: void 0,
|
|
538
|
+
error: { message: error.message || "Something went wrong" }
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/swap/index.ts
|
|
543
|
+
var swap_exports = {};
|
|
544
|
+
__export(swap_exports, {
|
|
545
|
+
calculateEthForExactNxm: () => calculateEthForExactNxm,
|
|
546
|
+
calculateExactEthForNxm: () => calculateExactEthForNxm,
|
|
547
|
+
calculateExactNxmForEth: () => calculateExactNxmForEth,
|
|
548
|
+
calculateNxmForExactEth: () => calculateNxmForExactEth,
|
|
549
|
+
calculatePriceImpactA: () => calculatePriceImpactA,
|
|
550
|
+
calculatePriceImpactB: () => calculatePriceImpactB,
|
|
551
|
+
calculateSpotPrice: () => calculateSpotPrice
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
// src/swap/calculateExactNxmForEth.ts
|
|
555
|
+
var calculateExactNxmForEth = (ethIn, reserves) => {
|
|
556
|
+
if (ethIn <= 0n) {
|
|
557
|
+
throw new Error("ETH in value must be greater than 0");
|
|
558
|
+
}
|
|
559
|
+
const k = reserves.nxmA * reserves.ethReserve;
|
|
560
|
+
const ethReservesAfter = reserves.ethReserve + ethIn;
|
|
561
|
+
const nxmReservesAfter = k / ethReservesAfter;
|
|
562
|
+
const nxmOut = reserves.nxmA - nxmReservesAfter;
|
|
563
|
+
if (nxmOut < 0n) {
|
|
564
|
+
throw new Error("Cannot swap this amount");
|
|
565
|
+
}
|
|
566
|
+
return nxmOut;
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/swap/calculateExactEthForNxm.ts
|
|
570
|
+
var calculateExactEthForNxm = (nxmIn, reserves) => {
|
|
571
|
+
if (nxmIn <= 0n) {
|
|
572
|
+
throw new Error("NXM in value must be greater than 0");
|
|
573
|
+
}
|
|
574
|
+
const k = reserves.nxmB * reserves.ethReserve;
|
|
575
|
+
const nxmReservesAfter = reserves.nxmB + nxmIn;
|
|
576
|
+
const ethReservesAfter = k / nxmReservesAfter;
|
|
577
|
+
const ethOut = reserves.ethReserve - ethReservesAfter;
|
|
578
|
+
if (ethOut < 0n) {
|
|
579
|
+
throw new Error("Cannot swap this amount");
|
|
580
|
+
}
|
|
581
|
+
return ethOut;
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
// src/swap/calculateNxmForExactEth.ts
|
|
585
|
+
var calculateNxmForExactEth = (ethOut, reserves) => {
|
|
586
|
+
if (ethOut <= 0n || ethOut >= reserves.ethReserve) {
|
|
587
|
+
throw new Error("ETH out value must be greater than 0 and less than the reserves");
|
|
588
|
+
}
|
|
589
|
+
const k = reserves.nxmB * reserves.ethReserve;
|
|
590
|
+
const ethReservesAfter = reserves.ethReserve - ethOut;
|
|
591
|
+
const nxmReservesAfter = k / ethReservesAfter;
|
|
592
|
+
const nxmIn = nxmReservesAfter - reserves.nxmB;
|
|
593
|
+
return nxmIn;
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// src/swap/calculateEthForExactNxm.ts
|
|
597
|
+
var calculateEthForExactNxm = (nxmOut, reserves) => {
|
|
598
|
+
if (nxmOut <= 0n || nxmOut >= reserves.nxmA) {
|
|
599
|
+
throw new Error("NXM out value must be greater than 0 and less than the reserves");
|
|
600
|
+
}
|
|
601
|
+
const k = reserves.nxmA * reserves.ethReserve;
|
|
602
|
+
const nxmReservesAfter = reserves.nxmA - nxmOut;
|
|
603
|
+
const ethReservesAfter = k / nxmReservesAfter;
|
|
604
|
+
const ethIn = ethReservesAfter - reserves.ethReserve;
|
|
605
|
+
return ethIn;
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
// src/swap/calculateSpotPrice.ts
|
|
609
|
+
var calculateSpotPrice = (reserves) => {
|
|
610
|
+
const oneEth = BigInt(1e18);
|
|
611
|
+
return {
|
|
612
|
+
spotPriceA: oneEth * reserves.ethReserve / reserves.nxmA,
|
|
613
|
+
spotPriceB: oneEth * reserves.ethReserve / reserves.nxmB
|
|
614
|
+
};
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
// src/swap/calculatePriceImpactA.ts
|
|
618
|
+
var calculatePriceImpactA = (ethIn, reserves) => {
|
|
619
|
+
const { spotPriceA } = calculateSpotPrice(reserves);
|
|
620
|
+
const nxmOut = calculateExactNxmForEth(ethIn, reserves);
|
|
621
|
+
const nxmOutAtSpotPrice = BigInt(1e18) * ethIn / spotPriceA;
|
|
622
|
+
return BigInt(1e6) - BigInt(1e6) * nxmOut / nxmOutAtSpotPrice;
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
// src/swap/calculatePriceImpactB.ts
|
|
626
|
+
var calculatePriceImpactB = (nxmIn, reserves) => {
|
|
627
|
+
const { spotPriceB } = calculateSpotPrice(reserves);
|
|
628
|
+
const ethOut = calculateExactEthForNxm(nxmIn, reserves);
|
|
629
|
+
const ethOutAtSpotPrice = spotPriceB * nxmIn / BigInt(1e18);
|
|
630
|
+
return BigInt(1e6) - BigInt(1e6) * ethOut / ethOutAtSpotPrice;
|
|
626
631
|
};
|
|
627
632
|
|
|
633
|
+
// src/types/index.ts
|
|
634
|
+
var types_exports = {};
|
|
635
|
+
|
|
628
636
|
// generated/product-types.json
|
|
629
637
|
var product_types_default = [
|
|
630
638
|
{
|
|
@@ -2152,7 +2160,7 @@ var products_default = [
|
|
|
2152
2160
|
"DAI"
|
|
2153
2161
|
],
|
|
2154
2162
|
isPrivate: true,
|
|
2155
|
-
timestamp:
|
|
2163
|
+
timestamp: 1684312727
|
|
2156
2164
|
},
|
|
2157
2165
|
{
|
|
2158
2166
|
id: 88,
|
|
@@ -2169,7 +2177,7 @@ var products_default = [
|
|
|
2169
2177
|
"DAI"
|
|
2170
2178
|
],
|
|
2171
2179
|
isPrivate: true,
|
|
2172
|
-
timestamp:
|
|
2180
|
+
timestamp: 1684312727
|
|
2173
2181
|
},
|
|
2174
2182
|
{
|
|
2175
2183
|
id: 89,
|
|
@@ -2413,7 +2421,7 @@ var products_default = [
|
|
|
2413
2421
|
"DAI"
|
|
2414
2422
|
],
|
|
2415
2423
|
isPrivate: true,
|
|
2416
|
-
timestamp:
|
|
2424
|
+
timestamp: 1704981731
|
|
2417
2425
|
},
|
|
2418
2426
|
{
|
|
2419
2427
|
id: 105,
|
|
@@ -2430,7 +2438,7 @@ var products_default = [
|
|
|
2430
2438
|
"DAI"
|
|
2431
2439
|
],
|
|
2432
2440
|
isPrivate: true,
|
|
2433
|
-
timestamp:
|
|
2441
|
+
timestamp: 1704980411
|
|
2434
2442
|
},
|
|
2435
2443
|
{
|
|
2436
2444
|
id: 106,
|
|
@@ -2552,7 +2560,7 @@ var products_default = [
|
|
|
2552
2560
|
"DAI"
|
|
2553
2561
|
],
|
|
2554
2562
|
isPrivate: true,
|
|
2555
|
-
timestamp:
|
|
2563
|
+
timestamp: 1691493035
|
|
2556
2564
|
},
|
|
2557
2565
|
{
|
|
2558
2566
|
id: 114,
|
|
@@ -2895,7 +2903,7 @@ var products_default = [
|
|
|
2895
2903
|
"ETH"
|
|
2896
2904
|
],
|
|
2897
2905
|
isPrivate: false,
|
|
2898
|
-
timestamp:
|
|
2906
|
+
timestamp: 1709310707
|
|
2899
2907
|
},
|
|
2900
2908
|
{
|
|
2901
2909
|
id: 137,
|
|
@@ -2970,7 +2978,7 @@ var products_default = [
|
|
|
2970
2978
|
"DAI"
|
|
2971
2979
|
],
|
|
2972
2980
|
isPrivate: false,
|
|
2973
|
-
timestamp:
|
|
2981
|
+
timestamp: 1715349875
|
|
2974
2982
|
},
|
|
2975
2983
|
{
|
|
2976
2984
|
id: 142,
|
|
@@ -3003,7 +3011,7 @@ var products_default = [
|
|
|
3003
3011
|
"ETH"
|
|
3004
3012
|
],
|
|
3005
3013
|
isPrivate: false,
|
|
3006
|
-
timestamp:
|
|
3014
|
+
timestamp: 1715349875
|
|
3007
3015
|
},
|
|
3008
3016
|
{
|
|
3009
3017
|
id: 144,
|
|
@@ -3020,7 +3028,7 @@ var products_default = [
|
|
|
3020
3028
|
"DAI"
|
|
3021
3029
|
],
|
|
3022
3030
|
isPrivate: false,
|
|
3023
|
-
timestamp:
|
|
3031
|
+
timestamp: 1710254231
|
|
3024
3032
|
},
|
|
3025
3033
|
{
|
|
3026
3034
|
id: 145,
|
|
@@ -3037,7 +3045,7 @@ var products_default = [
|
|
|
3037
3045
|
"DAI"
|
|
3038
3046
|
],
|
|
3039
3047
|
isPrivate: false,
|
|
3040
|
-
timestamp:
|
|
3048
|
+
timestamp: 1710254231
|
|
3041
3049
|
},
|
|
3042
3050
|
{
|
|
3043
3051
|
id: 146,
|
|
@@ -3054,7 +3062,7 @@ var products_default = [
|
|
|
3054
3062
|
"DAI"
|
|
3055
3063
|
],
|
|
3056
3064
|
isPrivate: false,
|
|
3057
|
-
timestamp:
|
|
3065
|
+
timestamp: 1715349875
|
|
3058
3066
|
},
|
|
3059
3067
|
{
|
|
3060
3068
|
id: 147,
|
|
@@ -3117,7 +3125,7 @@ var products_default = [
|
|
|
3117
3125
|
"ETH"
|
|
3118
3126
|
],
|
|
3119
3127
|
isPrivate: false,
|
|
3120
|
-
timestamp:
|
|
3128
|
+
timestamp: 1715349875
|
|
3121
3129
|
},
|
|
3122
3130
|
{
|
|
3123
3131
|
id: 151,
|
|
@@ -3150,7 +3158,7 @@ var products_default = [
|
|
|
3150
3158
|
"DAI"
|
|
3151
3159
|
],
|
|
3152
3160
|
isPrivate: false,
|
|
3153
|
-
timestamp:
|
|
3161
|
+
timestamp: 1715349875
|
|
3154
3162
|
},
|
|
3155
3163
|
{
|
|
3156
3164
|
id: 153,
|
|
@@ -3198,7 +3206,7 @@ var products_default = [
|
|
|
3198
3206
|
"ETH"
|
|
3199
3207
|
],
|
|
3200
3208
|
isPrivate: false,
|
|
3201
|
-
timestamp:
|
|
3209
|
+
timestamp: 1715349875
|
|
3202
3210
|
},
|
|
3203
3211
|
{
|
|
3204
3212
|
id: 156,
|
|
@@ -3279,7 +3287,7 @@ var products_default = [
|
|
|
3279
3287
|
"ETH"
|
|
3280
3288
|
],
|
|
3281
3289
|
isPrivate: false,
|
|
3282
|
-
timestamp:
|
|
3290
|
+
timestamp: 1713185771
|
|
3283
3291
|
},
|
|
3284
3292
|
{
|
|
3285
3293
|
id: 161,
|
|
@@ -3295,7 +3303,7 @@ var products_default = [
|
|
|
3295
3303
|
"ETH"
|
|
3296
3304
|
],
|
|
3297
3305
|
isPrivate: false,
|
|
3298
|
-
timestamp:
|
|
3306
|
+
timestamp: 1713185771
|
|
3299
3307
|
},
|
|
3300
3308
|
{
|
|
3301
3309
|
id: 162,
|
|
@@ -3311,7 +3319,7 @@ var products_default = [
|
|
|
3311
3319
|
"ETH"
|
|
3312
3320
|
],
|
|
3313
3321
|
isPrivate: false,
|
|
3314
|
-
timestamp:
|
|
3322
|
+
timestamp: 1713185771
|
|
3315
3323
|
},
|
|
3316
3324
|
{
|
|
3317
3325
|
id: 163,
|