@nexusmutual/sdk 0.3.12 → 0.3.14
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 +15 -15
- package/dist/index.d.mts +33 -33
- package/dist/index.d.ts +33 -33
- package/dist/index.js +250 -245
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +250 -245
- 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";
|
|
@@ -343,7 +122,6 @@ var ProductCategoryEnum = /* @__PURE__ */ ((ProductCategoryEnum2) => {
|
|
|
343
122
|
ProductCategoryEnum2["Perpetuals"] = "perpetuals";
|
|
344
123
|
ProductCategoryEnum2["SmartWallet"] = "smart-wallet";
|
|
345
124
|
ProductCategoryEnum2["RWA"] = "rwa";
|
|
346
|
-
ProductCategoryEnum2["Coverage"] = "coverage";
|
|
347
125
|
ProductCategoryEnum2["ETHStaking"] = "eth-staking";
|
|
348
126
|
ProductCategoryEnum2["Unity"] = "unity";
|
|
349
127
|
return ProductCategoryEnum2;
|
|
@@ -356,8 +134,7 @@ var categoryLabelByEnum = {
|
|
|
356
134
|
["yield-optimizer" /* YieldOptimizer */]: "Yield Optimizer",
|
|
357
135
|
["perpetuals" /* Perpetuals */]: "Perpetuals",
|
|
358
136
|
["smart-wallet" /* SmartWallet */]: "Smart Wallet",
|
|
359
|
-
["rwa" /* RWA */]: "RWA",
|
|
360
|
-
["coverage" /* Coverage */]: "Coverage",
|
|
137
|
+
["rwa" /* RWA */]: "RWA Coverage",
|
|
361
138
|
["eth-staking" /* ETHStaking */]: "ETH Staking",
|
|
362
139
|
["unity" /* Unity */]: "Unity"
|
|
363
140
|
// [ProductCategoryEnum.Uncategorized]: 'Uncategorized',
|
|
@@ -405,8 +182,7 @@ var productCategoryMap = {
|
|
|
405
182
|
// 27: ProductCategoryEnum.Uncategorized, // Curve sETH LP (eCrv)
|
|
406
183
|
28: "perpetuals" /* Perpetuals */,
|
|
407
184
|
// dydx Perpetual
|
|
408
|
-
29:
|
|
409
|
-
// Ease v1
|
|
185
|
+
// 29: ProductCategoryEnum.Uncategorized, // Ease v1
|
|
410
186
|
// 30: ProductCategoryEnum.Uncategorized, // Enzyme v3
|
|
411
187
|
31: "yield-optimizer" /* YieldOptimizer */,
|
|
412
188
|
// Enzyme v4
|
|
@@ -619,12 +395,241 @@ var productCategoryMap = {
|
|
|
619
395
|
// Etherfi (Zircuit) Pendle
|
|
620
396
|
161: "liquid-restaking" /* LiquidRestaking */,
|
|
621
397
|
// KelpDAO (Zircuit) Pendle
|
|
622
|
-
162: "liquid-restaking" /* LiquidRestaking
|
|
398
|
+
162: "liquid-restaking" /* LiquidRestaking */,
|
|
623
399
|
// Renzo (Zircuit) Pendle
|
|
624
400
|
// 163: ProductCategoryEnum.Uncategorized, // Pocket Universe
|
|
625
401
|
// 164: ProductCategoryEnum.Uncategorized, // Request Finance
|
|
402
|
+
165: "yield-optimizer" /* YieldOptimizer */,
|
|
403
|
+
// Etherfi Liquid Market-Neutral USD Vault
|
|
404
|
+
166: "yield-optimizer" /* YieldOptimizer */,
|
|
405
|
+
// Superform
|
|
406
|
+
167: "liquid-restaking" /* LiquidRestaking */,
|
|
407
|
+
// EigenLayer + Etherfi
|
|
408
|
+
168: "yield-optimizer" /* YieldOptimizer */,
|
|
409
|
+
// Beefy CLM + Uniswap v3
|
|
410
|
+
169: "eth-staking" /* ETHStaking */
|
|
411
|
+
// RockX
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
// src/quote/index.ts
|
|
415
|
+
var quote_exports = {};
|
|
416
|
+
__export(quote_exports, {
|
|
417
|
+
getQuoteAndBuyCoverInputs: () => getQuoteAndBuyCoverInputs
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
// src/quote/getQuoteAndBuyCoverInputs.ts
|
|
421
|
+
var import_axios = __toESM(require("axios"));
|
|
422
|
+
async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, coverAsset, coverBuyerAddress, slippage = DEFAULT_SLIPPAGE / SLIPPAGE_DENOMINATOR, ipfsCid = "", coverRouterUrl = "https://api.nexusmutual.io/v2") {
|
|
423
|
+
if (!Number.isInteger(productId) || productId <= 0) {
|
|
424
|
+
return { result: void 0, error: { message: "Invalid productId: must be a positive integer" } };
|
|
425
|
+
}
|
|
426
|
+
if (typeof coverAmount !== "string" || !/^\d+$/.test(coverAmount) || parseInt(coverAmount, 10) <= 0) {
|
|
427
|
+
return { result: void 0, error: { message: "Invalid coverAmount: must be a positive integer string" } };
|
|
428
|
+
}
|
|
429
|
+
if (!Number.isInteger(coverPeriod) || coverPeriod < MINIMUM_COVER_PERIOD || coverPeriod > MAXIMUM_COVER_PERIOD) {
|
|
430
|
+
return {
|
|
431
|
+
result: void 0,
|
|
432
|
+
error: {
|
|
433
|
+
message: `Invalid coverPeriod: must be between ${MINIMUM_COVER_PERIOD} and ${MAXIMUM_COVER_PERIOD} days`
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
const coverAssetsString = Object.keys(CoverAsset).filter((k) => isNaN(+k)).map((k) => `CoverAsset.${k}`).join(", ");
|
|
438
|
+
if (!Object.values(CoverAsset).includes(coverAsset)) {
|
|
439
|
+
return {
|
|
440
|
+
result: void 0,
|
|
441
|
+
error: {
|
|
442
|
+
message: `Invalid coverAsset: must be one of ${coverAssetsString}`
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(coverBuyerAddress)) {
|
|
447
|
+
return { result: void 0, error: { message: "Invalid coverBuyerAddress: must be a valid Ethereum address" } };
|
|
448
|
+
}
|
|
449
|
+
if (typeof slippage !== "number" || slippage < 0 || slippage > 1) {
|
|
450
|
+
return {
|
|
451
|
+
result: void 0,
|
|
452
|
+
error: { message: "Invalid slippage: must be a number between 0 and 1" }
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (typeof ipfsCid !== "string") {
|
|
456
|
+
return { result: void 0, error: { message: "Invalid ipfsCid: must be a valid IPFS CID" } };
|
|
457
|
+
}
|
|
458
|
+
slippage = slippage * SLIPPAGE_DENOMINATOR;
|
|
459
|
+
try {
|
|
460
|
+
const { quote } = await getQuote(productId, coverAmount, coverPeriod, coverAsset, coverRouterUrl);
|
|
461
|
+
const maxPremiumInAsset = calculatePremiumWithCommissionAndSlippage(
|
|
462
|
+
BigInt(quote.premiumInAsset),
|
|
463
|
+
DEFAULT_COMMISSION_RATIO,
|
|
464
|
+
slippage
|
|
465
|
+
);
|
|
466
|
+
const yearlyCostPerc = calculatePremiumWithCommissionAndSlippage(
|
|
467
|
+
BigInt(quote.annualPrice),
|
|
468
|
+
DEFAULT_COMMISSION_RATIO,
|
|
469
|
+
slippage
|
|
470
|
+
);
|
|
471
|
+
const result = {
|
|
472
|
+
displayInfo: {
|
|
473
|
+
premiumInAsset: maxPremiumInAsset.toString(),
|
|
474
|
+
coverAmount,
|
|
475
|
+
yearlyCostPerc: Number(yearlyCostPerc) / TARGET_PRICE_DENOMINATOR,
|
|
476
|
+
maxCapacity: await getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl) ?? ""
|
|
477
|
+
},
|
|
478
|
+
buyCoverInput: {
|
|
479
|
+
buyCoverParams: {
|
|
480
|
+
coverId: 0 /* BUY */,
|
|
481
|
+
owner: coverBuyerAddress,
|
|
482
|
+
productId,
|
|
483
|
+
coverAsset,
|
|
484
|
+
amount: coverAmount,
|
|
485
|
+
period: coverPeriod * 60 * 60 * 24,
|
|
486
|
+
// seconds
|
|
487
|
+
maxPremiumInAsset: maxPremiumInAsset.toString(),
|
|
488
|
+
paymentAsset: coverAsset,
|
|
489
|
+
commissionRatio: DEFAULT_COMMISSION_RATIO,
|
|
490
|
+
commissionDestination: NEXUS_MUTUAL_DAO_TREASURY_ADDRESS,
|
|
491
|
+
ipfsData: ipfsCid
|
|
492
|
+
},
|
|
493
|
+
poolAllocationRequests: quote.poolAllocationRequests
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
return { result, error: void 0 };
|
|
497
|
+
} catch (error) {
|
|
498
|
+
const errorResponse = await handleError(error, productId, coverPeriod, coverAsset, coverRouterUrl);
|
|
499
|
+
return errorResponse;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
async function getQuote(productId, coverAmount, coverPeriod, coverAsset, coverRouterUrl) {
|
|
503
|
+
const params = { productId, amount: coverAmount, period: coverPeriod, coverAsset };
|
|
504
|
+
const response = await import_axios.default.get(coverRouterUrl + "/quote", { params });
|
|
505
|
+
if (!response.data) {
|
|
506
|
+
throw new Error("Failed to fetch cover quote");
|
|
507
|
+
}
|
|
508
|
+
return response.data;
|
|
509
|
+
}
|
|
510
|
+
async function getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl) {
|
|
511
|
+
const params = { period: coverPeriod };
|
|
512
|
+
const capacityUrl = coverRouterUrl + `/capacity/${productId}`;
|
|
513
|
+
const response = await import_axios.default.get(capacityUrl, { params });
|
|
514
|
+
if (!response.data) {
|
|
515
|
+
throw new Error("Failed to fetch cover capacities");
|
|
516
|
+
}
|
|
517
|
+
return response.data.availableCapacity.find((av) => av.assetId === coverAsset)?.amount;
|
|
518
|
+
}
|
|
519
|
+
async function handleError(error, productId, coverPeriod, coverAsset, coverRouterUrl) {
|
|
520
|
+
const axiosError = error;
|
|
521
|
+
if (axiosError.isAxiosError) {
|
|
522
|
+
if (axiosError.response?.data?.error?.includes("Not enough capacity")) {
|
|
523
|
+
const maxCapacity = await getProductCapacity(productId, coverPeriod, coverAsset, coverRouterUrl);
|
|
524
|
+
return {
|
|
525
|
+
result: void 0,
|
|
526
|
+
error: {
|
|
527
|
+
message: axiosError.response?.data.error,
|
|
528
|
+
data: maxCapacity ? { maxCapacity } : void 0
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return {
|
|
534
|
+
result: void 0,
|
|
535
|
+
error: { message: error.message || "Something went wrong" }
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// src/swap/index.ts
|
|
540
|
+
var swap_exports = {};
|
|
541
|
+
__export(swap_exports, {
|
|
542
|
+
calculateEthForExactNxm: () => calculateEthForExactNxm,
|
|
543
|
+
calculateExactEthForNxm: () => calculateExactEthForNxm,
|
|
544
|
+
calculateExactNxmForEth: () => calculateExactNxmForEth,
|
|
545
|
+
calculateNxmForExactEth: () => calculateNxmForExactEth,
|
|
546
|
+
calculatePriceImpactA: () => calculatePriceImpactA,
|
|
547
|
+
calculatePriceImpactB: () => calculatePriceImpactB,
|
|
548
|
+
calculateSpotPrice: () => calculateSpotPrice
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
// src/swap/calculateExactNxmForEth.ts
|
|
552
|
+
var calculateExactNxmForEth = (ethIn, reserves) => {
|
|
553
|
+
if (ethIn <= 0n) {
|
|
554
|
+
throw new Error("ETH in value must be greater than 0");
|
|
555
|
+
}
|
|
556
|
+
const k = reserves.nxmA * reserves.ethReserve;
|
|
557
|
+
const ethReservesAfter = reserves.ethReserve + ethIn;
|
|
558
|
+
const nxmReservesAfter = k / ethReservesAfter;
|
|
559
|
+
const nxmOut = reserves.nxmA - nxmReservesAfter;
|
|
560
|
+
if (nxmOut < 0n) {
|
|
561
|
+
throw new Error("Cannot swap this amount");
|
|
562
|
+
}
|
|
563
|
+
return nxmOut;
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
// src/swap/calculateExactEthForNxm.ts
|
|
567
|
+
var calculateExactEthForNxm = (nxmIn, reserves) => {
|
|
568
|
+
if (nxmIn <= 0n) {
|
|
569
|
+
throw new Error("NXM in value must be greater than 0");
|
|
570
|
+
}
|
|
571
|
+
const k = reserves.nxmB * reserves.ethReserve;
|
|
572
|
+
const nxmReservesAfter = reserves.nxmB + nxmIn;
|
|
573
|
+
const ethReservesAfter = k / nxmReservesAfter;
|
|
574
|
+
const ethOut = reserves.ethReserve - ethReservesAfter;
|
|
575
|
+
if (ethOut < 0n) {
|
|
576
|
+
throw new Error("Cannot swap this amount");
|
|
577
|
+
}
|
|
578
|
+
return ethOut;
|
|
626
579
|
};
|
|
627
580
|
|
|
581
|
+
// src/swap/calculateNxmForExactEth.ts
|
|
582
|
+
var calculateNxmForExactEth = (ethOut, reserves) => {
|
|
583
|
+
if (ethOut <= 0n || ethOut >= reserves.ethReserve) {
|
|
584
|
+
throw new Error("ETH out value must be greater than 0 and less than the reserves");
|
|
585
|
+
}
|
|
586
|
+
const k = reserves.nxmB * reserves.ethReserve;
|
|
587
|
+
const ethReservesAfter = reserves.ethReserve - ethOut;
|
|
588
|
+
const nxmReservesAfter = k / ethReservesAfter;
|
|
589
|
+
const nxmIn = nxmReservesAfter - reserves.nxmB;
|
|
590
|
+
return nxmIn;
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
// src/swap/calculateEthForExactNxm.ts
|
|
594
|
+
var calculateEthForExactNxm = (nxmOut, reserves) => {
|
|
595
|
+
if (nxmOut <= 0n || nxmOut >= reserves.nxmA) {
|
|
596
|
+
throw new Error("NXM out value must be greater than 0 and less than the reserves");
|
|
597
|
+
}
|
|
598
|
+
const k = reserves.nxmA * reserves.ethReserve;
|
|
599
|
+
const nxmReservesAfter = reserves.nxmA - nxmOut;
|
|
600
|
+
const ethReservesAfter = k / nxmReservesAfter;
|
|
601
|
+
const ethIn = ethReservesAfter - reserves.ethReserve;
|
|
602
|
+
return ethIn;
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// src/swap/calculateSpotPrice.ts
|
|
606
|
+
var calculateSpotPrice = (reserves) => {
|
|
607
|
+
const oneEth = BigInt(1e18);
|
|
608
|
+
return {
|
|
609
|
+
spotPriceA: oneEth * reserves.ethReserve / reserves.nxmA,
|
|
610
|
+
spotPriceB: oneEth * reserves.ethReserve / reserves.nxmB
|
|
611
|
+
};
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
// src/swap/calculatePriceImpactA.ts
|
|
615
|
+
var calculatePriceImpactA = (ethIn, reserves) => {
|
|
616
|
+
const { spotPriceA } = calculateSpotPrice(reserves);
|
|
617
|
+
const nxmOut = calculateExactNxmForEth(ethIn, reserves);
|
|
618
|
+
const nxmOutAtSpotPrice = BigInt(1e18) * ethIn / spotPriceA;
|
|
619
|
+
return BigInt(1e6) - BigInt(1e6) * nxmOut / nxmOutAtSpotPrice;
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
// src/swap/calculatePriceImpactB.ts
|
|
623
|
+
var calculatePriceImpactB = (nxmIn, reserves) => {
|
|
624
|
+
const { spotPriceB } = calculateSpotPrice(reserves);
|
|
625
|
+
const ethOut = calculateExactEthForNxm(nxmIn, reserves);
|
|
626
|
+
const ethOutAtSpotPrice = spotPriceB * nxmIn / BigInt(1e18);
|
|
627
|
+
return BigInt(1e6) - BigInt(1e6) * ethOut / ethOutAtSpotPrice;
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// src/types/index.ts
|
|
631
|
+
var types_exports = {};
|
|
632
|
+
|
|
628
633
|
// generated/product-types.json
|
|
629
634
|
var product_types_default = [
|
|
630
635
|
{
|
|
@@ -2152,7 +2157,7 @@ var products_default = [
|
|
|
2152
2157
|
"DAI"
|
|
2153
2158
|
],
|
|
2154
2159
|
isPrivate: true,
|
|
2155
|
-
timestamp:
|
|
2160
|
+
timestamp: 1684312727
|
|
2156
2161
|
},
|
|
2157
2162
|
{
|
|
2158
2163
|
id: 88,
|
|
@@ -2169,7 +2174,7 @@ var products_default = [
|
|
|
2169
2174
|
"DAI"
|
|
2170
2175
|
],
|
|
2171
2176
|
isPrivate: true,
|
|
2172
|
-
timestamp:
|
|
2177
|
+
timestamp: 1684312727
|
|
2173
2178
|
},
|
|
2174
2179
|
{
|
|
2175
2180
|
id: 89,
|
|
@@ -2413,7 +2418,7 @@ var products_default = [
|
|
|
2413
2418
|
"DAI"
|
|
2414
2419
|
],
|
|
2415
2420
|
isPrivate: true,
|
|
2416
|
-
timestamp:
|
|
2421
|
+
timestamp: 1704980411
|
|
2417
2422
|
},
|
|
2418
2423
|
{
|
|
2419
2424
|
id: 105,
|
|
@@ -2430,7 +2435,7 @@ var products_default = [
|
|
|
2430
2435
|
"DAI"
|
|
2431
2436
|
],
|
|
2432
2437
|
isPrivate: true,
|
|
2433
|
-
timestamp:
|
|
2438
|
+
timestamp: 1704980411
|
|
2434
2439
|
},
|
|
2435
2440
|
{
|
|
2436
2441
|
id: 106,
|
|
@@ -2552,7 +2557,7 @@ var products_default = [
|
|
|
2552
2557
|
"DAI"
|
|
2553
2558
|
],
|
|
2554
2559
|
isPrivate: true,
|
|
2555
|
-
timestamp:
|
|
2560
|
+
timestamp: 1691493035
|
|
2556
2561
|
},
|
|
2557
2562
|
{
|
|
2558
2563
|
id: 114,
|
|
@@ -2895,7 +2900,7 @@ var products_default = [
|
|
|
2895
2900
|
"ETH"
|
|
2896
2901
|
],
|
|
2897
2902
|
isPrivate: false,
|
|
2898
|
-
timestamp:
|
|
2903
|
+
timestamp: 1709310707
|
|
2899
2904
|
},
|
|
2900
2905
|
{
|
|
2901
2906
|
id: 137,
|
|
@@ -2987,7 +2992,7 @@ var products_default = [
|
|
|
2987
2992
|
"DAI"
|
|
2988
2993
|
],
|
|
2989
2994
|
isPrivate: false,
|
|
2990
|
-
timestamp:
|
|
2995
|
+
timestamp: 1710254231
|
|
2991
2996
|
},
|
|
2992
2997
|
{
|
|
2993
2998
|
id: 143,
|
|
@@ -3020,7 +3025,7 @@ var products_default = [
|
|
|
3020
3025
|
"DAI"
|
|
3021
3026
|
],
|
|
3022
3027
|
isPrivate: false,
|
|
3023
|
-
timestamp:
|
|
3028
|
+
timestamp: 1710254231
|
|
3024
3029
|
},
|
|
3025
3030
|
{
|
|
3026
3031
|
id: 145,
|
|
@@ -3037,7 +3042,7 @@ var products_default = [
|
|
|
3037
3042
|
"DAI"
|
|
3038
3043
|
],
|
|
3039
3044
|
isPrivate: false,
|
|
3040
|
-
timestamp:
|
|
3045
|
+
timestamp: 1710254231
|
|
3041
3046
|
},
|
|
3042
3047
|
{
|
|
3043
3048
|
id: 146,
|
|
@@ -3101,7 +3106,7 @@ var products_default = [
|
|
|
3101
3106
|
"DAI"
|
|
3102
3107
|
],
|
|
3103
3108
|
isPrivate: false,
|
|
3104
|
-
timestamp:
|
|
3109
|
+
timestamp: 1715349875
|
|
3105
3110
|
},
|
|
3106
3111
|
{
|
|
3107
3112
|
id: 150,
|
|
@@ -3166,7 +3171,7 @@ var products_default = [
|
|
|
3166
3171
|
"ETH"
|
|
3167
3172
|
],
|
|
3168
3173
|
isPrivate: false,
|
|
3169
|
-
timestamp:
|
|
3174
|
+
timestamp: 1711473767
|
|
3170
3175
|
},
|
|
3171
3176
|
{
|
|
3172
3177
|
id: 154,
|
|
@@ -3246,7 +3251,7 @@ var products_default = [
|
|
|
3246
3251
|
"ETH"
|
|
3247
3252
|
],
|
|
3248
3253
|
isPrivate: false,
|
|
3249
|
-
timestamp:
|
|
3254
|
+
timestamp: 1715349875
|
|
3250
3255
|
},
|
|
3251
3256
|
{
|
|
3252
3257
|
id: 159,
|
|
@@ -3279,7 +3284,7 @@ var products_default = [
|
|
|
3279
3284
|
"ETH"
|
|
3280
3285
|
],
|
|
3281
3286
|
isPrivate: false,
|
|
3282
|
-
timestamp:
|
|
3287
|
+
timestamp: 1713185771
|
|
3283
3288
|
},
|
|
3284
3289
|
{
|
|
3285
3290
|
id: 161,
|
|
@@ -3295,7 +3300,7 @@ var products_default = [
|
|
|
3295
3300
|
"ETH"
|
|
3296
3301
|
],
|
|
3297
3302
|
isPrivate: false,
|
|
3298
|
-
timestamp:
|
|
3303
|
+
timestamp: 1713185771
|
|
3299
3304
|
},
|
|
3300
3305
|
{
|
|
3301
3306
|
id: 162,
|
|
@@ -3311,7 +3316,7 @@ var products_default = [
|
|
|
3311
3316
|
"ETH"
|
|
3312
3317
|
],
|
|
3313
3318
|
isPrivate: false,
|
|
3314
|
-
timestamp:
|
|
3319
|
+
timestamp: 1713185771
|
|
3315
3320
|
},
|
|
3316
3321
|
{
|
|
3317
3322
|
id: 163,
|