Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/saucepoint/median-oracles
Experimental variations for a Median Price Oracle with Uni v4
https://github.com/saucepoint/median-oracles
Last synced: 14 days ago
JSON representation
Experimental variations for a Median Price Oracle with Uni v4
- Host: GitHub
- URL: https://github.com/saucepoint/median-oracles
- Owner: saucepoint
- License: mit
- Created: 2023-07-21T16:16:27.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-07T14:40:20.000Z (about 1 year ago)
- Last Synced: 2024-08-01T18:24:28.487Z (3 months ago)
- Language: Solidity
- Size: 51.8 KB
- Stars: 15
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-uniswap-v4-hooks - Median Price Oracle
- awesome-uniswap-hooks - Median Price Oracle
README
# median-oracles
### **An experimental suite of Median Price Oracles**> *from ETHGlobal Paris 2023, built with Uniswap v4 Hooks π¦*
Why not use TWAP? lower-liquidity pools are prone to manipulation!
---
| Algorithm | Gas (Read) |
|---------------------------|---------------------------------------- |
| Quickselect | comically a lot (hundreds of thousands) |
| Frugal-2U | comically a lot (but less than QS) |
| Running Frugal-2U | 4812 |
| Quickselect Time-weighted | TBD |
| Frugal-2U Time-weighted | TBD |> Methodology: obtain 50 *unique* tick observations by running swaps in both directions. Each swap is spaced 12 seconds apart. Use `gasleft()` before and after reading the median
## Median Price Oracle (Quickselect)
The classic: given a sequence of unordered ticks, fetch the median with the quickselect algorithm
* Uses an O(logn) algorithm on-read
* Depends on tick observations written to storage## Frugal Median Price Oracle
Approximates the median using `Frugal-2U` from a sequence of numbers (naive implementation)
Frugal median algorithm compares new numbers against the current approximation and updates the approximation according to a dynamic *step*
* Uses the frugal median-approximation algorithm
* Depends on tick observations written to storage## Running Frugal Median Price Oracle
The gas-optimized implementation of the frugal median approximation: calculating an on-going approximation of the median
* Uses the frugal median-approxiation algorithm
* Stores the *running* median (approximated) in direct storage
* *additional researchβ’οΈ required for windowed support*### Future work: step-optimization
In the frugal median algorithm, the dynamic *step* can be modified to favor stabilization or responsiveness/accuracy. The repo uses 1-tick as a step, but the implementation is set up for additional experimentation### Future work: time-weighted medians
The repo is in its early stages and did not have sufficient time to implement time-weighted medians. Time-weighted medians are likely to better represent the price since they account for the duration of a tick (price observation). The repo currently treats unique price observations as having equivalent durations.
---
```
src/
βββ RunningFrugalMedianHook.sol Running median approximation
βββ TickObserver.sol store tick observations for windowed median reads
βββ lens
βΒ Β βββ FrugalMedianLens.sol read TickObserver and approximate median
βΒ Β βββ MedianLens.sol read TickObserver and calculate true median
βββ lib
βββ FrugalMedianLibrary.sol median approximation library
βββ MedianLibrary.sol median calculation library (quickselect)
βββ RingBufferLibrary.sol optimized ring buffer for price observationstest/
βββ FrugalMedianLens.t.sol test the naive frugal median
βββ MedianLens.t.sol test the true median (quickselect)
βββ RunningMedian.t.sol test the running median approximation
βββ TickObserver.t.sol test the tick observer
βββ implementation
βΒ Β βββ ... Uniswap overrides for testing
βββ median
βΒ Β βββ FrugalMedianTest.t.sol test frugal median algo
βΒ Β βββ MedianLibrary.t.sol test quickselect algo
βββ utils
βββ HookTest.sol
βββ RingBuffer.t.sol test ring buffer
```---
Additional resources:
[v4-periphery](https://github.com/uniswap/v4-periphery) contains advanced hook implementations that serve as a great reference
[v4-core](https://github.com/uniswap/v4-core)
---
*requires [foundry](https://book.getfoundry.sh)*
```
forge install
forge test
```---
Credits
* [Frugal Streaming for Estimating Quantiles:One (or two)
memory suffices](https://arxiv.org/pdf/1407.1121v1.pdf)* [euler-xyz/median-oracle](https://github.com/euler-xyz/median-oracle) and [median oracle discussions](https://ethresear.ch/t/median-prices-as-alternative-to-twap-an-optimised-proof-of-concept-analysis-and-simulation/12778)
* [Uniswap v3 TWAP Oracles in Proof of Stake](https://blog.uniswap.org/uniswap-v3-oracles)