更多文章见 yuque.com/jiangbo9510 jiangbo.near
更多文章见 yuque.com/jiangbo9510 jiangbo.near
Subscribe to jiangbo
Subscribe to jiangbo
Share Dialog
Share Dialog
<100 subscribers
<100 subscribers
所谓闪电贷,就是在一个区块内,完成借贷和还款操作,这种情况下,可以用来进行套利等操作。 闪电贷的基本步骤: 1、调用flashLoan接口,传入需要借贷的ERC20的token合约及其数量,
function flashLoan (
address _receiver, //接受借贷款的地址
address _reserve, //借贷的ERC20的地址
uint256 _amount, //借贷的数量
bytes calldata _params //回调的时候会传入的参数
) external;
2、发货后回调executeOperation
function executeOperation(
address _reserve, //借贷代币的地址
uint256 _amount, //借贷代币的数量
uint256 _fee, //借贷所需利息
bytes calldata _params //调用flashloan时传入的参数
)external
solidity的开发,版本控制需要注意。 https://github.com/aave/flashloan-box 这个库目前支持的是0.6.6及以上版本,0.7以下版本的solc编译器。这里除了需要重新选择solc版本外,还需要重新安装zp库。 npm uninstall @openzeppelin/contracts npm install @openzeppelin/contracts@3.4 也可以直接去node_module目录地下删除 @openzeppelin目录。 编译器和oz库的对应关系如下 序号 Solidity OpenZeppelin 1 0.5.x 2.3.x ~ 2.5.x 2 0.6.x 3.0.x ~ 3.1.x 或 3.4 3 0.7.x 3.2.x ~ 3.3.x 或 3.4 4 0.8.x 4.0.x ~ 4.5.x
需要在代码路径下,clone flash-loan的库 git clone https://github.com/aave/flashloan-box.git 完整代码如下:
// SPDX-License-Identifier: UNLICENSED
pragma solidity >0.6.6;
import { FlashLoanReceiverBase } from "./flashloan-box/contracts/aave/FlashLoanReceiverBase.sol";
import { ILendingPoolAddressesProvider } from "./flashloan-box/contracts/aave/ILendingPoolAddressesProvider.sol";
import { ILendingPool } from "./flashloan-box/contracts/aave/ILendingPool.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
//基于FlashLoanReceiverBase实现的
contract MyFlashLoan is FlashLoanReceiverBase{
event CurrBalance(uint256 data);
//初始化flashloan的资金池,aave v版本的测试网地址 https://docs.aave.com/developers/v/1.0/deployed-contracts/deployed-contract-instances
constructor(address pool) FlashLoanReceiverBase(pool) public payable {
}
function executeOperation(
address _reserve,
uint256 _amount,
uint256 _fee,
bytes calldata _params
)external override
{
uint256 balance = getBalanceInternal(address(this), _reserve);
require(_amount <= balance, "Invalid balance, was the flashLoan successful?"); emit CurrBalance(balance);
uint totalDebt = _amount.add(_fee); //计算回退的本息
transferFundsBackToPoolInternal(_reserve, totalDebt); //回退本息
}
function myFirstFlashLoanCall(address _asset) public onlyOwner {
bytes memory params = "";
uint amount = 1 ether;
ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());
lendingPool.flashLoan(address(this), _asset, amount, params); //调用借贷的接口
}
}
这里以DAI为例,选择Kovan测试网 传入的pool地址是 0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5 传入的DAI的合约地址是 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD

这里有一点需要特别注意,需要提前给合约地址转入一定的DAI代币,因为这里还本息的时候,会超过贷款的金额,合约的DAI余额为0的话,会导致还款失败,进而整个交易失败,可以去Uniswap上换一点DAI。
这里有一个交易成功的例子 https://kovan.etherscan.io/tx/0x21addc5056bd7317d3dd2d9b13f59b7a0640b30683d7901ec8a3982d03456aea
所谓闪电贷,就是在一个区块内,完成借贷和还款操作,这种情况下,可以用来进行套利等操作。 闪电贷的基本步骤: 1、调用flashLoan接口,传入需要借贷的ERC20的token合约及其数量,
function flashLoan (
address _receiver, //接受借贷款的地址
address _reserve, //借贷的ERC20的地址
uint256 _amount, //借贷的数量
bytes calldata _params //回调的时候会传入的参数
) external;
2、发货后回调executeOperation
function executeOperation(
address _reserve, //借贷代币的地址
uint256 _amount, //借贷代币的数量
uint256 _fee, //借贷所需利息
bytes calldata _params //调用flashloan时传入的参数
)external
solidity的开发,版本控制需要注意。 https://github.com/aave/flashloan-box 这个库目前支持的是0.6.6及以上版本,0.7以下版本的solc编译器。这里除了需要重新选择solc版本外,还需要重新安装zp库。 npm uninstall @openzeppelin/contracts npm install @openzeppelin/contracts@3.4 也可以直接去node_module目录地下删除 @openzeppelin目录。 编译器和oz库的对应关系如下 序号 Solidity OpenZeppelin 1 0.5.x 2.3.x ~ 2.5.x 2 0.6.x 3.0.x ~ 3.1.x 或 3.4 3 0.7.x 3.2.x ~ 3.3.x 或 3.4 4 0.8.x 4.0.x ~ 4.5.x
需要在代码路径下,clone flash-loan的库 git clone https://github.com/aave/flashloan-box.git 完整代码如下:
// SPDX-License-Identifier: UNLICENSED
pragma solidity >0.6.6;
import { FlashLoanReceiverBase } from "./flashloan-box/contracts/aave/FlashLoanReceiverBase.sol";
import { ILendingPoolAddressesProvider } from "./flashloan-box/contracts/aave/ILendingPoolAddressesProvider.sol";
import { ILendingPool } from "./flashloan-box/contracts/aave/ILendingPool.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
//基于FlashLoanReceiverBase实现的
contract MyFlashLoan is FlashLoanReceiverBase{
event CurrBalance(uint256 data);
//初始化flashloan的资金池,aave v版本的测试网地址 https://docs.aave.com/developers/v/1.0/deployed-contracts/deployed-contract-instances
constructor(address pool) FlashLoanReceiverBase(pool) public payable {
}
function executeOperation(
address _reserve,
uint256 _amount,
uint256 _fee,
bytes calldata _params
)external override
{
uint256 balance = getBalanceInternal(address(this), _reserve);
require(_amount <= balance, "Invalid balance, was the flashLoan successful?"); emit CurrBalance(balance);
uint totalDebt = _amount.add(_fee); //计算回退的本息
transferFundsBackToPoolInternal(_reserve, totalDebt); //回退本息
}
function myFirstFlashLoanCall(address _asset) public onlyOwner {
bytes memory params = "";
uint amount = 1 ether;
ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());
lendingPool.flashLoan(address(this), _asset, amount, params); //调用借贷的接口
}
}
这里以DAI为例,选择Kovan测试网 传入的pool地址是 0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5 传入的DAI的合约地址是 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD

这里有一点需要特别注意,需要提前给合约地址转入一定的DAI代币,因为这里还本息的时候,会超过贷款的金额,合约的DAI余额为0的话,会导致还款失败,进而整个交易失败,可以去Uniswap上换一点DAI。
这里有一个交易成功的例子 https://kovan.etherscan.io/tx/0x21addc5056bd7317d3dd2d9b13f59b7a0640b30683d7901ec8a3982d03456aea
No activity yet