fix: the swap decoder shows the ETH of a WRAP_ETH that does not set the input side (closes #721)
check / check (push) Waiting to run
e2e / e2e-chrome (push) Waiting to run
e2e / e2e-firefox (push) Waiting to run

A later WRAP_ETH of a stated amount now counts as a take from the sender.
When it pays anyone but the router, it gets its own Moved from the sender
lines, with the recipient on Moved to. When it pays the router, it joins
the Amount line if Token In is ETH, and otherwise gets those lines too.
When the router may already hold ETH, the amount reads Up to.

A WRAP_ETH whose input this decoder cannot read gets a line saying the
amount could not be decoded.

A WRAP_ETH of CONTRACT_BALANCE states no amount and reads as before, so
Uniswap's own routes do not change.

Model: opus-5-5
This commit is contained in:
2026-11-03 04:34:24 +00:00
committed by sneak
parent 076726b13d
commit 894d972caa
4 changed files with 225 additions and 17 deletions
+169
View File
@@ -2593,6 +2593,9 @@ describe("uniswap decoder", () => {
],
lines: [
["Amount", "1.0000 ETH"],
["Moved from the sender", "Up to 1.0000 ETH"],
["Token moved", "ETH"],
["Moved to", OTHER_ADDR],
...ethLines("Up to 1.0000 ETH"),
],
},
@@ -2622,6 +2625,7 @@ describe("uniswap decoder", () => {
],
lines: [
["Amount", "600.0000 USDT"],
...wrapLines("The swap step that spends it"),
...ethLines("Up to 1.0000 ETH"),
],
},
@@ -2753,6 +2757,171 @@ describe("uniswap decoder", () => {
expect(linesBetweenTokens(result)).toEqual(lines);
});
// https://git.eeqj.de/sneak/AutistMask/issues/721 — a WRAP_ETH that
// does not set the input side takes the amount it states from the
// sender. On next no line gave that ETH. The rows marked "reads as on
// next" take nothing from the sender. Uniswap's own routes wrap
// CONTRACT_BALANCE after their first step, as the last two rows do.
const CONTRACT_BALANCE = 2n ** 255n;
const WRAP_TO_OTHER = encodeWrapEth(OTHER_ADDR, ONE_ETH);
test.each([
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH of 1 ETH to another address",
commands: [0x00, 0x0b],
inputs: [V3_600, WRAP_TO_OTHER],
lines: [["Amount", "600.0000 USDT"], ...wrapLines(OTHER_ADDR)],
},
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH of 1 ETH to address(1)",
commands: [0x00, 0x0b],
inputs: [V3_600, encodeWrapEth(MSG_SENDER, ONE_ETH)],
lines: [
["Amount", "600.0000 USDT"],
...wrapLines("The sender"),
],
},
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH of 1 ETH to address(2)",
commands: [0x00, 0x0b],
inputs: [V3_600, WRAP_INTO_ROUTER],
lines: [
["Amount", "600.0000 USDT"],
...wrapLines("The swap step that spends it"),
],
},
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH of 1 ETH to the router by its own address",
commands: [0x00, 0x0b],
inputs: [V3_600, encodeWrapEth(ROUTER_ADDR, ONE_ETH)],
lines: [
["Amount", "600.0000 USDT"],
...wrapLines("The swap step that spends it"),
],
},
{
kind: "a WRAP_ETH of 1 ETH into the router, then a WRAP_ETH of 1 ETH to another address",
commands: [0x0b, 0x0b],
inputs: [WRAP_INTO_ROUTER, WRAP_TO_OTHER],
lines: [["Amount", "1.0000 ETH"], ...wrapLines(OTHER_ADDR)],
},
{
kind: "two WRAP_ETHs of 1 ETH into the router, which the Amount line adds up",
commands: [0x0b, 0x0b],
inputs: [WRAP_INTO_ROUTER, WRAP_INTO_ROUTER],
lines: [["Amount", "2.0000 ETH"]],
},
{
kind: "a V3 exact-in of 600 USDT for WETH paid to the router, an UNWRAP_WETH into the router, then a WRAP_ETH of 1 ETH to another address",
commands: [0x00, 0x0c, 0x0b],
inputs: [
encodeV3SwapExactIn(ADDRESS_THIS, USDT_600, 1n, [
USDT_ADDR,
WETH_ADDR,
]),
UNWRAP_INTO_ROUTER,
WRAP_TO_OTHER,
],
lines: [
["Amount", "600.0000 USDT"],
["Moved from the sender", "Up to 1.0000 ETH"],
["Token moved", "ETH"],
["Moved to", OTHER_ADDR],
],
},
{
kind: "a WRAP_ETH of 1 ETH into the router, an UNWRAP_WETH into the router, then a WRAP_ETH of 1 ETH into the router",
commands: [0x0b, 0x0c, 0x0b],
inputs: [
WRAP_INTO_ROUTER,
UNWRAP_INTO_ROUTER,
WRAP_INTO_ROUTER,
],
lines: [
["Amount", "1.0000 ETH"],
...ethLines("Up to 1.0000 ETH"),
],
},
{
kind: "a PERMIT2_PERMIT of 5,000 USDT to the router, a V3 exact-in of 600 USDT, then a WRAP_ETH of 1 ETH to another address",
commands: [0x0a, 0x00, 0x0b],
inputs: [
encodePermit2(USDT_ADDR, 5000000000n, ROUTER_ADDR),
V3_600,
WRAP_TO_OTHER,
],
lines: [["Amount", "5000.0000 USDT"], ...wrapLines(OTHER_ADDR)],
},
{
kind: "a V3 exact-in of 600 USDT, a WRAP_ETH of 1 ETH to another address, then an EXECUTE_SUB_PLAN",
commands: [0x00, 0x0b, 0x21],
inputs: [V3_600, WRAP_TO_OTHER, SUB_PLAN],
lines: [
...USDT_600_GROUP,
...wrapLines(OTHER_ADDR),
["Moved from the sender", UNDECODED],
],
},
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH whose input this decoder cannot read",
commands: [0x00, 0x0b],
inputs: [V3_600, "0x1234"],
lines: [
...USDT_600_GROUP,
["Moved from the sender", UNDECODED],
],
},
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH of nothing to another address, reads as on next",
commands: [0x00, 0x0b],
inputs: [V3_600, encodeWrapEth(OTHER_ADDR, 0n)],
lines: [["Amount", "600.0000 USDT"]],
},
{
kind: "a V3 exact-in of 600 USDT, then a WRAP_ETH of CONTRACT_BALANCE to the sender, reads as on next",
commands: [0x00, 0x0b],
inputs: [V3_600, encodeWrapEth(MSG_SENDER, CONTRACT_BALANCE)],
lines: [["Amount", "600.0000 USDT"]],
},
{
kind: "a V4 swap of 600 USDT that takes ETH into the router, a WRAP_ETH of CONTRACT_BALANCE into the router, then a V3 exact-in of it, reads as on next",
commands: [0x10, 0x0b, 0x00],
inputs: [
encodeV4Swap(
new Uint8Array([
V4_SWAP_EXACT_IN_SINGLE_ID,
V4_SETTLE_ID,
V4_TAKE_ID,
]),
[
encodeV4ExactInSingle(
USDT_ADDR,
ZERO_ADDR,
USDT_600,
1n,
),
settle(USDT_ADDR, 0n, true),
take(ZERO_ADDR, ADDRESS_THIS, 0n),
],
),
encodeWrapEth(ADDRESS_THIS, CONTRACT_BALANCE),
coder.encode(
["address", "uint256", "uint256", "bytes", "bool"],
[
USER_ADDR,
CONTRACT_BALANCE,
1n,
encodeV3Path([WETH_ADDR, USDC_ADDR]),
false,
],
),
],
lines: [["Amount", "600.0000 USDT"]],
},
])("$kind", ({ commands, inputs, lines }) => {
const result = decodeSteps(commands, inputs);
expect(linesBetweenTokens(result)).toEqual(lines);
});
// A V3_SWAP_EXACT_OUT the user pays for, of at most `amountInMax`
// USDT for USDC.
const v3ExactOut = (amountInMax) =>