Impermanent loss is a form of opportunity cost in which individuals who lend assets through Automated Market Maker lending pools can see a decrease in the currency value of their assets compared to the value they would have received by holding (HODL) their assets outside of the lending pool. When considering whether to lend or HODL, individuals balance the projected impermanent loss against the projected fees they will earn from lending.
The impermanentlosscalc package allows users to calculate hypothetical Impermanent Loss (IL) and Net Profit and Loss (PnL) projections under a variety of projected price scenarios.
A single function handles the more commonly used two asset Constant Function Market Maker (CFMM) pools, such as Uniswap and extends to \(\mathbf{N}\)-asset pools, such as Balancer.
Tiruviluamala et al. (2023) showed that impermanent loss for an \(\mathbf{N}\)-asset pool can be calculated as \[ \text{IL} = \frac{\text{Geometric Mean of the } t_j\text{'s}}{\text{Arithmetic Mean of the } t_j\text{'s}} - 1 \] where the \(t_{j}\) represent the relative price changes for each asset in the pool.
As demonstrated in the referenced literature, impermanent loss is determined solely by the ratio of price change for each asset, making the calculation straightforward regardless of the number of assets. This function takes the following arguments:
The function then produces the following outputs:
The most common AMMs use a 50/50 weight between two assets. If one asset doubles in price, we expect IL to be approximately \(5.7\%\). This shows the package can be used as a standard IL calculator.
# 50/50 Pool, Token 1 price doubles (100 -> 200)
library(impermanentlosscalc)
impermanent_loss(
prices_old = c(100, 10),
prices_new = c(200, 10),
weights = c(0.5, 0.5), # Standard 50/50 weights
investment = 1000,
fees = 0,
plot = FALSE
)## $`Value if held`
## [1] 1500
##
## $`Value in pool`
## [1] 1414.214
##
## $impermanent_loss_percent
## [1] 0.05719096
##
## $`Nominal impermanent loss`
## [1] 85.78644
##
## $`Fee offset ($)`
## [1] 0
##
## $`Net gain`
## [1] -85.78644
Though less common, multi asset AMM lending pools with unequal asset weights also exist. This function allows users to calculate impermanent loss for \(\mathbf{N}\)-asset pools with unequal weights.
# 20/40/40 Pool, Token 1 price triples (10 -> 30).
library(impermanentlosscalc)
impermanent_loss(
prices_old = c(10, 10, 10),
prices_new = c(30, 10, 10),
weights = c(0.2, 0.4, 0.4), # Unequal weights for three assets
investment = 1000,
fees = 0,
plot = FALSE
)## $`Value if held`
## [1] 1400
##
## $`Value in pool`
## [1] 1245.731
##
## $impermanent_loss_percent
## [1] 0.1101922
##
## $`Nominal impermanent loss`
## [1] 154.2691
##
## $`Fee offset ($)`
## [1] 0
##
## $`Net gain`
## [1] -154.2691
Tiruviluamala, N., Port, A., & Lewis, E. (2022). A general framework for impermanent loss in automated market makers. arXiv preprint arXiv:2203.11352.
## R version 4.3.3 (2024-02-29)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Linux Mint 22.2
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/Denver
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] impermanentlosscalc_0.1.0
##
## loaded via a namespace (and not attached):
## [1] vctrs_0.6.5 cli_3.6.5 knitr_1.50 rlang_1.1.6
## [5] xfun_0.51 generics_0.1.3 S7_0.2.1 jsonlite_2.0.0
## [9] glue_1.8.0 htmltools_0.5.8.1 sass_0.4.9 scales_1.4.0
## [13] rmarkdown_2.29 grid_4.3.3 tibble_3.2.1 evaluate_1.0.3
## [17] jquerylib_0.1.4 fastmap_1.2.0 yaml_2.3.10 lifecycle_1.0.4
## [21] compiler_4.3.3 dplyr_1.1.4 RColorBrewer_1.1-3 pkgconfig_2.0.3
## [25] rstudioapi_0.17.1 farver_2.1.2 digest_0.6.37 R6_2.6.1
## [29] tidyselect_1.2.1 dichromat_2.0-0.1 pillar_1.10.1 magrittr_2.0.3
## [33] bslib_0.9.0 tools_4.3.3 gtable_0.3.6 ggplot2_4.0.1
## [37] cachem_1.1.0