Flashloan
Flash loan 是特殊的无抵押贷款,只要在交易结束前归还借入的金额(和费用),就可以借入资产。现实世界中没有和闪电贷相似的示例,因此它需要对区块链中的区块内的状态管理有一些基本的了解。
闪电贷是针对开发人员的高级概念。必须对以太坊、编程和智能合约有很好的了解才能利用他们
Overview
对于开发人员,在开发解决方案时,可以考虑以下有用的心智模型:
- 您的合约通过调用
LendingPool
合约的flashLoan()
函数,申请一定数量的储备金进行闪电贷。 - 经过健全性检查后,
LendingPool
会将所请求的储备金转入您的合约,并调用您的合约或您指定的接收者合约的executeOperation()
函数。 - 您的合约在持有闪电贷资金后,可以执行任意的操作。
- 如果是“传统”闪电贷,执行完操作后,您需要将借贷的金额归还给
LendingPool
。LendingPool
更新储备金的相关数据,并扣除借贷金额加上手续费。- 与 v1 不同,v1 需要主动将资金推回
LendingPool
,而 v2 则是LendingPool
主动拉取资金。
- 与 v1 不同,v1 需要主动将资金推回
- 如果无法偿还(因余额不足或未授权),交易将回滚。
- 如果是借债型闪电贷(参见
flashLoan()
函数的模式参数),将会产生债务。
- 如果是“传统”闪电贷,执行完操作后,您需要将借贷的金额归还给
- 上述所有操作都在一个交易中完成,发生在同一个以太坊区块内。
闪电贷的申请
Aave 闪电贷已经与 Aave V2 一起广泛用于交换和/或迁移头寸。其他在野的例子包括:
- 资产之间的套利,无需本金即可执行套利。Example:ArbitrageDAO
- swap 贷款头寸的抵押品,无需偿还头寸的债务。Example:Collateral Swap, DeFiSaver.
- 其他示例: here and here.
闪电贷费用
闪电贷费用目前是 0.09%,可通过正常的治理流程进行更改。要获取当前值,请在LendingPool
合约上调用FLASHLOAN_PERMIUM_TOTAL()
。
Step by Step
Setting up
接受闪电贷的合约必须通过实现相关的 executeOperation()
函数来符合 IFlashLoanReceiver
接口。在下面的示例中,我们从 IFlashLoanReceiverBase
继承,它符合 IFlashLoanReceiver()
另外需要注意:由于欠款将由我们的合约中提取,因此我们的合约必须向 LendingPool
授权相关的allowance
,以提供这些资金来偿还闪电贷债务以及费用
pragma solidity 0.6.12;
import { FlashLoanReceiverBase } from "./FlashLoanReceiverBase.sol";
import { ILendingPool } from "./ILendingPool.sol";
import { ILendingPoolAddressesProvider } from "./ILendingPoolAddressesProvider.sol";
import { IERC20 } from "./IERC20.sol";
/**
!!!
Never keep funds permanently on your FlashLoanReceiverBase contract as they could be
exposed to a 'griefing' attack, where the stored funds are used by an attacker.
!!!
*/
contract MyV2FlashLoan is FlashLoanReceiverBase {
/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
)
external
override
returns (bool)
{
//
// This contract now has the funds requested.
// Your logic goes here.
//
// At the end of your logic above, this contract owes
// the flashloaned amounts + premiums.
// Therefore ensure your contract has enough to repay
// these amounts.
// Approve the LendingPool contract allowance to *pull* the owed amount
for (uint i = 0; i < assets.length; i++) {
uint amountOwing = amounts[i].add(premiums[i]);
IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
}
return true;
}
}
Call flashLoan()
要在LendingPool
上调用 flashLoan()
,我们需要传入相关参数。有三种方法可以实现这一点
从 EOA 账户
要使用 EOA 账户,需要使用 flashLoan()
函数将交易发送到相关的 LendingPool
。有关参数详细信息,参阅 flashLoan 函数文档,确保将步骤一中的合约地址作用于 receiverAddress
从其他合约
与上述从 EOA 发送交易类似,请确保 receiverAddress
是第一部中的合约地址。
来自同一合约
如果要使用与步骤一中相同的合约,请在 flashLoan()
函数中对 receiverAddress()
参数使用address(this)
。
下面的示例使用了第三种情况,其中 executeOperation()
在同一个合约中调用 LendingPool
上的flashLoan()
切勿将资金永久保存在 FlashLoanReceiverBase
合约上,因为他们可能会受到“恶意破坏”攻击,其中存储的资金被攻击者使用
pragma solidity 0.6.12;
import { FlashLoanReceiverBase } from "./FlashLoanReceiverBase.sol";
import { ILendingPool } from "./ILendingPool.sol";
import { ILendingPoolAddressesProvider } from "./ILendingPoolAddressesProvider.sol";
import { IERC20 } from "./IERC20.sol";
/**
!!!
Never keep funds permanently on your FlashLoanReceiverBase contract as they could be
exposed to a 'griefing' attack, where the stored funds are used by an attacker.
!!!
*/
contract MyV2FlashLoan is FlashLoanReceiverBase {
/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
)
external
override
returns (bool)
{
//
// This contract now has the funds requested.
// Your logic goes here.
//
// At the end of your logic above, this contract owes
// the flashloaned amounts + premiums.
// Therefore ensure your contract has enough to repay
// these amounts.
// Approve the LendingPool contract allowance to *pull* the owed amount
for (uint i = 0; i < assets.length; i++) {
uint amountOwing = amounts[i].add(premiums[i]);
IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
}
return true;
}
function myFlashLoanCall() public {
address receiverAddress = address(this);
address[] memory assets = new address[](2);
assets[0] = address(INSERT_ASSET_ONE_ADDRESS);
assets[1] = address(INSERT_ASSET_TWO_ADDRESS);
uint256[] memory amounts = new uint256[](2);
amounts[0] = INSERT_ASSET_ONE_AMOUNT;
amounts[1] = INSERT_ASSET_TWO_AMOUNT;
// 0 = no debt, 1 = stable, 2 = variable
uint256[] memory modes = new uint256[](2);
modes[0] = INSERT_ASSET_ONE_MODE;
modes[1] = INSERT_ASSET_TWO_MODE;
address onBehalfOf = address(this);
bytes memory params = "";
uint16 referralCode = 0;
LENDING_POOL.flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
params,
referralCode
);
}
}
完成闪电贷
一旦我们对闪电贷资产执行了逻辑(在 executeOperation()
函数中),如果我们在modes
参数中对任意资产使用了 mode=0
,则需要偿还闪电贷金额。
偿还闪电贷资产
确保我们的合约有相应的 amount + fee 来偿还借出的资产。我们可以通过传递给 executeOperation()
函数的 amounts
和 premiums
数字种相关条目的综合来计算这一点。
我们无需将欠款转回 LendingPool
。资金将在我们操作结束时自动提取。
个人认为:使用这种方法偿还闪电贷的金额可以很好的避免发送偿还闪电贷的资金,同时被用户用作其他目的的情况发生。
产生债务(即不立即归还债务)
如果我们最初对 modes
参数种的任何资产使用 mode=1
或 mode=2
,则如果 onBehalfOf
地址之前已批准 msg.sender
代表他们承担债务,则为 onBehalfOf
传入的地址产生债务。
这意味着我们可以拥有一些立即偿还的资产,而其他资产会产生债务。
在 Aave 的闪电贷中,如果选择了产生债务(即 modes[]
中的值为 1 或 2,表示稳定利率或浮动利率的借款),onBehalfOf
地址必须有足够的抵押才能支持该债务。也就是说,协议会检查该地址是否已经抵押了足够的资产来借款。
对于无抵押的债务,如果借款人不偿还债务,协议会触发清算机制,卖出借款人的抵押资产来偿还贷款。如果没有足够的抵押,协议可能面临损失,因此合理的抵押机制至关重要。
编码和解码参数
如果我们需要将参数传递到 flash loan 函数中,我们首先需要对其进行编码,然后在 executeOperation()
种对其进行编码。
Encoding
如果我们在 solidity 中编码,可以使用其内置的 abi.encode()
函数:
// Encoding an address and a uint256
bytes memory parms = abi.encode(address(this), 1234);
如果要在链下编码,则可以使用类似于 web3.js
的包,其中包含 abi.encodeParameters()
:
const params = web3.eth.abi.encodeParameters(
["bytes32"],
[
web3.utils.utf8ToHex("some_value")
]
)
Decoding
在 executeOperation()
中解码时,我们需要使用内置的 abi.decode()
函数:
(bytes32 someValue) = abi.decode(params, (bytes32));
有关更多信息,参阅 Solidity 官方文档。
IFlashLoanReceiver
切勿将资金永久保存在我们的 FlashLoanReceiverBase
合约上,因为他们可能会受到“恶意破坏”攻击,其中存储的资金被攻击者使用
在进行闪电贷时,接受资金的合约必须符合以下的 solidity 接口:
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.12;
import { ILendingPoolAddressesProvider } from './ILendingPoolAddressesProvider.sol';
import { ILendingPool } from './ILendingPool.sol';
/**
* @title IFlashLoanReceiver interface
* @notice Interface for the Aave fee IFlashLoanReceiver.
* @author Aave
* @dev implement this interface to develop a flashloan-compatible flashLoanReceiver contract
**/
interface IFlashLoanReceiver {
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
) external returns (bool);
function ADDRESSES_PROVIDER() external view returns (ILendingPoolAddressesProvider);
function LENDING_POOL() external view returns (ILendingPool);
}
FlashLoanReceiverBase
下面是一个可以用作实际生产基础的抽象合约示例:
可以在此处找到 ILendingPoolAddressesProvider
的接口
“FlashLoanReceiverBase.sol”
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import { SafeMath } from './SafeMath.sol';
import { IERC20 } from './IERC20.sol';
import { SafeERC20 } from './SafeERC20.sol';
import { IFlashLoanReceiver } from './IFlashLoanReceiver.sol';
import { ILendingPoolAddressesProvider } from './ILendingPoolAddressesProvider.sol';
import { ILendingPool } from './ILendingPool.sol';
/**
!!!
Never keep funds permanently on your FlashLoanReceiverBase contract as they could be
exposed to a 'griefing' attack, where the stored funds are used by an attacker.
!!!
*/
abstract contract FlashLoanReceiverBase is IFlashLoanReceiver {
using SafeERC20 for IERC20;
using SafeMath for uint256;
ILendingPoolAddressesProvider public immutable override ADDRESSES_PROVIDER;
ILendingPool public immutable override LENDING_POOL;
constructor(ILendingPoolAddressesProvider provider) public {
ADDRESSES_PROVIDER = provider;
LENDING_POOL = ILendingPool(provider.getLendingPool());
}
}
“IERC20.sol”
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.8;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
“SafeERC20.sol”
// SPDX-License-Identifier: MIT
pragma solidity 0.6.8;
import {IERC20} from './IERC20.sol';
import {SafeMath} from './SafeMath.sol';
import {Address} from './Address.sol';
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
'SafeERC20: approve from non-zero to non-zero allowance'
);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function callOptionalReturn(IERC20 token, bytes memory data) private {
require(address(token).isContract(), 'SafeERC20: call to non-contract');
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = address(token).call(data);
require(success, 'SafeERC20: low-level call failed');
if (returndata.length > 0) {
// Return data is optional
// solhint-disable-next-line max-line-length
require(abi.decode(returndata, (bool)), 'SafeERC20: ERC20 operation did not succeed');
}
}
}
“SafeMath.sol”
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.8;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, 'SafeMath: addition overflow');
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, 'SafeMath: subtraction overflow');
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, 'SafeMath: multiplication overflow');
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, 'SafeMath: division by zero');
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, 'SafeMath: modulo by zero');
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
“Address.sol”
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.8;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly {
codehash := extcodehash(account)
}
return (codehash != accountHash && codehash != 0x0);
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, 'Address: insufficient balance');
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success, ) = recipient.call{value: amount}('');
require(success, 'Address: unable to send value, recipient may have reverted');
}
}