January 26, 2026

Capitalizations Index – B ∞/21M

Building with Kyber Network – QuikNode –

Building with Kyber Network – QuikNode –

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:

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

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:

kycBalance1
omgBalance1

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

await kncInstance.approve(tradeInstance.address , web3.utils.toWei('100000'), )

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)

Here,

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"), )

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
omgBalance2

You will see the change in balance.

Conclusion

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!! 😃

Published at Thu, 23 May 2019 07:04:04 +0000

Previous Article

Crypto Market Wrap: $14 Billion Blitzed as Bitcoin Beats a Retreat

Next Article

BTC Price Swings above $7600

You might be interested in …

_mg_7053

_MG_7053

_MG_7053The Peace Plus One – World Sustainability Project www.WorldSustainability.Org is interested in the opportunities afforded to young people in the new economy of Caring Currency. The currency of the 99%. The concept of Caring Currency […]