Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
Automated Market Makers - Liquidity Pool - Question about the calculation

Esteemed, I would like to code all the steps for the correct calculation of a pool balance according to Uniswap V2 logic.
Anyone who knew how to help can write in any programming language (Python, Javascript etc.), in this example I used R.
The balancing process for a liquidity pool can be seen here: example1 and example2. However, it is not clear how to do it taking into account the Uniswap fee which is 0.3% for every trade.

library(tidyverse)
##### INITIAL PARAMETERS
#Uniswap charges users a flat 0.30% fee for
#every trade that takes place on the platform and 
#automatically sends it to a liquidity reserve
Uniswap.fee <- 0.30 / 100

ETH.initial.price <- 100
ETH.pool.price    <- 100

BNT.initial.price <- 1
BNT.pool.price    <- 1

##### INITIAL SITUATION

BNT.units <- 1000
BNT.total <- BNT.units * BNT.initial.price

ETH.units <- BNT.total / ETH.initial.price
ETH.total <- ETH.units * ETH.initial.price

Pool.Value <- BNT.total + ETH.total
Pool.DF <- data.frame(Symbol = c("BNT", "ETH"), Share = c(BNT.total,ETH.total))
ggplot(Pool.DF, aes(x = Symbol, y=Share, fill=Symbol)) +
  geom_bar(width = 1, position = "dodge", stat="identity") + labs(title="Initial POOL")

enter image description here

##### FINAL SITUATION

ETH.final.price   <- 120
ETH.pool.price    <- 100
BNT.final.price   <- 1
BNT.pool.price    <- 1

##### Imbalanced.Pool
Imbalanced.Pool <- data.frame(Symbol = c("BNT", "ETH"), 
                              Share = c(BNT.total,ETH.units * ETH.final.price ))
ggplot(Imbalanced.Pool, aes(x = Symbol, y=Share, fill=Symbol)) +
  geom_bar(width = 1, position = "dodge", stat="identity")+ labs(title="Imbalanced POOL: ETH valorization")

#### need to balance:

enter image description here

... Now I don't know how to continue the steps to correctly balance and obtain the impermanent loss and arb profit values.

Thank you very much,

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For DeFi protocols using automated market makers, we implement a pool of a pair of assets (for example, BTC-USDT), and we price the two assets simply with:

b * u = constant

Here b is the amount of BTC in the pool, and u is the amount of USDT. Besides, the constant is often written as K in many papers.
Now assume that you want to swap an amount of b' BTC to USDT. You give the b' BTC to the DeFi protocol, and the protocol finds that it should reduce the amount of USDT (which will be given to you) in order to keep b * u = constant. Then with simple calculations, the pool decides to give you u' USDT so that: (b+b')*(u-u') = constant

With this transaction (assuming that no 0.3% fee is accrued), you obtain u' USDT by giving b' BTC.

Then it comes to the 0.3% fee. When you give b' BTC to the pool, the pool acts as if you give only the deducted amount b'' == 99.7% * b'. Then the pool computes u'' so that

(b+b'')*(u-u'') = constant

In actual computation there are always errors due to limited number of decimals (you can only receive 0.1272 USDT instead of 0.127272727272...). Therefore, the asserted constant can be slowly increasing as more and more swap transactions are executed. This slowly increase the volume of the liquidity pool. Note that the accrued 0.3% fee may not serve as part of the liquidity pool.

For codes, I would use the codes of Flamingo as an example.

First you initialize a swap through FlamingoSwapRouter. You give your sold tokens to the router https://github.com/flamingo-finance/flamingo-contract-swap/blob/74e61f8406f9e8ededed72f9cb7e0139091ae17c/Swap/flamingo-contract-swap/FlamingoSwapRouter/FlamingoSwapRouterContract.cs#L237

Then the router SafeTransfer your sold token to its swapping pairContract, and call the swap method of the swapping contract to give you the token you buy.
https://github.com/flamingo-finance/flamingo-contract-swap/blob/74e61f8406f9e8ededed72f9cb7e0139091ae17c/Swap/flamingo-contract-swap/FlamingoSwapPair/FlamingoSwapPairContract.cs#L140
You can see the code var balance0Adjusted = balance0 * 1000 - amount0In * 3; which adjusts the assumed amount of token in the liquidity pool. There is a slightly exeeding amount of token in the liquidity pool which are accrued fees, not part of the liquidity.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda