GithubHelp home page GithubHelp logo

median-oracles's Introduction

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 observations

test/
β”œβ”€β”€ 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 contains advanced hook implementations that serve as a great reference

v4-core


requires foundry

forge install
forge test

Credits

median-oracles's People

Contributors

saucepoint avatar

Stargazers

Camotelli avatar zetsukhun avatar INSOMNIAK avatar Mr. Kaseta avatar jacopo avatar  avatar  avatar Etch avatar zerosnacks avatar sudo whoami avatar Waylon Jepsen avatar indigo avatar  avatar Suthan Somadeva avatar Kell (K42) avatar

Watchers

Camotelli avatar  avatar  avatar

Forkers

1999321

median-oracles's Issues

Invite you to come to Artela hackathon!

Hi there,

I’m Mike, head of DevRel of Artela. I’m reaching out to invite you to participate in our hackathon and become our early contributor.

We are building an L1 blockchain called Artela and successfully raised a $6M seed round in July 2023.

I came across your Hook contribution in the Uniswap v4 hooks collection, and we believe Artela shares the same vision of Hook design, which is to improve the modularity of on-chain programming and give autonomy back to developers. Artela is a blockchain network allowing developers to utilize WasmVM to build native extensions to coprocess with EVM. While Uni v4 hooks are application-specific, Artela supports native-level hook design, which is more cost-effective, computationally powerful, and provides more access to the runtime context. So that Artela can truly unlock more functionalities and in-depth customization for dApps.

We value your contribution to UNi Hooks and sincerely look forward to making you part of Artela’s developer community. Since your experience and work can be easily transformed into Artela.

By participating in our hackathon, you have a good chance of winning a prize and potentially receiving incubation support from a strong L1 foundation. If your contributed idea is included in Artela’s Dictionary, you will be able to participate in the prize pool and receive a contributor tag, which would be highly relevant to future airdrops. If you successfully deploy it on Artela or invite other developers who also make contributions, you will also earn bonus points for additional airdrop.

The hackathon will kick off on January 21st and run for 10 days, you will also be welcome if you can transfer your Uniswap Hook design to Artela.

Please feel free to contact us in Telegram:

Mike, DevRel: @QiyuanMa

Kevin, cofounder: @creolophus123

Developer portal: https://artela.network/build/developer-portal

For an overview of the hackathon and an introduction to Artela, you can find more information here:

[Artela Aspect Usecase BUIDLathon Overview](https://www.notion.so/Artela-Aspect-Usecase-BUIDLathon-Overview-fbd78f8e502640829d2dd0e54fca355c?pvs=21)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.