How to Use DAI for Your Smart Contract – Filip Sundgren – Medium
DAI is a stablecoin, which is backed by collaterals on the Maker platform, with the aim to always be equal to $1. Looking at the price history of DAI, it has impressively traded around $1 even at times when 70% of the whole crypto market has disappeared. There has been some spikes, but they are always short and quite soon the price returns to $1.
There’s around $300 million worth of ETH inside the CDP (collateralised debt position), which makes MakerDAO (the whole ecosystem which maintains the CDP and makes the DAI work). I won’t explain how their economical incentive works and how they always manage to keep the price at $1 here, but let’s say that they give traders reason to always stabilise the price if it goes over/under $1. If you’re interested in this, read more here.
How to Use DAI
The reason why you’d want to use a Stablecoin is to eliminate the currency risk of your smart contract (or if you’re a trader, you might want to hedge away some risk of your portfolio). Say that you have a bounty contract where 100 ETH will be paid out in a time of 6–12 months. That’s a long time in a volatile crypto world, and it could be smart to exchange the ethers in that smart contract to DAI until it’s time to withdraw from the contract. But how do you do that?
DAI is living on Mainnet and on Kovan, and I’ll talk here about Kovan so you can test it before wasting any real money when you’re making something wrong.
Here you can find the contract https://kovan.etherscan.io/address/0x4a6bc4e803c62081ffebcc8d227b5a87a58f1f8f#contracts (on mainnet: https://etherscan.io/address/0x39755357759ce0d7f32dc8dc45414cca409ae24e#contracts).
Here you want to call the functions inside the contract. buyAllAmount and sellAllAmount will help you to buy/sell ETH to DAI. So you only need to get the price you’re trading at. pay_gem and buy_gem are the two exchanging tokens, which on Kovan are WETH and DAI.
On Kovan, these are the addresses:
Don’t forget to write an interface for your contract so it can actually find the functions of the DAI contract. Something like this:
If you look at their documentation however, you can also make your dApp interact with Maker by using something like Infura. Play around with it and read the documentation.