From c3a2e26f61349cf89071615fcf60ee2d04e8a2c3 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Tue, 10 May 2022 18:49:10 +0530 Subject: [PATCH] linted code --- contracts/arbitrum/connectors/basic/main.sol | 188 +++++---- .../avalanche/connectors/aave/v2/helpers.sol | 98 +++-- .../avalanche/connectors/aave/v2/main.sol | 375 ++++++++++-------- contracts/avalanche/connectors/basic/main.sol | 188 +++++---- contracts/fantom/connectors/basic/main.sol | 188 +++++---- contracts/optimism/connectors/basic/main.sol | 187 +++++---- .../polygon/connectors/aave/v2/helpers.sol | 100 +++-- contracts/polygon/connectors/aave/v2/main.sol | 375 ++++++++++-------- contracts/polygon/connectors/basic/main.sol | 188 +++++---- 9 files changed, 1042 insertions(+), 845 deletions(-) diff --git a/contracts/arbitrum/connectors/basic/main.sol b/contracts/arbitrum/connectors/basic/main.sol index 2f13bfcf..6c18de0e 100644 --- a/contracts/arbitrum/connectors/basic/main.sol +++ b/contracts/arbitrum/connectors/basic/main.sol @@ -1,7 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; - /** * @title Basic. * @dev Deposit & Withdraw from DSA. @@ -15,97 +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 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); + /** + * @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 = "LogDepositFrom(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, from, getId, setId); - } + setUint(setId, _amt); - /** - * @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); + _eventName = "LogDepositFrom(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, from, getId, setId); + } - _eventName = "LogWithdraw(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, to, 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.1"; + string public constant name = "Basic-v1.1"; } diff --git a/contracts/avalanche/connectors/aave/v2/helpers.sol b/contracts/avalanche/connectors/aave/v2/helpers.sol index d964c6db..71064965 100644 --- a/contracts/avalanche/connectors/aave/v2/helpers.sol +++ b/contracts/avalanche/connectors/aave/v2/helpers.sol @@ -6,60 +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 OnBehalfOf user's total debt balance & fee for an asset + /** + * @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 getOnBehalfOfPaybackBalance(address token, uint256 rateMode, address onBehalfOf) + 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 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 (uint bal) { - (bal, , , , , , , ,) = aaveData.getUserReserveData(token, address(this)); - } + /** + * @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) + ); + } } diff --git a/contracts/avalanche/connectors/aave/v2/main.sol b/contracts/avalanche/connectors/aave/v2/main.sol index b10162aa..52fa67cc 100644 --- a/contracts/avalanche/connectors/aave/v2/main.sol +++ b/contracts/avalanche/connectors/aave/v2/main.sol @@ -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,162 +13,182 @@ 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; - if (_amt == uint(-1)) { - uint _amtDSA = isAVAX ? address(this).balance : tokenContract.balanceOf(address(this)); - uint _amtDebt = getPaybackBalance(_token, rateMode); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; - } + 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); - /** + _eventName = "LogPayback(address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, rateMode, getId, setId); + } + + /** * @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) @@ -196,15 +215,21 @@ abstract contract AaveResolver is Events, Helpers { AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); bool isAVAX = token == avaxAddr; - address _token = isAVAX ? wavaxAddr : token; + address _token = isAVAX ? wavaxAddr : token; TokenInterface tokenContract = TokenInterface(_token); - if (_amt == uint(-1)) { - uint _amtDSA = isAVAX ? address(this).balance : tokenContract.balanceOf(address(this)); - uint _amtDebt = getOnBehalfOfPaybackBalance(_token, rateMode, onBehalfOf); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; - } + 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 (isAVAX) convertAvaxToWavax(isAVAX, tokenContract, _amt); @@ -215,56 +240,66 @@ abstract contract AaveResolver is Events, Helpers { setUint(setId, _amt); _eventName = "LogPaybackOnBehalfOf(address,uint256,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, rateMode, onBehalfOf, getId, setId); + _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) { - uint _length = tokens.length; - require(_length > 0, "0-tokens-not-allowed"); + /** + * @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()); + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); - for (uint i = 0; i < _length; i++) { - address token = tokens[i]; - if (getCollateralBalance(token) > 0 && !getIsColl(token)) { - aave.setUserUseReserveAsCollateral(token, true); - } - } + 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); - } + _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, - uint rateMode - ) external payable returns (string memory _eventName, bytes memory _eventParam) { - AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + /** + * @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()); - uint currentRateMode = rateMode == 1 ? 2 : 1; + uint256 currentRateMode = rateMode == 1 ? 2 : 1; - if (getPaybackBalance(token, currentRateMode) > 0) { - aave.swapBorrowRateMode(token, rateMode); - } + if (getPaybackBalance(token, currentRateMode) > 0) { + aave.swapBorrowRateMode(token, rateMode); + } - _eventName = "LogSwapRateMode(address,uint256)"; - _eventParam = abi.encode(token, rateMode); - } + _eventName = "LogSwapRateMode(address,uint256)"; + _eventParam = abi.encode(token, rateMode); + } } contract ConnectV2AaveV2Avalanche is AaveResolver { - string constant public name = "AaveV2-v1.1"; + string public constant name = "AaveV2-v1.1"; } diff --git a/contracts/avalanche/connectors/basic/main.sol b/contracts/avalanche/connectors/basic/main.sol index 741633f1..3239f1ff 100644 --- a/contracts/avalanche/connectors/basic/main.sol +++ b/contracts/avalanche/connectors/basic/main.sol @@ -1,7 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; - /** * @title Basic. * @dev Deposit & Withdraw from DSA. @@ -15,97 +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 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) { - uint _amt = getUint(getId, amt); - require(token != avaxAddr, "avax-not-supported"); - IERC20 tokenContract = IERC20(token); - _amt = _amt == uint(-1) ? tokenContract.balanceOf(from) : _amt; - tokenContract.safeTransferFrom(from, address(this), _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 = "LogDepositFrom(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, from, getId, setId); - } + setUint(setId, _amt); - /** - * @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); + _eventName = "LogDepositFrom(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, from, getId, setId); + } - _eventName = "LogWithdraw(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, to, 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.1"; + string public constant name = "Basic-v1.1"; } diff --git a/contracts/fantom/connectors/basic/main.sol b/contracts/fantom/connectors/basic/main.sol index 98bb541f..8c8bb775 100644 --- a/contracts/fantom/connectors/basic/main.sol +++ b/contracts/fantom/connectors/basic/main.sol @@ -1,7 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; - /** * @title Basic. * @dev Deposit & Withdraw from DSA. @@ -15,97 +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 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) { - 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 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) { - uint _amt = getUint(getId, amt); - require(token != ftmAddr, "ftm-not-supported"); - IERC20 tokenContract = IERC20(token); - _amt = _amt == uint(-1) ? tokenContract.balanceOf(from) : _amt; - tokenContract.safeTransferFrom(from, address(this), _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 = "LogDepositFrom(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, from, getId, setId); - } + setUint(setId, _amt); - /** - * @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); + _eventName = "LogDepositFrom(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, from, getId, setId); + } - _eventName = "LogWithdraw(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, to, 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.1"; + string public constant name = "Basic-v1.1"; } diff --git a/contracts/optimism/connectors/basic/main.sol b/contracts/optimism/connectors/basic/main.sol index c52fc024..82c045e7 100644 --- a/contracts/optimism/connectors/basic/main.sol +++ b/contracts/optimism/connectors/basic/main.sol @@ -14,97 +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 ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE and need to pass `value` parameter equal to `amt` in cast function ```dsa.cast({..., value: amt})```.
For ERC20: 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.
(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE and need to pass `value` parameter equal to `amt` in cast function ```dsa.cast({..., value: amt})```.
For ERC20: 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 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); + /** + * @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 = "LogDepositFrom(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, from, getId, setId); - } + setUint(setId, _amt); - /** - * @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); + _eventName = "LogDepositFrom(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, from, getId, setId); + } - _eventName = "LogWithdraw(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, to, 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.1"; + string public constant name = "Basic-v1.1"; } diff --git a/contracts/polygon/connectors/aave/v2/helpers.sol b/contracts/polygon/connectors/aave/v2/helpers.sol index 6274699a..890315f9 100644 --- a/contracts/polygon/connectors/aave/v2/helpers.sol +++ b/contracts/polygon/connectors/aave/v2/helpers.sol @@ -6,60 +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 OnBehalfOf user's total debt balance & fee for an asset - * @param token token address of the debt.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + /** + * @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 getOnBehalfOfPaybackBalance(address token, uint256 rateMode, address onBehalfOf) + 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 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 (uint bal) { - (bal, , , , , , , ,) = aaveData.getUserReserveData(token, address(this)); - } + /** + * @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) + ); + } } diff --git a/contracts/polygon/connectors/aave/v2/main.sol b/contracts/polygon/connectors/aave/v2/main.sol index 43a31ba3..59911d9b 100644 --- a/contracts/polygon/connectors/aave/v2/main.sol +++ b/contracts/polygon/connectors/aave/v2/main.sol @@ -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,162 +13,182 @@ 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 isMatic = token == maticAddr; - address _token = isMatic ? wmaticAddr : token; + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); - TokenInterface tokenContract = TokenInterface(_token); + bool isMatic = token == maticAddr; + address _token = isMatic ? wmaticAddr : token; - if (_amt == uint(-1)) { - uint _amtDSA = isMatic ? address(this).balance : tokenContract.balanceOf(address(this)); - uint _amtDebt = getPaybackBalance(_token, rateMode); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; - } + TokenInterface tokenContract = TokenInterface(_token); - if (isMatic) convertMaticToWmatic(isMatic, 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); - /** + _eventName = "LogPayback(address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, rateMode, getId, setId); + } + + /** * @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) @@ -196,15 +215,21 @@ abstract contract AaveResolver is Events, Helpers { AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); bool isMatic = token == maticAddr; - address _token = isMatic ? wmaticAddr : token; + address _token = isMatic ? wmaticAddr : token; TokenInterface tokenContract = TokenInterface(_token); - if (_amt == uint(-1)) { - uint _amtDSA = isMatic ? address(this).balance : tokenContract.balanceOf(address(this)); - uint _amtDebt = getOnBehalfOfPaybackBalance(_token, rateMode, onBehalfOf); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; - } + 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 (isMatic) convertMaticToWmatic(isMatic, tokenContract, _amt); @@ -215,56 +240,66 @@ abstract contract AaveResolver is Events, Helpers { setUint(setId, _amt); _eventName = "LogPaybackOnBehalfOf(address,uint256,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, rateMode, onBehalfOf, getId, setId); + _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) { - uint _length = tokens.length; - require(_length > 0, "0-tokens-not-allowed"); + /** + * @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()); + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); - for (uint i = 0; i < _length; i++) { - address token = tokens[i]; - if (getCollateralBalance(token) > 0 && !getIsColl(token)) { - aave.setUserUseReserveAsCollateral(token, true); - } - } + 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); - } + _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, - uint rateMode - ) external payable returns (string memory _eventName, bytes memory _eventParam) { - AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + /** + * @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()); - uint currentRateMode = rateMode == 1 ? 2 : 1; + uint256 currentRateMode = rateMode == 1 ? 2 : 1; - if (getPaybackBalance(token, currentRateMode) > 0) { - aave.swapBorrowRateMode(token, rateMode); - } + if (getPaybackBalance(token, currentRateMode) > 0) { + aave.swapBorrowRateMode(token, rateMode); + } - _eventName = "LogSwapRateMode(address,uint256)"; - _eventParam = abi.encode(token, rateMode); - } + _eventName = "LogSwapRateMode(address,uint256)"; + _eventParam = abi.encode(token, rateMode); + } } contract ConnectV2AaveV2Polygon is AaveResolver { - string constant public name = "AaveV2-v1.1"; + string public constant name = "AaveV2-v1.1"; } diff --git a/contracts/polygon/connectors/basic/main.sol b/contracts/polygon/connectors/basic/main.sol index 960c0f36..ac049a78 100644 --- a/contracts/polygon/connectors/basic/main.sol +++ b/contracts/polygon/connectors/basic/main.sol @@ -1,7 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; - /** * @title Basic. * @dev Deposit & Withdraw from DSA. @@ -15,97 +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 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) { - uint _amt = getUint(getId, amt); - require(token != maticAddr, "eth-not-supported"); - IERC20 tokenContract = IERC20(token); - _amt = _amt == uint(-1) ? tokenContract.balanceOf(from) : _amt; - tokenContract.safeTransferFrom(from, address(this), _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 = "LogDepositFrom(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, from, getId, setId); - } + setUint(setId, _amt); - /** - * @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); + _eventName = "LogDepositFrom(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, from, getId, setId); + } - _eventName = "LogWithdraw(address,uint256,address,uint256,uint256)"; - _eventParam = abi.encode(token, _amt, to, 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.1"; + string public constant name = "Basic-v1.1"; }