fix: a V4 step of chained swap actions shows what the user receives (closes #563)
A V4 step's input side is now the currency its SETTLE or SETTLE_ALL names, and its output side the currency its TAKE or TAKE_ALL names. A side none of them names comes from the swap actions, put in the order the currencies pass through them: each exact-in action after the ones before it, each exact-out action in front of them, since the router runs exact-out actions from the output end. The input and Amount come from the first action in that order, the output and Min. received from the last. SETTLE_ALL and TAKE_ALL were not read before, and the first swap action set both sides, so a chain showed the first one's output. Model: opus-5-5
This commit is contained in:
+239
-20
@@ -107,6 +107,7 @@ function encodeV4Swap(actions, params) {
|
||||
// V4 inner action IDs, as src/shared/uniswap.js names them.
|
||||
const V4_SWAP_EXACT_IN = 0x07;
|
||||
const V4_SWAP_EXACT_IN_SINGLE_ID = 0x06;
|
||||
const V4_SWAP_EXACT_OUT_SINGLE_ID = 0x08;
|
||||
const V4_SETTLE_ID = 0x0b;
|
||||
const V4_TAKE_ID = 0x0e;
|
||||
const ZERO_ADDR = "0x0000000000000000000000000000000000000000";
|
||||
@@ -163,6 +164,31 @@ function encodeV4ExactInSingle(currency0, currency1, amountIn, amountOutMin) {
|
||||
);
|
||||
}
|
||||
|
||||
// Helper: V4 ExactOutputSingleParams —
|
||||
// (tuple(address,address,uint24,int24,address) poolKey, bool zeroForOne,
|
||||
// uint128 amountOut, uint128 amountInMaximum, bytes hookData)
|
||||
function encodeV4ExactOutSingle(
|
||||
currency0,
|
||||
currency1,
|
||||
amountOut,
|
||||
amountInMaximum,
|
||||
) {
|
||||
return coder.encode(
|
||||
[
|
||||
"tuple(tuple(address,address,uint24,int24,address),bool,uint128,uint128,bytes)",
|
||||
],
|
||||
[
|
||||
[
|
||||
[currency0, currency1, 100, 1, ZERO_ADDR],
|
||||
true, // zeroForOne: in = currency0, out = currency1
|
||||
amountOut,
|
||||
amountInMaximum,
|
||||
"0x",
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// Each V4 swap action's parameters in two shapes: the struct V4Router has
|
||||
// deployed on mainnet and Sepolia, and the one v4-periphery `main` has since
|
||||
// moved to, which adds a `minHopPriceX36` field. Each swaps WETH for USDC.
|
||||
@@ -267,9 +293,7 @@ function v4SwapCalldata(actionId, swapParams) {
|
||||
);
|
||||
}
|
||||
|
||||
// V4 SETTLE_ALL (currency, maxAmount) and TAKE_ALL (currency, minAmount). The
|
||||
// decoder reads neither, so in a V4_SWAP of a swap action and these, only the
|
||||
// swap action names the currencies.
|
||||
// V4 SETTLE_ALL (currency, maxAmount) and TAKE_ALL (currency, minAmount).
|
||||
const V4_SETTLE_ALL_ID = 0x0c;
|
||||
const V4_TAKE_ALL_ID = 0x0f;
|
||||
function encodeV4SettleAll(currency, maxAmount) {
|
||||
@@ -358,10 +382,9 @@ describe("uniswap decoder", () => {
|
||||
},
|
||||
);
|
||||
|
||||
// Only the unread swap action names the currencies here. The token lines
|
||||
// must not say the calldata did not name them.
|
||||
// SETTLE_ALL and TAKE_ALL name both sides as SETTLE and TAKE do.
|
||||
test.each(V4_SWAP_ACTION_SHAPES)(
|
||||
"a V4 $action in the newer shape, then SETTLE_ALL and TAKE_ALL, says both tokens could not be decoded",
|
||||
"a V4 $action in the newer shape, then SETTLE_ALL and TAKE_ALL, takes both tokens from them",
|
||||
({ id, newer }) => {
|
||||
const data = buildExecute(
|
||||
"0x10",
|
||||
@@ -381,6 +404,27 @@ describe("uniswap decoder", () => {
|
||||
expect(detail(result, "Steps").value).toBe(
|
||||
"V4 Swap (could not be decoded)",
|
||||
);
|
||||
expect(detail(result, "Token In").address).toBe(WETH_ADDR);
|
||||
expect(detail(result, "Token Out").address).toBe(USDC_ADDR);
|
||||
expect(detail(result, "Amount")).toBeUndefined();
|
||||
expect(detail(result, "Min. received")).toBeUndefined();
|
||||
},
|
||||
);
|
||||
|
||||
// Only the unread swap action names the currencies here. The token lines
|
||||
// must not say the calldata did not name them.
|
||||
test.each(V4_SWAP_ACTION_SHAPES)(
|
||||
"a V4 step of only a $action in the newer shape says both tokens could not be decoded",
|
||||
({ id, newer }) => {
|
||||
const data = buildExecute(
|
||||
"0x10",
|
||||
[encodeV4Swap(new Uint8Array([id]), [newer])],
|
||||
9999999999n,
|
||||
);
|
||||
const result = uniswap.decode(data, ROUTER_ADDR);
|
||||
expect(detail(result, "Steps").value).toBe(
|
||||
"V4 Swap (could not be decoded)",
|
||||
);
|
||||
expect(detail(result, "Token In").value).toBe(
|
||||
"Unknown (could not be decoded)",
|
||||
);
|
||||
@@ -394,7 +438,7 @@ describe("uniswap decoder", () => {
|
||||
// action names. The V3 step's WETH and its Min. received figure were
|
||||
// shown instead.
|
||||
test.each(V4_SWAP_ACTION_SHAPES)(
|
||||
"a V3 step, then a V4 $action in the newer shape and TAKE_ALL, does not show the V3 step's output",
|
||||
"a V3 step, then a V4 step of only a $action in the newer shape, does not show the V3 step's output",
|
||||
({ id, newer }) => {
|
||||
const data = buildExecute(
|
||||
solidityPacked(["uint8", "uint8"], [0x00, 0x10]),
|
||||
@@ -403,10 +447,7 @@ describe("uniswap decoder", () => {
|
||||
USDT_ADDR,
|
||||
WETH_ADDR,
|
||||
]),
|
||||
encodeV4Swap(new Uint8Array([id, V4_TAKE_ALL_ID]), [
|
||||
newer,
|
||||
encodeV4TakeAll(USDC_ADDR, 990000n),
|
||||
]),
|
||||
encodeV4Swap(new Uint8Array([id]), [newer]),
|
||||
],
|
||||
9999999999n,
|
||||
);
|
||||
@@ -423,15 +464,12 @@ describe("uniswap decoder", () => {
|
||||
// The user pays the V4 step's WETH, which only its unread swap action
|
||||
// names. The V3 step's USDC and its amount were shown instead.
|
||||
test.each(V4_SWAP_ACTION_SHAPES)(
|
||||
"a V4 $action in the newer shape and SETTLE_ALL, then a V3 step, does not show the V3 step's input",
|
||||
"a V4 step of only a $action in the newer shape, then a V3 step, does not show the V3 step's input",
|
||||
({ id, newer }) => {
|
||||
const data = buildExecute(
|
||||
solidityPacked(["uint8", "uint8"], [0x10, 0x00]),
|
||||
[
|
||||
encodeV4Swap(new Uint8Array([id, V4_SETTLE_ALL_ID]), [
|
||||
newer,
|
||||
encodeV4SettleAll(WETH_ADDR, ONE_WETH),
|
||||
]),
|
||||
encodeV4Swap(new Uint8Array([id]), [newer]),
|
||||
encodeV3SwapExactIn(
|
||||
USER_ADDR,
|
||||
990000n,
|
||||
@@ -458,10 +496,7 @@ describe("uniswap decoder", () => {
|
||||
const data = buildExecute(
|
||||
solidityPacked(["uint8", "uint8"], [0x10, 0x0c]),
|
||||
[
|
||||
encodeV4Swap(new Uint8Array([id, V4_TAKE_ALL_ID]), [
|
||||
newer,
|
||||
encodeV4TakeAll(USDC_ADDR, 990000n),
|
||||
]),
|
||||
encodeV4Swap(new Uint8Array([id]), [newer]),
|
||||
encodeWrapEth(USER_ADDR, 0n), // UNWRAP_WETH, same encoding
|
||||
],
|
||||
9999999999n,
|
||||
@@ -1124,6 +1159,190 @@ describe("uniswap decoder", () => {
|
||||
expect(detail(result, "Min. received").value).toBe("0.9900 USDC");
|
||||
});
|
||||
|
||||
// https://git.eeqj.de/sneak/AutistMask/issues/563 — one V4 step of two
|
||||
// chained single-pool exact-in swap actions, USDT to WETH and then WETH to
|
||||
// USDC. The user pays USDT and receives USDC, but the screen showed the
|
||||
// first action's WETH as Token Out and its minimum as Min. received.
|
||||
test.each([
|
||||
{
|
||||
ending: "SETTLE_ALL and TAKE_ALL",
|
||||
ids: [V4_SETTLE_ALL_ID, V4_TAKE_ALL_ID],
|
||||
params: [
|
||||
encodeV4SettleAll(USDT_ADDR, 1000000000n),
|
||||
encodeV4TakeAll(USDC_ADDR, 990000000n),
|
||||
],
|
||||
},
|
||||
{
|
||||
ending: "SETTLE and TAKE",
|
||||
ids: [V4_SETTLE_ID, V4_TAKE_ID],
|
||||
params: [encodeV4Settle(USDT_ADDR), encodeV4Take(USDC_ADDR)],
|
||||
},
|
||||
{
|
||||
// Only the swap actions name the currencies.
|
||||
ending: "nothing",
|
||||
ids: [],
|
||||
params: [],
|
||||
},
|
||||
])(
|
||||
"two chained V4 swap actions followed by $ending show the first one's input and the last one's output",
|
||||
({ ids, params }) => {
|
||||
const data = buildExecute(
|
||||
"0x10",
|
||||
[
|
||||
encodeV4Swap(
|
||||
new Uint8Array([
|
||||
V4_SWAP_EXACT_IN_SINGLE_ID,
|
||||
V4_SWAP_EXACT_IN_SINGLE_ID,
|
||||
...ids,
|
||||
]),
|
||||
[
|
||||
// 1,000 USDT for at least 0.4 WETH
|
||||
encodeV4ExactInSingle(
|
||||
USDT_ADDR,
|
||||
WETH_ADDR,
|
||||
1000000000n,
|
||||
400000000000000000n,
|
||||
),
|
||||
// all of that WETH (the open delta) for at least
|
||||
// 990 USDC
|
||||
encodeV4ExactInSingle(
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
0n,
|
||||
990000000n,
|
||||
),
|
||||
...params,
|
||||
],
|
||||
),
|
||||
],
|
||||
9999999999n,
|
||||
);
|
||||
|
||||
const result = uniswap.decode(data, ROUTER_ADDR);
|
||||
expect(result.name).toBe("Swap USDT → USDC");
|
||||
expect(detail(result, "Token In").address).toBe(USDT_ADDR);
|
||||
expect(detail(result, "Amount").value).toBe("1000.0000 USDT");
|
||||
expect(detail(result, "Token Out").address).toBe(USDC_ADDR);
|
||||
expect(detail(result, "Min. received").value).toBe("990.0000 USDC");
|
||||
},
|
||||
);
|
||||
|
||||
// The router runs a chain of exact-out actions from the output end: the
|
||||
// first buys the USDC the user receives, owing WETH, and the second buys
|
||||
// that WETH (the open delta) with the USDT the user pays.
|
||||
test("two chained V4 exact-out actions show the last one's input and the first one's output", () => {
|
||||
const data = buildExecute(
|
||||
"0x10",
|
||||
[
|
||||
encodeV4Swap(
|
||||
new Uint8Array([
|
||||
V4_SWAP_EXACT_OUT_SINGLE_ID,
|
||||
V4_SWAP_EXACT_OUT_SINGLE_ID,
|
||||
]),
|
||||
[
|
||||
// exactly 990 USDC for at most 0.5 WETH
|
||||
encodeV4ExactOutSingle(
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
990000000n,
|
||||
500000000000000000n,
|
||||
),
|
||||
// exactly that WETH (the open delta) for at most
|
||||
// 1,000 USDT
|
||||
encodeV4ExactOutSingle(
|
||||
USDT_ADDR,
|
||||
WETH_ADDR,
|
||||
0n,
|
||||
1000000000n,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
9999999999n,
|
||||
);
|
||||
|
||||
const result = uniswap.decode(data, ROUTER_ADDR);
|
||||
expect(result.name).toBe("Swap USDT → USDC");
|
||||
expect(detail(result, "Token In").address).toBe(USDT_ADDR);
|
||||
expect(detail(result, "Token Out").address).toBe(USDC_ADDR);
|
||||
expect(detail(result, "Amount")).toBeUndefined();
|
||||
expect(detail(result, "Min. received")).toBeUndefined();
|
||||
});
|
||||
|
||||
// A swap action that could not be read still stands first or last in its
|
||||
// step, so the side and figure it would have given are unknown, while
|
||||
// those from the readable action are shown. The unread action is
|
||||
// V4_SWAP_ACTION_SHAPES[0].newer, a SWAP_EXACT_IN_SINGLE of WETH to USDC.
|
||||
test("a readable V4 swap action, then an unread one, shows the first one's input and no output", () => {
|
||||
const data = buildExecute(
|
||||
"0x10",
|
||||
[
|
||||
encodeV4Swap(
|
||||
new Uint8Array([
|
||||
V4_SWAP_EXACT_IN_SINGLE_ID,
|
||||
V4_SWAP_EXACT_IN_SINGLE_ID,
|
||||
]),
|
||||
[
|
||||
encodeV4ExactInSingle(
|
||||
USDT_ADDR,
|
||||
WETH_ADDR,
|
||||
1000000000n,
|
||||
400000000000000000n,
|
||||
),
|
||||
V4_SWAP_ACTION_SHAPES[0].newer,
|
||||
],
|
||||
),
|
||||
],
|
||||
9999999999n,
|
||||
);
|
||||
|
||||
const result = uniswap.decode(data, ROUTER_ADDR);
|
||||
expect(detail(result, "Steps").value).toBe(
|
||||
"V4 Swap (could not be decoded)",
|
||||
);
|
||||
expect(detail(result, "Token In").address).toBe(USDT_ADDR);
|
||||
expect(detail(result, "Amount").value).toBe("1000.0000 USDT");
|
||||
expect(detail(result, "Token Out").value).toBe(
|
||||
"Unknown (could not be decoded)",
|
||||
);
|
||||
expect(detail(result, "Min. received")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("an unread V4 swap action, then a readable one, shows the last one's output and no input", () => {
|
||||
const data = buildExecute(
|
||||
"0x10",
|
||||
[
|
||||
encodeV4Swap(
|
||||
new Uint8Array([
|
||||
V4_SWAP_EXACT_IN_SINGLE_ID,
|
||||
V4_SWAP_EXACT_IN_SINGLE_ID,
|
||||
]),
|
||||
[
|
||||
V4_SWAP_ACTION_SHAPES[0].newer,
|
||||
encodeV4ExactInSingle(
|
||||
USDC_ADDR,
|
||||
DAI_ADDR,
|
||||
0n,
|
||||
990000000000000000000n,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
9999999999n,
|
||||
);
|
||||
|
||||
const result = uniswap.decode(data, ROUTER_ADDR);
|
||||
expect(detail(result, "Steps").value).toBe(
|
||||
"V4 Swap (could not be decoded)",
|
||||
);
|
||||
expect(detail(result, "Token In").value).toBe(
|
||||
"Unknown (could not be decoded)",
|
||||
);
|
||||
expect(detail(result, "Amount")).toBeUndefined();
|
||||
expect(detail(result, "Token Out").address).toBe(DAI_ADDR);
|
||||
expect(detail(result, "Min. received").value).toBe("990.0000 DAI");
|
||||
});
|
||||
|
||||
// https://git.eeqj.de/sneak/AutistMask/issues/415 — the router's V2
|
||||
// exact-in reads an amountIn of zero as universal-router
|
||||
// Constants.ALREADY_PAID: an earlier step sent the tokens to the pair, and
|
||||
|
||||
Reference in New Issue
Block a user