EtherDelta: Don’t leave crypto on the table!

CryptoAtelier
10 min readSep 26, 2017

--

Daniel Kao — Founder of CryptoAtelier.io

Preface

I want to preface this article by saying that I’m grateful for the team at EtherDelta for providing a relatively robust Ethereum-based exchange platform which runs in a decentralized manner with a DApp front-end. Obviously, the Ethereum network when congested can cause issues especially during high ICO network traffic, but otherwise in a trust-less environment, it does its job very well. Other than the recently patched code-injection vulnerability which occurred while I was writing this article, I use EtherDelta on a daily basis and it is one of the most stable and predictable crypto exchanges available today.

Introduction to “The Problem”

After I started trading with EtherDelta earlier this year for buying and selling ERC20 tokens, I started to notice discrepancies with amounts I expected when withdrawing purchased ERC20 tokens or Ether, even after accounting for the “takers” fee of 0.3%. The problem ended up being the fact that most tokens traded can have up to 18 decimal points of precision which the EtherDelta DApp front-end doesn’t quite allow that sort of precision when executing trades or withdrawing from their wallet to your private wallet.

What usually ends up happening, there are small amounts of your ERC20 tokens left on EtherDelta after you think you’ve withdrawn all of it. EtherDelta isn’t deceiving you on purpose. And usually, this isn’t that much of a problem; The amounts are usually minuscule. However, for Ether and high fiat value tokens (i.e. OmiseGO $OMG, Metal $MTL, etc. at least during the time of publishing this article), the amounts can add up and could potentially be significant. For most people, this probably isn’t an issue. For some people, like myself, I have an OCD when it comes to leaving “money” on the table, no matter if the amount ends up being less than a US dollar, and in most cases, just pennies in fiat terms.

So how did I determine I still had little amounts of tokens left on EtherDelta after I thought I had completely withdrawn all of them through the EtherDelta DApp front-end? I “asked” the EtherDelta smart-contract itself on the Ethereum blockchain.

The Smart Contract

EtherDelta, at the core, is basically an extremely well thought out and very well engineered smart contract which runs on the Ethereum blockchain. It’s basically a program that’s written as a trust-less exchange, where you execute certain functions, like any other modern computer program. It’s also a smart-contract which holds all of its users’ deposited Ether and tokens used for trading. At the time of this article’s publishing, the current version of the deployed contract on the main Ethereum blockchain can be viewed here, courtesy of Etherscan.io:

https://etherscan.io/address/0x8d12a197cb00d4747a1fe03395095ce2a5cc6819

If you click on the “Contract Source” tab, you can see it’s basically 312 lines of code running on the Ethereum blockchain. It’s pretty impressive for a smart contract which holds, handles & transacts so much crypto between an extraordinary number of users on a daily-basis.

Now click on the “Read Smart Contract” tab. Here, you will see the information and queries that can be run against the contract. This is the area where we’re going to start with.

The Solution — Part 1 — Finding the crypto in-between the “seat cushions”

If you look at Option #11 from up above, it is a query method named “balanceOf” which has two input fields. Back in the previous “Contract Source” tab, there was a box labeled “Contract ABI”. I won’t go into details what it is, but it’s basically a JSON representation of the interface of methods (e.g. functions, queries, etc.) in the smart-contract. All you need to know, and you can safely take my word for it for now, is the first “token (address)” input field is for the address for the token contract you want to retrieve the current balance of. The second input field, “user (address)” is the Ethereum address of the owner you want to query about.

Form for finding the balance of either a token or Ether ($ETH) owned by a particular Ethereum Address (e.g. wallet)

For example, I want to know what my balance of OmiseGO ($OMG) is on EtherDelta is. The token contract for $OMG is as follows (and more on how to easily find this information later in the article):

0xd26114cd6ee289accf82350c8d8487fedb8a0c07

And let’s say, hypothetically, my private Ethereum wallet address is (which it isn’t; Just found someone who is actively trading $OMG on EtherDelta):

