Learn R Programming

uniswappeR

R Package to Interact and Trade with Uniswap Platform and Exploration of Uniswap data from GraphQL. Install Using devtools::install_github("OmniacsDAO/uniswappeR")

The package has 3 Functionalities

Trade Functionality

Description

Our uniswappeR R package includes the functionality to trade and query prices from the Uniswap Platform. To interact with the Uniswap Platform we need to configure the environment and then we can use the functions to make trades on the uniswap platform and query prices.

View the video walkthrough on how to conduct trades through UniswappeR here.

Walkthrough

0. (Optional Environment Setup) If you want to use the trade functionality of the package.

  • Install the reticulate package usinglibrary(reticulate)
  • Use python3, You can use py_discover_config() to figure the path for python3use_python("/usr/local/bin/python3")
  • Create a Virtual Environment to keep the backend sandboxed usingvirtualenv_create("uniswappeR-env")
  • Install uniswap-python package usingvirtualenv_install(envname="uniswappeR-env",packages=c("uniswap-python"))
  • Use the Virtual Environment usinguse_virtualenv("uniswappeR-env",required=TRUE)
  • Extract checksum function from Web3 moduleWeb3_checksum <- import("web3",convert=FALSE)$Web3$to_checksum_address

1. Use the Virtual Environment generated above

  • library(reticulate)
  • library(uniswappeR)
  • use_virtualenv("uniswappeR-env",required=TRUE)
  • Web3_checksum <- import("web3",convert=FALSE)$Web3$to_checksum_address

2. Use your Infura Node

set_infura_node("https://mainnet.infura.io/v3/XXXXXXXXXXXXXXXXXXX")

3. Setup a uniswap session using your address and private key. Choose UniswapV2 or UniswapV3 by supplying the version as 2 or 3 respectively.

u_w <- uniswap_session(user_add = "**", pvt_key = "***", version = 3)

