Close Menu
Chain Tech Daily

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Wrapping up Devconnect, looking ahead!

    September 6, 2025

    XRP price Murrey Math Lines indicate surge ahead of ETF approvals

    September 6, 2025

    Bitcoin and M2: A link that falls apart on close inspection

    September 6, 2025
    Facebook X (Twitter) Instagram
    Chain Tech Daily
    • Altcoins
      • Litecoin
      • Coinbase
      • Crypto
      • Blockchain
    • Bitcoin
    • Ethereum
    • Lithosphere News Releases
    Facebook X (Twitter) Instagram YouTube
    Chain Tech Daily
    Home » Calculating Uniswap V2 Fees – CoinCodeCap
    Crypto

    Calculating Uniswap V2 Fees – CoinCodeCap

    James WilsonBy James WilsonJuly 10, 20254 Mins Read
    Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    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
    James Wilson

    Related Posts

    Crypto September 6, 2025

    XRP price Murrey Math Lines indicate surge ahead of ETF approvals

    Crypto September 6, 2025

    Litecoin trades technicals for taunts, amid influencer feud

    Crypto September 6, 2025

    Ethereum spot ETFs see second-largest outflow surge ever

    Crypto September 6, 2025

    Zexpire targets growth backed by rising 0DTE trading trend

    Crypto September 6, 2025

    SEC under fire after Gary Gensler’s texts wiped during FTX collapse

    Crypto September 6, 2025

    How traders are earning from burning the largest altcoin 

    Leave A Reply Cancel Reply

    Don't Miss
    Ethereum September 6, 2025

    Wrapping up Devconnect, looking ahead!

    A few weeks ago, we came together in Amsterdam for the first-ever Devconnect. Over eight…

    XRP price Murrey Math Lines indicate surge ahead of ETF approvals

    September 6, 2025

    Bitcoin and M2: A link that falls apart on close inspection

    September 6, 2025

    Grantee Roundup – May 2022

    September 6, 2025
    Stay In Touch
    • Facebook
    • Twitter
    • YouTube
    • LinkedIn
    Our Picks

    Wrapping up Devconnect, looking ahead!

    September 6, 2025

    XRP price Murrey Math Lines indicate surge ahead of ETF approvals

    September 6, 2025

    Bitcoin and M2: A link that falls apart on close inspection

    September 6, 2025

    Grantee Roundup – May 2022

    September 6, 2025

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    Don't Miss
    Ethereum September 6, 2025

    Wrapping up Devconnect, looking ahead!

    A few weeks ago, we came together in Amsterdam for the first-ever Devconnect. Over eight…

    XRP price Murrey Math Lines indicate surge ahead of ETF approvals

    September 6, 2025

    Bitcoin and M2: A link that falls apart on close inspection

    September 6, 2025

    Grantee Roundup – May 2022

    September 6, 2025

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    About Us
    About Us

    ChainTechDaily.xyz delivers the latest updates and trends in the world of cryptocurrency. Stay informed with daily news, insights, and analysis tailored for crypto enthusiasts.

    Our Picks
    Lithosphere News Releases

    Imagen Network (IMAGE) Adds XRP Ledger Support to Improve Blockchain Interoperability

    September 5, 2025

    Imagen Network (IMAGE) Developer Presents Plan to Buy $150M in Ethereum (ETH)

    September 4, 2025

    Imagen Network (IMAGE) Integrates Grok Intelligence to Expand Adaptive Creator Engagement

    September 2, 2025

    Imagen Network (IMAGE) Integrates Grok Models to Advance Creator Personalization

    August 29, 2025
    X (Twitter) Instagram YouTube LinkedIn
    © 2025 Copyright

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