5/5
# Router Exercises In this exercise, you'll learn how to swap tokens with the [`PoolManager`](https://github.com/Uniswap/v4-core/blob/main/src/PoolManager.sol) contract. The starter code for this exercise is provided in [`foundry/src/exercises/Router.sol`](https://github.com/Cyfrin/defi-uniswap-v4/blob/main/foundry/src/exercises/Router.sol) Solution is in [`foundry/src/solutions/Router.sol`](https://github.com/Cyfrin/defi-uniswap-v4/blob/main/foundry/src/solutions/Router.sol) ## Task 1 - Single hop swap exact input ```solidity function swapExactInputSingle(ExactInputSingleParams calldata params) external payable setAction(SWAP_EXACT_IN_SINGLE) returns (uint256 amountOut) { // Write your code here } ``` Implement this function so it performs a swap against a single pool in the `PoolManager` contract. - Swap the full amount of the input currency specified by `ExactInputSingleParams.amountIn` for the maximum possible amount of the other currency in the pool. - Revert if the received output amount is less than `ExactInputSingleParams.amountOutMin`. - Return any unused tokens back to the caller. - Return the amount of currency out (`amountOut`). ## Task 2 - Single hop swap exact output ```solidity function swapExactOutputSingle(ExactOutputSingleParams calldata params) external payable setAction(SWAP_EXACT_OUT_SINGLE) returns (uint256 amountIn) { // Write your code here } ``` Implement this function so it performs a swap against a single pool in the `PoolManager` contract. - Swap the minimum amount of the input currency for the exact amount of output specified by `ExactOutputSingleParams.amountOut` of the other currency in the pool. - Revert if the input amount is greater than `ExactOutputSingleParams.amountInMax`. - Return any unused tokens back to the caller. - Return the amount of currency in (`amountIn`) ## Task 3 - Multi hops swap exact input ```solidity function swapExactInput(ExactInputParams calldata params) external payable setAction(SWAP_EXACT_IN) returns (uint256 amountOut) { // Write your code here } ``` Implement this function so it performs a swap against multiple pools in the `PoolManager` contract. - Swap the full amount of the input currency specified by `ExactInputParams.amountIn` for the maximum possible amount of the final currency specified in the last element of `ExactInputParams.path`. - Revert if the received output amount is less than `ExactInputParams.amountOutMin`. - Return any unused tokens back to the caller. - Return the amount of currency out (`amountOut`). ## Task 4 - Multi hops swap exact output ```solidity function swapExactOutput(ExactOutputParams calldata params) external payable setAction(SWAP_EXACT_OUT) returns (uint256 amountIn) { // Write your code here } ``` Implement this function so it performs a swap against multiple pools in the `PoolManager` contract. - Swap the minimum amount of the input currency for the exact amount of output specified by `ExactOutputParams.amountOut` of the final currency specified in `ExactOutputParams.currencyOut`. - Revert if the input amount is greater than `ExactOutputParams.amountInMax`. - Return any unused tokens back to the caller. - Return the amount of currency in (`amountIn`) ## Test ```shell forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/Router.test.sol -vvv ```
In this exercise, you'll learn how to swap tokens with the PoolManager
contract.
The starter code for this exercise is provided in foundry/src/exercises/Router.sol
Solution is in foundry/src/solutions/Router.sol
Implement this function so it performs a swap against a single pool in the PoolManager
contract.
Swap the full amount of the input currency specified by ExactInputSingleParams.amountIn
for the maximum possible amount of the other currency in the pool.
Revert if the received output amount is less than ExactInputSingleParams.amountOutMin
.
Return any unused tokens back to the caller.
Return the amount of currency out (amountOut
).
Implement this function so it performs a swap against a single pool in the PoolManager
contract.
Swap the minimum amount of the input currency for the exact amount of output specified by ExactOutputSingleParams.amountOut
of the other currency in the pool.
Revert if the input amount is greater than ExactOutputSingleParams.amountInMax
.
Return any unused tokens back to the caller.
Return the amount of currency in (amountIn
)
Implement this function so it performs a swap against multiple pools in the PoolManager
contract.
Swap the full amount of the input currency specified by ExactInputParams.amountIn
for the maximum possible amount of the final currency specified in the last element of ExactInputParams.path
.
Revert if the received output amount is less than ExactInputParams.amountOutMin
.
Return any unused tokens back to the caller.
Return the amount of currency out (amountOut
).
Implement this function so it performs a swap against multiple pools in the PoolManager
contract.
Swap the minimum amount of the input currency for the exact amount of output specified by ExactOutputParams.amountOut
of the final currency specified in ExactOutputParams.currencyOut
.
Revert if the input amount is greater than ExactOutputParams.amountInMax
.
Return any unused tokens back to the caller.
Return the amount of currency in (amountIn
)
A hands-on exercise to Router Exercises - Learn how to build a custom Uniswap v4 router by implementing single-hop and multi-hop token swaps for both exact input and exact output amounts.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Difference between Uniswap v3 and v4
Uniswap v4 PoolManager
Uniswap v4 Hooks
Uniswap v4 Singleton architecture
Uniswap v4 flash accounting
Uniswap v4 operations
Uniswap v4 lifecycle
How to build a Uniswap v4 swap router
Last updated on September 22, 2025
Duration: 5min
Duration: 7min
Duration: 39min
Duration: 35min
Course Overview
About the course
Difference between Uniswap v3 and v4
Uniswap v4 PoolManager
Uniswap v4 Hooks
Uniswap v4 Singleton architecture
Uniswap v4 flash accounting
Uniswap v4 operations
Uniswap v4 lifecycle
How to build a Uniswap v4 swap router
Last updated on September 22, 2025