@rhea-finance/confidential-swap 0.2.8 → 0.2.9

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.cjs CHANGED
@@ -987,6 +987,13 @@ var SessionManager = class {
987
987
  };
988
988
 
989
989
  // src/quote.ts
990
+ var MAX_FLEX_FUNDING_REQUOTES = 3;
991
+ function assertFundingQuoteWithinSlippage(baselineMinOut, nextAmountOut) {
992
+ assert(
993
+ nextAmountOut >= baselineMinOut,
994
+ "Funding quote moved beyond max slippage."
995
+ );
996
+ }
990
997
  function aggregateWithdrawalAppFee(quotes, assetId) {
991
998
  if (quotes.length === 0 || quotes.some((quote) => !quote.quoteRequest?.amount || !Array.isArray(quote.quoteRequest.appFees))) return void 0;
992
999
  let totalInput = 0n;
@@ -1048,23 +1055,42 @@ async function quoteDepositAndWithdrawals(params) {
1048
1055
  slippageBps: req.slippageBps
1049
1056
  };
1050
1057
  const initialSwapQuote = await deps.swap.quote(clone(swapRequest), signal);
1051
- raw(initialSwapQuote.amountOut);
1052
- assert(raw(initialSwapQuote.minAmountOut) <= raw(initialSwapQuote.amountOut), "Invalid funding swap minimum output.");
1053
- depositQuote = await deps.api.quote({
1054
- ...base,
1055
- amount: initialSwapQuote.amountOut,
1056
- refundTo: signerId,
1057
- refundType: "CONFIDENTIAL_INTENTS",
1058
- depositType: "ORIGIN_CHAIN",
1059
- swapType: "FLEX_INPUT",
1060
- dry: false
1061
- }, signal);
1062
- validateQuote(depositQuote, true);
1063
- swapRequest = { ...swapRequest, recipient: depositQuote.quote.depositAddress };
1064
- swapQuote = await deps.swap.quote(clone(swapRequest), signal);
1065
- raw(swapQuote.amountOut);
1066
- assert(raw(swapQuote.minAmountOut) <= raw(swapQuote.amountOut), "Invalid funding swap minimum output.");
1067
- fundingAmount = swapQuote.amountOut;
1058
+ const baselineOut = raw(initialSwapQuote.amountOut);
1059
+ const baselineMinOut = raw(initialSwapQuote.minAmountOut);
1060
+ assert(baselineMinOut <= baselineOut, "Invalid funding swap minimum output.");
1061
+ let latestDepositAmountIn = initialSwapQuote.amountOut;
1062
+ let covered = false;
1063
+ for (let attempt = 0; attempt < MAX_FLEX_FUNDING_REQUOTES; attempt++) {
1064
+ depositQuote = await deps.api.quote({
1065
+ ...base,
1066
+ // Always the most recent funding-swap amountOut — never stick on pass 1
1067
+ // after a requote.
1068
+ amount: latestDepositAmountIn,
1069
+ refundTo: signerId,
1070
+ refundType: "CONFIDENTIAL_INTENTS",
1071
+ depositType: "ORIGIN_CHAIN",
1072
+ swapType: "FLEX_INPUT",
1073
+ dry: false
1074
+ }, signal);
1075
+ validateQuote(depositQuote, true);
1076
+ swapRequest = { ...swapRequest, recipient: depositQuote.quote.depositAddress };
1077
+ swapQuote = await deps.swap.quote(clone(swapRequest), signal);
1078
+ const nextOut = raw(swapQuote.amountOut);
1079
+ const nextMinOut = raw(swapQuote.minAmountOut);
1080
+ assert(nextMinOut <= nextOut, "Invalid funding swap minimum output.");
1081
+ assertFundingQuoteWithinSlippage(baselineMinOut, nextOut);
1082
+ const flexMinIn = depositQuote.quote.minAmountIn !== void 0 ? raw(depositQuote.quote.minAmountIn) : void 0;
1083
+ if (flexMinIn === void 0 || nextMinOut >= flexMinIn) {
1084
+ fundingAmount = swapQuote.amountOut;
1085
+ covered = true;
1086
+ break;
1087
+ }
1088
+ latestDepositAmountIn = swapQuote.amountOut;
1089
+ }
1090
+ assert(
1091
+ covered && swapQuote,
1092
+ "Final funding quote does not cover FLEX minimum input."
1093
+ );
1068
1094
  }
1069
1095
  }
1070
1096
  const shares = splitConfidentialRawAmount(fundingAmount, req.recipients.length, randomValues);