Skip to content

Instantly share code, notes, and snippets.

@alexroan
Last active August 9, 2022 13:51
Show Gist options
  • Save alexroan/71b38d387ed2a86bf3abdf3acd0f8415 to your computer and use it in GitHub Desktop.
Save alexroan/71b38d387ed2a86bf3abdf3acd0f8415 to your computer and use it in GitHub Desktop.
Uniswap V3 Oracle Gas Tests - Observation Cardinality

Uniswap V3 Oracle Gas Tests - Observation Cardinality

Results

┌─────────┬──────────┬─────────────────────────────────┬────────────┬────────────┬────────────────────────┬─────────┐
│ (index) │   kind   │              name               │ minGasUsed │ maxGasUsed │ observationCardinality │ avgGas  │
├─────────┼──────────┼─────────────────────────────────┼────────────┼────────────┼────────────────────────┼─────────┤
│    0    │ 'uni-v3' │     'AAVE/ETH (Uniswap V3)'     │   65550    │   65550    │           50           │  65550  │
│    1    │ 'uni-v3' │     'APE/ETH (Uniswap V3)'      │   65142    │   65158    │           50           │ 65148.4 │
│    2    │ 'uni-v3' │     'COMP/ETH (Uniswap V3)'     │   38388    │   38388    │           50           │  38388  │
│    3    │ 'uni-v3' │     'DAI/ETH (Uniswap V3)'      │   77708    │   77708    │          300           │  77708  │
│    4    │ 'uni-v3' │ 'ETH/USDC (Uniswap V3, 0.05%)'  │   82791    │   87619    │          720           │  85561  │
│    5    │ 'uni-v3' │  'ETH/USDC (Uniswap V3, 0.3%)'  │   91182    │   92684    │          1440          │ 91514.6 │
│    6    │ 'uni-v3' │ 'FRAX/USDC (Uniswap V3, 0.05%)' │   38474    │   38474    │          300           │  38474  │
│    7    │ 'uni-v3' │     'GNO/ETH (Uniswap V3)'      │   68571    │   68587    │           64           │ 68583.8 │
│    8    │ 'uni-v3' │     'LINK/ETH (Uniswap V3)'     │   67303    │   67303    │          144           │  67303  │
│    9    │ 'uni-v3' │     'MKR/ETH (Uniswap V3)'      │   77166    │   77166    │          144           │  77166  │
│   10    │ 'uni-v3' │   'OHM/ETH (Uniswap V3, 1%)'    │   38486    │   38486    │           36           │  38486  │
│   11    │ 'uni-v3' │  'OHM/USDC (Uniswap V3, 0.3%)'  │   38402    │   38402    │           1            │  38402  │
│   12    │ 'uni-v3' │     'SHIB/ETH (Uniswap V3)'     │   38486    │   38486    │          150           │  38486  │
│   13    │ 'uni-v3' │ 'stETH/ETH (Uniswap V3, 0.3%)'  │   38422    │   38422    │           1            │  38422  │
│   14    │ 'uni-v3' │    'SUSHI/ETH (Uniswap V3)'     │   38416    │   38416    │           2            │  38416  │
│   15    │ 'uni-v3' │     'UNI/ETH (Uniswap V3)'      │   79236    │   79236    │          300           │  79236  │
│   16    │ 'uni-v3' │     'WBTC/ETH (Uniswap V3)'     │   72739    │   72739    │          200           │  72739  │
│   17    │ 'uni-v3' │    'WBTCv2/ETH (Uniswap V3)'    │   72725    │   72725    │          200           │  72725  │
│   18    │ 'uni-v3' │     'YFI/ETH (Uniswap V3)'      │   38472    │   38472    │           30           │  38472  │
└─────────┴──────────┴─────────────────────────────────┴────────────┴────────────┴────────────────────────┴─────────┘
Gas used range: [38388, 92684]

Graph 1 - Full data set

univ3-1

There is a base gas cost of around 38400~ that is exhibited by pools despite their cardinality being higher (the line of data points along the bottom of the field of data, almost perfectly horizontal). This is likely due to the binary search reaching its target very early in the search, which is more likely with smaller cardinalities (fewer values to search through). With this interpretation, these values represent something more like best-case, rather than worst-case scenarios.

Graph 2 - Calculating logarithmic formula

We can assume that the lower bound results are better than the worst-case scenarios for those observation cardinalities. Removing these, we get the following graph, which shows the logarithmic trend and equation:

univ3-2

Logarithmic formula

The maximum observation cardinality possible on a UniV3 pool is restricted to uin16, which has a max value of 65536.

Using 33013 + 8019 ln N where N is the observation cardinality, the max gas cost of observing a pool which has it's observation cardinality set to the maximum, is calculated:

33013 + 8019 ln 65536 = ~121_946

Given that this is a limited data set, we should add a buffer to this value to be safe: 140,000.

Applying values to UAV Updates

Updating the value of ETH in the UAV via validate only calls observe on a single Uniswap v3 Liquidity pool for its anchor (ETH / USDC).

However, updating any other configured asset value via validate in the UAV, for example, LINK, calls out to two pools:

  • ETH / USDC - to get the anchor price of ETH in USD
  • LINK / ETH

The anchor price of LINK denominated in USD is calculated using the results of these two calls (this is because Compound denominates in USD, not ETH, whereas Uniswap pools are predominantly ETH pairs).

So, Most updates will call out to two pairs. Taking our max value, if both pairs have a maxed out observation cardinality, then: 140,000 x 2 = 280,000

280,000 purely for reading from two Uniswap v3 pools. This value does not include any gas costs associated with computation or storage touches inside the UAV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment