[go: up one dir, main page]

0% found this document useful (0 votes)
83 views4 pages

Pragma Solidity For Flash Loans

The document outlines a Solidity smart contract for a Flash Loan Provider that allows users to borrow ERC-20 tokens on the Ethereum blockchain. It includes functionalities for adding/removing supported tokens, executing flash loans, swapping tokens, and managing liquidity through the Uniswap protocol. The contract also implements a fee structure and ensures safe operations using a reentrancy guard.

Uploaded by

prish4life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

Pragma Solidity For Flash Loans

The document outlines a Solidity smart contract for a Flash Loan Provider that allows users to borrow ERC-20 tokens on the Ethereum blockchain. It includes functionalities for adding/removing supported tokens, executing flash loans, swapping tokens, and managing liquidity through the Uniswap protocol. The contract also implements a fee structure and ensures safe operations using a reentrancy guard.

Uploaded by

prish4life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Pragma Solidity for Flash Loans – Ethereum Based Tokens

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";

interface IFlashLoanReceiver {
function executeOperation(address token, uint256 amount, uint256 fee, address initiator)
external;
}

contract FlashLoanProvider is ReentrancyGuard {


uint256 public feeRate; // Fee rate in basis points (e.g., 50 = 0.5%)
mapping(address => bool) public supportedTokens; // Track supported ERC-20 tokens
IUniswapV2Router02 public uniswapRouter;

event FlashLoanExecuted(address borrower, address token, uint256 amount, uint256 fee);

constructor(uint256 _feeRate, address _uniswapRouter) {


feeRate = _feeRate;
uniswapRouter = IUniswapV2Router02(_uniswapRouter);
}

function addSupportedToken(address token) external {


supportedTokens[token] = true;
}

function removeSupportedToken(address token) external {


supportedTokens[token] = false;
}

function flashLoan(address token, uint256 amount) external nonReentrant {


require(supportedTokens[token], "Token not supported");
require(amount > 0, "Loan amount must be greater than zero");

IERC20 erc20 = IERC20(token);


uint256 balanceBefore = erc20.balanceOf(address(this));
require(balanceBefore >= amount, "Insufficient liquidity");

uint256 fee = (amount * feeRate) / 10000; // Calculate fee


uint256 totalRepayment = amount + fee;

erc20.transfer(msg.sender, amount);
IFlashLoanReceiver(msg.sender).executeOperation(token, amount, fee, msg.sender);

require(erc20.balanceOf(address(this)) >= balanceBefore + fee, "Loan not repaid");

emit FlashLoanExecuted(msg.sender, token, amount, fee);


}

function swapToken(address tokenIn, address tokenOut, uint256 amountIn) external


returns (uint256 amountOut) {
require(supportedTokens[tokenIn] && supportedTokens[tokenOut], "Tokens not
supported");
IERC20(tokenIn).approve(address(uniswapRouter), amountIn);

address[] memory path = new address[](2);


path[0] = tokenIn;
path[1] = tokenOut;

uint256[] memory amounts = uniswapRouter.swapExactTokensForTokens(


amountIn,
1,
path,
address(this),
block.timestamp
);
return amounts[1];
}

function addLiquidity(address tokenA, address tokenB, uint256 amountA, uint256


amountB) external {
IERC20(tokenA).approve(address(uniswapRouter), amountA);
IERC20(tokenB).approve(address(uniswapRouter), amountB);
uniswapRouter.addLiquidity(
tokenA,
tokenB,
amountA,
amountB,
1,
1,
address(this),
block.timestamp
);
}

function removeLiquidity(address tokenA, address tokenB, uint256 liquidity) external {


address pair = IUniswapV2Factory(uniswapRouter.factory()).getPair(tokenA, tokenB);
IERC20(pair).approve(address(uniswapRouter), liquidity);
uniswapRouter.removeLiquidity(
tokenA,
tokenB,
liquidity,
1,
1,
address(this),
block.timestamp
);
}
}
Pragma Solidity for Flash Loans – Solana Based Tokens

You might also like