4. Helper Functions to Check Balances and Query Prices

  • We would use Uniswap(UNI)/ETH pair to query on

    • UNI Token Address

    t_a <- Web3_checksum("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

    • UNI Token Decimals

    t_d <- 18

  • We would use Uniswap(UNI)/ DAI Stablecoin pair to query for Token to Token swap

    • UNI Token Address

    t1_a <- Web3_checksum("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

    • UNI Token Decimals

    t1_d <- 18

    • DAI Token Address

    t2_a <- Web3_checksum("0x6b175474e89094c44da98b954eedeac495271d0f")

    • DAI Token Decimals

    t2_d <- 18

  • Check Your ETH Balance

    • check_eth_balance(u_w)
  • Check Your Uniswap (UNI) Token Balance

    • check_tok_balance(t_a,t_d,u_w)
  • How much UNI Token you would get for .5 ETH, When you Swap ETH for UNI

    • Ethereum Quantity

    e_q <- .5

    • check_eth.to.tok_eth.fix(t_a,t_d,e_q,u_w)
  • How much ETH you need to get 2 UNI Tokens, When you Swap ETH for UNI

    • UNI Tokens Quantity

    t_q <- 2

    • check_eth.to.tok_tok.fix(t_a,t_d,t_q,u_w)
  • How much ETH you would get for 2 UNI Tokens, When you Swap UNI for ETH

    • Ethereum Quantity

    t_q <- 2

    • check_tok.to.eth_tok.fix(t_a,t_d,t_q,u_w)
  • How much UNI Token you need to get .5 ETH Tokens, When you Swap UNI for ETH

    • Ethereum Quantity

    e_q <- .5

    • check_tok.to.eth_eth.fix(t_a,t_d,e_q,u_w)
  • How much DAI Token you would get for 2 UNI, When you Swap UNI for DAI (Uses UNI->ETH->DAI Route)

    • UNI Quantity

    t1_q <- 2

    • check_tok1.to.tok2_tok1.fix(t1_a,t1_d,t2_a,t2_d,t1_q,u_w)
  • How much UNI Token you would need to get 50 DAI, When you Swap UNI for DAI (Uses UNI->ETH->DAI Route)

    • UNI Quantity

    t2_q <- 50

    • check_tok1.to.tok2_tok2.fix(t1_a,t1_d,t2_a,t2_d,t2_q,u_w)

5. Trade Functions to make swaps on Uniswap

  • We would use Uniswap(UNI)/ETH pair to make swaps

    • UNI Token Address

    t_a <- Web3_checksum("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

    • UNI Token Decimals

    t_d <- 18

  • We would use Uniswap(UNI)/ DAI Stablecoin pair for Token to Token swap

    • UNI Token Address

    t1_a <- Web3_checksum("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

    • UNI Token Decimals

    t1_d <- 18

    • DAI Token Address

    t2_a <- Web3_checksum("0x6b175474e89094c44da98b954eedeac495271d0f")

    • DAI Token Decimals

    t2_d <- 18

  • Swap .5 ETH for UNI Tokens

    • Ethereum Quantity

    e_q <- .5

    • trade_eth.to.tok_eth.fix(t_a,t_d,e_q,u_w)
  • Swap as much ETH required to get 2 UNI Tokens

    • UNI Quantity

    t_q <- .5

    • trade_eth.to.tok_tok.fix(t_a,t_d,t_q,u_w)
  • Swap 2 UNI Tokens for ETH

    • UNI Quantity

    t_q <- 2

    • trade_tok.to.eth_tok.fix(t_a,t_d,t_q,u_w)
  • Swap as much UNI Tokens required to get .5 ETH

    • Ethereum Quantity

    e_q <- .5

    • trade_tok.to.eth_eth.fix(t_a,t_d,e_q,u_w)
  • Swap 2 UNI Tokens for DAI Tokens

    • UNI Quantity

    t1_q <- 2

    • trade_tok1.to.tok2_tok1.fix(t1_a,t1_d,t2_a,t2_d,t1_q,u_w)
  • Swap as much UNI Tokens required to get 20 DAI Tokens

    • DAI Quantity

    t2_q <- 20

    • trade_tok1.to.tok2_tok2.fix(t1_a,t1_d,t2_a,t2_d,t2_q,u_w)

Pull Data Functionality

Description

Our uniswappeR R package contains the codebase that abstracts away the GraphQL layer of querying for uniswap data into a user-friendly R package.

View the video walkthrough on how to pull data into R using UniswappeR.

Walkthrough

1. Load the package.

  • library(uniswappeR)

2. View Stats or Historical Stats across all pairs generated by Uniswap Factory

  • factory_stats_v2()
  • factory_stats_v3()
  • uniswap_stats_hist_v2()
  • uniswap_stats_hist_v3()

3. View Stats or Historical Stats for a specific token across all pairs that token is included in

  • token_stats_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
  • token_stats_v3(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
  • token_stats_hist_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
  • token_stats_hist_v3(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

4. For a specific Token get stats for all pairs the Token is either Base or Quote

  • token_pair_map_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
  • token_pair_map_v3(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

5. Fetch All Pairs deployed

  • pairs_all_v2()
  • pairs_all_v3()

6. View Stats or Historical Stats for a specific Pair

  • pair_stats_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_stats_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")
  • pair_stats_hist_hourly_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_stats_hist_hourly_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")
  • pair_stats_hist_daily_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_stats_hist_daily_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

7. View Liquidity Positions or Historical LPs for a specific Pair

  • pair_liq_positions_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_liq_positions_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")
  • pair_liq_positions_hist_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_liq_positions_hist_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

8. Get Mints/Burns/Swaps Txs from a specific Pair

  • pair_mint_txs_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_mint_txs_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")
  • pair_burn_txs_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_burn_txs_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")
  • pair_swap_txs_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • pair_swap_txs_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

9. View Liquidity Positions or Historical LPs for a specific User

  • user_lps_v2(user_address = "0x2502f65d77ca13f183850b5f9272270454094a08")
  • user_lps_v3(user_address = "0xF1c206dd83ee2b8E6Ea675Cf827C93c58486B972")
  • user_hist_lps_v2(user_address = "0x2502f65d77ca13f183850b5f9272270454094a08")
  • user_hist_lps_v3(user_address = "0xF1c206dd83ee2b8E6Ea675Cf827C93c58486B972")

10. Get Mints/Burns/Swaps Txs from a specific User

  • user_swaps_v2(user_address = "0xcd8aa390e6eabbd2169b3580c1f7ce854675fd03")
  • user_swaps_v3(user_address = "0x431B5A84aCC1297Eda88259f300262F1bc3A74f3")
  • user_mints_v2(user_address = "0xcd8aa390e6eabbd2169b3580c1f7ce854675fd03")
  • user_mints_v3(user_address = "0x431B5A84aCC1297Eda88259f300262F1bc3A74f3")
  • user_burns_v2(user_address = "0xcd8aa390e6eabbd2169b3580c1f7ce854675fd03")
  • user_burns_v3(user_address = "0x431B5A84aCC1297Eda88259f300262F1bc3A74f3")

11. Export any of the pulled data as CSV file

  • data_to_export <- token_stats_hist_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
  • path_to_export <- "~/Desktop/uniswappeR_export.csv"
  • export_data(data_to_export,path_to_export)

Data Exploration Functionality

Description

Our uniswappeR R package includes a number of high level functions for interacting and visualising the data from uniswap:

Walkthrough

1. UniswapV3 Pair Token Liquidity Range Forecast.

The below code snippet provides a forecast using geometric Brownian motion its tunable parameters are the Pair's Address, forecast window, max % increase that can occur in a single day, and number of simulations, respectively. The resulting output is a data frame with the liquidity range forecast.

  • liquidity_range_all_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801",days=30,cap=10,sims=1000)

This provides the suggested range for the liquidity.

  • liquidity_range_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

2. Liquidity Range Estimates Visualisation

Allows for the visualisation of the GBM process for a Pair's address. Additionally ggplot pipelines can be added to modify the visualisation.

  • liquidity_range_visualization("0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

3. Uniswap Platform Growth

This section introduces various visualisations within uniswappeR.

This allows the user to visualise various growth metrics of the UniswapV2 Platform. From this, insights about daily volume, liquidity, and transaction counts can be analyzed. For example, we can infer that the amount of transactions overtime is steadily increasing, and that there is not a clear, discernible pattern for the daily volume nor liquidity.

  • vis_uniswap_stats_hist_v2()

This allows the user to visualise various growth metrics of the UniswapV3 Platform. From this, insights about daily volume, liquidity, and transaction counts can be analyzed. This visual quickly allows us to see that sometime around March 2022, a large spike occured for the daily volume. This this enables the curious user to explore in further detail the events that occurred that day.

  • vis_uniswap_stats_hist_v3()

4. Token Growth

This allows the user to visualise various growth metrics of a given token in UniswapV2. Similar to the other historical stats functions, this allows the user to see past trends for a specific token on UniswapV2. From this we can quickly notice a downward trend in token liquidity signaling a rush on that token.

  • vis_token_stats_hist_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

This allows the user to visualise various growth metrics of a given token in UniswapV3. Similar to the other historical stats functions, this allows the user to see past trends for a specific token on UniswapV3. One quick insight we can gather is that there appears to be a progressively less popularity in this token's trade volume which is allow, perhaps, correlating with the token's price.

  • vis_token_stats_hist_v3(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

5. Token Number of Pairs Growth

The below visual provides an indication of how often a given token is in a pair where token0 and token1 are given by the sorting order.

  • vis_token_pair_map_v2(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

The below visual provides an indication of how often a given token is in a pair where token0 and token1 are given by the sorting order.

  • vis_token_pair_map_v3(token_address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")

6. Pair Growth

The following provides a vasual of various growth metrics of a given pair in UniswapV2. In this example the pair_address is UNI to DAI.

  • vis_pair_stats_hist_daily_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")

The following provides a vasual of various growth metrics of a given pair in UniswapV3.

  • vis_pair_stats_hist_daily_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

7. Liquidity Token Distribution

Allows for a user to visualise liquidity positions spread in a given pair via a histogram. One insight we can gather from this is that one address is providing about 1450 tokens to the LP whereas most others are contributing substaintially less.

  • vis_pair_liq_positions_v2(pair_address = "0xf00e80f0de9aea0b33aa229a4014572777e422ee")
  • vis_pair_liq_positions_v3(pair_address = "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801")

8. User Swap Performance

Allows a user to retrieve all the swaps data for a given address or addresses. This allows a user to readily recognize their level of activity.

  • addresses <- c("0x2e3381202988d535e8185e7089f633f7c9998e83", "0x4d9c274ADF71e4201B4aB1f28BF05D44eE4bA261")
  • swap_data <- swaps(addresses)
  • swap_visualizations(swap_data)

9. Save analysis plot

  • plot_to_export <- vis_uniswap_stats_hist_v2()
  • path_to_export <- "~/Desktop/uniswappeR_plot_export.png"
  • export_plot(plot_to_export,path_to_export)

Significant Updates

5/14/2022

  • Added UniswapV3 data pull support for pending 'Liquidity Positions'

3/9/2022

  • Added UniswapV3 data pull support for all stats, except 'Liquidity Positions'
  • Added trading functionality for V3
  • Updated the ReadMe to reflect the most recent changes

About Us

Omni Analytics Group is an incorporated group of passionate technologists who help others use data science to change the world. Our practice of data science leads us into many exciting areas where we enthusiastically apply our machine learning, artificial intelligence and analysis skills. Our flavor for this month, the blockchain! To learn more about what we do or just to have fun, join us over on Twitter @OmniAnalytics.

Copy Link

Version

Install

install.packages('uniswappeR')

Monthly Downloads

6

Version

0.6.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Eric Hare

Last Published

August 18th, 2023

Functions in uniswappeR (0.6.1)

liquidity_range_visualization

Get a visualization liquidity range estimates
trade_eth.to.tok_tok.fix

Swap ETH for a Token, Buy specified fixed Token Amount
pair_swap_txs_v3

Get UniswapV3 Swap Transactions in a pair
user_hist_lps_v3

Get UniswapV3 Historical User Liquidity Positions
pair_liq_positions_v2

Get UniswapV2 Current Liquidity Positions in a pair
trade_eth.to.tok_eth.fix

Swap ETH for a Token, Receive Tokens for specified ETH amount
pair_liq_positions_v3

Get UniswapV3 Current Liquidity Positions in a pair
pair_swap_txs_v2

Get UniswapV2 Swap Transactions in a pair
user_lps_v2

Get UniswapV2 User Liquidity Positions
user_burns_v3

Get UniswapV3 User Burn Txs
user_hist_lps_v2

Get UniswapV2 Historical User Liquidity Positions
token_pair_map_v2

Get UniswapV2 Token's associated pairs
token_pair_map_v3

Get UniswapV3 Token's associated pairs
get_infura_node

Gets the Infura Node
pair_stats_hist_hourly_v2

Get UniswapV2 Hourly Pair Historical Stats
pair_stats_hist_hourly_v3

Get UniswapV3 Hourly Pair Historical Stats
liquidity_range_all_v3

Forecast Price for liquidity range for a pair's tokens UniswapV3
pair_mint_txs_v2

Get UniswapV2 Mint Transactions in a pair
factory_stats_v2

Get UniswapV2 Factory Stats
trade_tok.to.eth_tok.fix

Swap Token for ETH, Receive ETH after swapping specified token amount
pair_stats_v2

Get UniswapV2 Pair Stats
trade_tok.to.eth_eth.fix

Swap Token for ETH, Swap tokens to receive specified ETH amount
pair_stats_v3

Get UniswapV3 Pair Stats
factory_stats_v3

Get UniswapV3 Factory Stats
py_int

Python builtin functions
r_num

python object to R
user_mints_v3

Get UniswapV3 User Mint Txs
vis_pair_liq_positions_v2

Visualise Liquidity Positions spread in a given pair
user_swaps_v3

Get UniswapV3 User Swap Txs
vis_token_stats_hist_v3

Visualise various growth metrics of a given token in UniswapV3
vis_uniswap_stats_hist_v2

Visualise various growth metrics of the UniswapV2 Platform
vis_pair_liq_positions_v3

Visualise Liquidity Positions spread in a given pair
user_swaps_v2

Get UniswapV2 User Swap Txs
swap_visualizations

Get a visualization on the swap data
token_stats_hist_v2

Get UniswapV2 Token Historical Stats
swaps

Get all the swaps data for a given address or addresses
uniswap_session

Start the python session
swap_statistics

Get statistics on the swaps data for a given address or addresses
uniswap_stats_hist_v2

Get UniswapV2 Historical Stats
set_infura_node

Sets the Infura Node
token_stats_v3

Get UniswapV3 Token Stats
pair_mint_txs_v3

Get UniswapV3 Mint Transactions in a pair
token_stats_v2

Get UniswapV2 Token Stats
token_stats_hist_v3

Get UniswapV3 Token Historical Stats
vis_pair_stats_hist_daily_v2

Visualise various growth metrics of a given pair UniswapV2
vis_pair_stats_hist_daily_v3

Visualise various growth metrics of a given pair UniswapV3
trade_tok1.to.tok2_tok1.fix

Swap Token1 for Token2, Receive Token2 for specified Token1 Amount
user_lps_v3

Get UniswapV3 User Liquidity Positions
user_mints_v2

Get UniswapV2 User Mint Txs
trade_tok1.to.tok2_tok2.fix

Swap Token1 for Token2, Receive specified Token2 Amount
pair_stats_hist_daily_v3

Get UniswapV3 Daily Pair Historical Stats
pair_stats_hist_daily_v2

Get UniswapV2 Daily Pair Historical Stats
pairs_all_v2

Get UniswapV2 All Pairs
vis_token_pair_map_v2

Visualise Number of pairs the token is present UniswapV2
vis_token_stats_hist_v2

Visualise various growth metrics of a given token in UniswapV2
user_burns_v2

Get UniswapV2 User Burn Txs
vis_uniswap_stats_hist_v3

Visualise various growth metrics of the UniswapV3 Platform
vis_token_pair_map_v3

Visualise Number of pairs the token is present UniswapV3
pairs_all_v3

Get UniswapV3 All Pairs
uniswap_stats_hist_v3

Get UniswapV3 Historical Stats
check_eth_balance

Get your ETH Balance
check_tok.to.eth_tok.fix

Swap Token for ETH, Given Token Qty check how much ETH you would get
check_tok.to.eth_eth.fix

Swap Token for ETH, Given ETH Qty check how much Token you need
check_tok1.to.tok2_tok1.fix

Swap Token1 for Token2, Given Token1 Qty check how much Token2 you would get (Use Token1 -> ETH -> Token2 Route)
check_eth.to.tok_eth.fix

Swap ETH for a Token, Given ETH Qty check how much token you would get
export_data

Write out the data object given file name
check_tok_balance

Check any Token Balance
check_eth.to.tok_tok.fix

Swap ETH for a Token, Given Token Qty check how much ETH you need
check_tok1.to.tok2_tok2.fix

Swap Token1 for Token2, Given Token2 Qty check how much Token1 you would need (Use Token1 -> ETH -> Token2 Route)
export_plot

Write out the analysis plots
liquidity_range_v3

Get a suggested range for liquidity
pair_liq_positions_hist_v2

Get Unsiwap2 Historical Liquidity Positions in a pair
pair_liq_positions_hist_v3

Get Unsiwap3 Historical Liquidity Positions in a pair
pair_burn_txs_v2

Get UniswapV2 Burn Transactions in a pair
pair_burn_txs_v3

Get UniswapV3 Burn Transactions in a pair