A better EtherDelta? Making an ERC20 token exchange that does not hold your tokens.

bzz
4 min readFeb 8, 2018

--

With the rise of ICOs, we have a lot of exchanges that you can trade your tokens, but, when we are talking about a newly issued token, there are not that many. We already have a few exchanges adopting the brand new 0x protocol, that is supposed to provide on-chain finalisation of previously (and off-chain) arranged trade. For some reason, decision is made, to work off-chain. The reasoning is quite simple: having an on-chain order book consumes too much gas. In this article, we will see, how much is this an issue.

Having 0x protocol in draft, we already have EtherDelta, and it is currently the #1 gas burner on Ethereum Network. Does it have the on-chain order book? Let’s look in the source code.

The code is surprisingly short and clear, leaving out the non-trivial ecrecover and nonce stuff. The way the contract stores the orders is, in fact, on-chain! Events (i.e. logs) are used to store the data. Here is what it looks like:

function order(address tokenGet, uint amountGet, address tokenGive, uint amountGive, uint expires, uint nonce) {
bytes32 hash = sha256(this, tokenGet, amountGet, tokenGive, amountGive, expires, nonce);
orders[msg.sender][hash] = true;
Order(tokenGet, amountGet, tokenGive, amountGive, expires, nonce, msg.sender);
}

Here’s what it does: it takes hash of order properties, then it sets the state variable as true to show that the order exists. After doing that, it raises an event, where it stores the order data. Besides, there is another state variable to store the the volume filled on this order. It is being set in trade() method.

So, EtherDelta does store orders on-chain, now can we look up the price of a token? Not really! The problem is the way events (logs) work in EVM (and Solidity). The data in logs cannot be accessed from a contract. The way we get order info is parsing all the Order events from the contract, looking for the token of our interest. Let’s say, we only interested in prices in ether, which is encoded as token 0x00. So, if we want to buy, tokenGet will be our token and tokenGive will be 0x00. And expires is the block number after which the order is no longer valid.

What could easily be seen is that the EtherDelta code is not perfect. There were few steps to make it better. First, I removed the unnecessary true state variable and replaced it with the current amount available on the order. Second, if we want to have an open trading platform, then we really want to get rid of ecrecover, nonce, r, s, v (you can see it in trade() method signature) and all the esoteric stuff which is, in my opinion, designed to lock the contract to etherdelta.com frontend. Besides, sha256 can be replaced with sha3. Removing the expires parameter removes the feature of expiring orders (most traders don’t want their orders to ever “expire”). So many changes, it’s already so much better!

How do you operate such a simplified EtherDelta? When you want to sell a token, what do you do? OK, first you approve EtherDelta to handle your tokens. Second, you call depositToken() method… Wait… Why would you deposit your tokens when EtherDelta contract is already allowed to transfer them? That step costs you extra gas! It can and should be removed!

OK, that’s where we change the inner parts of trade() method. We only deposit ether and withdraw ether. Our contract does not hold any tokens anymore. We even add an admin method to return tokens that are deposited by mistake.

We really want to get rid of the complicated maker-taker fee system. There is vague reasoning to take a share of traded volume as a fee. The real world exchanges do take the flexible fee because they do hold your assets and they would be in some way responsible in the case of loss. In the world of crypto, we are not holding anything. Why not take just a small fixed taker fee (let’s say $1), leaving the makers alone? My opinion is, you shouldn’t pay anything to place orders (as you already pay with gas!). And you surely can pay the fee if you want to quickly buy/sell stock by filling someone else’s order.

So, we have a completely rewritten EtherDelta, with simplified fee system. We even have deployed it on the main net. Any frontend can make orders and fill orders. But wait…, what about the hack? EtherDelta website was hacked lately, there is a lot of youtube videos on how to withdraw your deposited tokens (sic!) and ether. Can’t we really rewrite the whole system to work just by calling methods, without the need to parse thousands and thousands of event logs? In fact, we can.

We can rewrite the contract abi the way so you could trade even without using a website or dapp. That’s the idea behind BURSA.

In the next article, we’ll learn how to trade tokens in completely trustless manner using BURSA smart contract and the wallet of your choice.

--

--