Skip to content

Commit 5bff9d5

Browse files
mgrabinaclaude
andauthored
fix: adapter instance address mismatch causing infinite approval loop (#2885)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f0ef25b commit 5bff9d5

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/components/transactions/Swap/actions/DebtSwap/DebtSwapActionsViaCoW.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ export const DebtSwapActionsViaCoW = ({
8181
[state.expiry]
8282
);
8383

84-
// Pre-compute instance address
84+
// Pre-compute instance address.
85+
// Skip recalculation while approval is in progress or succeeded to prevent in-flight quote
86+
// responses from changing the adapter address and invalidating the user's signature.
8587
useEffect(() => {
88+
if (approvalTxState.loading || approvalTxState.success) return;
8689
calculateInstanceAddress({
8790
user,
8891
validTo,
@@ -113,6 +116,8 @@ export const DebtSwapActionsViaCoW = ({
113116
state.orderType,
114117
state.chainId,
115118
APP_CODE_PER_SWAP_TYPE[state.swapType],
119+
approvalTxState.loading,
120+
approvalTxState.success,
116121
]);
117122

118123
const amountToApprove = useMemo(() => {

src/components/transactions/Swap/actions/RepayWithCollateral/RepayWithCollateralActionsViaCoW.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ export const RepayWithCollateralActionsViaCoW = ({
8181
[state.expiry]
8282
);
8383

84-
// Pre-compute instance address
84+
// Pre-compute instance address.
85+
// Skip recalculation while approval is in progress or succeeded to prevent in-flight quote
86+
// responses from changing the adapter address and invalidating the user's signature.
8587
useEffect(() => {
88+
if (approvalTxState.loading || approvalTxState.success) return;
8689
calculateInstanceAddress({
8790
user,
8891
validTo,
@@ -113,6 +116,8 @@ export const RepayWithCollateralActionsViaCoW = ({
113116
state.orderType,
114117
state.chainId,
115118
APP_CODE_PER_SWAP_TYPE[state.swapType],
119+
approvalTxState.loading,
120+
approvalTxState.success,
116121
]);
117122

118123
// Approval is aToken ERC20 Approval

src/components/transactions/Swap/helpers/cow/adapters.helpers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export const calculateInstanceAddress = async ({
5858
const {
5959
sellAmountWithMarginForDustProtection,
6060
buyAmountWithMarginForDustProtection,
61-
buyAmount,
6261
sellToken,
6362
buyToken,
6463
quoteId,
@@ -77,7 +76,6 @@ export const calculateInstanceAddress = async ({
7776
state.orderType
7877
),
7978
sellToken: state.sellAmountToken,
80-
buyAmount: state.buyAmountBigInt,
8179
buyToken: state.buyAmountToken,
8280
quoteId: isCowProtocolRates(state.swapRate) ? state.swapRate?.quoteId : undefined,
8381
side: state.processedSide,
@@ -100,7 +98,7 @@ export const calculateInstanceAddress = async ({
10098
buyToken: buyToken.underlyingAddress,
10199
buyTokenDecimals: buyToken.decimals,
102100
sellAmount: sellAmountToSign.toString(),
103-
buyAmount: buyAmount.toString(),
101+
buyAmount: buyAmountWithMarginForDustProtection.toString(),
104102
kind: side === 'buy' ? OrderKind.BUY : OrderKind.SELL,
105103
quoteId,
106104
validTo,

src/components/transactions/Swap/hooks/useSwapQuote.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,13 @@ export const useSwapQuote = ({
227227
}
228228
}, [ratesError]);
229229

230+
// Apply incoming quote to state. Discard in-flight responses that arrive after
231+
// the user has started the approval flow (quoteRefreshPaused) to prevent
232+
// amount changes that would invalidate the adapter instance address / signature.
230233
useEffect(() => {
231234
if (swapQuote) {
235+
if (state.quoteRefreshPaused) return;
236+
232237
const isAutoRefreshed = Boolean(state.quoteLastUpdatedAt);
233238
trackingHandlers?.trackSwapQuote(isAutoRefreshed, swapQuote);
234239

0 commit comments

Comments
 (0)