Merge pull request #221 from Richa-iitr/aaveV2-updates

Aave v2 and basic connector updates
This commit is contained in:
0xPradyuman 2022-05-11 15:09:33 +05:30 committed by GitHub
commit 79394b916d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 1328 additions and 705 deletions

View File

@ -2,6 +2,24 @@
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 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

@ -1,7 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw from DSA.
@ -15,69 +14,116 @@ import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for MATIC))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token != ethAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount");
_amt = msg.value;
}
setUint(setId, _amt);
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for MATIC))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token != ethAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(msg.sender)
: _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(
msg.value == _amt || _amt == uint256(-1),
"invalid-ether-amount"
);
_amt = msg.value;
}
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
* @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint amt,
address payable to,
uint getId,
uint setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token == ethAddr) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
to.call{value: _amt}("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
/**
* @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)
{
uint256 _amt = getUint(getId, amt);
require(token != ethAddr, "eth-not-supported");
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1) ? tokenContract.balanceOf(from) : _amt;
tokenContract.safeTransferFrom(from, address(this), _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
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
* @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
address payable to,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token == ethAddr) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
to.call{ value: _amt }("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
}
contract ConnectV2BasicArbitrum is BasicResolver {
string constant public name = "Basic-v1";
string public constant name = "Basic-v1.1";
}

View File

@ -2,10 +2,40 @@
pragma solidity ^0.7.0;
contract Events {
event LogDeposit(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogWithdraw(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogBorrow(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId);
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 LogDeposit(
address indexed token,
uint256 tokenAmt,
uint256 getId,
uint256 setId
);
event LogWithdraw(
address indexed token,
uint256 tokenAmt,
uint256 getId,
uint256 setId
);
event LogBorrow(
address indexed token,
uint256 tokenAmt,
uint256 indexed rateMode,
uint256 getId,
uint256 setId
);
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

@ -6,45 +6,78 @@ import { Basic } from "../../../common/basic.sol";
import { AaveLendingPoolProviderInterface, AaveDataProviderInterface } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
/**
* @dev Aave Lending Pool Provider
*/
AaveLendingPoolProviderInterface constant internal aaveProvider = AaveLendingPoolProviderInterface(0xb6A86025F0FE1862B372cb0ca18CE3EDe02A318f);
/**
* @dev Aave Lending Pool Provider
*/
AaveLendingPoolProviderInterface internal constant aaveProvider =
AaveLendingPoolProviderInterface(
0xb6A86025F0FE1862B372cb0ca18CE3EDe02A318f
);
/**
* @dev Aave Protocol Data Provider
*/
AaveDataProviderInterface constant internal aaveData = AaveDataProviderInterface(0x65285E9dfab318f57051ab2b139ccCf232945451);
/**
* @dev Aave Protocol Data Provider
*/
AaveDataProviderInterface internal constant aaveData =
AaveDataProviderInterface(0x65285E9dfab318f57051ab2b139ccCf232945451);
/**
* @dev Aave Referral Code
*/
uint16 constant internal referralCode = 3228;
/**
* @dev Aave Referral Code
*/
uint16 internal constant referralCode = 3228;
/**
* @dev Checks if collateral is enabled for an asset
* @param token token address of the asset.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getIsColl(address token) internal view returns (bool isCol) {
(, , , , , , , , isCol) = aaveData.getUserReserveData(token, address(this));
}
/**
* @dev Checks if collateral is enabled for an asset
* @param token token address of the asset.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getIsColl(address token) internal view returns (bool isCol) {
(, , , , , , , , isCol) = aaveData.getUserReserveData(
token,
address(this)
);
}
/**
* @dev Get total debt balance & fee for an asset
* @param token token address of the debt.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Borrow rate mode (Stable = 1, Variable = 2)
*/
function getPaybackBalance(address token, uint rateMode) internal view returns (uint) {
(, uint stableDebt, uint variableDebt, , , , , , ) = aaveData.getUserReserveData(token, address(this));
return rateMode == 1 ? stableDebt : variableDebt;
}
/**
* @dev Get total debt balance & fee for an asset
* @param token token address of the debt.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Borrow rate mode (Stable = 1, Variable = 2)
*/
function getPaybackBalance(address token, uint256 rateMode)
internal
view
returns (uint256)
{
(, uint256 stableDebt, uint256 variableDebt, , , , , , ) = aaveData
.getUserReserveData(token, address(this));
return rateMode == 1 ? stableDebt : variableDebt;
}
/**
* @dev Get total collateral balance for an asset
* @param token token address of the collateral.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getCollateralBalance(address token) internal view returns (uint bal) {
(bal, , , , , , , ,) = aaveData.getUserReserveData(token, address(this));
}
/**
* @dev Get OnBehalfOf user's total debt balance & fee for an asset
* @param token token address of the debt.(For AVAX: 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 AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getCollateralBalance(address token)
internal
view
returns (uint256 bal)
{
(bal, , , , , , , , ) = aaveData.getUserReserveData(
token,
address(this)
);
}
}

View File

@ -6,7 +6,6 @@ pragma solidity ^0.7.0;
* @dev Lending & Borrowing.
*/
import { TokenInterface } from "../../../common/interfaces.sol";
import { Stores } from "../../../common/stores.sol";
import { Helpers } from "./helpers.sol";
@ -14,204 +13,293 @@ import { Events } from "./events.sol";
import { AaveInterface } from "./interface.sol";
abstract contract AaveResolver is Events, Helpers {
/**
* @dev Deposit AVAX/ERC20_Token.
* @notice Deposit a token to Aave v2 for lending / collaterization.
* @param token The address of the token to deposit.(For AVAX: 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 deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
/**
* @dev Deposit AVAX/ERC20_Token.
* @notice Deposit a token to Aave v2 for lending / collaterization.
* @param token The address of the token to deposit.(For AVAX: 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 deposit(
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.getLendingPool());
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
TokenInterface tokenContract = TokenInterface(_token);
if (isAVAX) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
convertAvaxToWavax(isAVAX, tokenContract, _amt);
} else {
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
}
if (isAVAX) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
convertAvaxToWavax(isAVAX, tokenContract, _amt);
} else {
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
}
approve(tokenContract, address(aave), _amt);
approve(tokenContract, address(aave), _amt);
aave.deposit(_token, _amt, address(this), referralCode);
aave.deposit(_token, _amt, address(this), referralCode);
if (!getIsColl(_token)) {
aave.setUserUseReserveAsCollateral(_token, true);
}
if (!getIsColl(_token)) {
aave.setUserUseReserveAsCollateral(_token, true);
}
setUint(setId, _amt);
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw AVAX/ERC20_Token.
* @notice Withdraw deposited token from Aave v2
* @param token The address of the token to withdraw.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
/**
* @dev Withdraw AVAX/ERC20_Token.
* @notice Withdraw deposited token from Aave v2
* @param token The address of the token to withdraw.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
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.getLendingPool());
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
TokenInterface tokenContract = TokenInterface(_token);
uint initialBal = tokenContract.balanceOf(address(this));
aave.withdraw(_token, _amt, address(this));
uint finalBal = tokenContract.balanceOf(address(this));
uint256 initialBal = tokenContract.balanceOf(address(this));
aave.withdraw(_token, _amt, address(this));
uint256 finalBal = tokenContract.balanceOf(address(this));
_amt = sub(finalBal, initialBal);
_amt = sub(finalBal, initialBal);
convertWavaxToAvax(isAVAX, tokenContract, _amt);
setUint(setId, _amt);
convertWavaxToAvax(isAVAX, tokenContract, _amt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
setUint(setId, _amt);
/**
* @dev Borrow AVAX/ERC20_Token.
* @notice Borrow a token using Aave v2
* @param token The address of the token to borrow.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to borrow.
* @param rateMode The type of borrow debt. (For Stable: 1, Variable: 2)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
*/
function borrow(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
/**
* @dev Borrow AVAX/ERC20_Token.
* @notice Borrow a token using Aave v2
* @param token The address of the token to borrow.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to borrow.
* @param rateMode The type of borrow debt. (For Stable: 1, Variable: 2)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
*/
function borrow(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
aave.borrow(_token, _amt, rateMode, referralCode, address(this));
convertWavaxToAvax(isAVAX, TokenInterface(_token), _amt);
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
setUint(setId, _amt);
aave.borrow(_token, _amt, rateMode, referralCode, address(this));
convertWavaxToAvax(isAVAX, TokenInterface(_token), _amt);
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
setUint(setId, _amt);
/**
* @dev Payback borrowed AVAX/ERC20_Token.
* @notice Payback debt owed.
* @param token The address of the token to payback.(For AVAX: 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 getId ID to retrieve amt.
* @param setId ID stores the amount of tokens paid back.
*/
function payback(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
/**
* @dev Payback borrowed AVAX/ERC20_Token.
* @notice Payback debt owed.
* @param token The address of the token to payback.(For AVAX: 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 getId ID to retrieve amt.
* @param setId ID stores the amount of tokens paid back.
*/
function payback(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
TokenInterface tokenContract = TokenInterface(_token);
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
_amt = _amt == uint(-1) ? getPaybackBalance(_token, rateMode) : _amt;
TokenInterface tokenContract = TokenInterface(_token);
if (isAVAX) convertAvaxToWavax(isAVAX, tokenContract, _amt);
if (_amt == uint256(-1)) {
uint256 _amtDSA = isAVAX
? address(this).balance
: tokenContract.balanceOf(address(this));
uint256 _amtDebt = getPaybackBalance(_token, rateMode);
_amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt;
}
approve(tokenContract, address(aave), _amt);
if (isAVAX) convertAvaxToWavax(isAVAX, tokenContract, _amt);
aave.repay(_token, _amt, rateMode, address(this));
approve(tokenContract, address(aave), _amt);
setUint(setId, _amt);
aave.repay(_token, _amt, rateMode, address(this));
_eventName = "LogPayback(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
setUint(setId, _amt);
/**
* @dev Enable collateral
* @notice Enable an array of tokens as collateral
* @param tokens Array of tokens to enable collateral
*/
function enableCollateral(
address[] calldata tokens
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _length = tokens.length;
require(_length > 0, "0-tokens-not-allowed");
_eventName = "LogPayback(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
/**
* @dev Payback borrowed AVAX/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 AVAX: 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);
for (uint i = 0; i < _length; i++) {
address token = tokens[i];
if (getCollateralBalance(token) > 0 && !getIsColl(token)) {
aave.setUserUseReserveAsCollateral(token, true);
}
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
_eventName = "LogEnableCollateral(address[])";
_eventParam = abi.encode(tokens);
}
bool isAVAX = token == avaxAddr;
address _token = isAVAX ? wavaxAddr : token;
/**
* @dev Swap borrow rate mode
* @notice Swaps user borrow rate mode between variable and stable
* @param token The address of the token to swap borrow rate.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2)
*/
function swapBorrowRateMode(
address token,
uint rateMode
) external payable returns (string memory _eventName, bytes memory _eventParam) {
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
TokenInterface tokenContract = TokenInterface(_token);
uint currentRateMode = rateMode == 1 ? 2 : 1;
if (_amt == uint256(-1)) {
uint256 _amtDSA = isAVAX
? address(this).balance
: tokenContract.balanceOf(address(this));
uint256 _amtDebt = getOnBehalfOfPaybackBalance(
_token,
rateMode,
onBehalfOf
);
_amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt;
}
if (getPaybackBalance(token, currentRateMode) > 0) {
aave.swapBorrowRateMode(token, rateMode);
}
if (isAVAX) convertAvaxToWavax(isAVAX, tokenContract, _amt);
_eventName = "LogSwapRateMode(address,uint256)";
_eventParam = abi.encode(token, rateMode);
}
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
* @param tokens Array of tokens to enable collateral
*/
function enableCollateral(address[] calldata tokens)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _length = tokens.length;
require(_length > 0, "0-tokens-not-allowed");
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
for (uint256 i = 0; i < _length; i++) {
address token = tokens[i];
if (getCollateralBalance(token) > 0 && !getIsColl(token)) {
aave.setUserUseReserveAsCollateral(token, true);
}
}
_eventName = "LogEnableCollateral(address[])";
_eventParam = abi.encode(tokens);
}
/**
* @dev Swap borrow rate mode
* @notice Swaps user borrow rate mode between variable and stable
* @param token The address of the token to swap borrow rate.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2)
*/
function swapBorrowRateMode(address token, uint256 rateMode)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
uint256 currentRateMode = rateMode == 1 ? 2 : 1;
if (getPaybackBalance(token, currentRateMode) > 0) {
aave.swapBorrowRateMode(token, rateMode);
}
_eventName = "LogSwapRateMode(address,uint256)";
_eventParam = abi.encode(token, rateMode);
}
}
contract ConnectV2AaveV2Avalanche is AaveResolver {
string constant public name = "AaveV2-v1";
string public constant name = "AaveV2-v1.1";
}

View File

@ -2,6 +2,24 @@
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 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

@ -1,7 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw from DSA.
@ -15,69 +14,116 @@ import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for AVAX))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token != avaxAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount");
_amt = msg.value;
}
setUint(setId, _amt);
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for AVAX))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token != avaxAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(msg.sender)
: _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(
msg.value == _amt || _amt == uint256(-1),
"invalid-ether-amount"
);
_amt = msg.value;
}
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
* @param token The address of the token to withdraw. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint amt,
address payable to,
uint getId,
uint setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token == avaxAddr) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
to.call{value: _amt}("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
/**
* @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: AVAX 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)
{
uint256 _amt = getUint(getId, amt);
require(token != avaxAddr, "avax-not-supported");
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1) ? tokenContract.balanceOf(from) : _amt;
tokenContract.safeTransferFrom(from, address(this), _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
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
* @param token The address of the token to withdraw. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
address payable to,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token == avaxAddr) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
to.call{ value: _amt }("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
}
contract ConnectV2BasicAvalanche is BasicResolver {
string constant public name = "Basic-v1";
string public constant name = "Basic-v1.1";
}

View File

@ -2,6 +2,24 @@
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 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

@ -1,7 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw from DSA.
@ -15,69 +14,116 @@ import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for MATIC))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token != ftmAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount");
_amt = msg.value;
}
setUint(setId, _amt);
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For FTM: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for MATIC))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token != ftmAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(msg.sender)
: _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(
msg.value == _amt || _amt == uint256(-1),
"invalid-ether-amount"
);
_amt = msg.value;
}
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
* @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint amt,
address payable to,
uint getId,
uint setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token == ftmAddr) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
to.call{value: _amt}("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
/**
* @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: FTM 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)
{
uint256 _amt = getUint(getId, amt);
require(token != ftmAddr, "ftm-not-supported");
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1) ? tokenContract.balanceOf(from) : _amt;
tokenContract.safeTransferFrom(from, address(this), _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
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
* @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
address payable to,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token == ftmAddr) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
to.call{ value: _amt }("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
}
contract ConnectV2BasicFantom is BasicResolver {
string constant public name = "Basic-v1";
string public constant name = "Basic-v1.1";
}

View File

@ -2,6 +2,24 @@
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 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

@ -14,69 +14,116 @@ import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA.
* @param token The address of the token to deposit.<br>(For <b>ETH</b>: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE and need to pass `value` parameter equal to `amt` in cast function ```dsa.cast({..., value: amt})```.<br>For <b>ERC20</b>: Need to give allowance prior casting spells.)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for ETH))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token != ethAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount");
_amt = msg.value;
}
setUint(setId, _amt);
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA.
* @param token The address of the token to deposit.<br>(For <b>ETH</b>: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE and need to pass `value` parameter equal to `amt` in cast function ```dsa.cast({..., value: amt})```.<br>For <b>ERC20</b>: Need to give allowance prior casting spells.)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for ETH))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token != ethAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(msg.sender)
: _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(
msg.value == _amt || _amt == uint256(-1),
"invalid-ether-amount"
);
_amt = msg.value;
}
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
* @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint amt,
address payable to,
uint getId,
uint setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token == ethAddr) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
to.call{value: _amt}("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
/**
* @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)
{
uint256 _amt = getUint(getId, amt);
require(token != ethAddr, "eth-not-supported");
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1) ? tokenContract.balanceOf(from) : _amt;
tokenContract.safeTransferFrom(from, address(this), _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
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
* @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
address payable to,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token == ethAddr) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
to.call{ value: _amt }("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
}
contract ConnectV2BasicOptimism is BasicResolver {
string constant public name = "Basic-v1";
string public constant name = "Basic-v1.1";
}

View File

@ -2,10 +2,40 @@
pragma solidity ^0.7.0;
contract Events {
event LogDeposit(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogWithdraw(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogBorrow(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId);
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 LogDeposit(
address indexed token,
uint256 tokenAmt,
uint256 getId,
uint256 setId
);
event LogWithdraw(
address indexed token,
uint256 tokenAmt,
uint256 getId,
uint256 setId
);
event LogBorrow(
address indexed token,
uint256 tokenAmt,
uint256 indexed rateMode,
uint256 getId,
uint256 setId
);
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

@ -6,45 +6,78 @@ import { Basic } from "../../../common/basic.sol";
import { AaveLendingPoolProviderInterface, AaveDataProviderInterface } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
/**
* @dev Aave Lending Pool Provider
*/
AaveLendingPoolProviderInterface constant internal aaveProvider = AaveLendingPoolProviderInterface(0xd05e3E715d945B59290df0ae8eF85c1BdB684744);
/**
* @dev Aave Lending Pool Provider
*/
AaveLendingPoolProviderInterface internal constant aaveProvider =
AaveLendingPoolProviderInterface(
0xd05e3E715d945B59290df0ae8eF85c1BdB684744
);
/**
* @dev Aave Protocol Data Provider
*/
AaveDataProviderInterface constant internal aaveData = AaveDataProviderInterface(0x7551b5D2763519d4e37e8B81929D336De671d46d);
/**
* @dev Aave Protocol Data Provider
*/
AaveDataProviderInterface internal constant aaveData =
AaveDataProviderInterface(0x7551b5D2763519d4e37e8B81929D336De671d46d);
/**
* @dev Aave Referral Code
*/
uint16 constant internal referralCode = 3228;
/**
* @dev Aave Referral Code
*/
uint16 internal constant referralCode = 3228;
/**
* @dev Checks if collateral is enabled for an asset
* @param token token address of the asset.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getIsColl(address token) internal view returns (bool isCol) {
(, , , , , , , , isCol) = aaveData.getUserReserveData(token, address(this));
}
/**
* @dev Checks if collateral is enabled for an asset
* @param token token address of the asset.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getIsColl(address token) internal view returns (bool isCol) {
(, , , , , , , , isCol) = aaveData.getUserReserveData(
token,
address(this)
);
}
/**
* @dev Get 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 getPaybackBalance(address token, uint rateMode) internal view returns (uint) {
(, uint stableDebt, uint variableDebt, , , , , , ) = aaveData.getUserReserveData(token, address(this));
return rateMode == 1 ? stableDebt : variableDebt;
}
/**
* @dev Get 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 getPaybackBalance(address token, uint256 rateMode)
internal
view
returns (uint256)
{
(, uint256 stableDebt, uint256 variableDebt, , , , , , ) = aaveData
.getUserReserveData(token, address(this));
return rateMode == 1 ? stableDebt : variableDebt;
}
/**
* @dev Get total collateral balance for an asset
* @param token token address of the collateral.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
*/
function getCollateralBalance(address token) internal view returns (uint bal) {
(bal, , , , , , , ,) = aaveData.getUserReserveData(token, address(this));
}
/**
* @dev Get OnBehalfOf user's total debt balance & fee for an asset
* @param token token address of the debt.(For MATIC: 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)
*/
function getCollateralBalance(address token)
internal
view
returns (uint256 bal)
{
(bal, , , , , , , , ) = aaveData.getUserReserveData(
token,
address(this)
);
}
}

