DEX Whitepaper ENGLISH - v2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Order Books Multi-Blockchain Decentralized Exchange 1

Table of Contents
Abstract 3

About SwapreX 3

Decentralized Exchanges 4

Onchain Orderbook 5

xDai Chain 6

Hold and Earn 7

Always Filled Orders 7

Market and Limit Orders 7

DEX aggregator 8

Multi-blockchain Order Books DEX 9

Architecture 9

Design 10

Colors scheme 10

Home Page 11

Market Buy 17

Limit Buy 19

Market Sell 22

Limit Sell 24

Roadmap 31

Future 31

Order Books Multi-Blockchain Decentralized Exchange 2


Abstract
In January 2009, Satoshi Nakamoto mined the first block of a peer-to-peer
digital currency: Bitcoin. Bitcoin was a Blockchain pioneer and started a
technology revolution that connects people and businesses, offering new tools
for decentralized financial systems, empowering users with a new economic
universe.
In 2013 Vitalik Buterin introduced the Ethereum whitepaper. Later, in 2015, a
team led by Vitalik Buterin, Gavin Wood, released Ethereum as a general-
purpose Blockchain with a Turing-complete programming language. Since
then, the expanding universe of Smart Contracts has been lead by Ethereum.
In March 2020, the number of deployed contracts reached an all-time high of
1,971,632 new smart contracts in a month, with an average of more than a
million Smart Contracts per month in the last year.
The real-world use cases of Ethereum range from collectibles and games to
finance, law, healthcare, and sciences, just to mention a few.

About SwapreX
We Bring together Investors & Developers

SwapreX is a user-friendly and decentralised platform to conduct crowdfunding


campaigns, donations and payments.

Mutichain Support and Erc-20 Token

Using SwapreX platform anyone can easily create ERC-20 token in few Clicks,
no coding skill required. Users can create Token easily using Metamask for a
small fee.

SwapreX platform supports ERC-20, but soon we will add support to all popular
Chain like Binance Chain, Tronchain, etc, Which means you can raise fund
using this chain tokens as well.

The global crowdfunding market size was $84 billion as of 2018 and is
expected to reach $114 billion by 2021.

We will become the First DEFI Platform who will help projects in raising funds
without any 3rd party involvement.

The ultimate goal of Swaprex is to help developers with fund raising

After Uniswap popularity and acceptance there is also a need of decentralised


