npm package: @0xelod/uniswap-v3-backtest
Description: Fast and efficient method for testing Uniswap V3 LP Strategies
Author: staceb
Latest version: 1.1.2

# Uniswap V3 LP Strategy BackTester

Strategy Backtester for providing liquidity to a Uniswap V3 Pool. Based on logic described in the following article:

[Historical Performances of Uniswap V3 Pools](https://defi-lab.medium.com/historical-performances-of-uniswap-l3-pools-2de713f7c70f)

![backtest-performance](https://user-images.githubusercontent.com/5744432/167617903-efd0829f-0b32-4c7f-b611-47398d8e435c.png)

## Install

```shell
npm install uniswap-v3-backtest
```

## Obtain pool IDs for Taraxa testnet

- [Get the pool ids](https://indexer.lswap.app/subgraphs/name/lara-staking/uniswap-v3/graphql?query=query+poolIds%7B%0A++pools%28subgraphError%3A+deny%29%7B%0A++++id%0A++++token0+%7B%0A++++++id%0A++++%7D%0A++++token1+%7B%0A++++++id%0A++++%7D%0A++%7D%0A%7D)
- [Get the pool data](https://indexer.lswap.app/subgraphs/name/lara-staking/uniswap-v3/graphql?query=%0A++++%23%0A++++%23+Welcome+to+The+GraphiQL%0A++++%23%0A++++%23+GraphiQL+is+an+in-browser+tool+for+writing%2C+validating%2C+and%0A++++%23+testing+GraphQL+queries.%0A++++%23%0A++++%23+Type+queries+into+this+side+of+the+screen%2C+and+you+will+see+intelligent%0A++++%23+typeaheads+aware+of+the+current+GraphQL+type+schema+and+live+syntax+and%0A++++%23+validation+errors+highlighted+within+the+text.%0A++++%23%0A++++%23+GraphQL+queries+typically+start+with+a+%22%7B%22+character.+Lines+that+start%0A++++%23+with+a+%23+are+ignored.%0A++++%23%0A++++%23+An+example+GraphQL+query+might+look+like%3A%0A++++%23%0A++++%23+++++%7B%0A++++%23+++++++field%28arg%3A+%22value%22%29+%7B%0A++++%23+++++++++subField%0A++++%23+++++++%7D%0A++++%23+++++%7D%0A++++%23%0A++++%23+Keyboard+shortcuts%3A%0A++++%23%0A++++%23++Prettify+Query%3A++Shift-Ctrl-P+%28or+press+the+prettify+button+above%29%0A++++%23%0A++++%23+++++Merge+Query%3A++Shift-Ctrl-M+%28or+press+the+merge+button+above%29%0A++++%23%0A++++%23+++++++Run+Query%3A++Ctrl-Enter+%28or+press+the+play+button+above%29%0A++++%23%0A++++%23+++Auto+Complete%3A++Ctrl-Space+%28or+just+start+typing%29%0A++++%23%0A%0A++++query+poolHourData+%7B%0A++++++poolHourDatas+%28orderBy%3AperiodStartUnix%2C+orderDirection%3Adesc%2C+first%3A1000%29+%7B%0A++++periodStartUnix%0A++++liquidity%0A++++high%0A++++low%0A++++pool+%7B%0A++++++id%0A++++++totalValueLockedUSD%0A++++++totalValueLockedToken1%0A++++++totalValueLockedToken0%0A++++++token0%0A++++++++%7Bdecimals%7D%0A++++++token1%0A++++++++%7Bdecimals%7D%0A++++%7D%0A++++close%0A++++feeGrowthGlobal0X128%0A++++feeGrowthGlobal1X128%0A++++%7D%0A++++%7D&operationName=poolHourData)

## Usage

```js
// get results for last 25 days
import uniswapStrategyBacktest from "uniswap-v3-backtest";
const backtestResults = await uniswapStrategyBacktest(
  "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
  1000,
  2120.09,
  2662.99,
  { days: 25, period: "daily" }
);

// get results from start timestamp for lp from quote token
await uniswapStrategyBacktest(
  "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
  1,
  1 / 2662.99,
  1 / 2120.09,
  { startTimestamp: 1653364800, period: "daily", priceToken: 1 }
);

// get results from start timestamp to end timestamp for lp from quote token
await uniswapStrategyBacktest(
  "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
  1,
  1 / 2662.99,
  1 / 2120.09,
  {
    startTimestamp: 1653364800,
    endTimestamp: 1653374800,
    period: "daily",
    priceToken: 1,
  }
);

// get results for n days before end timestamp for lp from quote token
await uniswapStrategyBacktest(
  "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
  1,
  1 / 2662.99,
  1 / 2120.09,
  { endTimestamp: 1653364800, days: 1, period: "daily", priceToken: 1 }
);
```

Example Output:

```

// Hourly //

{
  periodStartUnix: 1652274000,
  liquidity: '7675942584871332685',
  high: '2266.726774269547858798641816695647',
  low: '2145.393138561593202680715136665708',
  pool: {
    id: '0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640',
    totalValueLockedUSD: '231193174.2181918276487229629805612',
    totalValueLockedToken1: '71499.990198836160751569',
    totalValueLockedToken0: '71417122.541685',
    token0: { decimals: '6' },
    token1: { decimals: '18' }
  },
  close: '2246.577649943233620476969923660446',
  feeGrowthGlobal0X128: '1432478142734251891146870279471391',
  feeGrowthGlobal1X128: '491787243029421936881695073823469456119843',
  day: 11,
  month: 4,
  year: 2022,
  fg0: 0,
  fg1: 0,
  activeliquidity: 100,
  feeToken0: 0,
  feeToken1: 0,
  tokens: [ 259.4720394308191, 0.3296249121804859 ],
  fgV: 0,
  feeV: 0,
  feeUnb: 0,
  amountV: 999.9999999999999,
  amountTR: 1000,
  feeUSD: 0,
  baseClose: '2246.577649943233620476969923660446'
}

// Daily //
{
  date: '5/11/2022',
  day: 11,
  month: 4,
  year: 2022,
  feeToken0: -3.4440601554207775,
  feeToken1: -0.0015405996636897285,
  feeV: -7.061140100789986,
  feeUnb: -0.1933135552371638,
  fgV: -3.677055720111889e-14,
  feeUSD: -5.640380083495151,
  activeliquidity: 100,
  amountV: 999.9999999999998,
  amountTR: 1000,
  amountVLast: 1032.5671065225727,
  percFee: -0.7061140100789988,
  close: '2241.121068655049392145921171725936',
  baseClose: '2241.121068655049392145921171725936',
  count: 14
}
```

## **uniswapStrategyBacktest() input**

uniswapStrategyBacktest() should be called with the following arguments:

```
uniswapStrategyBacktest(
  poolID,
  investmentAmount,
  minRange,
  maxRange,
  options
)
```

**poolID** = the ID of the pool you'd like to run the backtest for. Example for [ETH / USD 0.05%](https://info.uniswap.org/#/pools/0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640) would be "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640"

**investmentAmount** = the initial amount invested in the LP strategy. This value is presumed to be denominated in the base token of the pair (Token0) but can be overridden to use the quote token with the options argument.

**minRange** = the lower bound of the LP Strategy. As with investmentAmount, presumed to be in base but can be overridden to use quote.  
**maxRange** = the upper bound of the LP Strategy. As with investmentAmount, presumed to be in base but can be overridden to use quote.

**options** = Optional values that override default values. Formed as a JSON key value pair `{days: 30, protocol: 0, priceToken: 0, period: "hourly"}`  
 **days** = number of days to run the backtest from todays date. Defaults to 30, Currently maxed to 30.  
 **startTimestamp** = timestamp in seconds for LP start. Optional.  
 **endTimestamp** = timestamp in seconds for LP end. Optional. If used with _days_ provides results for `n` days before timestamp  
 **priceToken** = 0 = values in baseToken, 1 = values in quoteToken (Token0, Token1)
**period** = Calculate fees "daily" or "hourly", defaults to "hourly"  
 **protocol** - Which chain, sidechain or L2 to use:  
 0 = Ethereum (default)  
 1 = Optimism  
 2 = Arbitrum  
 3 = Polygon

## **uniswapStrategyBacktest() output**

**amountV** = the total value of the LP position for the specified period.  
**feeV** = the fees generated for the specified period.  
**activeliquidity** = the % of the strategies liquidity that was active within the specified period.  
**feeUSD** = the total fees in USD