New functions added

This commit is contained in:
Shriya Tyagi 2022-04-27 00:53:09 +04:00
parent 6aa6c4ea7e
commit 46ae8e333a
4 changed files with 138 additions and 1 deletions

View File

@ -32,4 +32,10 @@ contract Events {
event LogDisableCollateral(address[] tokens);
event LogSwapRateMode(address indexed token, uint256 rateMode);
event LogSetUserEMode(uint8 categoryId);
event LogApproveDelegation(
address token,
uint16 debtType,
address delegateTo,
uint256 amount
);
}

View File

@ -64,4 +64,21 @@ abstract contract Helpers is DSMath, Basic {
address(this)
);
}
/**
* @dev Get debt token address for an asset
* @param token token address of the asset
* @param rateMode Debt type: stable-1, variable-2
*/
function getDTokenAddr(address token, uint16 rateMode)
internal
view
returns(address dToken)
{
if (rateMode == 1) {
(, dToken, ) = aaveData.getReserveTokensAddresses(token);
} else {
(, , dToken) = aaveData.getReserveTokensAddresses(token);
}
}
}

View File

@ -90,3 +90,7 @@ interface AaveAddressProviderRegistryInterface {
interface ATokenInterface {
function balanceOf(address _user) external view returns (uint256);
}
interface DTokenInterface {
function approveDelegation(address delegatee, uint256 amount) external;
}

View File

@ -10,7 +10,7 @@ import { TokenInterface } from "../../../common/interfaces.sol";
import { Stores } from "../../../common/stores.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
import { AaveInterface } from "./interface.sol";
import { AaveInterface, DTokenInterface } from "./interface.sol";
abstract contract AaveResolver is Events, Helpers {
/**
@ -63,6 +63,52 @@ abstract contract AaveResolver is Events, Helpers {
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Deposit Matic/ERC20_Token.
* @notice Deposit a token to Aave v3 for lending / collaterization.
* @param token The address of the token to deposit.(For Matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function depositWithoutCollateral(
address token,
uint256 amt,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
AaveInterface aave = AaveInterface(aaveProvider.getPool());
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
if (isMatic) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
convertMaticToWmatic(isMatic, tokenContract, _amt);
} else {
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
}
approve(tokenContract, address(aave), _amt);
aave.supply(_token, _amt, address(this), referralCode);
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw matic/ERC20_Token.
* @notice Withdraw deposited token from Aave v3
@ -139,6 +185,44 @@ abstract contract AaveResolver is Events, Helpers {
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
/**
* @dev Borrow matic/ERC20_Token on behalf of a user.
* @notice Borrow a token using Aave v3 on behalf of a user
* @param token The address of the token to borrow.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to borrow.
* @param rateMode The type of borrow debt. (For Stable: 1, Variable: 2)
* @param onBehalfOf The user who will incur the debt
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
*/
function borrowOnBehalfOf(
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.getPool());
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
aave.borrow(_token, _amt, rateMode, referralCode, onBehalfOf);
convertWmaticToMatic(isMatic, TokenInterface(_token), _amt);
setUint(setId, _amt);
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
/**
* @dev Payback borrowed matic/ERC20_Token.
* @notice Payback debt owed.
@ -317,6 +401,32 @@ abstract contract AaveResolver is Events, Helpers {
_eventName = "LogSetUserEMode(uint8)";
_eventParam = abi.encode(categoryId);
}
/**
* @dev Approve Delegation
* @notice Gives approval to delegate debt tokens
* @param token The address of token
* @param rateMode The type of borrow debt
* @param delegateTo The address to whom the user is delegating
* @param amount The amount
*/
function approveDelegation(address token, uint16 rateMode, address delegateTo, uint256 amount)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
require(rateMode == 1 || rateMode == 2, "Invalid debt type");
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
address _dToken = getDTokenAddr(_token, rateMode);
DTokenInterface(_dToken).approveDelegation(delegateTo, amount);
_eventName = "LogApproveDelegation(address,uint16,address,uint256)";
_eventParam = abi.encode(token, rateMode, delegateTo, amount);
}
}
contract ConnectV2AaveV3Polygon is AaveResolver {