RESTful API

circle-exclamation

Introductionarrow-up-right

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 Mitigationarrow-up-right

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

We break this guide into 3 sections:

  1. Trading Tokensarrow-up-right - 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 Routingarrow-up-right - This advanced section covers the reserve routing feature to include / exclude reserves, or to split trades amongst multiple reserves.

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

Things to notearrow-up-right

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

  2. Refer to the API overviewarrow-up-right 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 Tokensarrow-up-right

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 Packagesarrow-up-right

  • We use ethers for connecting to the Ethereum blockchain

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

Connect to an Ethereum Nodearrow-up-right

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 Constantsarrow-up-right

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 herearrow-up-right

  • PLATFORM_FEE: Platform fee amount to be charged, in basis points. Read more about platform fees herearrow-up-right

  • 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 Supportarrow-up-right

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 herearrow-up-right.

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 Approvalarrow-up-right

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 herearrow-up-right.

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 herearrow-up-right.

Get Destination Token Amount Receivablearrow-up-right

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 herearrow-up-right.

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 Executionarrow-up-right

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 herearrow-up-right.

Tying Everything Togetherarrow-up-right

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 Examplearrow-up-right

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 Routingarrow-up-right

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 Informationarrow-up-right

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 herearrow-up-right.

The id return parameter will be useful for building hints.

Examplesarrow-up-right

Get reserves information for a WBTC -> ETH trade.

Get reserves information for a ETH -> KNC trade.

Building Hintsarrow-up-right

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 herearrow-up-right.

Examplesarrow-up-right

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

Pass in the built hint into the /trade_data endpoint.

Examplearrow-up-right

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

Obtaining Token and Market Infoarrow-up-right

Basic Token Informationarrow-up-right

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

Examplearrow-up-right

Outputarrow-up-right

Token Price & Volume Informationarrow-up-right

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 herearrow-up-right.

Examplearrow-up-right

Outputarrow-up-right

Token/ETH and Token/USD Price Informationarrow-up-right

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 herearrow-up-right.

Examplearrow-up-right

Outputarrow-up-right

Last updated

Was this helpful?