Home > Blockchain >  Calculating appropriate rewards for Uniswap like Liquidity pool contract (Solidity)
Calculating appropriate rewards for Uniswap like Liquidity pool contract (Solidity)

Time:12-20

I'm trying to calculate rewards for liquidity providers and I found this equation that Uniswap apparently uses: Basic Formula (L = liquidity): (L_you / L_others) * (24h_swap_volume * pool_fee_rate) And I'm trying to implement this in my smart contract but I can't seem to be able to because the liquidity held by others will always be larger than the liquidity you hold which requires a decimal value so my question is: How do I use this equation in a Solidity smart contract without falling into floating-point hell?

CodePudding user response:

After doing a little more digging I found that you can use ABDKMath to achieve this you can find more information using the links below: Library: https://github.com/abdk-consulting/abdk-libraries-solidity Article: https://www.bitcoininsider.org/article/68630/10x-better-fixed-point-math-solidity As a code example here is a quick snippet of how I implemented this:

uint256 fee = ABDKMath64x64.mulu(
        ABDKMath64x64.divu(
          tokensProvided,
          others
        ),
        taxableValue
      );

This is probably not the best solution but it worked for me. Also, take a look at https://github.com/hifi-finance/prb-math for a higher precision floating-point math library. Regarding gas efficiency here is a quote from the PRB math lib:

The typeless PRBMath library is faster than ABDKMath for abs, exp, exp2, gm, inv, ln, log2. Conversely, it is slower than ABDKMath for avg, div, mul, powu and sqrt. There are two technical reasons why PRBMath lags behind ABDKMath's mul and div functions


So for this use case, I think it is a better idea to use ABDK Math instead of PRB math unless you plan on transferring more than 2 to the 128th power of Wei.

  • Related