Aave flash loan

Aave flash loan_第1张图片
Lending Pool Core
Lending Pool Core 持有所有的储备资产,以及计算逻辑。

LendingPoolAddressesProvider
LendingPoolAddressesProvider是个全局地址注册中心

LendingPoolDataProvider
计算用户余额的ETH等价值,和其头寸的健康因子。
聚合LendingPoolCore 数据。
计算平均贷款价值比和平均清算比率

Lending Pool

LendingPoo合约使用LendingPoolCore和LendingPoolDataProvider与存储交互。这是开发人员应该与之交互的主要合约。请参阅文档的LendingPool部分。

The InterestRateStrategy contract holds the information needed to calculate and update the interest rates of specific reserves.

Aave flash loan_第2张图片

borrow()
function borrow(address _reserve, uint256 _amount, uint256 _interestRateMode, uint16 _referralCode)

borrow支持固定利率和可变利率,在价格波动的情况下,如果抵押品的价格下降到某一阈值以下,借款头寸将被清算

/// Retrieve LendingPool address
LendingPoolAddressesProvider provider = LendingPoolAddressesProvider(address(0x24a42fD28C976A61Df5D00D0599C34c4f90748c8)); // mainnet address, for other addresses: https://docs.aave.com/developers/developing-on-aave/deployed-contract-instances
LendingPool lendingPool = LendingPool(provider.getLendingPool());

/// Input variables
address daiAddress = address(0x6B175474E89094C44Da98b954EedeAC495271d0F); // mainnet DAI
uint256 amount = 1000 * 1e18;

/// 1 is stable rate, 2 is variable rate
uint256 variableRate = 2;
uint256 referral = 0;

/// Borrow method call
lendingPool.borrow(daiAddress, amount, variableRate, referral);

Aave flash loan_第3张图片
repay()

function repay( address _reserve, uint256 _amount, address payable _onBehalfOf)

当第三方代表他们偿还另一个用户债务时,第三方地址需要使用_reserve契约的底层ERC20的_amount来批准()LendingPoolCore契约(与LendingPool契约不同)。
Aave flash loan_第4张图片
swapBorrowRateMode()
转换sender的利率模式

liquidationCall()

function liquidationCall(address _collateral, address _reserve, address _user, uint256 _purchaseAmount, bool _receiveaToken)

当头寸的健康系数低于1时,清算人代表借款人偿还部分或全部未偿贷款,同时获得折价的抵押品(也称为清算“奖金”)。清算人可以决定,他们是希望获得等值的抵押贷款,还是直接获得标的资产。当清算成功完成时,该头寸的健康因子增加,使健康因子大于1。

清算人只能清算由封闭因子定义的一定数量的抵押品。目前的近似值是0.5。换句话说,清算人最多只能清算未清偿头寸金额的50%。清算的折扣是按这个金额计算的。

_collateral
清算抵押物准备金的地址
_reserve
贷款标的资产的地址
_user
借款用户地址
_purchaseAmount
折扣购买的金额

_receiveaToken
如果为真,用户将获得与所购买抵押品等价的aTokens。如果为false,则用户直接接收基础资产

import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";

/// Retrieve the LendingPool address
LendingPoolAddressesProvider provider = LendingPoolAddressesProvider(address(0x24a42fD28C976A61Df5D00D0599C34c4f90748c8)); // mainnet address, for other addresses: https://docs.aave.com/developers/developing-on-aave/deployed-contract-instances
LendingPool lendingPool = LendingPool(provider.getLendingPool());

/// Input variables
address collateralAddress = /*collateral_address*/;
address daiAddress = address(0x6B175474E89094C44Da98b954EedeAC495271d0F); // mainnet DAI
address userAddress = /*user_address_being_liquidated*/;
uint256 purchaseAmount = 100 * 1e18;
bool receiveATokens = true;

/// Approve LendingPool contract to move your DAI
IERC20(daiAddress).approve(provider.getLendingPoolCore(), purchaseAmount);

/// LiquidationCall method call
lendingPool.liquidationCall(
    collateralAddress,
    daiAddress,
    userAddress,
    purchaseAmount,
    receiveATokens
);

flashLoan()
function flashLoan(address payable _receiver, address _reserve, uint _amount, bytes memory _params) external

允许调用契约从_reserve池(不含抵押品)借款,即一定数量的流动性,必须在交易结束前归还。
Aave flash loan_第5张图片

function getReserveConfigurationData(address _reserve)
返回特定储备的配置参数。

function getReserveData(address _reserve)
返回任何资产储备池的全局信息

function getUserAccountData(address _user)
返回与特定用户地址专门相关的reserve的信息

function getUserReserveData(address _reserve, address _user)
返回与特定备用项上的用户数据相关的信息

function getReserves()
返回所有活动储备地址的数组。

###aTokens
aTokens是计息衍生令牌,在存款(从LendingPool调用)和赎回(从aToken契约调用)时铸造和烧毁。aTokens的价值与相应的存款资产价值按1:1的比例挂钩,可以安全地存储、转移或交易。aTokens准备金所收集的所有利息通过不断增加aTokens持有人的钱包余额直接分配给他们。用户可以决定将他们的利息支付流重定向到任何Ethereum公共地址。

Price Oracle

你可能感兴趣的:(Aave flash loan)