vProtocol Docs
  • Introduction
    • Welcome To vProtocol
    • Protocol Overview
    • Key Features and Benefits
    • Use Case
    • Ecosystem Partners
  • USER GUIDE
    • For Lenders
      • Supply Assets
    • For Borrower
      • Depositing Collateral
      • Borrowing From The Lending Pool
    • For Trader & Developers
      • Using FlashLoans
      • Example Implementation
  • Protocol Architecture
    • Diamond Standard Implementation
    • Core Component
    • Smart Contract Structure
Powered by GitBook
On this page
  1. USER GUIDE
  2. For Trader & Developers

Example Implementation

Here's a basic example of a flash loan receiver for arbitrage:

contract ArbitrageFlashLoanReceiver is IFlashLoanReceiver {
    address public immutable fiveProtocol;
    
    constructor(address _fiveProtocol) {
        fiveProtocol = _fiveProtocol;
    }
    
    function executeOperation(
        address[] calldata tokens,
        uint256[] calldata amounts,
        uint256[] calldata fees,
        address initiator,
        bytes calldata params
    ) external override returns (bool) {
        // Ensure caller is the Five Protocol
        require(msg.sender == fiveProtocol, "Unauthorized");
        require(initiator == address(this), "Unauthorized initiator");
        
        // Perform arbitrage operations here
        // ...
        
        // Approve repayment of flash loan + fee
        for (uint i = 0; i < tokens.length; i++) {
            uint amountOwed = amounts[i] + fees[i];
            IERC20(tokens[i]).approve(fiveProtocol, amountOwed);
        }
        
        return true;
    }
    
    function executeArbitrage(
        address[] calldata tokens,
        uint256[] calldata amounts
    ) external {
        // Call flash loan
        IFiveProtocol(fiveProtocol).flashLoan(
            address(this),
            tokens,
            amounts,
            bytes("")
        );
    }
}

Introduction

PreviousUsing FlashLoansNextDiamond Standard Implementation

Last updated 2 months ago