@rhea-finance/confidential-swap 0.3.0 → 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
@@ -993,6 +993,20 @@ function assertFundingQuoteWithinSlippage(baselineMinOut, nextAmountOut) {
993
993
  "Funding quote moved beyond max slippage."
994
994
  );
995
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
+ }
996
1010
  function aggregateWithdrawalAppFee(quotes, assetId) {
997
1011
  if (quotes.length === 0 || quotes.some((quote) => !quote.quoteRequest?.amount || !Array.isArray(quote.quoteRequest.appFees))) return void 0;
998
1012
  let totalInput = 0n;
@@ -1057,43 +1071,37 @@ async function quoteDepositAndWithdrawals(params) {
1057
1071
  const baselineOut = raw(initialSwapQuote.amountOut);
1058
1072
  const baselineMinOut = raw(initialSwapQuote.minAmountOut);
1059
1073
  assert(baselineMinOut <= baselineOut, "Invalid funding swap minimum output.");
1060
- const seededDeposit = await deps.api.quote({
1061
- ...base,
1062
- amount: initialSwapQuote.amountOut,
1063
- refundTo: signerId,
1064
- refundType: "CONFIDENTIAL_INTENTS",
1065
- depositType: "ORIGIN_CHAIN",
1066
- swapType: "FLEX_INPUT",
1067
- dry: false
1068
- }, signal);
1069
- validateQuote(seededDeposit, true);
1070
- swapRequest = { ...swapRequest, recipient: seededDeposit.quote.depositAddress };
1071
- swapQuote = await deps.swap.quote(clone(swapRequest), signal);
1072
- const latestOut = raw(swapQuote.amountOut);
1073
- assert(raw(swapQuote.minAmountOut) <= latestOut, "Invalid funding swap minimum output.");
1074
- assertFundingQuoteWithinSlippage(baselineMinOut, latestOut);
1075
- if (latestOut === baselineOut) {
1076
- depositQuote = seededDeposit;
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);
1077
1083
  } else {
1078
- depositQuote = await deps.api.quote({
1084
+ const seededDeposit = await deps.api.quote({
1079
1085
  ...base,
1080
- amount: swapQuote.amountOut,
1086
+ amount: initialSwapQuote.amountOut,
1081
1087
  refundTo: signerId,
1082
1088
  refundType: "CONFIDENTIAL_INTENTS",
1083
1089
  depositType: "ORIGIN_CHAIN",
1084
1090
  swapType: "FLEX_INPUT",
1085
1091
  dry: false
1086
1092
  }, signal);
1087
- validateQuote(depositQuote, true);
1088
- if (depositQuote.quote.depositAddress !== seededDeposit.quote.depositAddress) {
1089
- swapRequest = { ...swapRequest, recipient: depositQuote.quote.depositAddress };
1090
- swapQuote = await deps.swap.quote(clone(swapRequest), signal);
1091
- const boundOut = raw(swapQuote.amountOut);
1092
- assert(raw(swapQuote.minAmountOut) <= boundOut, "Invalid funding swap minimum output.");
1093
- assertFundingQuoteWithinSlippage(baselineMinOut, boundOut);
1094
- }
1093
+ validateQuote(seededDeposit, true);
1094
+ swapRequest = { ...swapRequest, recipient: seededDeposit.quote.depositAddress };
1095
+ swapQuote = await deps.swap.quote(clone(swapRequest), signal);
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
+ };
1095
1104
  }
1096
- fundingAmount = swapQuote.amountOut;
1097
1105
  }
1098
1106
  }
1099
1107
  const shares = splitConfidentialRawAmount(fundingAmount, req.recipients.length, randomValues);