Close Menu

    Subscribe to Updates

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

    What's Hot

    Bitcoin breaks ATH again, Dow Jones gains 200 points as Trump threatens new tariffs

    July 10, 2025

    EASE AutoTasks strips smart contracts deployment down to a button press

    July 10, 2025

    BlockDAG signs sports partnerships, SHIB rises, SOL sinks

    July 10, 2025
    Facebook X (Twitter) Instagram
    Cryptify Now
    • Home
    • Features
      • Typography
      • Contact
      • View All On Demos
    • Typography
    • Buy Now
    X (Twitter) Instagram YouTube LinkedIn
    Cryptify Now
    You are at:Home » Calculating Uniswap V2 Fees – CoinCodeCap
    Crypto

    Calculating Uniswap V2 Fees – CoinCodeCap

    James WilsonBy James WilsonJuly 10, 2025No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn 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 Tumblr Email
    Previous ArticleTop 10 sales trends to watch in 2023
    Next Article 10 Best Books on Crypto | Free Download Links 2025
    James Wilson

    Related Posts

    Bitcoin breaks ATH again, Dow Jones gains 200 points as Trump threatens new tariffs

    July 10, 2025

    EASE AutoTasks strips smart contracts deployment down to a button press

    July 10, 2025

    BlockDAG signs sports partnerships, SHIB rises, SOL sinks

    July 10, 2025
    Leave A Reply Cancel Reply

    Top Posts

    Remittix (RTX) hits $4m presale as XRP holders take notice

    February 4, 2025

    Here’s why OKB price spiked 20% today

    February 4, 2025

    iDEGEN price prediction: Is this the AI agent token to buy?

    February 4, 2025

    Gate.io to list CYBRO token on Dec 14 after $7M presale success

    February 4, 2025
    Don't Miss

    Bitcoin breaks ATH again, Dow Jones gains 200 points as Trump threatens new tariffs

    By James WilsonJuly 10, 2025

    Bitcoin has broken a new all-time high for a second day in a row, while…

    EASE AutoTasks strips smart contracts deployment down to a button press

    July 10, 2025

    BlockDAG signs sports partnerships, SHIB rises, SOL sinks

    July 10, 2025

    breakout or correction within a pennant?

    July 10, 2025
    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo

    Subscribe to Updates

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

    Demo
    About Us
    About Us

    CryptifyNow: Your daily source for the latest insights, news, and analysis in the ever-evolving world of cryptocurrency.

    X (Twitter) Instagram YouTube LinkedIn
    Our Picks

    Bitcoin breaks ATH again, Dow Jones gains 200 points as Trump threatens new tariffs

    July 10, 2025

    EASE AutoTasks strips smart contracts deployment down to a button press

    July 10, 2025

    BlockDAG signs sports partnerships, SHIB rises, SOL sinks

    July 10, 2025
    Lithosphere News Releases

    Colle AI’s iOS App Launch Brings Multichain NFT Creation to Mobile

    February 4, 2025

    AGII Transforms Web3 Infrastructure with AI-Optimized Smart Contracts

    February 4, 2025

    Colle AI (COLLE) Allocates $250M for AI Tool Development and Liquidity Growth on Solana

    February 4, 2025
    Copyright © 2025

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