@nexusmutual/sdk 0.3.0 → 0.3.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/index.mjs CHANGED
@@ -1551,7 +1551,7 @@ var products_default = [
1551
1551
  "DAI"
1552
1552
  ],
1553
1553
  isPrivate: true,
1554
- timestamp: 1704981731
1554
+ timestamp: 1684312727
1555
1555
  },
1556
1556
  {
1557
1557
  id: 84,
@@ -1613,7 +1613,7 @@ var products_default = [
1613
1613
  "DAI"
1614
1614
  ],
1615
1615
  isPrivate: true,
1616
- timestamp: 1704981731
1616
+ timestamp: 1684312727
1617
1617
  },
1618
1618
  {
1619
1619
  id: 88,
@@ -1891,7 +1891,7 @@ var products_default = [
1891
1891
  "DAI"
1892
1892
  ],
1893
1893
  isPrivate: true,
1894
- timestamp: 1704980411
1894
+ timestamp: 1689782903
1895
1895
  },
1896
1896
  {
1897
1897
  id: 106,
@@ -3391,7 +3391,7 @@ __export(quote_exports, {
3391
3391
  var import_dotenv = __toESM(require_main());
3392
3392
  import axios from "axios";
3393
3393
  import_dotenv.default.config();
3394
- async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, coverAsset, coverBuyerAddress, slippage = DEFAULT_SLIPPAGE, ipfsCid = "") {
3394
+ async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, coverAsset, coverBuyerAddress, slippage = DEFAULT_SLIPPAGE / SLIPPAGE_DENOMINATOR, ipfsCid = "") {
3395
3395
  if (!process.env.COVER_ROUTER_URL) {
3396
3396
  return { result: void 0, error: { message: "Missing COVER_ROUTER_URL env var" } };
3397
3397
  }
@@ -3421,15 +3421,16 @@ async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, co
3421
3421
  if (!/^0x[a-fA-F0-9]{40}$/.test(coverBuyerAddress)) {
3422
3422
  return { result: void 0, error: { message: "Invalid coverBuyerAddress: must be a valid Ethereum address" } };
3423
3423
  }
3424
- if (typeof slippage !== "number" || slippage < 0 || slippage > SLIPPAGE_DENOMINATOR) {
3424
+ if (typeof slippage !== "number" || slippage < 0 || slippage > 1) {
3425
3425
  return {
3426
3426
  result: void 0,
3427
- error: { message: `Invalid slippage: must be a number between 0 and ${SLIPPAGE_DENOMINATOR}` }
3427
+ error: { message: "Invalid slippage: must be a number between 0 and 1" }
3428
3428
  };
3429
3429
  }
3430
3430
  if (typeof ipfsCid !== "string") {
3431
3431
  return { result: void 0, error: { message: "Invalid ipfsCid: must be a valid IPFS CID" } };
3432
3432
  }
3433
+ slippage = slippage * SLIPPAGE_DENOMINATOR;
3433
3434
  try {
3434
3435
  const { quote, capacities } = await getQuote(productId, coverAmount, coverPeriod, coverAsset);
3435
3436
  const maxPremiumInAsset = calculatePremiumWithCommissionAndSlippage(
@@ -3469,14 +3470,27 @@ async function getQuoteAndBuyCoverInputs(productId, coverAmount, coverPeriod, co
3469
3470
  };
3470
3471
  return { result, error: void 0 };
3471
3472
  } catch (error) {
3472
- return handleError(error);
3473
+ const errorResponse = await handleError(error, productId, coverPeriod, coverAsset);
3474
+ return errorResponse;
3473
3475
  }
3474
3476
  }
3475
3477
  async function getQuote(productId, coverAmount, coverPeriod, coverAsset) {
3476
3478
  const params = { productId, amount: coverAmount, period: coverPeriod, coverAsset };
3477
3479
  const response = await axios.get(process.env.COVER_ROUTER_URL + "/quote", { params });
3480
+ if (!response.data) {
3481
+ throw new Error("Failed to fetch cover quote");
3482
+ }
3478
3483
  return response.data;
3479
3484
  }
3485
+ async function getProductCapacity(productId, coverPeriod, coverAsset) {
3486
+ const params = { period: coverPeriod };
3487
+ const capacityUrl = process.env.COVER_ROUTER_URL + `/capacity/${productId}`;
3488
+ const response = await axios.get(capacityUrl, { params });
3489
+ if (!response.data) {
3490
+ throw new Error("Failed to fetch cover capacities");
3491
+ }
3492
+ return response.data.availableCapacity.find((av) => av.assetId === coverAsset)?.amount;
3493
+ }
3480
3494
  function sumPoolCapacities(capacities) {
3481
3495
  let totalAmount = BigInt(0);
3482
3496
  capacities.forEach((poolCapacity) => {
@@ -3484,23 +3498,23 @@ function sumPoolCapacities(capacities) {
3484
3498
  });
3485
3499
  return totalAmount.toString();
3486
3500
  }
3487
- async function handleError(error) {
3501
+ async function handleError(error, productId, coverPeriod, coverAsset) {
3488
3502
  const axiosError = error;
3489
3503
  if (axiosError.isAxiosError) {
3490
- console.log("axiosError.response.data: ", __require("util").inspect(axiosError.response?.data, { depth: null }));
3491
3504
  if (axiosError.response?.data?.error?.includes("Not enough capacity")) {
3505
+ const maxCapacity = await getProductCapacity(productId, coverPeriod, coverAsset);
3492
3506
  return {
3493
3507
  result: void 0,
3494
3508
  error: {
3495
3509
  message: axiosError.response?.data.error,
3496
- data: {}
3510
+ data: maxCapacity ? { maxCapacity } : void 0
3497
3511
  }
3498
3512
  };
3499
3513
  }
3500
3514
  }
3501
3515
  return {
3502
3516
  result: void 0,
3503
- error: { message: "Something went wrong" }
3517
+ error: { message: error.message || "Something went wrong" }
3504
3518
  };
3505
3519
  }
3506
3520