1/5
### The Collect Function In this lesson, we'll be going over the `collect` function, and we'll be covering how the collect function works in the Uniswap V3 contracts. The `collect` function is used to collect fees on swaps and to transfer tokens out when removing liquidity. To remove liquidity, we'll first need to call the `burn` function to decrease the liquidity for a position, and to transfer the tokens out, we'll need to call the function `collect`. The inputs are going to be: - The recipient, which is the address the tokens will be sent to. - The tick lower and tick upper, which define a position. - The amount of tokens to transfer out. If these amounts are greater than the amount owed to a position, the maximum amount that will be transferred out will be the amount owed to the position. Inside the function, we'll first get the position that's defined by `msg.sender`, and the two ticks, tick lower and tick upper. ```javascript position = positions.get(msg.sender, tickLower, tickUpper); ``` Then, it calculates the actual amount of tokens that can be transferred out. If the amount requested is less than the amount of tokens that are owed, then the amount that will be transferred is the amount requested. In other words, the amount of tokens that will go out is the minimum between the tokens that are owed and the amount requested. If there's any amount of tokens that can be transferred out, then the position will be updated. `tokensOwed` will be decreased, and the amount of tokens will be transferred out. We can see this for token 0: ```javascript if (amount0 > 0) { position.tokensOwed0 -= amount0; TransferHelper.safeTransfer(token0, recipient, amount0); } ``` And for token 1: ```javascript if (amount1 > 0) { position.tokensOwed1 -= amount1; TransferHelper.safeTransfer(token1, recipient, amount1); } ``` This ends our code walkthrough for the function `collect`.
A comprehensive guide to the "collect" function in UniswapV3PoolActions - The lesson explains the purpose of the "collect" function and how it is used in UniswapV3PoolActions. It also explains the inputs and outputs of the function and provides a detailed code walkthrough.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Concentrated liquidity and derive its equations
Uniswap V3 math
How to calculate the spot price of tokens
Single and multi position swapping
Factory contract architecture
How to calculate liquidity requirements
Uniswap V3 fee algorithm
Flash loans
TWAP price oracle
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on May 15, 2025
DeFi Developer
Uniswap V3Duration: 3min
Duration: 35min
Duration: 25min
Duration: 22min
Duration: 1h 43min
Duration: 11min
Duration: 1h 03min
Duration: 12min
Duration: 51min
Duration: 41min
Duration: 10min
Duration: 7min
Course Overview
About the course
Concentrated liquidity and derive its equations
Uniswap V3 math
How to calculate the spot price of tokens
Single and multi position swapping
Factory contract architecture
How to calculate liquidity requirements
Uniswap V3 fee algorithm
Flash loans
TWAP price oracle
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on May 15, 2025