Defi ‘Yield Farming’:How to implement Defi Yield Farming with A Solidity .

Summary

Yield farming is a reward scheme that’s taken hold in the Defi crypto world over the last year.We have lived in traditional investing world until now and all peoples like yield on a bond or a dividend.

Yield farming(Yield + Farming) is a new term of crypto space and it is one of the latest trends that has rapidly forced its way into the decentralized (DeFi) world.According to CoinMarketCap data,total locked value of liquidity pools in yield farming projects exceeded $13 billion as of 10th May 2021(note that the statics are constantly updated).

In this article , I am not going to focus the detailed conception of the yield farming ,why It needs and gives awesome profits of yield,how yield farming works ,how risky it is and so on because there are many articles and the informations to read them anywhere. Instead of it, I am going to explain how to implement Yield Farming programming with the Solidity smart contract language to help the peoples who wants to make their own yield farming projects.

Requirements Of the Yield Farming

  • Liquidity providers
  • Liquidity pools (Smart contract)

First of all, it’s worth noting that to function, yield farming requires liquidity providers and liquidity pools.

1. Liquidity provider

To become a liquidity provider, all you have to do is to add your funds to a liquidity pool (smart contract), which is responsible for powering a marketplace where users carry out several procedures with their tokens, including borrowing, lending, and exchanging.We can think you are going to prepare a crop seeds simply now.

2. Liquidity pool (Smart contract)

This is the smart contract and It is responsible for powering a marketplace where users carry out several procedures with their tokens, including borrowing, lending, and exchanging.you’ve locked up your funds in pool, you will get fees that have been generated from the underlying DFi platform or reward tokens.

Yield Farming Implementation

  • Liquidity providing and Yield Farming
  • Metamask wallet
  • Binance Smart Chain
  • Prepare the some BNB,BUSD crypto currencies
  • Create your specific liquidity pool

Let’s build BNB/BUSD pool on the Binance Smart Chain network in this article.

  1. Liquidity providing and Yield Farming

We need to distingue the liquidity providing and yield farming correctly because we can reach out to mistaken conclusion with liquidity pool. Liquidity providing is exactly that , lending your money to a liquidity pool in return for a cut of the transaction fee profits.Yield farming is taking advantage of liquidity provider bonuses usually provided in a specific token for contributing to a specific pool during a specific time.

2.Metamask wallet

First of all, we are going to import the Metamask which is useful to develop the smart contract deployment and interact with the users. We need to install Chrome browser extension of the Metamask.

3. Binance Smart Chain

In the next step, we will switch the smart chain network because we are going to build pool in the binance blockchain platform. But the Metmask has not smart chain network status in the default when you import the Metamask into your browser.So we will import Binance smart chain network in the network setting of the Metamask wallet extension.

4. Prepare the some BNB ,BUSD crypto currencies

We are going to build BNB/BUSD pool now so I have put some BNB /BUSD already. Because I have created 0.3BNB/10BUSD pool in Dodo Defi platform , current picture only shows available BNB amount and the BUSD amount is zero.

5. Create specific liquidity pool

So we are going to create the specific liquidity pool now with BNB-BUSD pair.

Contracts

  • LP token contract
  • UnderlyingToken contract
  • LiquidityPool contract
  1. LP token contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0-solc-0.7/contracts/token/ERC20/ERC20.sol";
contract LPToken is ERC20{
constructor()ERC20('LP token','LPT'){}
}

2. UnderlyingToken contract

The BNB and BUSD is the underlying tokens to provide into the liquidity pool (BNB-BUSD).

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0-solc-0.7/contracts/token/ERC20/ERC20.sol";
contract UnderlyingToken is ERC20{
constructor()ERC20('LP token','LPT'){}function faucet(address to, uint256 amount) external{ _mint(to, amount);
}
}

3. LiquidityPool contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import LPToken './LPToken.sol';
import UnderlyingToken './UnderlyingToken';
contract LiquidityPool is LPToken { mapping(address => uint256) public checkpoints;
UnderlyingToken public underlyingToken;
constant public REWARD_PER_BLOCK = 1;
constructor(address _underlyingToken){
underlyingToken = UnderlyingToken(_underlyingToken);
}
// deposit function function deposit(uint256 _amount) external{ if(checkpoints[msg.sender] == 0){
checkpoints[msg.sender] = block.number;
}
distribute(msg.sender);
underlyigToken.transferFrom(msg.sender,address(this),_amount);
_mint(msg.sender,_amount);
}
// withdraw function

function withdraw(uint256 _amount) external{
require(balanceOf(msg.sender) > _amount,'not LP token withdarw');
distribute(msg.sender);
underlyingToken.transfer(msg.sender,_amount)
_burn(_amount);

}
// distribute reward function
function distribute(address beneficiary) external{
uint256 checkpoint = checkpoints[beneficiary];
require(block.number > checkpoint);
uint256 reward = balanceOf(beneficiary) *(block.number-checkpoint); checkpoints[beneficiary] = block.number;
}
}

Conclusion

We have built one liquidity pool of Yield farming until now with solidity. Yield farming is the latest trends of earning a passive income recently. In this article, I have mentioned how to build the yield farming simply with solidity.I will write second article for yield farming in next time. The second article will cover how to calculate yield farming return .Hopefully , this article should help the peoples who want to know how to configure the structure of yield farming with solidity. Thanks for reading my article in the end.

--

--

--

😀 Blockchain Specialist | Smart contract Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Galaxy Digital postpones the acquisition of BitGo until later in 2022

Galaxy Digital postpones the acquisition of BitGo until later in 2022

Could ICOs Fill The Void Of IPOs?

Utrust is changing invoicing with crypto for good

Introduction to Singular DAO

The number of active BTC addresses reached 22.3

Bitcoin At It’s Highest Price

Block by Block Weekly Newsletter #20

Why should Plan B’s S2F model work?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ding Tian

Ding Tian

😀 Blockchain Specialist | Smart contract Developer

More from Medium

Creaticles: Product Roadmap Update

The GooD Sleep DApp was officially released on January 18th.

HOW TO AGAVE : LONGING AND SHORTING

The GRAPH and Uniswap v3 Queries