RADAR RELAY V2

We’ve updated to 0x V2.See what changed.

Building a Trading Bot

This tutorial walks you through building a trading bot.

Prerequisites

1

Setup An Ethereum Node

Trading on a Relayer requires access to an Ethereum node. The easiest way to get setup with an Ethereum node is to obtain an API key from Infura.

Learn more about Ethereum nodes

Infura Kovan Node

https://kovan.infura.io/[your-api-key]
2

Install Node.js

The Radar Relay SDK and example bot requires node.js to run. (version 8 - LTS release recommended).

Installing Node.JS with Homebrew

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$ brew install node

3

Download the Radar Relay Bot Example

The Radar Relay Bot example is a Github repository that provides a basic example of trading with the Radar Relay SDK.

View The Repository On Github

Clone the Repository on Github

$ git clone https://github.com/RadarRelay/sdk-bot-example.git

Bot Setup

1

Install dependencies

Install the required dependencies with NPM.

Install Dependencies

$ npm install
2

Set a password

The bot depends on the environment variable RADAR_WALLET_PASSWORD. Set the ENV with your desired password.

Export Password ENV Variable

$ export RADAR_WALLET_PASSWORD=yourpassword
3

Run the bot

Run the bot via NPM.

Run the bot

$ npm start

The Bot

1

SDK Initialization

Setup SDK
Use the SdkManager to setup the SDK. This will instantiate the RadarRelay class and setup for the initialization lifecycle.

Add Event Listeners (Optional)
If you would like to perform an action during wallet / SDK initialization, you can listen for various initialization events.

In this example, we'll listen for the Loading event and display a progress bar. Learn More About SDK Events

Light Wallet
Call initialize to hook up the wallet, set the web3 provider, and spin up the classes necessary to begin trading. The easiest wallet to setup is a local LightWallet.

RPC Wallet
Alternatively, you can pass in the url to an unlocked Ethereum node to use as a wallet.


Initializing the SDK


  // Setup with local wallet (recommended)
  const rr = SdkManager.Setup({
    radarRestEndpoint: 'https://api.kovan.radarrelay.com/v2',
    radarWebsocketEndpoint: 'wss://ws.kovan.radarrelay.com/v2'
    dataRpcUrl: `https://kovan.infura.io/${YOUR_API_KEY}`,
    wallet: {
      password: 'asecretpassword'
    }
  });

  // ---------------------------------
  // Or setup with unlocked RPC node
  const rr = SdkManager.Setup({
    radarRestEndpoint: 'https://api.kovan.radarrelay.com/v2',
    radarWebsocketEndpoint: 'wss://ws.kovan.radarrelay.com/v2'
    rpcUrl: 'http://localhost:8545',
  });
  // --------------------------------

  // Listen to loading progress
  rr.events.on(EventName.Loading, data => {
    process.stdout.write('....');
    if (data.progress === 100) {
      process.stdout.write('🚀');
    }
  });

  // Initialize the SDK
  await SdkManager.InitializeAsync(rr);
2

Fetching Balances

The bot will then fetch balances for ETH, WETH, and ZRX.

Fetching Balances

const zrxEthMarket = await rr.markets.getAsync('ZRX-WETH');
let curEthBal = await rr.account.getEthBalanceAsync();
const curZrxBal  = await rr.account.getTokenBalanceAsync(zrxEthMarket.baseTokenAddress);
3

Setting Token Allowances

In order for orders to execute over the 0x smart contracts a token allowance must be set. The bot sets unlimited token allowances for WETH and ZRX.

Additional options can be passed to set a specific gas price for the transaction as well as wait for the transaction to be mined before returning.

Setting Allowances


const receipt = await rr.account.setUnlimitedTokenAllowanceAsync(zrxEthMarket.quoteTokenAddress, {
  transactionOpts: { gasPrice: new BigNumber(1) },
  awaitTransactionMined: true
});
4

Wrapping ETH

In order for ETH to be tradable with other tokens it must first be converted to Wrapped ETH. The bot wraps 0.1 ETH to convert it to WETH for trading.

Wrapping ETH

const txHash = await rr.account.wrapEthAsync(new BigNumber('0.01'))
5

Setting Up A Book Subscription

Setup a websocket subscription for the ZRX/WETH book in order to listen to order events.

Setting up a Book Subscription

const subscription = zrxEthMarket.subscribe(WebsocketRequestTopic.BOOK, (mssg => {
  // handle a subscription event
});
6

Posting a Limit Order

Create an order on the ZRX/WETH book.

Posting a Limit Order

await zrxEthMarket.limitOrderAsync(UserOrderType.BUY,
  new BigNumber(quantity),
  new BigNumber(rate),
  new BigNumber(unixExpirationDate)
);
SDK

View SDK Documentation