How to build your own ERC20 token and use MARKET Protocol to make trustless derivatives on the Ethereum blockchain!

Joshua Herman
7 min readMay 24, 2018

--

The Market Protocol is an safe, solvent and trustless trading of any asset through the use of smart contract technology. In this tutorial we will use their recently released the first beta version of a dApp that allows you to deploy such trading contracts on the Ethereum blockchain. I will also guide you through all necessary steps needed to deploy your own contract on Rinkeby Testnet.

Step 0: Setup Metamask

Metamask is a browser extension that allows your wallet to interface with dApps. Since we will be using the Market Protocol dApp you need this to continue. If you have never setup a wallet or Metamask before it is relatively straight forward. First you set a password and then it will prompt you to save a seed words file. If you have issues creating a wallet a good how to guide on how to do this can be found at https://www.cryptocompare.com/wallets/guides/how-to-use-metamask/

Once you have created a wallet you then want to switch networks to Rinkleby. You can do that by clicking on the dropdown I have highlighted.

What metamask looks like when Rinkeby is selected.

I order to proceed, you will need to acquire some Ether for your wallet. Since the MARKET Protocol dApp is only on the Rinkeby Testnet, you will need to visit their faucet and follow the instructions on how to obtain the testnet ETH. The other reason is that you need ETH to deploy smart contracts and we will have to deploy a Token and the smart contracts that the Market Protocol requires.

The first step to do so is by creating a social media post with your wallet address from MetaMask.

The next step is to the rinkeby faucet https://faucet.rinkeby.io and submit the URL of the social media post to the Faucet. Now if you did everything correctly you should receive a confirmation about the funding being accepted, as shown below:

Successful request to the RInkeby Faucet.

The last step is to verify that your Metamask wallet is funded. It would look similar to the screenshot below.

MetaMask funded with Testnet ETH from Rinkeby faucet.

Step 1: Create a Token on Rinkleby

This tutorial has two parts -first one that explains how to trade derivatives using ERC20 tokens as a base asset on the Rinkeby Testnet and the second which demonstrates how to create your own token on Rinkeby and use it for the previously mentioned trade. This part was a very good demonstration of flexibility that MARKET Protocol contracts have. Now there are actually four networks where you can issue an ERC-20 token. Due to the fact that Marketprotocol.io is still in beta we need to deploy our Token to Rinkleby. The easiest way to do this is using remix.ethereum.org.

To start off we need the smart contract code. You can use the code below.

pragma solidity ^0.4.19;
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
contract SecurityCoin is MintableToken {
string public name = "Security Token"; //CHANGE THIS LINE
string public symbol = "STK"; //CHANGE THIS LINE
uint8 public decimals = 18;
}

Then open remix.ethereum.org and click on the plus button on the upper left corner. Name the file token.sol and your screen should look like below.

Now you have to compile the smart contract so click Start to Compile. Once that has completed we need to deploy the contract on the Rinkeby Test Network. To do this click the Run tab and then click the red Deploy button.

You now should see a prompt by Metamask. Click confirm and your smart contract should be deployed on the Rinkleby test network.

After deploying the token the Right hand side should look like the following.

Interacting with your token using remix

After deploying it on the Rinkleby test network you should see the following in the console.

Creation of your ERC20 Token

If you click on the link that starts with rinkeby.etherscan.io you should see the following which confirms that you made a smart contract.

Step 2 Choose your Oracle using Oracalize

Due to the fact that smart contracts can’t fetch external data we need a way to get that data so we can settle the trade. The best way to be sure your chosen oracle solution is working properly is to run a Test Query prior to contract deployment. To keep this tutorial simple we will use the Price of BTC-USD on GDAX.

json(https://api.gdax.com/products/BTC-USD/ticker).price

When this query is executed it will tell us the price of Bitcoin in USD. The result of this query is below.

Query Result8232.90000000

For more information about writing queries using Oracalize visit http://docs.oraclize.it/#home . If you want to execute queries yourself I recommend to watch the following youtube video tutorial made by the MARKET Protocol team.

Step 4 Use the dApp to create a Market Protocol Contract

So now we have all of the pieces to make a Market Protocol Contract. So navigate to dapp.marketprotocol.io and then select a Guided Contract deployment. Before we start we will need the following

  1. The address of the ERC-20 token we just created.
  2. The Oracle Query that decides the price of the contract.

The next screen will ask us to name the contract and also for the address of the ERC-20 token. The guided contract creation will give you good suggestions on what to name your contract. The important thing here is to put in the Base Token Address. This can be found in your Metamask wallet and just click the first transaction we did called “Contract Deployment”.

How to find your contract address.

your screen should look like the following.

Contract name and Collateral Token

For the next screen set the data source to URL and take the Oraclize.it Query and put it in the Orcaclize.it Query field. As mentioned above, for this purpose I have decided to use the price of BTC denominated in USD on GDAX.

json(https://api.gdax.com/products/BTC-USD/ticker).price

Now the third part is to set the pricing rules. First we want to set the Price Decimal places. We know that our query will return bitcoin with 10 decimal places and since we want the price of BTC in relation to the USD we set this to 10.

For the purposes of this exercise I will make an arbitrary Price Floor to be 7000 USD and also make an arbitrary price cap to be 10000 USD. So we have to add 10 decimal places for this to work.

So the numbers to input are

Price Floor : 7000000000
Price Cap : 100000000000

The last step is to set the price quantity multiplier. I set it to 1000000 due to the fact that the Orcalize.it query will return values with nine decimal places so this corresponds to setting our “wei” to 1000000.

The penultimate step is to set the expiration time and set the gas price to get our transaction confirmed. You can set the expiration time to whenever you want but for the purposes of this exercise we will set it to tomorrow. Check https://dapp.marketprotocol.io/contract/explorer tomorrow to confirm everything.

Setting Expiration Time

And the final step is to fund the contract (with our imaginary Ethereum from the faucet we discussed earlier). Please note that the higher you set the gas price the less time it will take for that transaction to be confirmed and contract deployed.

I set the gas price for mine to 15 since we are using the test network. Now that you have everything set we are ready to go! Click deploy

Ready to Deploy your contract to the moon!

Once you do this you will see a set of Metamask popups asking you to confirm the actions. When everything is complete you should see below:

Successful Deployment of Contract

And that’s it! Congrats on deploying your Market Protocol Contract!

To learn more about MARKET Protocol, visit www.marketprotocol.io, read their whitepaper , and join the ongoing discussion on Telegram .

For more information you can see their website at https://marketprotocol.io/# and their whitepaper at If you want to donate BTC to me you can at the following addresses:

BTC : 0x3DkrzVdJxAaNBmMNjidVR5roNJT9hkPLXN

--

--