@rhea-finance/confidential-swap 0.2.9 → 0.3.1

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,13 +987,26 @@ var SessionManager = class {
987
987
  };
988
988
 
989
989
  // src/quote.ts
990
- var MAX_FLEX_FUNDING_REQUOTES = 3;
991
990
  function assertFundingQuoteWithinSlippage(baselineMinOut, nextAmountOut) {
992
991
  assert(
993
992
  nextAmountOut >= baselineMinOut,
994
993
  "Funding quote moved beyond max slippage."
995
994
  );
996
995
  }
996
+ function swapQuoteRecipient(quote) {
997
+ const data = quote.data;
998
+ return String(data?.request?.recipient || "").trim();
999
+ }
1000
+ function depositQuoteForAddress(address, amount) {
1001
+ return {
1002
+ quote: {
1003
+ amountIn: amount,
1004
+ amountOut: amount,
1005
+ minAmountOut: amount,
1006
+ depositAddress: address
1007
+ }
1008
+ };
1009
+ }
997
1010
  function aggregateWithdrawalAppFee(quotes, assetId) {
998
1011
  if (quotes.length === 0 || quotes.some((quote) => !quote.quoteRequest?.amount || !Array.isArray(quote.quoteRequest.appFees))) return void 0;
999
1012
  let totalInput = 0n;
@@ -1058,39 +1071,37 @@ async function quoteDepositAndWithdrawals(params) {
1058
1071
  const baselineOut = raw(initialSwapQuote.amountOut);
1059
1072
  const baselineMinOut = raw(initialSwapQuote.minAmountOut);
1060
1073
  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({
1074
+ const knownRecipient = swapQuoteRecipient(initialSwapQuote);
1075
+ if (knownRecipient) {
1076
+ swapRequest = { ...swapRequest, recipient: knownRecipient };
1077
+ swapQuote = await deps.swap.quote(clone(swapRequest), signal);
1078
+ const latestOut = raw(swapQuote.amountOut);
1079
+ assert(raw(swapQuote.minAmountOut) <= latestOut, "Invalid funding swap minimum output.");
1080
+ assertFundingQuoteWithinSlippage(baselineMinOut, latestOut);
1081
+ fundingAmount = swapQuote.amountOut;
1082
+ depositQuote = depositQuoteForAddress(knownRecipient, swapQuote.amountOut);
1083
+ } else {
1084
+ const seededDeposit = await deps.api.quote({
1065
1085
  ...base,
1066
- // Always the most recent funding-swap amountOut — never stick on pass 1
1067
- // after a requote.
1068
- amount: latestDepositAmountIn,
1086
+ amount: initialSwapQuote.amountOut,
1069
1087
  refundTo: signerId,
1070
1088
  refundType: "CONFIDENTIAL_INTENTS",
1071
1089
  depositType: "ORIGIN_CHAIN",
1072
1090
  swapType: "FLEX_INPUT",
1073
1091
  dry: false
1074
1092
  }, signal);
1075
- validateQuote(depositQuote, true);
1076
- swapRequest = { ...swapRequest, recipient: depositQuote.quote.depositAddress };
1093
+ validateQuote(seededDeposit, true);
1094
+ swapRequest = { ...swapRequest, recipient: seededDeposit.quote.depositAddress };
1077
1095
  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;
1096
+ const latestOut = raw(swapQuote.amountOut);
1097
+ assert(raw(swapQuote.minAmountOut) <= latestOut, "Invalid funding swap minimum output.");
1098
+ assertFundingQuoteWithinSlippage(baselineMinOut, latestOut);
1099
+ fundingAmount = swapQuote.amountOut;
1100
+ depositQuote = {
1101
+ ...seededDeposit,
1102
+ quote: { ...seededDeposit.quote, amountIn: swapQuote.amountOut }
1103
+ };
1089
1104
  }
1090
- assert(
1091
- covered && swapQuote,
1092
- "Final funding quote does not cover FLEX minimum input."
1093
- );
1094
1105
  }
1095
1106
  }
1096
1107
  const shares = splitConfidentialRawAmount(fundingAmount, req.recipients.length, randomValues);