0xe300b3909aad54fd05e919e39f402c842e0037d3

Plug those strings into each of the respective fields and hit the “Query” button. You will get a method (function) response of what the balance of OmiseGO, owned by the above Ethereum wallet address, currently being held in the EtherDelta smart-contract, in a uint256 value.

Raw uint256 balance of OmiseGO ($OMG) for an Ethereum address

What is a uint256 value and what the heck does that mean? Honestly, for the purpose of this article, you don’t need to know. But basically, it’s a raw representation of your token balance BEFORE adjusting for decimals points that’s defined by a ERC2X (e.g. ERC20) token contract. Most of the time, it’s 18 decimals but it’s wise to double-check if you’re curious what your exact representative token balance is.

In the case above, OmiseGO ($OMG) is 18 decimal points so the actual balance is 350.257649257375882628 OMG tokens.

OH! And neat trick… If you want to know your exact amount of Ether ($ETH) owned by you on EtherDelta, use “0x0” (that’s “zero-by-zero”) as the string for the token address input field. This is your pro-tip.

The Solution — Part 2 — Getting to know MEW and the EtherDelta Contract

Up until earlier this month, I used a combination of either Parity or MEW (MyEtherWallet.com) in order to retrieve the “pennies of crypto” from EtherDelta by directly interacting with the deployed smart-contract outside of the official EtherDelta DApp front-end. I won’t go into details on how to do it with Parity, but the general premise is you add the EtherDelta smart-contract as a “Contract” where you can directly execute methods we’re going to cover.

However, I will be covering the instructions using MEW, and my guess is most folks who are familiar & knowledgeable of how Parity works, once you’ve seen how it works with MEW, you should be able to figure out how to do it in Parity.

Parity.io Interface for Interacting with Ethereum Contracts

