Merge pull request #214 from Instadapp/AaveV3-Updates

[Mainnet] Aave v2 and Basic connector update
This commit is contained in:
Thrilok kumar 2022-05-05 14:42:09 +05:30 committed by GitHub
commit 5cbbc71f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 4 deletions

View File

@ -8,4 +8,12 @@ contract Events {
event LogPayback(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId);
event LogEnableCollateral(address[] tokens);
event LogSwapRateMode(address indexed token, uint256 rateMode);
event LogPaybackOnBehalfOf(
address token,
uint256 amt,
uint256 rateMode,
address onBehalfOf,
uint256 getId,
uint256 setId
);
}

View File

@ -40,6 +40,21 @@ abstract contract Helpers is DSMath, Basic {
return rateMode == 1 ? stableDebt : variableDebt;
}
/**
* @dev Get OnBehalfOf user's total debt balance & fee for an asset
* @param token token address of the debt.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Borrow rate mode (Stable = 1, Variable = 2)
*/
function getOnBehalfOfPaybackBalance(address token, uint256 rateMode, address onBehalfOf)
internal
view
returns (uint256)
{
(, uint256 stableDebt, uint256 variableDebt, , , , , , ) = aaveData
.getUserReserveData(token, onBehalfOf);
return rateMode == 1 ? stableDebt : variableDebt;
}
/**
* @dev Get total collateral balance for an asset
* @param token token address of the collateral.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)

View File

@ -149,8 +149,12 @@ abstract contract AaveResolver is Events, Helpers {
address _token = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
_amt = _amt == uint(-1) ? getPaybackBalance(_token, rateMode) : _amt;
if (_amt == uint(-1)) {
uint _amtDSA = isEth ? address(this).balance : tokenContract.balanceOf(address(this));
uint _amtDebt = getPaybackBalance(_token, rateMode);
_amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt;
}
if (isEth) convertEthToWeth(isEth, tokenContract, _amt);
@ -164,6 +168,55 @@ abstract contract AaveResolver is Events, Helpers {
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
/**
* @dev Payback borrowed ETH/ERC20_Token on behalf of a user.
* @notice Payback debt owed on behalf os a user.
* @param token The address of the token to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to payback. (For max: `uint256(-1)`)
* @param rateMode The type of debt paying back. (For Stable: 1, Variable: 2)
* @param onBehalfOf Address of user who's debt to repay.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens paid back.
*/
function paybackOnBehalfOf(
address token,
uint256 amt,
uint256 rateMode,
address onBehalfOf,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
bool isEth = token == ethAddr;
address _token = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
if (_amt == uint(-1)) {
uint _amtDSA = isEth ? address(this).balance : tokenContract.balanceOf(address(this));
uint _amtDebt = getOnBehalfOfPaybackBalance(_token, rateMode, onBehalfOf);
_amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt;
}
if (isEth) convertEthToWeth(isEth, tokenContract, _amt);
approve(tokenContract, address(aave), _amt);
aave.repay(_token, _amt, rateMode, onBehalfOf);
setUint(setId, _amt);
_eventName = "LogPaybackOnBehalfOf(address,uint256,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, onBehalfOf, getId, setId);
}
/**
* @dev Enable collateral
* @notice Enable an array of tokens as collateral
@ -212,5 +265,5 @@ abstract contract AaveResolver is Events, Helpers {
}
contract ConnectV2AaveV2 is AaveResolver {
string constant public name = "AaveV2-v1.1";
string constant public name = "AaveV2-v1.2";
}

View File

@ -4,4 +4,5 @@ pragma solidity ^0.7.0;
contract Events {
event LogDeposit(address indexed erc20, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogWithdraw(address indexed erc20, uint256 tokenAmt, address indexed to, uint256 getId, uint256 setId);
event LogDepositFrom(address indexed erc20, uint256 tokenAmt, address indexed from, uint256 getId, uint256 setId);
}

View File

@ -45,6 +45,34 @@ abstract contract BasicResolver is Events, DSMath, Basic {
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Deposit Assets To Smart Account From any user.
* @notice Deposit a token to DSA from any user.
* @param token The address of the token to deposit. (Note: ETH is not supported. Use `deposit()`)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)`)
* @param from The address depositing the token.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function depositFrom(
address token,
uint256 amt,
address from,
uint256 getId,
uint256 setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
require(token != ethAddr, "eth-not-supported");
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(from) : _amt;
tokenContract.safeTransferFrom(from, address(this), _amt);
setUint(setId, _amt);
_eventName = "LogDepositFrom(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, from, getId, setId);
}
/**
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
@ -78,5 +106,5 @@ abstract contract BasicResolver is Events, DSMath, Basic {
}
contract ConnectV2Basic is BasicResolver {
string constant public name = "Basic-v1";
string constant public name = "Basic-v1.1";
}