Build a 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.

  1. Save the Kovan Network URL for the next step.

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).

  1. Head to Node.js and install node.

  2. If you're on Mac, you can install Node using Brew.

$ /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.

Clone the Repository

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

Bot Setup

1. Install Dependencies

Install the required dependencies with NPM or Yarn.

$ npm install
$ yarn install

2. Set a Password

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

$ export RADAR_WALLET_PASSWORD=yourpassword

3. Run the bot

Run the bot via NPM or Yarn.

$ npm start
$ yarn start

The Bot

1. SDK Initialization

Setup SDK

Use the SdkManager to setup the SDK. This will instantiate the RadarRelay class and start 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 SdkManager.InitializeAsync(...) to hook up the wallet, set the web3 provider, and spin up the classes necessary to begin trading. The simplest 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.

// 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.

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.

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.

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.

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

6. Posting a Limit Order

Create an order on the ZRX/WETH book.

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