Uniswap is a decentralized cryptocurrency exchange that allows for the permissionless trading of Ethereum based ERC-20 tokens. Through their grants program, the protocol’s governance has funded initiatives that support the creation of community infrastructure that lower the barriers of entry for those seeking to interact with the platform from within alternative development environments. Consistent with that mandate, through our grant we created an open source software library for the R language that allows developers, traders, researchers and enthusiasts the ability to query, analyze, export and model data generated from the Uniswap platform. Our uniswappeR R package includes the functionality to trade and query prices from the Uniswap platform. To interact with the Uniswap through this language we’ll first need to configure the environment and then use package defined functions to perform swaps and collect data. This blog post provides an overview of the motivation behind the creation of the package, who we expect our primary users to be, the steps needed to configure the environment, and a survey of the functionality contained within the package.
The growth of the Uniswap platform has come with both a need for tools that help enable interaction with the platform, as well as higher-level analytics about the growth of the platform itself. We’ve built
uniswappeR to target both of these key areas.
uniswappeRmaintains a broad set of trading and utility functions. Because these have been abstracted into simple R-based functions, the possibilities are significant in terms of automation. For example, perhaps a trader on the Uniswap platform wishes to execute a particular trade given a sit of criteria, or at a specific time. The automation afforded to us by the R language and this package means that workflows such as this are now easy and seamless. Perhaps another trader wishes to perform an analysis of their current positions in order to help decide on a trade. The suite of visualization and data analysis tools in R makes the process of doing so significantly more seamless. These could even be abstracted into an API – imagine a simple web-based REST API which allows the inputting of a list of tokens and quickly retrieves statistics on pool liquidity with respect to these tokens.
At a platform level,
uniswappeRallows for a visualization and statistical assessment of the growth and trends in growth of users and usage. This has implications both from the perspective of a Uniswap user and for the Uniswap community / development team as a whole. For the user, the easy to access metrics about platform usage help lend confidence in the platform itself, and to help indicate potential trends in when trades may be most lucrative. For the broader Uniswap community, our package provides a simple way of accessing growth metrics that can help with the governance and direction of the platform, and can be chained to down-stream analysis in order to make more informed and data-driven decisions.
Full use of
uniswappeR requires a Python environment available. Fortunately, thanks to the
reticulate package, we can easily interact with Python code within an R session. To setup your session, perform the following steps:
- Install the reticulate package using
- Install python to use as backend using
- Create a Virtual Environment to keep the backend sandboxed using:
virtualenv_create("uniswappeR-env", version = "3.8.7")
- Install uniswap-python package using
- Use the Virtual Environment using
- Extract checksum function from Web3 module using
Web3_checksum <- import("web3",convert=FALSE)$Web3$toChecksumAddress
All together, the code looks as follow:
library(reticulate) install_python("3.8.7") virtualenv_create("uniswappeR-env", version = "3.8.7") virtualenv_install(envname="uniswappeR-env",packages=c("uniswap-python==0.4.6")) use_virtualenv("uniswappeR-env", required = TRUE) Web3_checksum <- import("web3",convert=FALSE)$Web3$toChecksumAddress
Now, we need to simply activate the virtual environment:
library(reticulate) library(uniswappeR) Web3_checksum <- import("web3",convert=FALSE)$Web3$toChecksumAddress
To configure your Infura node, you use the following:
##  TRUE
At this stage, we need to set our address and private key:
u_w <- uniswap_session(user_add="<your_address>",pvt_key="<your_private_key>")
Now, we are ready to begin!
The package includes a variety of helper functions which perform queries against the uniswap API. For instance, with the following we can check our balance in ETH:
##  18.32611
We can use the Uniswap Token Address, along with the number of decimals, to check our token balance:
t_a <- Web3_checksum("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984") t_d <- 18 check_tok_balance(t_a,t_d,u_w)
##  17.81118
Suppose we wish to determine how much ETH we would need in order to get 2 UNI tokens. We can do that simple check easily:
t_q <- 2 check_eth.to.tok_tok.fix(t_a,t_d,t_q,u_w)
##  2.451879
A class of similar functions are available:
check_eth.to.tok_tok.fix: How much ETH you would get for 2 UNI Tokens, When you Swap UNI for ETH
check_tok.to.eth_tok.fix: How much UNI Token you need to get .5 ETH Tokens, When you Swap UNI for ETH
check_tok.to.eth_eth.fix: How much DAI Token you would get for 2 UNI, When you Swap UNI for DAI (Uses UNI->ETH->DAI Route)
check_tok1.to.tok2_tok1.fix: How much UNI Token you would need to get 50 DAI, When you Swap UNI for DAI (Uses UNI->ETH->DAI Route)
One of the fundamental components of the package is the ability to make trades. The basic trading functionality is encapsulated in a set of
trade_eth.to.tok_eth.fix: Swap ETH for UNI Tokens
trade_eth.to.tok_tok.fix: Swap as much ETH required to get N UNI Tokens
trade_tok.to.eth_tok.fix: Swap UNI Tokens for ETH
trade_tok.to.eth_eth.fix: Swap as much UNI Tokens required to get N ETH
trade_tok1.to.tok2_tok1.fix: Swap UNI Tokens for other Tokens
For example, suppose I wanted to swap .5 ETH for UNI tokens. I would simply execute:
e_q <- .5 trade_eth.to.tok_eth.fix(t_a,t_d,e_q,u_w)
Pulling Historical Data
In addition to lower-level functions for checking balances and performing trades,
uniswappeR has rich functionality to pull historical data to perform a down-stream analysis or produce visualizations. We provide the
factory_stats_v2() function which gives statistics across all pairs generated by the Uniswap Factory. There is also a corresponding
uniswap_stats_hist_v2() function which has the historical data.
We can look at the stats for a specific token by calling
token_stats_hist_v2 and passing in the corresponding address. For example:
token_stats_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
Similarly, we can get statistics for pairs following the same convention as previous:
pair_stats_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
Likewise for the liquidity positions:
pair_liq_positions_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
All of these datasets can be exported into a standard CSV file with a general data_export function, like this:
Last but not least, we turn to an exploration of growth metrics on the platform, using data visualization.
There are a suite of functions designed to illustrate the growth of the platform as a whole, at a high level. These functions produce visualizations. Here is a quick survey. The first provides a view of the growth in terms of volume, liquidity, and number of transactions of the platform. This can be shown both in terms of the Uniswap platform, and with respect to particular tokens by passing in an address. At the platform level, we can see some very obvious patterns. The total liquidity in ETH peaked during the late 2020 time frame, while the total liquidity when measured in USD peaked along with the overall crypto market in spring 2021. Transactions on the platform have steadily rose as a function of time, though over the past couple of months have tended to level off some compared to the more rapid growth prior. As illustrated, these metrics can be obtained at the per-token level, in this case UNI, which allows for an even more fine-grained assessment of how use of the platform is evolving over time.
There is also a set of visualization functions that are designed to operate on pairs, for visualization of the number of pairs, the growth in terms of the aforementioned metrics of volume with respect to the pairs, and in terms of the liquidity token distribution. Here we look at these stats for the UNI/DAI pair. The first plot illustrates UNI as both the first and the second token in the pair has seen a large increase, particularly in the late 2020 time frame. The latter two plots tell a consistent story: While the overall number of daily transactions with this pair has declined, there are still several daily spikes that indicate the pair is seeing significant volume on the platform. This is just one example of the type of visualizations that
uniswappeR can produce, which illustrates how this can be used to derive insights and metrics across the platform, including determining which pairs have seen the most recent growth, and so much more.
Finally, there is a function
swap_visualizations which creates a pre-set panel of visualizations designed to illustrate the amount of swaps over time, and by token. Here we pass in a set of addresses and the corresponding swap data is pulled. This allows a per-user view of the performance, so we can quickly at-a-glance see how the usage of the platform has changed at the per-user, per-pair level. You can imagine this could be used to produce per-user dashboards with respect to trades / swaps done on the Uniswap platform, so users can quickly use analytics to assess their own trading behavior.
We have encapsulated much of the content of this walkthrough into two video tutorials. The first covers the setup of the package itself, and the second covers the ability to make trades with the package. Those videos can be seen below:
As you can see, the
uniswappeR package provides a large host of functionality, ranging from smaller helper utilities, to functions for initializing trades, to aggregation and visualization functions. This package operates on both a micro / account level, but also provides a statistical overview that is useful from the perspective of the platform itself. While still early in its infancy, we do hope that this package serves as a useful tool for those familiar with the R language and opens up the Uniswap platform to more use.