Giter Club home page Giter Club logo

sol-dex's Introduction

DEX - Dezentralized Exchange

A truly decentralized exchange allowing users to swap ERC20 tokens and MATIC.

DEX

About

Swapping blockchain items like ERC-20 tokens is one of the most popular use cases for decentralized applications on the most popular Smart Contract platforms.

DEXs provide a trustless third-party platform for these kinds of trades. Taking into account the concept of liquidity pools, decentralized exchanges are able to utilize liquidity without the need of a centralized party, while also letting liquidity providers accumulate interest on their deposits over time.

This demo project gives an insight into how the most popular DeFi use case can be implemented. The project provides governance of the exchange through a democratized on-chain voting system, liquidity pools enabling users to earn interest and bullet-proof mathematics providing an automated market maker for fair trade.

User Interfaces

The Webpage provides a way to intuitively and easily use the DEX.

The Tally Webpage provides a way to use the governance features of the DEX.

Contracts

The project consists of five main contracts (DEX, NativeLiquidityPool, TokenLiquidityPool, DexGovernor, DexGovernanceToken) and the ILiquidityPool interface.

Implemented together, these provide a decentralized exchange platform, which is governed decentrally by governance token holders (shareholders), that govern the DEX through the governor contract (DexGovernor).

All of the contracts, except the liquidity pool contracts, are designed to be upgradeable using the Governor.

DEX

This contract stores all liquidity pools of the exchange and lets users swap ERC20 and native currency (ETH, MATIC) using these pools.

Adding and removing liquidity pools can only be done using the governor contract.

Adding a new liquidity pool will result in the deployment of such a pool, while native currency has to be transferred to the DEX and ERC20 tokens have to give allowance to the exchange prior to adding. Without the neccessary funds the exchange will not be able to successfully deploy and initialize the liquidity pool. Adding an already existing liquidity pool can be done by activating it on the DEX. Existing pools have to implement the ILiquidityPool interface.

Users can use the DEX to directly swap at all liquidity pools of the exchange. Swapping through the DEX requires allowance from the user for the exchange to swap on their behalf. This can be enabled through the liquidity pools directly.

The exchange can be access restricted by the governor using the different States of the DEX (OPEN, CLOSED, UPDATING). This can be done for updating the contract, or otherwise wanting to restrict functionality of the exchange.

  • For a detailed documentation of the functionality and code of the DEX contract see DEX Documentation

ILiquidityPool (Interface)

This interface specifies all common features of the liquidity pools available on the DEX.

Liquidity pools using this interface have an Allowance feature, allowing swaps to happen on their behalf through a third party, a kind (NativeLiquidityPool or TokenLiquidityPool) and a standardized swap feature with corresponding events.

NativeLiquidityPool

This contract provides a liquidity pool where users can swap between an ERC20 token and native currency.

Users may provide liquidity to the pool by depositing native currency to the pool. The user has to approve the exchange to withdraw the amount of tokens required prior to providing liquidity. The amount of native currency and tokens is and has to be in ratio to the current pool reserves. That way, the new deposit will not change the current ratio of the reserves, which would lead to a price change in the liquidity pool.

Providing liquidity to the pool will let liquidity providers accumulate rewards in form of swap fees obtained from swaps in the pool.

Users may withdraw their provided liquidity at any time.

TokenLiquidityPool

This contract is a liquidity pool where users can swap between two ERC20 tokens.

Users may provide liquidity to the pool by depositing one the tokens ("X" token, see TokenLiquidityPool Documentation for a more detailed explanation) to the pool. The user has to approve the exchange to withdraw the amount of tokens of the other token required prior to providing liquidity. The amount of the tokens is and has to be in ratio to the current pool reserves. That way, the new deposit will not change the current ratio of the reserves, which would lead to a price change in the liquidity pool.

Providing liquidity to the pool will let liquidity providers accumulate rewards in form of swap fees obtained from swaps in the pool.

Users may withdraw their provided liquidity at any time.

DexGovernor

This contract functions as the governor and owner of the exchange. Governance token holders can propose new proposals and vote on proposals. These proposals may include using access-restricted features of the DEX contract, adding/removing liquidity pools to the exchange or upgrading the DEX, DexGovernanceToken and DexGovernor contracts.

DexGovernanceToken

This contracts implements the ERC-20 governance token for shareholders of the DEX.

Built Using

This project was built using multiple technologies, the most notable ones being below.

Usage

The project can be used through the live implementation on Polygon.

WebpagePolyscanTally

Apart from that, one may clone this repo and use the Hardhat project on its own.

Hardhat

The Hardhat project consists of the Smart Contracts, tests, scripts and overall backend functionality of the project.

Prerequisites

  1. Clone the repo
    git clone https://github.com/keinberger/sol-dex
  2. Make sure you have a node package manager like YARN or NPM installed
  3. Install dependencies
    yarn install
  4. Edit hardat.config.js and helper-hardhat-config.js to fit your needs.

Testing

To test the contracts for functionality run the command below

yarn hardhat test

Deployment

In order to deploy the contracts on a blockchain run

yarn hardhat deploy (optional: network)

The deploy scripts will take care of setting up the contracts, so access-control, upgradeability and other features are set up properly.

Note: The deployer still retains all governance tokens after deployment. In a real implementation of the project, the tokens would need to be distributed among shareholders of the project, to provide true decentralized governance of the marketplace.

Scripts

The repo provides several scripts for interacting with the DEX, liquidity pools or the governor (DAO), which can be found in the hardhat/scripts folder.

The scripts uses the helper-hardhat-config.js for input values. In order to run these scripts, you have to edit the scriptsConfig section and the corresponding governance, dex, fundTimelock, nlp, tlp objects accordingly.

(back to top)

License

Distributed under the GPL-3.0 License. See LICENSE for more information.

(back to top)

Contact

Philipp Keinberger - philipp.keinberger.com - @phil10013 - [email protected]

Project Link: https://github.com/keinberger/sol-dex

Project Webpage: dex.philipp.keinberger.com

(back to top)

sol-dex's People

Contributors

keinberger avatar

Stargazers

 avatar  avatar  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.