Skip to content

Comments

Fix Uniswap V4 Universal Router encoding for three-action swap flow#1

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-uni-router-encoding-error
Draft

Fix Uniswap V4 Universal Router encoding for three-action swap flow#1
Copilot wants to merge 5 commits intomainfrom
copilot/fix-uni-router-encoding-error

Conversation

Copy link

Copilot AI commented Nov 17, 2025

The V4 swap implementation incorrectly encoded commands and inputs as a single action instead of the required three-step flow (swap → settle → take) that V4's PoolManager architecture requires.

Changes

Contract Updates (EtherTrialsTRIAv5_Simple.sol)

  • Added IUniversalRouter interface and immutable state variable
  • Added V4 command constants: V4_SWAP (0x00), SETTLE (0x09), TAKE (0x0a)
  • Extended constructor to accept _universalRouter address
  • Implemented _swapETHToTRIAV4() with proper command/input encoding

Key Fix:

// Three commands, three inputs (not 1:1)
bytes memory commands = abi.encodePacked(V4_SWAP, SETTLE, TAKE);
bytes[] memory inputs = new bytes[](3);

// Each command gets its own encoded parameters
inputs[0] = abi.encode(recipient, amountIn, minOut, path, payerIsUser);
inputs[1] = abi.encode(currency, amount, payerIsUser);
inputs[2] = abi.encode(currency, recipient, amount);

universalRouter.execute{value: ethAmount}(commands, inputs);

Documentation

  • V4_SWAP_ENCODING_EXPLAINED.md - Technical breakdown of encoding structure
  • SECURITY_SUMMARY.md - Security analysis (no vulnerabilities identified)
  • V4_INTEGRATION_GUIDE.md - Deployment and usage reference
  • IMPLEMENTATION_SUMMARY.md - Complete implementation overview

Deployment

Base Mainnet Universal Router: 0x6ff5693b99212da76ad316178a184ab56d299b43

Note: Constructor signature changed - requires Universal Router address parameter. Existing deployments incompatible due to immutable state.

Original prompt

Fix Uniswap V4 Universal Router encoding error in _swapETHToTRIAV4 function. The main issues are:

  1. The inputs array structure is incorrect - combining actions and params into a single element when it should be properly encoded
  2. The command/inputs mismatch - 1 command with 1 input containing multiple sub-actions needs proper encoding
  3. The parameter encoding for V4 swap doesn't match Universal Router expectations

The fix should:

  • Properly encode the V4_SWAP command with correct input structure
  • Use the correct ABI encoding format that Universal Router expects
  • Ensure commands and inputs array lengths/structure match
  • Handle the three sequential actions (swap, settle, take) with proper encoding

This pull request was created as a result of the following prompt from Copilot chat.

Fix Uniswap V4 Universal Router encoding error in _swapETHToTRIAV4 function. The main issues are:

  1. The inputs array structure is incorrect - combining actions and params into a single element when it should be properly encoded
  2. The command/inputs mismatch - 1 command with 1 input containing multiple sub-actions needs proper encoding
  3. The parameter encoding for V4 swap doesn't match Universal Router expectations

The fix should:

  • Properly encode the V4_SWAP command with correct input structure
  • Use the correct ABI encoding format that Universal Router expects
  • Ensure commands and inputs array lengths/structure match
  • Handle the three sequential actions (swap, settle, take) with proper encoding

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits November 17, 2025 03:00
Co-authored-by: ismaonezain <88978640+ismaonezain@users.noreply.github.com>
… settle, take)

Co-authored-by: ismaonezain <88978640+ismaonezain@users.noreply.github.com>
Co-authored-by: ismaonezain <88978640+ismaonezain@users.noreply.github.com>
Co-authored-by: ismaonezain <88978640+ismaonezain@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Uniswap V4 Universal Router encoding error in _swapETHToTRIAV4 function Fix Uniswap V4 Universal Router encoding for three-action swap flow Nov 17, 2025
Copilot AI requested a review from ismaonezain November 17, 2025 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants