From 9c83fd0924615e30ddab429c011181601490c8a2 Mon Sep 17 00:00:00 2001 From: Dimitri <69167058+dimsome@users.noreply.github.com> Date: Mon, 24 Jan 2022 22:55:44 +0700 Subject: [PATCH] chore: clean up --- .../mainnet/connectors/mstable/helpers.sol | 86 ++++++++++++++ contracts/mainnet/connectors/mstable/main.sol | 93 +--------------- .../polygon/connectors/mstable/helpers.sol | 105 +++++++++++++++--- contracts/polygon/connectors/mstable/main.sol | 99 +---------------- test/mainnet/mstable/mstable.helpers.ts | 2 +- test/polygon/mstable/mstable.helpers.ts | 2 +- 6 files changed, 183 insertions(+), 204 deletions(-) diff --git a/contracts/mainnet/connectors/mstable/helpers.sol b/contracts/mainnet/connectors/mstable/helpers.sol index 8100d0a1..dce9024f 100644 --- a/contracts/mainnet/connectors/mstable/helpers.sol +++ b/contracts/mainnet/connectors/mstable/helpers.sol @@ -3,6 +3,9 @@ pragma solidity ^0.7.6; import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { ISavingsContractV2, IBoostedSavingsVault } from "./interface.sol"; + abstract contract Helpers is DSMath, Basic { address internal constant mUsdToken = 0xe2f2a5C287993345a840Db3B0845fbC70f5935a5; @@ -10,4 +13,87 @@ abstract contract Helpers is DSMath, Basic { 0x30647a72Dc82d7Fbb1123EA74716aB8A317Eac19; address internal constant imUsdVault = 0x78BefCa7de27d07DC6e71da295Cc2946681A6c7B; + + /*************************************** + Internal + ****************************************/ + + /** + * @dev Deposit to Save from any asset + * @notice Called internally from deposit functions + * @param _token Address of token to deposit + * @param _amount Amount of token to deposit + * @param _path Path to mint mUSD (only needed for Feeder Pool) + * @return _eventName Event name + * @return _eventParam Event parameters + */ + + function _deposit( + address _token, + uint256 _amount, + address _path + ) internal returns (string memory _eventName, bytes memory _eventParam) { + // 1. Deposit mUSD to Save + approve(TokenInterface(mUsdToken), imUsdToken, _amount); + uint256 credits = ISavingsContractV2(imUsdToken).depositSavings( + _amount + ); + + // 2. Stake imUSD to Vault + approve(TokenInterface(imUsdToken), imUsdVault, credits); + IBoostedSavingsVault(imUsdVault).stake(credits); + + // 3. Log Events + _eventName = "LogDeposit(address,uint256,address)"; + _eventParam = abi.encode(_token, _amount, _path); + } + + /** + * @dev Withdraws from Save + * @notice Withdraws token supported by mStable from Save + * @param _credits Credits to withdraw + * @return amountWithdrawn Amount withdrawn in mUSD + */ + + function _withdraw(uint256 _credits) + internal + returns (uint256 amountWithdrawn) + { + // 1. Withdraw from Vault + // approve(TokenInterface(imUsdVault), imUsdToken, _credits); + IBoostedSavingsVault(imUsdVault).withdraw(_credits); + + // 2. Withdraw from Save + approve(TokenInterface(imUsdToken), imUsdVault, _credits); + amountWithdrawn = ISavingsContractV2(imUsdToken).redeemCredits( + _credits + ); + } + + /** + * @dev Returns the reward tokens + * @notice Gets the reward tokens from the vault contract + * @return rewardToken Address of reward token + */ + + function _getRewardTokens() internal view returns (address rewardToken) { + rewardToken = address( + IBoostedSavingsVault(imUsdVault).getRewardToken() + ); + } + + /** + * @dev Returns the internal balances of the rewardToken and platformToken + * @notice Gets current balances of rewardToken and platformToken, used for calculating rewards accrued + * @param _rewardToken Address of reward token + * @return a Amount of reward token + */ + + function _getRewardInternalBal(address _rewardToken) + internal + view + returns (uint256 a) + { + a = TokenInterface(_rewardToken).balanceOf(address(this)); + } } diff --git a/contracts/mainnet/connectors/mstable/main.sol b/contracts/mainnet/connectors/mstable/main.sol index 7e300e30..f42abf42 100644 --- a/contracts/mainnet/connectors/mstable/main.sol +++ b/contracts/mainnet/connectors/mstable/main.sol @@ -7,11 +7,11 @@ pragma solidity ^0.7.6; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; -import { IMasset, ISavingsContractV2, IBoostedSavingsVault, IFeederPool } from "./interface.sol"; + import { TokenInterface } from "../../common/interfaces.sol"; +import { IMasset, IBoostedSavingsVault, IFeederPool } from "./interface.sol"; abstract contract mStableResolver is Events, Helpers { - // /*************************************** CORE ****************************************/ @@ -31,7 +31,6 @@ abstract contract mStableResolver is Events, Helpers { uint256 _amount, uint256 _minOut ) external returns (string memory _eventName, bytes memory _eventParam) { - // uint256 mintedAmount = _amount; address path; @@ -71,7 +70,6 @@ abstract contract mStableResolver is Events, Helpers { uint256 _minOut, address _path ) external returns (string memory _eventName, bytes memory _eventParam) { - // require(_path != address(0), "Path must be set"); require( IMasset(mUsdToken).bAssetIndexes(_token) == 0, @@ -140,7 +138,6 @@ abstract contract mStableResolver is Events, Helpers { uint256 _minOut, address _path ) external returns (string memory _eventName, bytes memory _eventParam) { - // require(_path != address(0), "Path must be set"); require( IMasset(mUsdToken).bAssetIndexes(_token) == 0, @@ -203,7 +200,6 @@ abstract contract mStableResolver is Events, Helpers { uint256 _amount, uint256 _minOut ) external returns (string memory _eventName, bytes memory _eventParam) { - // approve(TokenInterface(_input), mUsdToken, _amount); uint256 amountSwapped; @@ -258,7 +254,6 @@ abstract contract mStableResolver is Events, Helpers { uint256 _minOut, address _path ) external returns (string memory _eventName, bytes memory _eventParam) { - // uint256 amountSwapped; approve(TokenInterface(_input), _path, _amount); @@ -276,90 +271,6 @@ abstract contract mStableResolver is Events, Helpers { _eventName = "LogSwap(address,address,uint256,uint256)"; _eventParam = abi.encode(_input, _output, _amount, amountSwapped); } - - /*************************************** - Internal - ****************************************/ - - /** - * @dev Deposit to Save from any asset - * @notice Called internally from deposit functions - * @param _token Address of token to deposit - * @param _amount Amount of token to deposit - * @param _path Path to mint mUSD (only needed for Feeder Pool) - * @return _eventName Event name - * @return _eventParam Event parameters - */ - - function _deposit( - address _token, - uint256 _amount, - address _path - ) internal returns (string memory _eventName, bytes memory _eventParam) { - // - // 1. Deposit mUSD to Save - approve(TokenInterface(mUsdToken), imUsdToken, _amount); - uint256 credits = ISavingsContractV2(imUsdToken).depositSavings( - _amount - ); - - // 2. Stake imUSD to Vault - approve(TokenInterface(imUsdToken), imUsdVault, credits); - IBoostedSavingsVault(imUsdVault).stake(credits); - - // 3. Log Events - _eventName = "LogDeposit(address,uint256,address)"; - _eventParam = abi.encode(_token, _amount, _path); - } - - /** - * @dev Withdraws from Save - * @notice Withdraws token supported by mStable from Save - * @param _credits Credits to withdraw - * @return amountWithdrawn Amount withdrawn in mUSD - */ - - function _withdraw(uint256 _credits) - internal - returns (uint256 amountWithdrawn) - { - // 1. Withdraw from Vault - // approve(TokenInterface(imUsdVault), imUsdToken, _credits); - IBoostedSavingsVault(imUsdVault).withdraw(_credits); - - // 2. Withdraw from Save - approve(TokenInterface(imUsdToken), imUsdVault, _credits); - amountWithdrawn = ISavingsContractV2(imUsdToken).redeemCredits( - _credits - ); - } - - /** - * @dev Returns the reward tokens - * @notice Gets the reward tokens from the vault contract - * @return rewardToken Address of reward token - */ - - function _getRewardTokens() internal view returns (address rewardToken) { - rewardToken = address( - IBoostedSavingsVault(imUsdVault).getRewardToken() - ); - } - - /** - * @dev Returns the internal balances of the rewardToken and platformToken - * @notice Gets current balances of rewardToken and platformToken, used for calculating rewards accrued - * @param _rewardToken Address of reward token - * @return a Amount of reward token - */ - - function _getRewardInternalBal(address _rewardToken) - internal - view - returns (uint256 a) - { - a = TokenInterface(_rewardToken).balanceOf(address(this)); - } } contract ConnectV2mStable is mStableResolver { diff --git a/contracts/polygon/connectors/mstable/helpers.sol b/contracts/polygon/connectors/mstable/helpers.sol index 4e174128..54e3d0da 100644 --- a/contracts/polygon/connectors/mstable/helpers.sol +++ b/contracts/polygon/connectors/mstable/helpers.sol @@ -3,25 +3,104 @@ pragma solidity ^0.7.6; import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; -// import { SaveWrapper } from "./interface.sol"; - -// interfaces here -// import { AaveLendingPoolProviderInterface, AaveDataProviderInterface } from "./interface.sol"; +import { ISavingsContractV2, IStakingRewardsWithPlatformToken } from "./interface.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; abstract contract Helpers is DSMath, Basic { - // Helpers go here - /* - * @dev SaveWrapper address - */ - // SaveWrapper internal constant saveWrapper = - // SaveWrapper(0x299081f52738A4204C3D58264ff44f6F333C6c88); - - // Addresses that will be important for the contract - address internal constant mUsdToken = 0xE840B73E5287865EEc17d250bFb1536704B43B21; address internal constant imUsdToken = 0x5290Ad3d83476CA6A2b178Cd9727eE1EF72432af; address internal constant imUsdVault = 0x32aBa856Dc5fFd5A56Bcd182b13380e5C855aa29; + + /*************************************** + Internal + ****************************************/ + + /** + * @dev Deposit to Save from any asset + * @notice Called internally from deposit functions + * @param _token Address of token to deposit + * @param _amount Amount of token to deposit + * @param _path Path to mint mUSD (only needed for Feeder Pool) + * @return _eventName Event name + * @return _eventParam Event parameters + */ + + function _deposit( + address _token, + uint256 _amount, + address _path + ) internal returns (string memory _eventName, bytes memory _eventParam) { + // 1. Deposit mUSD to Save + approve(TokenInterface(mUsdToken), imUsdToken, _amount); + uint256 credits = ISavingsContractV2(imUsdToken).depositSavings( + _amount + ); + + // 2. Stake imUSD to Vault + approve(TokenInterface(imUsdToken), imUsdVault, credits); + IStakingRewardsWithPlatformToken(imUsdVault).stake(credits); + + // 3. Log Events + _eventName = "LogDeposit(address,uint256,address)"; + _eventParam = abi.encode(_token, _amount, _path); + } + + /** + * @dev Withdraws from Save + * @notice Withdraws token supported by mStable from Save + * @param _credits Credits to withdraw + * @return amountWithdrawn Amount withdrawn in mUSD + */ + + function _withdraw(uint256 _credits) + internal + returns (uint256 amountWithdrawn) + { + // 1. Withdraw from Vault + IStakingRewardsWithPlatformToken(imUsdVault).withdraw(_credits); + + // 2. Withdraw from Save + approve(TokenInterface(imUsdToken), imUsdVault, _credits); + amountWithdrawn = ISavingsContractV2(imUsdToken).redeemCredits( + _credits + ); + } + + /** + * @dev Returns the reward tokens + * @notice Gets the reward tokens from the vault contract + * @return rewardToken Address of reward token + * @return platformToken Address of platform token + */ + + function _getRewardTokens() + internal + returns (address rewardToken, address platformToken) + { + rewardToken = IStakingRewardsWithPlatformToken(imUsdVault) + .getRewardToken(); + platformToken = IStakingRewardsWithPlatformToken(imUsdVault) + .getPlatformToken(); + } + + /** + * @dev Returns the internal balances of the rewardToken and platformToken + * @notice Gets current balances of rewardToken and platformToken, used for calculating rewards accrued + * @param _rewardToken Address of reward token + * @param _platformToken Address of platform token + * @return a Amount of reward token + * @return b Amount of platform token + */ + + function _getRewardInternalBal(address _rewardToken, address _platformToken) + internal + view + returns (uint256 a, uint256 b) + { + a = TokenInterface(_rewardToken).balanceOf(address(this)); + b = TokenInterface(_platformToken).balanceOf(address(this)); + } } diff --git a/contracts/polygon/connectors/mstable/main.sol b/contracts/polygon/connectors/mstable/main.sol index 1a63a698..b247fb4b 100644 --- a/contracts/polygon/connectors/mstable/main.sol +++ b/contracts/polygon/connectors/mstable/main.sol @@ -7,11 +7,10 @@ pragma solidity ^0.7.6; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; -import { IMasset, ISavingsContractV2, IStakingRewardsWithPlatformToken, IFeederPool } from "./interface.sol"; +import { IMasset, IStakingRewardsWithPlatformToken, IFeederPool } from "./interface.sol"; import { TokenInterface } from "../../common/interfaces.sol"; abstract contract PmStableResolver is Events, Helpers { - // /*************************************** CORE ****************************************/ @@ -31,7 +30,6 @@ abstract contract PmStableResolver is Events, Helpers { uint256 _amount, uint256 _minOut ) external returns (string memory _eventName, bytes memory _eventParam) { - // uint256 mintedAmount = _amount; address path; @@ -71,7 +69,6 @@ abstract contract PmStableResolver is Events, Helpers { uint256 _minOut, address _path ) external returns (string memory _eventName, bytes memory _eventParam) { - // require(_path != address(0), "Path must be set"); require( IMasset(mUsdToken).bAssetIndexes(_token) == 0, @@ -140,7 +137,6 @@ abstract contract PmStableResolver is Events, Helpers { uint256 _minOut, address _path ) external returns (string memory _eventName, bytes memory _eventParam) { - // require(_path != address(0), "Path must be set"); require( IMasset(mUsdToken).bAssetIndexes(_token) == 0, @@ -219,7 +215,6 @@ abstract contract PmStableResolver is Events, Helpers { uint256 _amount, uint256 _minOut ) external returns (string memory _eventName, bytes memory _eventParam) { - // approve(TokenInterface(_input), mUsdToken, _amount); uint256 amountSwapped; @@ -274,7 +269,6 @@ abstract contract PmStableResolver is Events, Helpers { uint256 _minOut, address _path ) external returns (string memory _eventName, bytes memory _eventParam) { - // uint256 amountSwapped; approve(TokenInterface(_input), _path, _amount); @@ -292,97 +286,6 @@ abstract contract PmStableResolver is Events, Helpers { _eventName = "LogSwap(address,address,uint256,uint256)"; _eventParam = abi.encode(_input, _output, _amount, amountSwapped); } - - /*************************************** - Internal - ****************************************/ - - /** - * @dev Deposit to Save from any asset - * @notice Called internally from deposit functions - * @param _token Address of token to deposit - * @param _amount Amount of token to deposit - * @param _path Path to mint mUSD (only needed for Feeder Pool) - * @return _eventName Event name - * @return _eventParam Event parameters - */ - - function _deposit( - address _token, - uint256 _amount, - address _path - ) internal returns (string memory _eventName, bytes memory _eventParam) { - // - // 1. Deposit mUSD to Save - approve(TokenInterface(mUsdToken), imUsdToken, _amount); - uint256 credits = ISavingsContractV2(imUsdToken).depositSavings( - _amount - ); - - // 2. Stake imUSD to Vault - approve(TokenInterface(imUsdToken), imUsdVault, credits); - IStakingRewardsWithPlatformToken(imUsdVault).stake(credits); - - // 3. Log Events - _eventName = "LogDeposit(address,uint256,address)"; - _eventParam = abi.encode(_token, _amount, _path); - } - - /** - * @dev Withdraws from Save - * @notice Withdraws token supported by mStable from Save - * @param _credits Credits to withdraw - * @return amountWithdrawn Amount withdrawn in mUSD - */ - - function _withdraw(uint256 _credits) - internal - returns (uint256 amountWithdrawn) - { - // 1. Withdraw from Vault - IStakingRewardsWithPlatformToken(imUsdVault).withdraw(_credits); - - // 2. Withdraw from Save - approve(TokenInterface(imUsdToken), imUsdVault, _credits); - amountWithdrawn = ISavingsContractV2(imUsdToken).redeemCredits( - _credits - ); - } - - /** - * @dev Returns the reward tokens - * @notice Gets the reward tokens from the vault contract - * @return rewardToken Address of reward token - * @return platformToken Address of platform token - */ - - function _getRewardTokens() - internal - returns (address rewardToken, address platformToken) - { - rewardToken = IStakingRewardsWithPlatformToken(imUsdVault) - .getRewardToken(); - platformToken = IStakingRewardsWithPlatformToken(imUsdVault) - .getPlatformToken(); - } - - /** - * @dev Returns the internal balances of the rewardToken and platformToken - * @notice Gets current balances of rewardToken and platformToken, used for calculating rewards accrued - * @param _rewardToken Address of reward token - * @param _platformToken Address of platform token - * @return a Amount of reward token - * @return b Amount of platform token - */ - - function _getRewardInternalBal(address _rewardToken, address _platformToken) - internal - view - returns (uint256 a, uint256 b) - { - a = TokenInterface(_rewardToken).balanceOf(address(this)); - b = TokenInterface(_platformToken).balanceOf(address(this)); - } } contract ConnectV2PmStable is PmStableResolver { diff --git a/test/mainnet/mstable/mstable.helpers.ts b/test/mainnet/mstable/mstable.helpers.ts index 3558b648..1dde1637 100644 --- a/test/mainnet/mstable/mstable.helpers.ts +++ b/test/mainnet/mstable/mstable.helpers.ts @@ -3,7 +3,7 @@ import { IERC20Minimal__factory } from "../../../typechain"; import { BigNumber as BN } from "ethers"; export const DEAD_ADDRESS = "0x0000000000000000000000000000000000000001"; -export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; +export const ZERO_ADDRESS = ethers.constants.AddressZero; export const DEFAULT_DECIMALS = 18; diff --git a/test/polygon/mstable/mstable.helpers.ts b/test/polygon/mstable/mstable.helpers.ts index 781559f8..f717efb3 100644 --- a/test/polygon/mstable/mstable.helpers.ts +++ b/test/polygon/mstable/mstable.helpers.ts @@ -3,7 +3,7 @@ import { IERC20Minimal__factory } from "../../../typechain"; import { BigNumber as BN } from "ethers"; export const DEAD_ADDRESS = "0x0000000000000000000000000000000000000001"; -export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; +export const ZERO_ADDRESS = ethers.constants.AddressZero; export const DEFAULT_DECIMALS = 18;