Radex.ai Overview

whitepaper

#1

3. Radex.ai Overview

3.1 What is Radex?

Radex[11] is a decentralized exchange for ethereum tokens. It is the first decentralized exchange for ERC223 tokens - a new token standard that has some advantages[12] over conventional ERC20[13].

Radex is the first 0% fee decentralized exchange. It consists of two parts: a smart contract[14] that handles all the financial transactions and the web application[15] that makes interacting with the smart contract easy and accessible to humans. The smart contract is well reviewed, tested and open source. The code for the smart contract is available on our Github[16].

When we say 0% fee we mean that the exchange does not have an admin or another entity of authority that extracts rent from exchange ownership. However it’s worth noting that there are certain fees for every transaction that you send to Ethereum blockchain. These fees go to Ethereum miners that secure the blockchain for all of us. Typically these fees are very cheap, and the most expensive fee ends up being less than 1 USD. More importantly though, this fee is flat and does not depend on the amount of transaction, allowing Radex users to rebalance their portfolio more often without giving up ownership of their tokens. Ongoing work by ETH dev team[17] will address blockchain scalability and will drive the fees further down.

In this section we will describe the protocol that allows anybody with access to Ethereum blockchain to exchange tokens with others in a trustless and pseudonymous way. We will then describe the architecture of Radex - the first exchange that uses this protocol, and will compare this architecture with that of other exchanges: GDAX[18] (as an example of a centralized exchange), EtherDelta[19] and 0xProtocol[20]. At the end of this section we will review and compare these different architectures and will outline further areas of improvement for Radex.

3.2 Radex protocol

Radex protocol is the smart contract that’s deployed on Ethereum blockchain and the interface that it speaks. The code for the smart contract is available on our Github[21] and you can explore the deployed version on Etherscan[22].

This smart contract has enough functions to implement a token exchange completely on-chain without any need to maintain off-chain centralized infrastructure.

You can skip this section if you’re not interested in code, and instead jump straight to architecture comparison. If you are, feel free to read this section while following the source code[23] and the tests[24].

3.2.1 Deposit

Deposits for ERC223 tokens and for ETH are handled differently due to technological reasons.

In order to deposit ETH you’ll have to call the fund function and attach the value you want to the transaction.

await radex.fund({value: 1}); // will deposit 1 wei

ERC223 transfers are very simple. Just send your tokens to the smart contract and it’ll handle the deposit.

await token.transfer(radex.address, 1000000000);

Make sure you don’t send any ERC20 tokens to this contract’s address, as they will not be handled and you will permanently lose them, as discussed in ERC223 section.

After any of these functions get executed a Deposit(token, trader, amount, time) event gets emitted.

3.2.2 Check Balance

This operation works very similar to checking one’s token balance, except you have to supply two arguments. The first argument is balance of what token you’re tracking (by convention, ETH is internally treated as a token with 0x0 address), and the second is whose balance you want to check.

// check 0xdeadbeef's STN balance on Radex
await radex.balanceOf(stn.address, '0xdeadbeef');
// check 0xdeadbeef's ETH balance on Radex
await radex.balanceOf('0x0', '0xdeadbeef');

There’s also a concept of commitments. Commitments are tokens that are locked into created orders. You can similarly check how much does a person have in opened orders.

await radex.commitmentsOf(stn.address, '0xdeadbeef');
await radex.commitmentsOf('0x0', '0xdeadbeef');

3.2.3 Withdraw

You can withdraw the funds up the the amount that balanceOf returns.

await radex.redeem('0x0', 1); // withdraw 1 wei

A Withdrawal(token, trader, amount, time) event gets emitted.

3.2.4 Create order

This is a slightly more complex operation, so let’s start with code here and explain what every argument means.

