-
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
cd dex-fronted
npm install
- open local ganache, create a new project by importing the truffle-config.js
- make sure the host is "127.0.0.1" and the port is 7545
- open Metamask on Chrome. Go to Network -> Add Network to add Ganache network.
- change to the root folder and run
truffle migrate --reset
cd dex-fronted
npm run start
- go to localhost:3000
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
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.
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.
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
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.
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.
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).
The DEX is integrated with Metamask and is able to display all the token amounts the user holds.
Select token type to trade
Input Price & Amount of Limit Order
The DEX is able to display a global order book which contains the orders from all users in the system.
The DEX is able to display a user order book which only contains the orders made by the current user
The DEX allows the user to cancel his own order if the order has not been executed yet
.