Code Monkey home page Code Monkey logo

twamm-contracts's Introduction

Pulsar TWAMM

Introduction

built-with openzeppelin

Time-Weighted Average Market Maker (TWAMM) is a new on-chain market making model, designed by @_Dave__White_, @danrobinson and @haydenzadams. TWAMM allows market participants to efficiently execute large orders over multiple blocks on Ethereum.

Pulsar TWAMM is the first implementation of TWAMM. The math involved in TWAMM can be found in this article: https://hackmd.io/@luffy/SJxSsOH1Y

Implementation Notes

Overview

TWAMM.sol directly implements most of the standard AMM functionality (liquidity provision, liquidity removal, and swapping). The logic for execution of long-term orders is split across two libraries, OrderPool.sol, LongTermOrders.sol and BinarySearchTree.sol.

Order Pool

The main abstraction for implementing long-term orders is the OrderPool. The order pool represents a set of long-term orders, which sell a given token to the embedded AMM at a constant rate. The token pool also handles the logic for the distribution of sales proceeds to the owners of the long-term orders.

The distribution of proceeds is done through a modified version of algorithm from Scalable Reward Distribution on the Ethereum Blockchain. Since order expiries are decoupled from proceeds distribution in the TWAMM model, the modified algorithm needs to keep track of additional parameters to compute proceeds correctly.

Long Term Orders

In addition to the order pools, the LongTermOrders struct keep the state of the virtual order execution. Most importantly, it keep track of the last block where virtual orders were executed. Before every interaction with the embedded AMM, the state of virtual order execution is brought forward to the present block. We can do this efficiently because only certain blocks are eligible for virtual order expiry. Thus, we can advance the state by a full block interval in a single computation. Crucially, advancing the state of long-term order execution is linear only in the number of block intervals since the last interaction with TWAMM, not linear in the number of orders.

Binary Search Tree

BinarySearchTree (BST) is a data structure that is used to store and organize data in a hierarchical manner. In Pulsar's particular BST, the nodes are long-term order expiration block heights. The tree allows for nodes to be inserted and deleted, and when execute virtual orders, it will retrieve a list of expirations from the lastVirtualOrderBlock up until the current time, as well as a list of expirations for the next week.

Fixed Point Math

This implementation uses the PRBMath Library for fixed point arithmetic, in order to implement the closed form solution to settling long-term trades. Efforts were made to make the computation numerically stable, but there's remaining work to be done here in order to ensure that the computation is correct for the full set of expected inputs.

How to run

# Install dependencies
npm install

# Compile contracts with hardhat
npx hardhat compile

# Test contracts with hardhat
npx hardhat test

twamm-contracts's People

Contributors

showenpeng avatar skyve2012 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.