await radex.createOrder(
  token1.address,
  token2.address,
  amount,
  price.numerator,
  price.denominator
)
  1. token1.address - address of the token that you want to sell. You must have enough of token1 deposited on the exchange for this order to successfully get created.
  2. token2.address - address of the token that you want to buy. Remember that ETH is token 0x0.
  3. amount - how much of the first token you’re trying to sell, or the capacity of your order. Pay attention to the token’s decimals!
  4. price.numerator and price.denominator - floating point numbers do not have any native support in Solidity. So in order to enable prices that are less than 1 we resorted to representing floating point numbers in the form of rational numbers[25].

The smart contract assigns an orderId to a newly created order that you should retrieve from the logs.

After an order gets created a NewOrder(orderId, trader, token1, token2, amount, priceMul, priceDiv, time) gets published.

3.2.5 Cancel order

If the market conditions have changed and you no longer wish to keep an order open at the same price point you can cancel the existing order.

await radex.cancelOrder(firstOrderId);

You can only cancel orders that you yourself have created. An OrderCancelled(orderId, time) event gets published after a successful transaction.

3.2.6 Execute order

Once you read the logs and recover the orderId that was assigned to an order that interests you, you can execute this order for the amount of tokens that’s less than or equal to its capacity.

This means that if there’s an open order selling 100 ETH you cannot buy 200 ETH from this order, but you can buy 10 ETH and leave the order with 90 ETH capacity after you’re done.

await radex.executeOrder(orderId, amount)

  1. orderId - which order you’re trading against.
  2. amount - how much you are trying to buy.

A number of events get published.

  • Trade(buyer, seller, orderId, soldTokens, boughtTokens, time) gets published after each trade and represents the details of this trade.

  • MarketMaker(seller, order.sellToken, fee, now) gets published after each trade and represents the market making bonus that the trader who created the order gets for providing liquidity to the market.

  • OrderFulfilled(orderId, time) gets published if the order’s capacity is reduced to 0. The user interface of an exchange working on Radex protocol, such as a web app or a desktop application, should remove such order from their order book as all subsequent executeOrder calls to this order will fail.

3.2.7 View the order book

This is the trickiest part. The order book is not stored on the blockchain in the most readable fashion due to the platform’s limitations. However, we produce just enough events in transactions that modify state that we can recreate the order book at any time by reading all events starting from the block when Radex was deployed on the blockchain and recreating the order book from scratch based on this information. This allows independent exchange implementations to be in sync among each other without having to trust a centralized API maintained by an exchange.

In other words, we treat the blockchain as an append-only log. We build on the architecture that is decades old in database research. Most recently this architecture received big praise in the Big Data ecosystem thanks to the work done by the great engineers of LinkedIn and SalesForce[26].


In the next section we will review the architecture of several popular decentralized exchanges and compare them to Radex’s architecture:


  1. https://radex.ai
  2. https://rados.io/advantages-of-erc223-tokens
  3. https://github.com/ethereum/EIPs/issues/20
  4. https://etherscan.io/address/0x9462eeb9124c99731cc7617348b3937a8f00b11f
  5. Read more about our development philosophy here.
  6. https://github.com/rados-io/radex
  7. Vitalik Buterin has mentioned in one of his conference talks that Casper (Proof-of-Stake
    for Ethereum) and Sharding (a native sidechain solution that is designed to drastically
    improve throughput of the blockchain) are scheduled to be gradually released by 2020.
  8. https://gdax.com/
  9. https://etherdelta.com/
  10. https://0xproject.com/
  11. https://github.com/rados-io/radex
  12. https://etherscan.io/address/0x9462eeb9124c99731cc7617348b3937a8f00b11f
  13. https://github.com/rados-io/radex/blob/master/contracts/Radex.sol
  14. https://github.com/rados-io/radex/blob/master/test/radex.js
  15. https://en.wikipedia.org/wiki/Rational_number
  16. You can read more about this software design pattern in our blog post.

Saturn Network Vision
Saturn Network Whitepaper v1.2
Our Philosophy
Other Blockchains