Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This Gist is about Defi

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

  1. Binance IDE
  2. Metamask (connected to BSC Binance Smart Chain)

Connection Method

Open the MetaMask Little Fox and select the custom RPC option.

The input content is as follows:

Network Name: BSC Testnet


Chain ID: 97

Currency Symbol and Block Explorer URL are optional and can be left blank

Operating Procedures

  1. Click on the Binance Chain Docs case on the homepage of Binance IDE

  1. 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.

  1. 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.

  1. After clicking compile, you can see the compilation result in the output console.

  1. 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")

  1. 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.

  1. After Approve is completed, perform the pledge operation (fill in the amount you want to pledge).

  1. 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.

  1. 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.

Copy link

AliArshadswl commented Jan 8, 2021


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment