RESTful API

Introduction

This guide will walk you through on how you can interact with our protocol implementation using our RESTful APIs. The most common group of users that can benefit from this guide are developers who have minimal smart contract experience, traders and wallets.

Risk Mitigation

There are some risks when utilising Kyber. To safeguard users, we kindly ask that you refer to the Slippage Rates Protection and Price Feed Security sections on what these risks are, and how to mitigate them.

Overview

We break this guide into 3 sections:

  1. Trading Tokens - This section will show the steps taken to execute a KNC -> DAI trade on Ropsten, as well as incorporating platform fees. The steps for performing token -> ether and ether -> token conversions are the same.

  2. Reserve Routing - This advanced section covers the reserve routing feature to include / exclude reserves, or to split trades amongst multiple reserves.

  3. Token Info & Price Data - This section covers how one can obtain token information and historical price data.

Things to note

  1. If the source token is not ETH (ie. an ERC20 token), the user is first required to call the /enabled_data endpoint to give an allowance to the smart contract executing the trade.

  2. Refer to the API overview for the test and mainnet network URLs to use.

  3. To prevent front running, the contract limits the gas price trade transactions can have. The transaction will be reverted if the limit is exceeded. To query for the maximum gas limit, check the public variable maxGasPrice.

Trading Tokens

Suppose we want to convert 100 KNC to DAI tokens on Ropsten, which is a token to token conversion. In addition, we want to charge a platform fee of 0.25%. Note that ETH is used as the base pair i.e. KNC -> ETH -> DAI.

The code example will also work for token -> ether and ether -> token conversions, by using 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee as the token address for ETH.

Import Relevant Packages

  • We use ethers for connecting to the Ethereum blockchain

  • The node-fetch module is used for making API queries

Connect to an Ethereum Node

ethers provides a very simple method getDefaultProvider to easily connect to the Ethereum blockchain. While not necessary, it is recommended to provide an API key for the various providers offered (Eg. Alchemy, Infura and Etherscan).

Define Constants

Next, we will define the constants that we will be using for this guide. This includes the following:

  • NETWORK_URL: Kyber's API Base URL

  • PRIVATE_KEY: The private key which we will be sending transactions from

  • PLATFORM_WALLET: The wallet address for which we can get commission fees from. Read more about platform fees here

  • PLATFORM_FEE: Platform fee amount to be charged, in basis points. Read more about platform fees here

  • SRC_TOKEN_ADDRESS: Ropsten KNC address

  • DEST_TOKEN_ADDRESS: Ropsten DAI address

  • SRC_QTY: 100 KNC tokens

  • GAS_PRICE: The gas price to use (affects the tx speed)

Check Token Support

We first have to check if the traded tokens are supported on Kyber. We make use of the /currencies endpoint, which returns basic information about all tokens supported on Kyber. Details about possible path parameters and output fields can be found here.

It is recommended to use the token contract address as the identifier instead of the token symbol, as multiple tokens may share the same symbol.

Check Source Token Approval

We use the /users/<user_address>/currencies endpoint to check whether the proxy contract has been approved for selling source tokens on behalf of the user. This endpoints returns a JSON of enabled statuses of ERC20 tokens for the given walletAddress. Details about the path parameters and output fields can be found here.

If the source token is not enabled for trading, querying the users/<user_address>/currencies/<currency_id>/enable_data endpoint returns a transaction payload needed to be signed and broadcasted by the user to enable the KyberNetwork contract to trade source tokens on his behalf. Details about the path parameters and output fields can be found here.

Get Destination Token Amount Receivable

Create a function to get an approximate of the destination token amount for the specified amount of source token. We will use the /quote_amount endpoint in this function. Details about the path parameters and output fields can be found here.

Note:

  • The rates via the API are cached, so the /quote_amount does not support reserve routing and platform fees. Those have to be accounted for separately.

Trade Execution

We now have all the required information to peform the trade transaction. Querying the /trade_data endpoint will return the transaction payload to be signed and broadcasted by the user to make the conversion. Details about the path parameters and output fields can be found here.

Tying Everything Together

The main function will combine the different functions together to obtain the conversion rate, check that conditions are met for the trade, and execute the trade.

Full Code Example

Before running this code example, the following fields need to be modified:

  1. Change INFURA_PROJECT_ID to your Infura Project ID.

  2. Change PRIVATE_KEY to the private key (with 0x prefix) of the Ethereum wallet holding Ether.

  3. Change PLATFORM_WALLET to a wallet address for platform fees.

Reserve Routing

Overview

In previous network versions, the hint parameter was used to filter permissionless reserves. With Katalyst, we utilise this parameter for routing trades to specific reserves.

There are 4 optional routing rules:

  1. BestOfAll - This is the default routing rule when no hint is provided, and is the classic reserve matching algorithm used by the Kyber smart contracts since the beginning.

  2. MaskIn (Whitelist) - Specify a list of reserves to be included and perform the BestOfAll routing on them

  3. MaskOut (Blacklist) - Specify a list of reserves to be excluded and perform the BestOfAll routing on the remaining reserves

  4. Split - Specify a list of reserves and their respective percentages of the total srcQty that will be routed to each reserve.

For token -> token trades, you can specify a routing rule for each half. For example, a MaskIn route can be used for the token -> ether side, while a Split route can be used for the ether -> token side.

Fetching Reserve Information

Query the /reserves endpoint to get a list of supporting reserves for a trade. Details about the path parameters and output fields can be found here.

The id return parameter will be useful for building hints.

Examples

Get reserves information for a WBTC -> ETH trade.

Get reserves information for a ETH -> KNC trade.

Building Hints

Querying the /hint endpoint will return data needed for the hint input parameter for the /trade_data endpoint. Details about the path parameters and output fields can be found here.

Examples

Build a KNC -> ETH MaskIn hint selecting the first reserve.

Build a ETH -> WBTC MaskOut hint excluding the first reserve.

Build a KNC -> WBTC hint with the following routes:

  • KNC -> ETH: Split trade among 2 reserves:

    • 1st reserve trades 70%, 2nd reserves trades 30%

  • ETH -> WBTC: BestOfAll trade

Using Hints

Pass in the built hint into the /trade_data endpoint.

Example

Get the parameters needed for a 100 KNC -> DAI trade with MaskIn reserve routing for KNC -> ETH.

Obtaining Token and Market Info

Basic Token Information

The /currencies endpoint returns basic information about all tokens supported on Kyber. Details about possible path parameters and output fields can be found here.

Example

Output

Token Price & Volume Information

The /market endpoint returns price and volume information on token to ETH pairs supported on Kyber. Details about possible path parameters and output fields can be found here.

Example

Output

Token/ETH and Token/USD Price Information

The /change24h endpoint returns current token to ETH and USD rates and price percentage changes against the previous day. Details about possible path parameters and output fields can be found here.

Example

Output

Last updated

Was this helpful?