platform where investors can easily invest in any project and help [projects. So

Order Books Multi-Blockchain Decentralized Exchange 3


we decided to fill this gap with Swaprex.

SwapreX features

• A DeFi project to allow anyone to create an ICO to sell new tokens and
collect funds.
• The site will have the list of all running ICOs at the time.
• Contributors will be able to contribute using the website interacting with
a wallet or sending contributions directly to the ICO Ethereum address.
• Contributions can be sent in Ether or in any ERC-20 token, like DAI,
Wrapped BTC, USDC, etc.
• ICO Pool: SwapreX system allow investors to evaluate different ICO at
once with a community drive star ratings system, which give reviews
from investors to investors. Also the ICOs are categorized for easy
navigation.
• Payments: this option let content creators and business receive
donations and payments with any ERC-20 token or Ether. This is an
easily, fast, and secure way to get collect money.
• Token creation: All users can create ERC-20 Standard Tokens with the
platform, without the need of coding, no tech-skills required.

Decentralized Exchanges
Decentralized exchanges were proposed by Vitalik Buterin 4 years ago. The
price movement in traditional exchanges happens when the seller price meets
the buyer price in an order book; buying pressure pushes the price high, and
vice versa.

In this project we are aiming to build a decentralized exchange based on Order


Books.

Order books are all about trading, like in stocks or cryptocurrencies. The
market change based on orders:

● Market orders: is an instruction to buy or sell immediately at the


market’s current price.
● Limit order: is an instruction to buy or sell at a fixed price. The user has
to wait until the price hits the fixed price before the order is executed.

Order Books Multi-Blockchain Decentralized Exchange 4


Users that place orders are charged by a small percentage or fee in the
destination coin.

This tension between buy and sell order are the ones that determine the
market’s price.

Onchain order book


Decentralized exchanges (DEX) solved the problem of intermediaries and
helped traders to exchange cryptocurrencies quickly and with minimal fees.
However, problems such as liquidity persisted, and this led to a DEX model
called Automated Market Making (AMM).
Automated Market Maker (AMM)
Many decentralized exchanges are still struggling with liquidity, Some
cryptocurrency pairs are harder to trade than others. Cryptocurrency owners
can invest their cryptocurrencies to provide liquidity in a pool of liquidity. In
other words, they become a kind of market maker. In exchange for providing
liquidity, they will earn money from exchange fees through AMM.
An AMM uses a mathematical formula that takes into account the current
liquidity of a trading pair and offers an instant quote to traders. In other words,
instead of querying an order book for a price, you will get it as a result of an
algorithm.
Currently, the most popular AMM exchange protocol is Uniswap.
Order-book
DEXs based on order books use the traditional trading model, which has been
around long before the rise of DeFi.
Traders set buy and sell orders for an asset, and the order book organizes
them based on their prices. This means that you can trade any asset as long
as there is supply and demand for it.
The problem with AMM
Liquidity pools are currently not that large in DEX AMMs, especially for less
traded assets, which results in a large shift when a large order is executed.
If an order occupies half of the liquidity pool, the huge slippage doubles the
symbolic price. Such a huge slip would never have been acceptable during
book exchanges.
Basically, AMM markets act as misinformed entities because the price is

Order Books Multi-Blockchain Decentralized Exchange 5


determined by a pricing algorithm rather than an order made by a human
trader.
Onchain order book are a perfect way to trade as slippage will remain low
even when trading volume is high.
At present Swaprex is the only DEX who is providing onchain order book

xDAI chain
Ethereum gas fees has been rocketing since the price of Ethereum and the
hype about cryptocurrencies has increased.

Layer 2 is a solution to scalability and more reasonable fees. Layer 2 is a


collective term for solutions designed to help scale your application by handling
transactions off the main Ethereum chain (layer 1). Transaction speed suffers
when the network is busy which can make the user experience poor for certain
types of dapps. And as the network gets busier, gas prices increase as
transaction senders aim to outbid each other. This can make using Ethereum
very expensive. 1

One of these L2 solutions that has been growing fast is xDAI chain.

The xDai chain is an EVM sidechain that uses a different consensus model
(POSDAO) than Ethereum. The underlying architecture, however, mirrors
Ethereum 1.0, meaning that smart contracts can be written and deployed in
exactly the same way using a different RPC endpoint.

Any contract that works on the Ethereum mainnet can be easily redeployed to
the xDai chain. On the xDai chain, transaction costs are minimized, and all fees
and transactions are paid with a single token, xDai. 2

● Reasons to Use the xDai Stable Chain:


● Fast transaction times (5 seconds) & low transaction fees (500 tx for
$.01).
● Digital cash. A stable chain is ideal for real world value exchange where
1 xDai = 1 US Dollar.
● A single token for transactions & gas fees.
● On-chain, decentralized Random Number Generator.
● Permissionless delegated Proof-Of-Stake based consensus with public
POSDAO.
● STAKE token allows community consensus participation and incentives.
● Wide-ranging Community Support (see xDai Validator Organizations).
● Outstanding energy efficiency.
● Extreme usability with tools like Burner Wallet & Burner Wallet 2.
1 https://ethereum.org/en/developers/docs/layer-2-scaling/
2 https://www.xdaichain.com/

Order Books Multi-Blockchain Decentralized Exchange 6


● Growing ecosystem designed to support stable person-to-person
transactions, micro transactions, conference currencies, community
currencies and more.
● Smart Contract, DApp & toolset compatibility with other Ethereum-
based chains like Ethereum, Ethereum Classic and others.
● Experiencing the magic of crypto for the first time!

Hold and Earn


One of the problems with current DeFi projects is the need for confidence in an
entity when interacting with complex contracts.
Also many contracts in the industry are prone to error and easy to hack.
Interaction with these contracts often results in the breakdown of new signals,
which require confusing (and often centralized) economic mechanisms that try
to give some value to the underlying reward signal.
Developers are usually unfamiliar with the economics of reward mechanisms.
This poses a significant risk to individuals who choose to interact with DeFi
smart contracts.
Hold and Earn
SwapreX token will be done in such a way that it will give user some share
from exchange income. The Exchange in a decentralized way, automatically
re-distributes fees to token holders instantly at the time of each transaction.
There is no central team or party that should order taxes. There is no interface
for requesting commissions. There is no need for the user to take any action
other than storing the tokens in the wallet and connect to our smart contract as
a holder signal.

Always Filled Orders


Orders in our protocol always will be filled following the next mechanisms:

Market and Limit Orders


When you are ready to trade, you have two important ways to determine the
price you will be trading: the market order and the order limit. With market
order, you buy assets for any cost. Limit orders, you can quote a price, and if
the price match, it is usually carried out.

Order Books Multi-Blockchain Decentralized Exchange 7


The most important difference between the order market and the limit order,
but each type can be more suitable for a given trading position.
Market orders: Trade now
This is the fastest market strategy that can be implemented quickly, because
you are asking the exchange to take the best price available at the moment. If
you are buying a currency, the market order will be executed at whatever price
the buyers are selling.
The biggest drawback of the order market is your inability to specify the
transaction price. Often it does not matter for large liquid markets.
However, if the price moves too quickly, the transaction may end up at a price
that is very different from the time you entered the order. That rarely happens
but it can.
Limit Orders: Make trades when the prices is right
The best about limit order is that you can label your price, and if the coin
reaches the price, the order will be filled out. Other times the entrepreneur fills
your order at a higher price. Generally, you can set limited orders to be
delivered up to three months after you sign up, which means you don’t have to
watch hard to get your price.
The biggest drawback: You are not sure to do the trade. If the share does not
meet the price limit, the transaction will not be executed. Even if your trade is
limited, there may not be enough demand or supply to fill the order.
For that reason we are including other ways to fill your orders in a
decentralized fashion.

DEX aggregator
Swaprex DEX will have DEX aggregator features, that works out the best
crypto prices across decentralized exchanges.

The Smart Contract automatically willl be monitoring other DEX markets like:
Uniswap, Balancer, Bancor, Mooniswap, etc. Which means that the Smart
Contract will search a handful of decentralized exchanges for the cheapest
prices and reroutes its customers’ trades between them to try and ensure that
they're getting the best prices.

In conclusion, you make your orders and they will be automagically filled in a
secure way while you sleep using the best method adapted to your order, and
the market conditions.

Order Books Multi-Blockchain Decentralized Exchange 8


Multi-blockchain Order Books
DEX
The current project aims to give users the power of an Order Books
Decentralized Exchanges in Ethereum and xDAI

Architecture

Order Books Multi-Blockchain Decentralized Exchange 9


Design

Colors scheme

Order Books Multi-Blockchain Decentralized Exchange 10


Home Page
Top Nav

Connect to Metamask
● Logo: link a dex.swaprex.com
● Connect to Metamask:

○ If the selected network in the system is Ethereum:
■ If you are logged into Metamask:
● If you are with the Ethereum mainnet network:
○ Show the user's account:
■ 0x1234 ... 7890
● If you are not with the Ethereum mainnet network
○ Show message:
■ Please switch to Ethereum
mainnet
■ If you are not logged in to Metamask:
● Show the link: Connect to Metamask
○ If the network selected in the system is xDai:
■ If you are logged in to Metamask:
● If you are connected to xDai
○ Show the user's account:
■ 0x1234 ... 7890
● If not connected to xDai
○ Show message:
■ Please switch to xDai
○ Show link:
■ Click here to learn how to connect
to xDai
■ (link to be defined)
■ If you are not logged into Metamask:
● Show link: Connect to Metamask

Network
● Ethereum: if Ethereum is selected, the Ethereum smart contract and the
Ethereum network will be used to interact with the system
● xDai: if xDai is selected, the xDai smart contract and the network of
xDai to interact with the system

Order Books Multi-Blockchain Decentralized Exchange 11


Calls to functions in both networks are the same, except for the Deposits and
Withdrawals option.

Top menu
● The home page is the “Trading” option section, underline the selected
section in red
● Analytics is another page to build, different from the home page
● Signup and Login will raise a modal window to register and log into the
API. This user will be used to mark the currencies as favorites,

Sub nav

Market
Shows the selected market (currency pairs / tokens).

The pair:

WBTC / USDT: it means how many USDT should I spend to buy a WBTC
WETH / DAI: how many DAI should I spend to buy a WETH

When the market changes, the entire interface is changed to show the charts,
operations, etc. of that coin.

Example: if WBTC / DAI is selected, the market and WBTC operations against
DAI are displayed, when changing to WETH / USDT everything changes to that
pair.

The information of the available pairs comes from the system API.

The administrator user can add or delete new currency pairs (tokens ERC20)

API call:
/ pairs / user_id

Returns all pairs with your basic data if you miss the id of the user returns all
your favorite pairs:

{
"WBTC ": {
" Contract ":" 0x123 ... ",
" name ":" Wrapped Bitcoin ",
" symbol ":" WBTC ",

Order Books Multi-Blockchain Decentralized Exchange 12


" decimals ": 8,
" pairs ": [
{
" contract ":" 0x345. .. ",
" name ":" Tether USD ",
" symbol ":" USDT ",
" decimals ": 6,
" favorite ": false,
" tradingview_code ":" wbtc / usdt "
},
{
" contract ":" 0x234 ... ",
" symbol ":" WETH ",
" decimals ": 18,
" favorite ": true,
" tradingview_code ":" wbtc / weth "
}
]
},
" WETH ": {
" contract ":" 0x123 ... ",
" name ":" Wrapped Ether ",
" symbol ":" WETH ",
" decimals ": 18,
" pairs ": [
{
" contract ":" 0x678 ... ",
" name ":" DAI ",
" symbol ":" DAI ",
" decimals ": 10,
" favorite ": true,
" tradingview_code ":" weth / dai "
}
]
}
}

call Contract:
priceToken (address token_address, address [] country)

Returns the prices of that token at the moment, for example the prices of
WBTC:

[
33000000000 , <---- 33000.000000 USDT to buy 1 WBTC
330106408568574, <---- 33010.640856 DAI to buy 1 WBTC
23573380013301064085, <---- 23.57 WETH to buy 1 WBTC
]

Order Books Multi-Blockchain Decentralized Exchange 13


Only favorites
If the user is logged in, he can mark some pairs of coins as favorites.

In this case, when selecting this checkbox, only the currencies selected by the
user will be shown in the dropdown.

If the user is not logged in, clicking on "Only favorites" should raise the "Login"
window.

If the user is logged in but does not have currencies selected as favorites,
show the message: "You have not selected any Favorite coin yet" and leave
the checkbox unmarked.

Balance
The dropdown will show the entire list of currencies in which the user has funds
greater than zero.

The user can select any currency regardless of the pair selected in Market.

Call to Contract:
balances (from: user_account, address [] tokens)

Returns:
[
100000000000000, <--- 10000 XDAI
345674637, <--- 3.45674637 WBTC
]

The interface must show the balance considering the decimal places.

Example:

To:

“balance”: 345674637,
“decimals”: 8

It will be shown as:

3.467464 WBTC

Order Books Multi-Blockchain Decentralized Exchange 14


Deposit
The idea is to create a popup, similar in design to Login, with the following
options:

● Dropdown of coins available in the system


● Specify the amount to deposit
● Deposit Button

In Ethereum this is the same as what was done in Donations, it is to call the
transfer function in the ERC20 contract to transfer those tokens to the DEX
contract.

In xDai it is more complex, it is yet to be defined.

Withdraw
This option allows the user to withdraw their funds.

The idea is to create a popup, similar in design to Login, which has the
following options:

● Dropdown of coins where you have funds


● Specify the amount to withdraw
● Withdraw button

If the user wants to withdraw 1 WBTC and the WBTC has 8 decimal places, he
must go to the function the amount as:

Amount: 100000000

This should call in the contract the method:

withdraw (token, amount, {from: user_account})

In the contract this method should fail if:

● The user (msg.sender) does not have enough funds in that token

● The user cannot withdraw funds if they are pending on sell orders.

Therefore, if the user (msg.sender) has enough funds, but the funds
minus the open sell orders of that token is less than the amount to be
withdrawn, it must fail.

Order Books Multi-Blockchain Decentralized Exchange 15


Otherwise, the method must transfer the funds to the user

Security notes:

● For the user's address it must always be used: msg.sender do not use
any parameter
● This method must be nonReentrant
.

24 hr Stats
Statistics of the last 24 hours:

● Last price: shows the last price of the pair


● Change: shows the percentage change of the pair
● High: maximum price
● Low: lowest price
● Volume: total amount spent on the token to pay

Call to contract / api?


stats24hr (token_to_buy, token_to_pay)

Example:
stats24hr (“0x123 ...”, “0x345 ...”)
^ WBTC ^ USDT

Returns:

{
“last_price”: 33000000000,
^ - If the USDT has 6 decimal places the last price is 33000
USDT
“yesterday_price”: 29098536758,
^ - This is used to calculate the change in the last 24 hr
Formula:

(1 - yesterday / today) * 100

Yesterday price Last price Change


270000000000000 300000000000000 10.0%
350000000000000 300000000000000 -16.7%

“high”: 34869068445,
^ - Yes USDT has 6 decimals the maximum is 34869 USDTs

Order Books Multi-Blockchain Decentralized Exchange 16


“low”: 28869068445,
^ - If the DAI has 10 decimal places the minimum is 28869
USDTs
“volume”: 12328869068445,
^ - If the USDT has 10 decimal places the volume is
12328869 USDTs

Orders
Allows users with funds in their wallets to buy or sell
tokens.

Market Buy
It is a purchase at the market price. If the pair is WBTC /
USDT it means that the user wants to buy WBTC paying
with USDT.

Amount
It is the total that you want to buy.

The percentage are buttons for the system to automatically


calculate the amount to buy according to the amount of
funds.

For example, if the buyer wants to buy WBTC, which has a


market price of 33,000 USDT and the user has 99,000 USDT in his portfolio,
then by pressing 100% the total will appear in 3 WBTC, if he presses 50%, 1.5
WBTC will appear, etc.

The formula for the percentages is:

100%: user_balance / market_price


50%: user_balance / market_price / 2
25%: user_balance / market_price / 4

The current market price is obtained with:

userBalance (user_account, token) => 99000000000 -> 99000 USDT

stats24hr (token_to_buy, token_to_pay) taking the field last_price =


market_price

So if the user has 99000000000 USDT (assuming 6 decimal places in USDT)

Order Books Multi-Blockchain Decentralized Exchange 17


AND the last_price field for the WBTC / USDT pair is: 33000000000

Assuming that WBTC is 8 decimal places, the amount Maximum WBTC to buy
is:
int (99000000000/33000000000 * 10 ** 8) = 300000000 = 3 WBTC
100% is 3 WBTC

Fee, rewards, total


These fields are not editable.

Both the fee, the swaprex profit and the estimated total are obtained with the
contract function:

getQuoteMarketBuy (token_to_buy, amount_token_to_buy, token_to_pay)

Example:

getQuoteMarketBuy (“0x123 ..”, 300000000, “0x456 ...”)


^ WBTC, ^ USDT
^ amount in WBTC: 3 WBTC
Returns:

{
"fee": 990000, <--- 0.0099 WBTC (8 decimal places)
"rewards": 9900000000000000000000, <--- 9900 SwapreX (18 decimal
places)
"total": 99000000000, <--- 99000 USDT (6 decimal places)
}

Ideally all the values in the box should be refreshed every so often. (every
minute maybe?)

Buy button
The function is called:

marketBuy (token_to_buy, amount_token_to_buy, token_to_pay)

If there are several sell orders of this pair, the sell orders with the lowest price
that match the buy order will be executed first .

If the buyer wants to buy at market price, the lowest possible price orders are
executed.

Order Books Multi-Blockchain Decentralized Exchange 18


Example if there are three WBTC sell orders:

● The first selling 2 WBTC at 32990 USDT / WBTC


● The second selling 2 WBTC at 32980 USDT / WBTC
● The third selling 1 WBTC at 33000 USDT / WBTC

Then 2 WBTC are sold to the buyer at 32980 and 1 WBTC to 32900, on
average the buyer pays 32987 USDT per WBTC.

Order Book:
User Sell amount Sell price Buy amount

A 2 WBTC 32980 USDT

B 2 WBTC 32990 USDT

C 1 WBTC 33000 USDT

D 3 WBTC

User D buys 3 WBTC from A and B in total. After this the Order Book remains:

User Sell amount Sell price Buy amount

B 1 WBTC 32990 USDT

C 1 WBTC 33000 USDT

Limit Buy
Limit Buy is the purchase at a price set by the user.

Price
It is the limit price that the buyer is willing to pay for the
currency to buy.

This field is editable by the user.

Amount
It is the total that you want to buy. The percentage are
buttons for the system to automatically calculate the amount
to buy according to the amount of funds.

Order Books Multi-Blockchain Decentralized Exchange 19


This field is editable by the user.

For example, if the user wants to buy WBTC, maximum at 32985 USDT and
the user has 98955 USDT in his portfolio then when pressing 100% the total
will appear in 3 WBTC, if he presses 50% it will appear 1.5 WBTC, etc.

The formula for the percentages is:

100%: user_balance / price


50%: user_balance / price / 2
25%: user_balance / price / 4

userBalance (user_account, token) => 98955000000 -> 98955 USDT

In this case the price is not needed from the market, since the user is setting
the price at which he wants to buy.

So if the user has 98955000000 USDT (98955 USDT)

AND the user wants to buy at 32985 USDT, we transform the price given by
the user to 6 decimal places: 32985000000

Assuming that WBTC is 8 decimal places, the maximum amount of WBTC to


buy is:

int (98955000000/32985000000 * 10 ** 8) = 300000000 (3 WBTC)

100% is: 3 WBTC

Fee, rewards, total


These fields are not editable.

Both the fee, the swaprex profit and the estimated total are obtained with the
contract function:

getQuoteLimitBuy (token_to_buy, amount_token_to_buy, token_to_pay,


price_to_pay)

Example:

getQuoteMarketBuyLimit ("0x123 ..", 30000000000 ..., "0x985006 ..",


30000000000 ... )

Order Books Multi-Blockchain Decentralized Exchange 20


^ WBTC, ^ USDT
^ amount in WBTC: 3 WBTC
^ amount in USDT:
32985
Returns:
{
“fee”: 989550, <--- 0.0099 WBTC (8 decimal places)
“rewards”: 9896000000000000000000, <--- 9896 SwapreX (18 decimal
places)
“Total”: 98955000000, <--- 98955 USDT (6 decimals)
}

Buy button
The function is called:

limitBuy (token_to_buy, amount_token_to_buy, token_to_pay, price_to_pay)

This order is executed when there are sell orders of the same minor pair or
equal to the purchase price. In other words, the buyer sets the maximum
purchase price, but it can be executed for a lower price.

In the same example above, suppose the user wants to buy 3 WBTCs at a
maximum of 32985 USDT each. As there are four sell orders of 2 WBTC each,
the first one selling at 32990 USDT / WBTC and the second 32980 USDT /
WBTC, then 2 WBTC is sold to the buyer at 32980 and the order is pending for
1 remaining WBTC.

Order Book
User Sell amount Sell price Buy amount Buy Limit

A 2 WBTC 32980 USDT

B 2 WBTC 32990 USDT

C 3 WBTC 32985

User C buys from A 2 WBTC at 32980 and the order of 1 WBTC is pending at a
maximum of 31985 USDT. Remaining the order book with:

Order Book
User Sell amount Sell price Buy amount Buy Limit

B 2 WBTC 32990 USDT

C 1 WBTC 32985

Order Books Multi-Blockchain Decentralized Exchange 21


Market Sell
In a sell order at the market price, the user does not
set the purchase price. Rather, you get an estimate
of the total to be obtained.

If the pair is WBTC / USDT it means that the user


wants to sell WBTC and receive USDT.

Amount
It is the total that you want to sell.

The percentage are buttons for the system to


automatically calculate the amount to sell according
to the amount of funds.

For example, if the buyer wants to sell WBTC and


has 2 WBTC in his portfolio, then by pressing 100%
the total will appear in 1 WBTC, if he presses 50%,
1 WBTC will appear, etc.

The formula for percentages is much simpler:

100%: user_balance
50%: user_balance / 2
25%: user_balance / 4

userBalance (user_account, token) => 200000000 -> 2 WBTC

Assuming the user has 2 WBTC, which is 8 decimal places, the maximum
amount of WBTC to sell is:

200000000 = 2 WBTC 100% is 2 WBTC

Fee, rewards, estimated total


These fields are not editable.

Both the fee, the swaprex profit and the estimated total are obtained with the
contract function:

getQuoteMarketSell (token_to_sell, amount_token_to_sell, token_to_get)

Example:

Order Books Multi-Blockchain Decentralized Exchange 22


getQuoteMarketSell (“0x123 ..”, 200000000, “0x456 ...”)
^ WBTC, ^ USDT
^ amount in WBTC: 3 WBTC
Returns:
{
"fee": 66000000, <--- 66 USDT (6 decimal places)
"rewards": 6600000000000000000000, <--- 6600 SwapreX (18 decimal
places)
"total": 66000000000, <--- 66000 USDT (6 decimal places)
}

Ideally, all values in the box should be refreshed every so often. (every minute
maybe?)

Sell button
The function is called:

marketSell (token_to_sell, amount_token_to_sell, token_to_get)

If there are several buy orders of this pair, the buy orders with the highest price
that match the buy order will be executed first .

If the seller wants to sell at market price, the highest possible price orders are
executed.

Example if there are three WBTC buy orders:

● The first buying 1 WBTC at 33500 USDT


● The second buying 1 WBTC at 33400 USDT
● The third buying 1 WBTC at 33200 USDT

Then 1 WBTC is sold at 33500 and 1 WBTC at 33400, on average the seller
charges 33450 USDT for WBTC.

Order Book:
User Sell amount Buy amount Buy price

A 1 33500 USDT

B 1 33400 USDT

C 1 33200 USDT

D 2 WBTC

Order Books Multi-Blockchain Decentralized Exchange 23


User D sells to A and B 2 WBTC in total. After this, the Order Book remains:
User Sell amount Buy amount Buy price

C 1 33200 USDT

Limit Sell
In a sell order at the price set by the user.

If the pair is WBTC / USDT it means that the user wants to


sell WBTC and receive USDT.

Price
It is the minimum price that the seller is willing to sell the
currency he owns.

This field is editable by the user.

Amount
It is the total that you want to sell.

The percentage are buttons for the system to automatically


calculate the amount to sell according to the amount of
funds.

For example, if the buyer wants to sell WBTC and has 2


WBTC in his portfolio, then when pressing 100% the total
will appear in 1 WBTC, if he presses 50%, 1 WBTC will
appear, etc.

The formula for the percentages is:

100%: user_balance
50%: user_balance / 2
25%: user_balance / 4

Assuming that the user has 2 WBTC, which is 8 decimal places, the maximum
amount of WBTC to sell is:

200000000 = 2 WBTC

100% is 2 WBTC

Order Books Multi-Blockchain Decentralized Exchange 24


Fee, rewards, total
These fields are not editable.

Both the fee, the swaprex profit and the estimated total are obtained with the
contract function:

getQuoteLimitSell (token_to_sell, amount_token_to_sell, token_to_get,


sell_price)

Example:

getQuoteMarketSellLimit ("0x123 ..", 200000000, "0x45000000",


33500000000 ... )
^ WBTC, ^ USDT
^ amount in WBTC: 2 WBTC
^ amount in USDT:
33500
Returns:
{
“fee”: 67000000, <--- 67 USDT (6 decimal places)
“rewards”: 6700000000000000000000, <--- 6700 SwapreX (18 decimal
places)
“Total”: 67000000000, <--- 67000 USDT (6 decimals)
}

Order Books Multi-Blockchain Decentralized Exchange 25


Sell button
The function is called:

limitSell (token_to_sell, amount_token_to_sell, token_to_get, sell_price)

If there are several buy orders for this pair, the orders will be executed first with
the highest price that match the purchase order.

Example if there are three WBTC buy orders:

● The first buying 0.3 WBTC at 33600 USDT


● The second buying 1 WBTC at 33500 USDT
● The third buying 1 WBTC at 33400 USDT

Then 0.3 WBTC is sold at 33600, 1 WBTC at 33500 and 0.7 remains pending
WBTC to sell at 33,500 USDT.

Order Book:
User Sell amount Sell price Buy amount Buy price

A 0.3 33600 USDT

B 1 33500 USDT

C 1 33400 USDT

D 2 WBTC 33500

After this the Order Book remains:

User Sell amount Sell price Buy amount Buy price

C 1 33400 USDT

D 0.7 WBTC 33500

Order Books Multi-Blockchain Decentralized Exchange 26


Trades
Returns the last 11 orders of a pair.
Desing
Sorted descendingly.

trades (token_to_sell, token_to_get)

Returns:
[
{
type: sell,
price: 32000000000, <- red because it dropped
from
previous
timestamp: 1612980073
},
{
type: sell,
Example real data:
price: 34000000000, <- green because it went up
compared to previous
timestamp: 1612980072
},
{
type: sell,
price: 33000000000, <- red because neither it went
up or down and the previous one is red
timestamp: 1612980071
},
{
type: sell,
price: 33000000000, <- red because it went down
compared to the previous
timestamp: 1612980070
},
....
{
type: sell,
price: 34000000000, <- the number 11 is the last one, it is not shown but it is
used as a
reference to show the color of the 10
timestamp: 1612980069
}
]

Order Books Multi-Blockchain Decentralized Exchange 27


Order Book
The order book has three sections: Design:

1 The upper part is the 10 sales orders plus close to the


last price. Sorted descendingly and in red.
2 The middle part is the last sale price (stats24hr
last_price)
3 The bottom part is the 10 buy orders closest to the last
price. Sorted descendingly and in green color.

orderBook (token_to_buy, token_to_sell)

Example:

orderBook (“0x123 ...”, “0x456 ...”)


^ WBTC ^ USDT

Returns:
{
sell: [
Example real data:
{
price: 3500000000000, <- 35000 USDT
amount : 3400000000, <- 34 WBTC
time: 1612997677, <- timestamp
This is: 34 WBTC were sold at
35000 USDT at 4:55 pm8
on February
},
{
price: 3400000000000, <- 34000 USDT
amount: 1500000000, <- 15 WBTC
time: 1612997676, <- timestamp
}
...
],
}

Order Books Multi-Blockchain Decentralized Exchange 28


User Open Orders

Returns the pending orders of a user in a given pair

openOrders (token_to_buy, token_to_sell)

Example: openOrders (“0x123 ..”, “0x356 ...”)


^ WBTC ^ USDT
Returns:
[
{
type: “buy_market”,
amount: “300000000”, <- 3 WBTC
timestamp: 16785994726
},
{
type: “buy_limit”,
amount: “300000000”, <- 3 WBTC
price: “3500000000 0 ”, <- 35000 USDT
total:“ 105000000000 ”, <- 105000 USDT
timestamp: 16785994725
},
{
type:“ sell_market ”,
amount:“ 100000000 ”, <- 1 WBTC
timestamp: 16785994724
},
{
type: “Sell_limit”,
amount: “50000000”, <- 0.5 WBTC
price: “32000000000”, <- 32000 USDT
total: “16000000000”, <- 16000 USDT
timestamp: 16785994723
},
]

Order Books Multi-Blockchain Decentralized Exchange 29


User History
Returns the executed orders of a user in a given pair

historyOrders (token_to_buy, token_to_sell)

Example: historyOrders ("0x123 ..", "0x356 ...")


^ WBTC ^ USDT
Returns:
[
{
type: "buy_market",
amount: "300000000", <- 3 WBTC
price: "33000000000", <- 33000 USDT
total: "99000000000", <- 99000 USDT
timestamp: 16785994726
},
{
type: "buy_limit",
amount: "300000000", < - 3 WBTC
price: “35000000000”, <- 35000 USDT
total: “105000000000”, <- 105000 USDT
timestamp: 16785994725
},
{
type: “sell_market”,
amount: “100000000”, <- 1 WBTC
price: “30000000000”, <- 30000 USDT
total: “30000000000”, <- 30000 USDT
timestamp: 16785994724
},
{
type: “Sell_limit”,
amount: “50000000”, <- 0.5 WBTC
price: “32000000000”, <- 32000 USDT
total: “16000000000”, <- 16000 USDT
timestamp: 16785994723
},
{
type: “stop_loss”,
amount : “50000000”, <- 0.5 WBTC
price: “30000000000”, <- 30000 USDT
total: “15000000000”, <- 15000 USDT
timestamp: 16785994722
}
]

Order Books Multi-Blockchain Decentralized Exchange 30


Roadmap
1 February 2021: Development of Order Books DEX
2 Estimated Launch April 2021.

Future
● Become a DEX Aggregator. Filling Order from all other dexes in Mainnet:
○ 1Inch
○ Uniswap
○ Sushiswap
○ Balancer
○ Bancor
○ AMong others
● Integration with other Blockchains:
○ Filling order from all other DEXes using xDai
○ Filling order from all other dexes using binance chain

Order Books Multi-Blockchain Decentralized Exchange 31

You might also like