Using MEW, up until recently, was a fairly manual process. Back on September 8, 2017, I submitted code (#990) to the official MyEtherWallet.com GitHub repository with a pull-request to add the EtherDelta smart-contract as one of the available pull-down contracts to interface with in MEW. The pull-request was approved days later and merged into the code base and was deployed into version 3.10.3.1 of MEW. Before the EtherDelta contract was added to the pull-down list, you had to manually put in the EtherDelta contract address, and the associated JSON blob of the “Contract ABI” so MEW would know how to display all the functions and queries to the end-user. Even though this was a simple copy & paste, it’s tedious each time you wanted to interact directly with the EtherDelta contract. — This is no longer needed nor the case as of today, luckily. Now you can:

Select “Contracts” on the top-navigation bar of MyEtherWallet (MEW)
  • You should see the “Interact with Contract or Deploy Contract” screen.
  • From the “Select Existing Contract” pull-down menu, scroll down the list and select “EtherDelta (02/09/2017)”.
EtherDelta (02/09/2017) Smart Contract
  • This should prefill the “Contract Address” and “ABI/JSON Interface” fields.
“Contract Address” and “ABI/JSON Interface” fields should populate
  • Click “Access”.
  • A new section should pop-up called “Read/Write Contract” with the Ethereum address of the current EtherDelta smart-contract directly below it.
  • Click on the “Select a function” pull-down.

Now you’re probably wondering, “Holy smokes! There’s so much stuff here!” And you’d be correct. What you see is nearly, if not all, the functions and methods of EtherDelta’s smart-contract. All of those functions are used as part of its decentralized exchange, including administration. Pretty neat, huh? That being said, there are primarily three different functions we’re interested in:

The three EtherDelta functions we’re interested in.
  1. withdraw — This is used to withdraw your owned balance of Ether from EtherDelta.
  2. withdrawToken — This is used to withdraw your owned balance of a particular token from EtherDelta.
  3. balanceOf — This is the same function we used above up on Etherscan.io. It allows you to see the balance of Ether or a token for a particular Ethereum address (e.g. your private ETH wallet).

The Solution — Part 3 — “Sucking” up crypto in-between the “seat cushions”

So my SOP (Standard Operating Procedure) basically works like this on a daily or weekly basis:

  1. At the “end” of my trading day (or week) on EtherDelta, I check if I have any left over balances of tokens I traded during that time period using the “balanceOf” method for each token.
  2. For each token with a small left over amount, I notate the raw number given by the “balanceOf” response and the token contract address. Note: During this entire process, there is no need to adjust for decimals. Everything is done in uint256 notation.
  3. For each token with balances, I “execute” the “withdrawToken” method using the token contract address and the amount previously given by the “balanceOf” method earlier for each respective token.
  4. Repeat steps above until I’ve cleared all of my tokens from EtherDelta.
  5. Finally, check the balanceOf “0x0” Ether ($ETH) on EtherDelta and execute “withdraw” to move any unused Ether back to my private wallet.

Now, something I want to stress before we move further. — When you execute/call the “withdrawToken” method, you’re creating and broadcasting a live transaction to be mined on the Ethereum network. THIS COSTS GAS. Keep this in mind when you set “Gas Prices” using the slider in MEW. ALWAYS set it to 1 Gwei.

Slide the “Gas Price” slider all the way to the left.

There is absolutely NO sense in spending Ether in gas that may be worth MORE than what you’re withdrawing in fiat terms, if that’s important to you which I assume for most people, it is. The EtherDelta contract is extremely efficient and doesn’t use much gas. But if you’re using anything other than 1 Gwei when you broadcast your transaction to be mined, I will almost guarantee every time it’s not cost-efficient to retrieve your leftover tokens.

Note: I know someone will proclaim, “You can set Gwei lower than 1 in Parity!” — That is true. You can set a Gas Price lower than 1 Gwei in Parity versus the lowest setting through MEW’s interface.

The Solution — Finale: Putting It All Together

You’ve gotten this far, so let me fully walk you through an example. But first, how do you find the token contract address for a particular token for all the stuff up above? Glad you asked. It’s pretty simple.

  • Launch EtherDelta.com in a browser.
  • Select the token/ETH pair you need the token address of. I will use OmiseGO ($OMG) for this example.
  • You will see a screen similar to this on the upper-left. Click on the abbreviated token name (e.g. OMG).
Click on the abbreviated token name (e.g. OMG)
  • You will see a pop-up dialog box with information about the token. One piece of that information is the Ethereum smart-contract address. Copy and save it. That’s it.
Easy way to find Token Smart Contract Address

Now onwards to an example:

  1. Go back to Part 2 above and follow all the steps if you still don’t have the “Select a function” pull-down for the EtherDelta contract.
  2. Double-check the MEW “Gas Price” slider at the top of the page. Make sure it’s set to “1 Gwei”.
  3. Select “balanceOf” method from the pull-down menu.
  4. Fill in the “token <address>” of the token.
    (e.g. 0xd26114cd6ee289accf82350c8d8487fedb8a0c07 for $OMG)
  5. Fill in the “user <address>” with your private ETH wallet address.
    (e.g. 0xe300b3909aad54fd05e919e39f402c842e0037d3 — Not Mine)
  6. Click “Read”.
  7. Copy the resulting string of numbers and save it somewhere.
    (e.g. 350257649257375882628 at the time of writing of this article)
  8. Go back to the drop-down menu and select “withdrawToken”.
  9. Fill in the “token <address>” of the token (same as earlier).
  10. Fill in the “amount <uint256>” from Step 7.
    (e.g. 350257649257375882628).
  11. Select a method of accessing your wallet. This has to be the same wallet that owns the token or Ether that you’ve been trading with on EtherDelta. This seems like common sense, but you know some people…
  12. Click “Write” and follow through the prompts.
  13. Once the transaction is mined, you can verify the transaction & balances with Etherscan.io.
balanceOf Method for $OMG for a particular ETH address

Hope this helps! Feel free to reach out if you have any questions by leaving comments below.

--

--

CryptoAtelier

We are an Advisory Agency specializing in Blockchain, FinTech, ICOs, CryptoCurrencies & SmartContracts with a planned Capital Fund launch in 2018.