Git Product home page Git Product logo

bwc_poc's Introduction

Multi-pool constant product automated market maker

This is proof-of-concept written in TypeScript to mimick a smart contract for a constant product automated market maker, with a common liquidity pool, requiring only n number of consolidated pools for any given n number currencies.

It solves the problem of having to establish nC2 discrete liquidity pairs, and hence 2nC2 liquidity pools, for any given n number of currencies.

Swap freely. Currencies can freely swap between each other as long as liquidity pools are present.

Balancing mechanism. In the case of market movement, or liquidity pool movements/imbalances, market arbitrages should re-balance to equilbrium pricing.

Formula


\Large&space;\gamma\Delta{R_{buying}}=\frac{R_{buying}*\Delta{R_{selling}}}{R_{selling}+\Delta{R_{selling}}}

where R are the reserves; and γ = 1 + commission (in %)

*Note: This varies from the constant product formula used by Uniswap, where commission is charged on the currency sold, and pooled together for distribution to all Liquidity Providers (LPs). In this version instead, actor will spend more to purchase an amount of desired currency that includes fees. I.e. Instead of buying 10 USD, one is to buy 10 USD + fees in USD. The fees are distributed to LPs of the purchased currency. This incentivises LPs to provide for pools there are being depleted, as currencies in-demand with depleting (or high turnover) pools will generate higher returns in fees, acting to maintain market equilibrium.


Therefore, re-arranging for ∆Rbuying (amount of buying currency to receive), given ∆Rselling (the target amount to sell):

\Large&space;\gamma\Delta{R_{buying}}=\frac{R_{buying}*\Delta{R_{selling}}}{R_{selling}+\Delta{R_{selling}}}


Or, re-arranging for ∆Rselling (cost in units of selling currency), given ∆Rbuying (target units of currency to acquire):

\Large&space;\Delta{R_{selling}}=\frac{R_{selling}*\gamma\Delta{R_{buying}}}{R_{buying}-\gamma\Delta{R_{buying}}}

Roles and Incentives

Liquidity Providers provide liquidity (staking) in return for commissions, akin to collecting interest with deposits in the bank. Using past 30 day (as at 25 December 2021) data from Uniswap, "interest" accruable by LPs are 8.5% per annum with 0.1% commissions assumed, or 25.5% with 0.3% (Uniswap charges 0.3%).1 This becomes risk-viable if fiat deposits are held in bank/bank-like custodians, and if crypto assets are held in decentralized, immutable, and algorithmically temper-proof vaults.

Arbitragers are natural profit-seeking individuals who will await and capture gains by equalizing differences between conventional forex and cryptocurrencies. Practically, such players typically hold sufficiently large amount of assets in reserves, and likely employ automated algorithms to conduct arbitrages at an efficient and effective scale, much akin existing mechanisms that balances crpyto prices across exchanges.

Users are natural persons or organisations seeking to swap their currencies. If given that liquidity pools are large enough, ongoing arbitraging to maintain price equilibrium, and an cost-efficient blockchain (Solana, Cardano, Polkadot or Ethereum V2), the cost of currency exchange on crypto for trading purposes would likely be lower than conventional forex (0.2-0.5%). Coupled with efficient fiat multicurrency account for money transfer solution (wise, remitly), or usage of funds directly on-chain, cost of money exchange will be much lower than existing remittance services (1-3%).

Try it

yarn  start:dev

Get /

See the balances of the simulated market.

Get swap/:userID/:buy/:amount/:sell

Simulate a user swapping for a currency from the pool, using a currency that he/she owns.

E.g: Dick buys 100 usd using sgd.


/swap/dick/usd/100/sgd

The AMM formula will:

  1. Calculate the exchange rate using the constant product formula, requiring the Dick pays in SGD the equivalent of 100 USD + 0.1 USD (fees of 0.1%).
  2. Adjust Dick's balance by deducting SGD and adding 100 USD.
  3. Adds SGD into the liquidity pool.
  4. Distribute the fees of 0.1USD (paid by Dick) proportionally to all USD liquidity providers.

Footnotes

  1. At the time of this writing (25 Decemeber 2021), Uniswap's 30-day average were 4.3b USD in liquidity, and a daily volume of 1b USD. Assuming one operates this AMM with 0.1% commission (Uniswap charges 0.3%), fees earned by LP = 1 / 4.3 * 0.001 * 365 = 8.49%)

bwc_poc's People

Contributors

calvintwr avatar

Watchers

 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.