SDK—v2.2.2

Last updated 26 days ago

The Radar Relay SDK is a software development kit that simplifies the interactions with Radar Relay’s APIs.

Installation

Using NPM

$ npm install @radarrelay/sdk

Using Yarn

$ yarn add @radarrelay/sdk

Setup & Initialize

Setup refers to the instantiation of the RadarRelay class and setup for the initialization lifecycle.

Initialize refers to the execution of the "initialization lifecycle" - A collection of asynchronous operations that hook up the wallet, set the web3 provider, and spin up the classes necessary to begin trading.

Setup and initialization of the SDK can be completed in a single call if you don't want to listen for initialization events.

import { SdkManager } from '@radarrelay/sdk';
const rr = await SdkManager.SetupAndInitializeAsync(Config); // Radar API and Wallet Configuration

Setup

Setup can be separated from initialization, which is useful if you would like to add listeners for the various initialization events.

import { SdkManager } from '@radarrelay/sdk';
const rr = SdkManager.Setup(Config); // Radar API and Wallet Configuration

Initialize

Initializing sets the desired Ethereum wallet configuration. The SDK can be initialized with three different wallet types: LightWallet, InjectedWallet, and an RpcWallet. See the below types for more information.

await SdkManager.InitializeAsync(rr);

Or directly on the SDK instance:

await rr.initializeAsync();

Wallet Configuration Types

interface SdkConfig {
sdkInitializationTimeoutMs?: number;
}
interface EndpointConfig {
radarRestEndpoint: string;
radarWebsocketEndpoint: string;
}
// Injected Wallets do not require an endpoint argument if using
// the wallet's connection to the Ethereum network
export interface OptionalEndpointConfig {
radarRestEndpoint?: string;
radarWebsocketEndpoint?: string;
}
interface EthereumConfig {
defaultGasPrice?: BigNumber;
}
interface LightWalletOptions {
password: string;
seedPhrase?: string;
salt?: string;
hdPathString?: string;
}
interface LightWalletConfig extends SdkConfig, EndpointConfig, EthereumConfig {
wallet: LightWalletOptions; // Wallet options for a local HD wallet
dataRpcUrl: string; // The rpc connection used to broadcast transactions and retreive Ethereum chain state
}
interface InjectedWalletConfig extends SdkConfig, OptionalEndpointConfig, EthereumConfig {
type: InjectedWalletType;
web3?: Web3; // Injected web3 object (Default: window.web3)
dataRpcUrl?: string; // Rpc connection used to broadcast transactions and retreive Ethereum chain state (Default: Injected Web3 Connection)
}
interface RpcWalletConfig extends SdkConfig, EndpointConfig, EthereumConfig {
rpcUrl: string; // The RPC connection to an unlocked node
}

Events

Anything that triggers state changes (like changing the network, or a fill) fires an event that you can listen to via the events object.

rr.events.on(
EventName.Loading |
EventName.EthereumInitialized |
EventName.EthereumNetworkIdInitialized |
EventName.ZeroExInitialized |
EventName.TokensInitialized |
EventName.AccountInitialized |
EventName.TradeInitialized |
EventName.MarketsInitialized |
EventName.TransactionPending |
EventName.TransactionComplete |
EventName.AddressChanged
)
rr.events.emit('see_above' | 'or emit anything', ...withSomeData)

Setting up an Ethereum Node

Install Parity

brew install parity

Run Parity Node

parity --jsonrpc-hosts=all \
--jsonrpc-interface=all \
--ws-origins=all \
--ws-hosts=all \
--ws-interface=all \
--chain=kovan \
--base-path /path/for/ethereum_node_data

Unlocked Parity Node

This is potentially dangerous, use at your own risk. Should be done on a computer free of malware and a strict firewall.

Create a trading Account

~parity account new
~enter password (don't lose this)

Create Node Config File

create /path/to/parity-config.toml

[account]
unlock = ["0x000000000000000000000000000000000000"] (account address created above)
password = ["/home/{account}/.parity-account-pass"] (password saved in plain text)

Run Parity with Unlocked Account

parity --jsonrpc-hosts=all \
--jsonrpc-interface=all \
--ws-origins=all \
--ws-hosts=all \
--ws-interface=all \
--chain=kovan \
--config /path/to/parity-config.toml \
--base-path /path/for/ethereum_node_data

SDK Init / State Change Lifecycle

Overview

The Ethereum and 0x.js application loading lifecycle is difficult to manage, especially when designing for optimized state changes. Specifically, changing RPC Networks, switching accounts, and updating API endpoints. To manage the lifecycle more efficiently, the Radar Relay SDK utilizes a combination of the following:

The SdkInitLifeCycle class works as follows:

Define an array that consists of:

  1. event, which when triggered will then call the defined

  2. func the function that is called when this event is triggered (ideally the next in priority)

Once all events have fired the promise will resolve. If an error occurs along the lifecycle, the timeout will occur after 10s and reject the promise.

Each init method must trigger an event on the EventEmitter, which indicates the method is done as well as return the SdkInitLifeCycle.promise()

Life Cycle