Flash Loans User Guide

Learn how to use Vybe's first completed module, Vybe Flash Loans.

Flash Loans Model

Flash Loans are special uncollateralised loans that allow temporary borrowing of an asset, which return the borrowed amount at the end of the transaction.

Flash Loans are an advanced concept aimed at developers and traders. You must be comfortable with Ethereum, Solidity, smart contracts, and software engineering best practices to safely utilize Vybe Flash Loans.

For developers, a helpful model to understand the Vybe Flash Loan protocol is as follows:

  1. Your contract calls the Flash Loan Contract, requesting a Flash Loan of a certain amount.

  2. After sanity checks, Vybe's Flash Loan Contract transfers the requested amount from the Staking Pool to your contract, then calls on your contract to generate the minimum profit.

  3. Your contract, now holding the flash loaned amount, executes a developer-defined operation in its code. When your code has finished, Vybe's Flash Loan Contract transfers the loaned amount back to Staking Pool.

  4. The Flash Loan Contract compares the balance before and after your code execution, ensuring that the balance is correct and the same as before the operation was called.

    • If the balance before and after your code execution is correct, then the process continues and completes. At this point, the Flash Loan Contract burns 1% profit as a loan fee.

    • If the balance is incorrect, then the function rolls back, undoing any state changes your contract made.

All of the above happens in a single transaction.

Visual Flow Diagram

Below you can see how this process, in a successful operation, is represented visually:

Implementing Flash Loans

Step 1: We recommend you use Truffle for your implementation of Flash Loans.

Step 2: Clone the Vybe repository

$ git clone https://github.com/vybetoken/vybe

Step 3: Sign up for Infura (or a similar provider)

Step 4: Update truffle-config.js with your Ethereum account private key and Infura deploy web socket

Step 5: Install Node Package Manager

$ npm install

Step 6: Navigate to the flash loan Solidity contract

$ cd <git-root>/contracts
$ open VybeFlashLoan.sol

Step 7: Update _loanAmount with the amount of your loan.

uint256 private _loanAmount = 100;

Vybe is 18 decimals.

Step 8: Add your custom logic to generate >= 1% profit at line 35 in the loaned function. You implement this logic in VybeFlashLoan.sol.

To test out flash loans, you can use the Vybe Test Faucet (limited to 0.000000000000000001 Vybe). An example of a faucet flash loan is below

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import "./SafeMath.sol";
import "./Vybe.sol";
import "./VybeLoan.sol";
import "./IVybeBorrower.sol";
import "./VybeTestFaucet.sol";
contract VybeFlashLoan is IVybeBorrower {
using SafeMath for uint256;
address deployedVybeContract = address(0x3A1c1d1c06bE03cDDC4d3332F7C20e1B37c97CE9);
address deployedVybeLoanContract = address(0x382EE41496E0Bb88F046F2C0D1Cf894F8D272BD5);
address deployedVybeTestFaucet = address(0x18fe13de34349838e3D3F9cf3a48326864F22d3E);
Vybe private _VYBE;
VybeLoan private _loan;
VybeTestFaucet private _faucet;
// update amount value with your loan amount
uint256 private _loanAmount = 100;
constructor() {
// create vybe instance
_VYBE = Vybe(deployedVybeContract);
// create loan instance
_loan = VybeLoan(deployedVybeLoanContract);
// create faucet instance
_faucet = VybeTestFaucet(deployedVybeTestFaucet);
}
function executeLoan() external {
// Trigger loan
_loan.loan(_testLoanAmount);
}
function loaned(uint256 amount, uint256 owed) override external {
// Add custom logic here to generate >1% profit
_faucet.claim();
// approve loan
_VYBE.approve(address(_loan), owed);
}
}

Step 9: Navigate to V2_vybe_migration.js in migrations and update with the following:

let VybeFlashLoan = artifacts.require("VybeFlashLoan");
module.exports = async (deployer) => {
let flashLoan = await deployer.deploy(VybeFlashLoan);
let deployedContract = await VybeFlashLoan.deployed();
deployedContract.executeLoan();
};

Step 10: Execute truffle deploy --network mainnetin terminal to deploy flash loan and wait for network to deploy contract.

Note: If execution fails due to gas price, update gas amount in truffle-config.js.