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 . 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 .
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 :
Now let’s swap by interacting with Kyber network contracts.
We will swap KNC (Kyber Network ) with OMG (OmiseGO ).
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 . 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 contract undercontracts → mockTokens . These are mock ERC20 . In production, you will need actual contract address for these 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 :
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 from our contract:
await kncInstance.approve(tradeInstance.address , web3.utils.toWei('100000'), )
We can now swap ! Let’s look at the Trade.sol method, which we will use to swap the :
function execSwap( ERC20 srcToken, uint srcQty, ERC20 destToken, address destAddress, uint maxDestAmount)
Here,
srcToken — source contract address
srcQty — amount of source
destToken — destination contract address
destAddress — address to send swapped to
maxDestAmount — address to send swapped 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 using Kyber Network protocol!
Let’s verify our 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 or ask questions in Kyber’s network . Kyber also has good . 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 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