Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
4nibhal committed Jul 9, 2023
0 parents commit d52e781
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PRIVATE_KEY=
PROVIDER=
153 changes: 153 additions & 0 deletions abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
[
{
"inputs": [
{
"internalType": "contract VaultFactory",
"name": "_vaultFactory",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "receiver",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "fee",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "treasuryFee",
"type": "uint256"
}
],
"name": "FlashLoan",
"type": "event"
},
{
"inputs": [],
"name": "CALLBACK_SUCCESS",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "flashFee",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IERC3156FlashBorrower",
"name": "receiver",
"type": "address"
},
{
"internalType": "address",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "flashLoan",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "token",
"type": "address"
}
],
"name": "maxFlashLoan",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "vaultFactory",
"outputs": [
{
"internalType": "contract VaultFactory",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]
54 changes: 54 additions & 0 deletions contractExample.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

interface IERC20 {
function approve(address spender, uint256 amount) external returns (bool);
}

interface IERC3156FlashBorrower {
/**
* @dev Receive a flash loan.
* @param initiator The initiator of the loan.
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @param fee The additional amount of tokens to repay.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
* @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan"
*/
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external returns (bytes32);
}

/*
* FlashBorrowerExample is a simple smart contract that enables
* to borrow and returns a flash loan.
*/
contract FlashBorrowerExample is IERC3156FlashBorrower {
uint256 MAX_INT = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;

// @dev ERC-3156 Flash loan callback
function onFlashLoan(
// If compiler error, ignore it
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external override returns (bytes32) {

// Set the allowance to payback the flash loan
IERC20(token).approve(msg.sender, MAX_INT);

// Build your trading business logic here
// e.g., sell on uniswapv2
// e.g., buy on uniswapv3

// Return success to the lender, he will transfer get the funds back if allowance is set accordingly
return keccak256('ERC3156FlashBorrower.onFlashLoan');
}
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "testsepolia",
"version": "1.0.0",
"description": "Flash Loan Test",
"main": "testnet.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Anibhal",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.3",
"ethers": "5.7.2"
}
}
54 changes: 54 additions & 0 deletions testnet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Imports
const ethers = require("ethers");
require('dotenv').config();

// Load the ABI. The flashloan Provider interface can be found on
// https://docs.equalizer.finance/getting-started/how-do-i-borrow-a-flash-loan-a-deep-dive#a.2-flash-loan-provider-abi
const providerAbi = require("./abi.json");

// Private key of the account that will borrow a flash loan
// Important! Never share your private key with anyone!
const privateKey = process.env.PRIVATE_KEY;

const jsonRpcURL = process.env.PROVIDER;

// the address of the FlashLoan Receiver that implements the interface
// this are the addres for Sepolia Tesnet
// If you want to use a different network, you have to look for address here: https://docs.equalizer.finance/equalizer-deep-dive/smart-contracts

const flashLoanReceiverAddress = ''; // This is the address of the contract you have deployed to carry out the flash loan

const flashLoanProviderAddress = '0x0837b2aCcF87De52a099409863CDb5f3588342AD';

const tokenAddress = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9';

// can be extracted from vaultFactory with tokenAddress
const vaultOfToken = '0x08E0EBbb47029FF59fba430eAC5fFb74561232C8';

(async () => {
// init the provider
const provider = new ethers.providers.JsonRpcProvider(jsonRpcURL);
// init the wallet
const wallet = new ethers.Wallet(privateKey, provider);

// init the contract of flashloan provider with wallet connected to it
const flashLoanProviderContract = new ethers.Contract(flashLoanProviderAddress, providerAbi, wallet);
// const receiverContract = new ethers.Contract (flashLoanReceiverAddress, )
try {

// check max loan amount for the token
const maxAmount = await flashLoanProviderContract.maxFlashLoan(tokenAddress);
console.log(maxAmount);
// make sure u set allowance for the provider for the vault
// get a loan

console.log(await (await flashLoanProviderContract.flashLoan(flashLoanReceiverAddress, tokenAddress, 100, "0x", {
gasLimit: 1000000,
gasPrice: 1000000000
})).wait());

} catch (e) {
console.log(e);
// Error handling
}
})();

0 comments on commit d52e781

Please sign in to comment.