Close Menu

    Subscribe to Updates

    What's Hot

    Ethereum price surges 6% to $2,800 as shorts suffer amid $500M crypto liquidation

    July 10, 2025

    Bitget Wallet launches new Pump.fun integrated mobile app for meme coin trading

    July 10, 2025

    Announcement – Certified Cryptocurrency Professional (CCP)™ Certification Launched

    July 10, 2025
    Facebook X (Twitter) Instagram
    laicryptolaicrypto
    Demo
    • Ethereum
    • Crypto
    • Altcoins
    • Blockchain
    • Bitcoin
    • Lithosphere News Releases
    laicryptolaicrypto
    Home Calculating Uniswap V2 Fees – CoinCodeCap
    Crypto

    Calculating Uniswap V2 Fees – CoinCodeCap

    John SmithBy John SmithJuly 10, 2025No Comments4 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Both Uniswap V1 and V2 have the same simple approach to fees. A flat 0.30% fee is applied to every trade, which is added to the Uniswap pair’s reserves.

    The way Uniswap processes fees is elegantly simple. Like everything else I have seen in the Uniswap V2 contracts, the system they use is lean and nothing is wasted. The contracts squeeze every drop of functionality they can out of every line of code.

    This focus on simplicity is one of the reasons it has been such a robust automated market maker exchange. However, this is also why it can be a non-trivial task to calculate the amount of fees a Uniswap pair has collected.

    To figure it all out, we must first understand its constant product formula and the meaning of k.

    Understanding “k”

    Trades on a Uniswap pair are priced using a constant product formula. This means that no matter what amount of tokens are added (or in the case of flash swaps, removed) from a pair’s reserves, the product of the reserves must remain constant.

    This formula, most simply expressed as x * y = k, states that trades must not change the product (k) of a pair’s reserve balances (x and y). (Uniswap V2)

    The value of k can only change when liquidity is deposited or withdrawn. This property is fundamental to how Uniswap calculates fees. By reducing the amount paid into a Uniswap pair by the 0.30% fee before checking that the k value has remained constant, the fee is essentially added to the reserves as new liquidity. Because no additional liquidity tokens are minted, this new liquidity is owned proportionally by all of a pair’s liquidity providers.

    This is what I meant by elegant simplicity. The fees are not some tacked on feature. There is no need for gas intensive internal accounting to calculate and distribute them. They fit perfectly into the Uniswap system.

    Using “k” to calculate fees

    Now we know that a Uniswap pair treats fees exactly like it treats liquidity. As a pair accumulates fees, its k value grows. But we also know that the k value can change as normal liquidity is deposited or withdrawn.

    A Uniswap pair discriminates between the two by calculating fees on every liquidity event, comparing the last k value to the most recent one. This captures the change in k independent of changes that resulted from minting or burning liquidity tokens.

    To go from the change in k between two liquidity events, to the amount of fees collected as a percentage of the pair’s total liquidity, we use the following formula from the “Uniswap v2 Core” whitepaper:

    (Adams et al. 5)(Adams et al. 5)
    (Adams et al. 5)

    Note the use of square roots. Because k is the product of a pair’s reserves, the square root of k is also the geometric mean of a pair’s reserves.

    The geometric mean is useful because it is a better representation of the total size of a pair’s reserves than k alone. Because collecting fees adds to a pair’s reserves, the growth of the geometric mean is the best way to calculate them.

    From here we can combine the amount of fees expressed as a percentage of the pair’s total liquidity, with the total value of the pair’s reserves, to get the value of collected fees.

    The total value of a Uniswap pair’s reserves can be calculated using the new Uniswap V2 oracles, but I will leave that topic for another article.

    What’s next?

    My team used these techniques in the HackMoney Virtual DeFi Hackathon to build theDALP(you can view our pitch video here). DALP stands for Decentralized Autonomous Liquidity Provider. Users can purchase DALP tokens and let the DALP automatically reallocated their liquidity to the highest-earning Uniswap pairs.

    To eliminate the need for manual governance, it was essential that we were able to rate Uniswap pairs against each other on-chain. This meant we had to fully understand how Uniswap calculated fees and how we could perform our own calculations. Check out our DALPManager contract to see how we did it.

    Calculating Uniswap V2 FeesCalculating Uniswap V2 Fees

    Special thanks to the DALP team

    Works Cited

    “Uniswap V2.” Uniswap/docs, https://uniswap.org/docs/v2. Accessed 28 May 2020.

    Adams, Hayden, et al. “Uniswap v2 Core.” March 2020, https://uniswap.org/whitepaper.pdf

    Connect with me on LinkedIn and Twitter.





    Source link

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    John Smith

    Related Posts

    Bitget Wallet launches new Pump.fun integrated mobile app for meme coin trading

    July 10, 2025

    Chinese creditors challenge FTX’s bid to block billions in payouts

    July 10, 2025

    XRP hits 7-week high above $2.38 as whale accumulation rises

    July 10, 2025
    Leave A Reply Cancel Reply

    Demo
    Don't Miss
    Altcoins

    Ethereum price surges 6% to $2,800 as shorts suffer amid $500M crypto liquidation

    By Benjamin LeeJuly 10, 20250

    Ethereum’s price rose 6% to above $2,800, driven by Bitcoin’s rally to $112,000 and optimism…

    Bitget Wallet launches new Pump.fun integrated mobile app for meme coin trading

    July 10, 2025

    Announcement – Certified Cryptocurrency Professional (CCP)™ Certification Launched

    July 10, 2025

    Chinese creditors challenge FTX’s bid to block billions in payouts

    July 10, 2025

    LAI Crypto is a user-friendly platform that empowers individuals to navigate the world of cryptocurrency trading and investment with ease and confidence.

    Our Posts
    • Altcoins (176)
    • Bitcoin (6)
    • Blockchain (34)
    • Crypto (2,461)
    • Ethereum (383)
    • Lithosphere News Releases (43)
    • Uncategorized (330)

    Subscribe to Updates

    • Twitter
    • Instagram
    • YouTube
    • LinkedIn

    Type above and press Enter to search. Press Esc to cancel.