fix: a V3 exact-out swap step shows its tokens and amounts (closes #601)
check / check (push) Canceled after 0s
e2e / e2e-chrome (push) Canceled after 0s
e2e / e2e-firefox (push) Canceled after 0s

A V3_SWAP_EXACT_OUT step was named in the Steps line but never read, so
its tokens read as not named in the calldata and it gave no amounts. It
is now read as a V2_SWAP_EXACT_OUT is: its path from the token it buys
to the token it spends, as the router reads it, its amountInMax as the
most it spends, its amountOut as the minimum, and its recipient as the
address it pays. One the user pays for after the step that sets the
input side joins the Amount total. A V3 path with bytes after its last
whole hop, which the router ignores, leaves either V3 swap step unread.

Also closes #637.

Model: opus-5-5
This commit was merged in pull request #636.
This commit is contained in:
2026-10-20 10:06:09 +02:00
parent ba27ddced0
commit 657dc14f04
4 changed files with 392 additions and 174 deletions
+63 -28
View File
@@ -592,13 +592,17 @@ function decodeV2SwapExactOut(input) {
}
}
// Decode V3 swap path (packed: token(20) + fee(3) + token(20) ...)
// The first and last token of a V3 swap path (packed: token(20) + fee(3) +
// token(20) ...), or null when it is not one token followed by whole hops of
// a fee and a token (40 hex digits, then 46 per hop). The router reads only
// whole hops and ignores any bytes after the last one, so the last 20 bytes
// of such a path are not the token it reads there.
function decodeV3Path(pathHex) {
const hex = pathHex.startsWith("0x") ? pathHex.slice(2) : pathHex;
if (hex.length < 40) return null;
const tokenIn = "0x" + hex.slice(0, 40);
const tokenOut = "0x" + hex.slice(-40);
return { tokenIn, tokenOut };
if (hex.length < 40 || (hex.length - 40) % 46 !== 0) return null;
const first = "0x" + hex.slice(0, 40);
const last = "0x" + hex.slice(-40);
return { first, last };
}
// Decode V3_SWAP_EXACT_IN (command 0x00) input bytes.
@@ -617,8 +621,39 @@ function decodeV3SwapExactIn(input) {
recipient: d[0],
amountIn: d[1],
amountOutMin: d[2],
tokenIn: path.tokenIn,
tokenOut: path.tokenOut,
tokenIn: path.first,
tokenOut: path.last,
payerIsUser: d[4],
};
} catch {
return null;
}
}
// Decode V3_SWAP_EXACT_OUT (command 0x01) input bytes.
// ABI: (address recipient, uint256 amountOut, uint256 amountInMax,
// bytes path, bool payerIsUser)
// The router reads an exact-out path from the token it buys to the token it
// spends: it buys `amountOut` of the path's first token from the path's first
// pool and pays at most `amountInMax` of its last token to its last pool
// (`v3SwapExactOutput()` and `uniswapV3SwapCallback()` in each release's
// `contracts/modules/uniswap/v3/V3SwapRouter.sol`, at the commit linked for
// it above).
// With `payerIsUser` false the router pays the input from its own balance.
function decodeV3SwapExactOut(input) {
try {
const d = coder.decode(
["address", "uint256", "uint256", "bytes", "bool"],
input,
);
const path = decodeV3Path(d[3]);
if (!path) return null;
return {
recipient: d[0],
amountOut: d[1],
amountInMax: d[2],
tokenIn: path.last,
tokenOut: path.first,
payerIsUser: d[4],
};
} catch {
@@ -1180,7 +1215,7 @@ function decode(data, toAddress, chainId, sources, fromAddress) {
let minOutput = null;
let outputEstablished = false;
let hasUnwrapWeth = false;
let hasV2ExactOut = false;
let hasV2OrV3ExactOut = false;
let swapUndecoded = false;
let hasSubPlan = false;
const commandNames = [];
@@ -1236,9 +1271,8 @@ function decode(data, toAddress, chainId, sources, fromAddress) {
};
// A step may take from the sender what this decoder did not read: a
// V4 step whose input or one of whose SETTLEs or SETTLE_ALLs it
// cannot read; a V2 or V3 exact-in or V2 exact-out step or
// PERMIT2_TRANSFER_FROM whose input it cannot read; a
// PERMIT2_TRANSFER_FROM_BATCH; a V3_SWAP_EXACT_OUT; an
// cannot read; a V2 or V3 swap step or PERMIT2_TRANSFER_FROM whose
// input it cannot read; a PERMIT2_TRANSFER_FROM_BATCH; an
// EXECUTE_SUB_PLAN; and a step it fails on. It is recorded with a null
// token and amount: one line says so, there is no Amount line, whose
// figure may then be too low, and the amount of a permit after it
@@ -1493,14 +1527,6 @@ function decode(data, toAddress, chainId, sources, fromAddress) {
stepsUnread.push(i);
spentUnread();
}
} else if (command === "V3_SWAP_EXACT_OUT") {
// This decoder does not read it
// (https://git.eeqj.de/sneak/AutistMask/issues/601). It
// may pay any address and take from the sender, so
// wherever it stands it leaves no Amount figure.
payeeUnread = true;
stepsUnread.push(i);
spentUnread();
} else if (command === "V2_SWAP_EXACT_IN") {
const s = decodeV2SwapExactIn(inputs[i]);
if (s) {
@@ -1521,10 +1547,19 @@ function decode(data, toAddress, chainId, sources, fromAddress) {
stepsUnread.push(i);
spentUnread();
}
} else if (command === "V2_SWAP_EXACT_OUT") {
} else if (
command === "V2_SWAP_EXACT_OUT" ||
command === "V3_SWAP_EXACT_OUT"
) {
// Buys exactly amountOut and spends at most amountInMax.
hasV2ExactOut = true;
const s = decodeV2SwapExactOut(inputs[i]);
// Paid from the router, it spends an amount the calldata
// does not state, so it spends nothing spendFromRouter()
// could count.
hasV2OrV3ExactOut = true;
const s =
command === "V2_SWAP_EXACT_OUT"
? decodeV2SwapExactOut(inputs[i])
: decodeV3SwapExactOut(inputs[i]);
if (s) {
if (!inputEstablished) {
setInputOnce(s.tokenIn, s.amountInMax);
@@ -1943,12 +1978,12 @@ function decode(data, toAddress, chainId, sources, fromAddress) {
if (amount >= MAX_UINT160) {
return { raw: "Unlimited", display: "Unlimited" };
}
if (hasV2ExactOut) {
// A V2 exact-out swap spends at most this figure, whichever
// steps gave it (an amountInMax, the WRAP_ETH of a swap paid
// in ETH, a permit, or several steps' total), so it is said to
// be a maximum, in `raw` too: the wait, success and error
// screens show `raw` as the transaction's amount.
if (hasV2OrV3ExactOut) {
// A V2 or V3 exact-out swap spends at most this figure,
// whichever steps gave it (an amountInMax, the WRAP_ETH of a
// swap paid in ETH, a permit, or several steps' total), so it
// is said to be a maximum, in `raw` too: the wait, success and
// error screens show `raw` as the transaction's amount.
const most = amountText(amount, info);
return {
raw: "Up to " + most.raw,