Decentralized finance (Defi)
Hello everyone! this time we deploy a simple decentralized finance (Defi) smart contract project on the Binance Smart Chain (BSC) through ChainIDE. This project contains two ERC20-based tokens. We temporarily call these two tokens ChainToken and RevenueToken (hereinafter referred to as CT and RT). CT is used to simulate the digital currency held by users themselves and RT is the interest obtained after the user pledges CT. The workflow of the entire Defi project is executed by a Farm contract. The user pledges the digital currency, he/she holds into the Farm contract. The contract generates RT according to the amount of pledged by the user and returns it to the user as interest.
Tools used in the project
Open the MetaMask Little Fox and select the custom RPC option.
The input content is as follows:
Network Name: BSC Testnet
New RPC URL: https://data-seed-prebsc-1-s1.binance.org:8545/
Chain ID: 97
Currency Symbol and Block Explorer URL are optional and can be left blank
- Click on the Binance Chain Docs case on the homepage of Binance IDE
2. In the directory on the left, we have prepared the three smart contracts mentioned above. Among them, ChainToken and Revenue Token are smart contracts for issuing tokens based on ERC20. The contract declares the name of the digital currency, the total amount of issuance, verification, transfers, and third-party transfers. The code of the Farm contract implements the operational logic of our overall defi project. The stakeToken method is to pledge CT to this contract, untakeTokens is to take out the pledged digital currency, and issueToken is to generate RT interest based on the pledged CT.
3. Below we deploy these three contracts on the Binance Smart Chain, first select the compiler in the upper right corner, any version of 0.5.x.
4. After clicking compile, you can see the compilation result in the output console.
5. Deploy the contract (set value and wei). In this step, you need to deploy the three compiled contracts separately. In Compiled Contracts, first deploy ChainToken and RevenueToken, and finally pass in the addresses of the first two contracts when deploying the Farm contract.(the first two the address of the contract can be viewed in the output console, such as “contractAddress”: “0x79a377715E31D5F9eE736f8087aC0Ca230F8C48e”)
6. After all the contracts are deployed, the Farm contract implements the digital currency pledge function (stakeToken). The essence of this method is to call the TransferFrom function in the chainToken contract to transfer the chaintoken in the user account to the Farm contract. We need to have enough digital currency in the approve account.
7. After Approve is completed, perform the pledge operation (fill in the amount you want to pledge).
8. Next, the RevenueToken is transferred to the Farm contract to generate dividends, and the parameters are passed into the Farm contract address and the amount of digital currency that needs to be transferred.
9. The last step is to call the issueToken function in the Farm contract to generate dividends for users who have been pledged.
Thanks for reading this post. We hope this post will help you develop Defi apps in an easy way.