View File

@ -6,7 +6,6 @@ pragma solidity ^0.7.0;
* @dev Lending & Borrowing.
*/
import { TokenInterface } from "../../../common/interfaces.sol";
import { Stores } from "../../../common/stores.sol";
import { Helpers } from "./helpers.sol";
@ -14,204 +13,293 @@ import { Events } from "./events.sol";
import { AaveInterface } from "./interface.sol";
abstract contract AaveResolver is Events, Helpers {
/**
* @dev Deposit ETH/ERC20_Token.
* @notice Deposit a token to Aave v2 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 deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
/**
* @dev Deposit ETH/ERC20_Token.
* @notice Deposit a token to Aave v2 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 deposit(
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.getLendingPool());
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
TokenInterface tokenContract = TokenInterface(_token);
if (isEth) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
convertMaticToWmatic(isEth, tokenContract, _amt);
} else {
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
}
if (isEth) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
convertMaticToWmatic(isEth, tokenContract, _amt);
} else {
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
}
approve(tokenContract, address(aave), _amt);
approve(tokenContract, address(aave), _amt);
aave.deposit(_token, _amt, address(this), referralCode);
aave.deposit(_token, _amt, address(this), referralCode);
if (!getIsColl(_token)) {
aave.setUserUseReserveAsCollateral(_token, true);
}
if (!getIsColl(_token)) {
aave.setUserUseReserveAsCollateral(_token, true);
}
setUint(setId, _amt);
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw ETH/ERC20_Token.
* @notice Withdraw deposited token from Aave v2
* @param token The address of the token to withdraw.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
/**
* @dev Withdraw ETH/ERC20_Token.
* @notice Withdraw deposited token from Aave v2
* @param token The address of the token to withdraw.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
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.getLendingPool());
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
TokenInterface tokenContract = TokenInterface(_token);
uint initialBal = tokenContract.balanceOf(address(this));
aave.withdraw(_token, _amt, address(this));
uint finalBal = tokenContract.balanceOf(address(this));
uint256 initialBal = tokenContract.balanceOf(address(this));
aave.withdraw(_token, _amt, address(this));
uint256 finalBal = tokenContract.balanceOf(address(this));
_amt = sub(finalBal, initialBal);
_amt = sub(finalBal, initialBal);
convertWmaticToMatic(isEth, tokenContract, _amt);
setUint(setId, _amt);
convertWmaticToMatic(isEth, tokenContract, _amt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
setUint(setId, _amt);
/**
* @dev Borrow ETH/ERC20_Token.
* @notice Borrow a token using Aave v2
* @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 getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
*/
function borrow(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
/**
* @dev Borrow ETH/ERC20_Token.
* @notice Borrow a token using Aave v2
* @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 getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
*/
function borrow(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
aave.borrow(_token, _amt, rateMode, referralCode, address(this));
convertWmaticToMatic(isEth, TokenInterface(_token), _amt);
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
setUint(setId, _amt);
aave.borrow(_token, _amt, rateMode, referralCode, address(this));
convertWmaticToMatic(isEth, TokenInterface(_token), _amt);
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
setUint(setId, _amt);
/**
* @dev Payback borrowed ETH/ERC20_Token.
* @notice Payback debt owed.
* @param token The address of the token to payback.(For MATIC: 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 getId ID to retrieve amt.
* @param setId ID stores the amount of tokens paid back.
*/
function payback(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
/**
* @dev Payback borrowed ETH/ERC20_Token.
* @notice Payback debt owed.
* @param token The address of the token to payback.(For MATIC: 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 getId ID to retrieve amt.
* @param setId ID stores the amount of tokens paid back.
*/
function payback(
address token,
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
bool isEth = token == maticAddr;
address _token = isEth ? wmaticAddr : token;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
TokenInterface tokenContract = TokenInterface(_token);
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
_amt = _amt == uint(-1) ? getPaybackBalance(_token, rateMode) : _amt;
TokenInterface tokenContract = TokenInterface(_token);
if (isEth) convertMaticToWmatic(isEth, tokenContract, _amt);
if (_amt == uint256(-1)) {
uint256 _amtDSA = isMatic
? address(this).balance
: tokenContract.balanceOf(address(this));
uint256 _amtDebt = getPaybackBalance(_token, rateMode);
_amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt;
}
approve(tokenContract, address(aave), _amt);
if (isMatic) convertMaticToWmatic(isMatic, tokenContract, _amt);
aave.repay(_token, _amt, rateMode, address(this));
approve(tokenContract, address(aave), _amt);
setUint(setId, _amt);
aave.repay(_token, _amt, rateMode, address(this));
_eventName = "LogPayback(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
setUint(setId, _amt);
/**
* @dev Enable collateral
* @notice Enable an array of tokens as collateral
* @param tokens Array of tokens to enable collateral
*/
function enableCollateral(
address[] calldata tokens
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _length = tokens.length;
require(_length > 0, "0-tokens-not-allowed");
_eventName = "LogPayback(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
/**
* @dev Payback borrowed MATIC/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 AVAX: 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);
for (uint i = 0; i < _length; i++) {
address token = tokens[i];
if (getCollateralBalance(token) > 0 && !getIsColl(token)) {
aave.setUserUseReserveAsCollateral(token, true);
}
}
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
_eventName = "LogEnableCollateral(address[])";
_eventParam = abi.encode(tokens);
}
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
/**
* @dev Swap borrow rate mode
* @notice Swaps user borrow rate mode between variable and stable
* @param token The address of the token to swap borrow rate.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2)
*/
function swapBorrowRateMode(
address token,
uint rateMode
) external payable returns (string memory _eventName, bytes memory _eventParam) {
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
TokenInterface tokenContract = TokenInterface(_token);
uint currentRateMode = rateMode == 1 ? 2 : 1;
if (_amt == uint256(-1)) {
uint256 _amtDSA = isMatic
? address(this).balance
: tokenContract.balanceOf(address(this));
uint256 _amtDebt = getOnBehalfOfPaybackBalance(
_token,
rateMode,
onBehalfOf
);
_amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt;
}
if (getPaybackBalance(token, currentRateMode) > 0) {
aave.swapBorrowRateMode(token, rateMode);
}
if (isMatic) convertMaticToWmatic(isMatic, tokenContract, _amt);
_eventName = "LogSwapRateMode(address,uint256)";
_eventParam = abi.encode(token, rateMode);
}
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
* @param tokens Array of tokens to enable collateral
*/
function enableCollateral(address[] calldata tokens)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _length = tokens.length;
require(_length > 0, "0-tokens-not-allowed");
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
for (uint256 i = 0; i < _length; i++) {
address token = tokens[i];
if (getCollateralBalance(token) > 0 && !getIsColl(token)) {
aave.setUserUseReserveAsCollateral(token, true);
}
}
_eventName = "LogEnableCollateral(address[])";
_eventParam = abi.encode(tokens);
}
/**
* @dev Swap borrow rate mode
* @notice Swaps user borrow rate mode between variable and stable
* @param token The address of the token to swap borrow rate.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2)
*/
function swapBorrowRateMode(address token, uint256 rateMode)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
uint256 currentRateMode = rateMode == 1 ? 2 : 1;
if (getPaybackBalance(token, currentRateMode) > 0) {
aave.swapBorrowRateMode(token, rateMode);
}
_eventName = "LogSwapRateMode(address,uint256)";
_eventParam = abi.encode(token, rateMode);
}
}
contract ConnectV2AaveV2Polygon is AaveResolver {
string constant public name = "AaveV2-v1";
string public constant name = "AaveV2-v1.1";
}

View File

@ -2,6 +2,24 @@
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 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

@ -1,7 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw from DSA.
@ -15,69 +14,116 @@ import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for MATIC))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token != maticAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount");
_amt = msg.value;
}
setUint(setId, _amt);
/**
* @dev Deposit Assets To Smart Account.
* @notice Deposit a token to DSA
* @param token The address of the token to deposit. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for MATIC))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
uint256 amt,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token != maticAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(msg.sender)
: _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
} else {
require(
msg.value == _amt || _amt == uint256(-1),
"invalid-ether-amount"
);
_amt = msg.value;
}
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
* @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint amt,
address payable to,
uint getId,
uint setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
if (token == maticAddr) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
to.call{value: _amt}("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
/**
* @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: MATIC 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)
{
uint256 _amt = getUint(getId, amt);
require(token != maticAddr, "eth-not-supported");
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1) ? tokenContract.balanceOf(from) : _amt;
tokenContract.safeTransferFrom(from, address(this), _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
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
* @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
uint256 amt,
address payable to,
uint256 getId,
uint256 setId
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
if (token == maticAddr) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
to.call{ value: _amt }("");
} else {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
tokenContract.safeTransfer(to, _amt);
}
setUint(setId, _amt);
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(token, _amt, to, getId, setId);
}
}
contract ConnectV2Basic is BasicResolver {
string constant public name = "Basic-v1";
contract ConnectV2BasicPolygon is BasicResolver {
string public constant name = "Basic-v1.1";
}