Published in



Building with Kyber Network

For better liquidity and enhanced Token utility!

What is the Kyber Network?

There are tons of tokens out there, yet they all strive for maxium liquidity and utility. For example, if you have token ‘X’, then you can use this token on its ‘X’ platform. This limits the token’s usage! The Kyber network solves this by creating a token reserve so you can swap tokens in real-time. So now if a website accepts DAI for payments, but you have only OMG, you can pay with OMG, which will get swapped in real-time to DAI.

The Kyber Protocol brings token inventories and prices on-chain, hence allowing developers to directly embed value exchanges into their smart-contracts without any technical or security overhead.

This allows for:

  • Instant confirmation
  • Operation certainty (there is no transactional risk, settlement uncertainty or counterparty risk)
  • Global and diverse pool of different tokens

Swap Tokens using Kyber Network

We will go through the Kyber network workshop demo and swap tokens using Kyber network smart-contracts.

Set up:

  • Install NodeJs
  • Install Ganache-Cli
sudo npm install -g ganache-cli
  • Install Truffle
sudo npm install -g truffle@latest

Clone Repository:

git clone https://github.com/KyberNetwork/workshop.git

Repository Overview

Let’s see what we’ve got in the repository:

config — Configs used by Kyber contracts for “migrations”. These configs are parameters used by contracts once deployed.

contracts — Kyber network contracts, examples and some mock token and contracts for testing purposes.

examples —This is a key directory which we will use throughout this tutorial. This contains the Kyber network example.

  • Truffle — This contains a JS file which we can use with truffle-cli or in a Truffle project.
  • Web3 — This contains normal JS files, which you can with node command
  • Solidity — A contract written in Solidity, which we will use with Truffle console.

migrations — Contains migration script to set up and deploy Kyber network contracts.

Kyber network set up and contract migrations:

Now we will set up Kyber network contracts.

We will use a predefined mnemonic, so we don’t need to configure addresses for the contract and for our wallet. Run this command to start Ganache:

ganache-cli --accounts 10 --defaultBalanceEther 1000 --mnemonic 'gesture rather obey video awake genuine patient base soon parrot upset lounge' --networkId 5777 --debug

Run Migration:

Before starting, we need to deploy all of our contracts. For that we need to run the migration:

truffle migrate --network development

This will take some time…

We are using a predefined mnemonic, so you will get the same addresses defined below:

Running Truffle example:

Let's run an example from truffle directory.

truffle exec examples/truffle/getExpectedRate.js

This will give us the exchange rates but with different configured tokens.

Running Web3 example:

Let’s get the exchange rate again — but this time, we will use NodeJs.

cd examples/web3
node getExpectedRate.js

Using solidity contract for swapping token:

Now let’s swap tokens by interacting with Kyber network contracts.

We will swap KNC (Kyber Network Token) with OMG (OmiseGO Token).

Remember, you can do the same with using Web3 and Truffle examples, but running the test with Truffle console is a lot easier and interactive. So let’s get truffle console:

truffle console

Our user address is 0x47a793D7D0AA5727095c3Fe132a6c1A46804c8D2 — this we will use again, so let’s assign it to a variable:

let userWallet = '0x47a793D7D0AA5727095c3Fe132a6c1A46804c8D2'

We have a Trade.sol in our Solidity folder, which we help us in swapping the token. You can check out the code for the contract: examples > solidity > Trade.sol.

Now, we need to get an instance for that Contract:

let tradeInstance = await Trade.at(Trade.address)

Next, as we are going to swap KNC with OMG, let's get the Instance for both contracts. You can find these token contract undercontracts → mockTokens . These are mock ERC20 tokens. In production, you will need actual contract address for these tokens on ETH MainNet.

let kncInstance = await KyberNetworkCrystal.at(KyberNetworkCrystal.address)let omgInstance = await OmiseGo.at(OmiseGo.address)

Now, let’s get our balance for each token:

let kycBalance1 = (await kncInstance.balanceOf(userWallet)).toString()let omgBalance1 = (await omgInstance.balanceOf(userWallet)).toString()

You can check these balance by running:


Now, we need to give Trade contract approval to withdraw tokens from our contract:

await kncInstance.approve(tradeInstance.address , web3.utils.toWei('100000'), {from : userWallet})

We can now swap tokens! Let’s look at the Trade.sol method, which we will use to swap the tokens:

function execSwap( ERC20 srcToken, uint srcQty, ERC20 destToken, address destAddress, uint maxDestAmount)


srcToken — source token contract address

srcQty — amount of source tokens

destToken — destination token contract address

destAddress — address to send swapped tokens to

maxDestAmount — address to send swapped tokens to

So let’s call the method accordingly:

tradeInstance.execSwap(kncInstance.address, web3.utils.toWei("100") , omgInstance.address,userWallet, web3.utils.toWei("1000000"), {from: userWallet})

If you have followed the tutorial ‘till now, you have successfully swapped two tokens using Kyber Network protocol!

Let’s verify our token balance again:

let kycBalance2 = (await kncInstance.balanceOf(userWallet)).toString()let omgBalance2 = (await omgInstance.balanceOf(userWallet)).toString()kycBalance2

You will see the change in balance. Here is a video demo for the above tutorial.


If you faced any problem you can check here or ask questions in Kyber’s network telegram group. Kyber also has good documentation. So, what are you waiting for? Integrate your Dapp with Kyber network!

Let us know what you want to learn about in the comment section.👇

About QuikNode

QuikNode is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers & companies, helping scale dApps and providing high-performance Ethereum nodes. We’re working on something interesting from the past few months and will be launching soon, so subscribe our newsletter for more updates!! 😃




A platform to access ETH, BTC, BSC, MATIC, OPTIMISM, ARBITRUM, FTM, CELO, SOL, TERRA & xDAI blockchains via API. Infra & tools to build + scale your #Web3 dApp!

Recommended from Medium

React Redux tutorials for beginners-4

30 Programming Jokes That Will Make You Laugh

API Project

Ditsmod — new NodeJS webframework writen in TypeScript

Implementing Dynamic reCaptcha Vue plugin

About WebRTC

How to write an upgradeable Solidity Smart Contract for a react dapp

Typescript Microservice Project: From 0 to 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Agrawal

Gaurav Agrawal

Telegram @gaurav_zen , Editor — Coinmonks publication (medium.com/coinmonks) and working on (https://coincodecap.com)

More from Medium

How Octopus Network Built A Web 3.0 Incubator

My impressions of Moxie’s first impression of web3 (Part 2)

Deploying a Dapp to Mainnet