Code Monkey home page Code Monkey logo

cz4153_blockchain_decentralized_exchange_application's Introduction

CZ4153_Blockchain

1. Deployment

1.1 Requirements

  • Truffle v5.6.1 (core: 5.6.1)

  • Ganache v7.4.4

  • Solidity - 0.5.16 (solc-js)

  • Node v16.16.0

  • Web3.js v1.7.4

  • npm

1.2 Setup of Frontend

  1. cd dex-fronted
  2. npm install

1.3 Setup of Ganache and Metamask

  1. open local ganache, create a new project by importing the truffle-config.js
  2. make sure the host is "127.0.0.1" and the port is 7545
  3. open Metamask on Chrome. Go to Network -> Add Network to add Ganache network.

Screenshot 2022-11-03 at 11 43 39 AM

4. Set New RPC URL = HTTP://127.0.0.1:7545, Chain ID = 1337 Currency symbol = ETH.

Screenshot 2022-11-03 at 11 45 20 AM

5. Import account[0] of Ganache to Metamask by Accounts -> Import Accounts (with the private key of accounts[0])

Screenshot 2022-11-03 at 11 47 58 AM

accounts[0] will be the issuer of Token A, Token B and Token C.

1.4 Build Project

  1. change to the root folder and run truffle migrate --reset
  2. cd dex-fronted
  3. npm run start
  4. go to localhost:3000

2. Roles

2.1 Swapper - ETH vs BasicToken

To get BasicToken to execute trade in our smart contracts, the user needs to use his ETH and swap BasicToken with the swapper (1 ETH = 1 BasicToken). The user can withdraw their BasicToken and get back corresponding amount of ETH

2.2 Issuer

The Ganache accounts[0] will be the issuer of Token A, B and C in the Ganache network, with a total supply of 1,000,000 each. The issuer will sell these tokens to the market at a reasonable price.

2.3 Users

The users can be other accounts in the Ganache network. The user can use their BasicToken to buy Token A, B or C from the issuer or other users. The users can sell thier token at hand to other users subsequently.

3. Order book

In this project, we are using a central limit order book (CLOB). It matches customer orders (e.g. bids and offers) on a 'price time priority' basis.

  • A token list will be used to store all supported tokens. Each token will have a buy order book and sell order book
  • The sell order book stores prices in ascending order, i.e. first_price = lowest price and last_price = highest price
  • The buy order book stores prices in descending order, i.e. first_price = highest price and last_price = lowest price
  • At each price, the orders are stored using a self-defined double linked list for easy insertion and deletion.
  • When an order is matched and fully executed, the order pointer will point to the next order at this price (inserted after current order)
  • When orders at certain price runs out, the price pointer will point to the next price
  • Orders at the same price are stored on a First-In-First-Out basis. Therefore, the priority is based on order price and order age Flowchart

4. Supported Trades

4.1 Market Order (Buy and Sell)

A market order is an instruction by a user to a broker to buy or sell tokens at the best available price in the current financial market. The user will only input amount of token he wants, and the order will be executed as long as there is enough amount of orders. Flowchart (10)

4.2 Limit Order (Buy and Sell)

The order is placed "at the limit": Limit orders set the maximum or minimum price at which you are willing to buy or sell. The user will input the price and amount of the order. For buy limit order, it will only be executed when the sell order's price is lower than the input price. For sell limit order, it will only be executed when the buy order's price is higher than the input price. Flowchart (7)

4.3 Order Cancellation

At a given price, each offer stored has a unique priority. Therefore, given price and priority, a unique offer can be removed. On the webpage, users can see all the buy/sell offers they made and choose to remove any one of the unfulfilled/partially fulfilled offers. The lower_priority pointer of previous offer will point to the next offer while the higher_priority pointer of the next offer will point to the previous offer (linked list element removal process). Flowchart (3)

5. DEX Features

5.1 Integration with Metamask & User Profile

The DEX is integrated with Metamask and is able to display all the token amounts the user holds. user profile

5.2 Basic Token & ETH Swap

Screenshot 2022-11-04 at 11 37 03 AM

5.3 Basic Token Withdraw

Screenshot 2022-11-04 at 11 38 03 AM

5.4 Trades

5.4.1 Limit Order (Buy/Sell)

Token Selection Select token type to trade Input Trade Input Price & Amount of Limit Order

5.4.2 Market Order (Buy/Sell)

market order

5.5 Display of Global Order Book

The DEX is able to display a global order book which contains the orders from all users in the system. Global order book

5.6 Display of User Order Book

The DEX is able to display a user order book which only contains the orders made by the current user user order book

5.7 Cancellation of Trade

The DEX allows the user to cancel his own order if the order has not been executed yet user order book

.

cz4153_blockchain_decentralized_exchange_application's People

Contributors

geezzzyyy avatar louis-feng avatar agnesye1029 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.