From 093d7b30c6b551bf23f6ac69c2d7f81b95b8ef2d Mon Sep 17 00:00:00 2001 From: Dimitri <69167058+dimsome@users.noreply.github.com> Date: Wed, 29 Dec 2021 20:07:27 +0700 Subject: [PATCH] wip: withdraw from vautl --- .../polygon/connectors/mstable/events.sol | 2 +- contracts/polygon/connectors/mstable/main.sol | 174 ++++++++++++++---- 2 files changed, 136 insertions(+), 40 deletions(-) diff --git a/contracts/polygon/connectors/mstable/events.sol b/contracts/polygon/connectors/mstable/events.sol index 652c916d..52e31fa4 100644 --- a/contracts/polygon/connectors/mstable/events.sol +++ b/contracts/polygon/connectors/mstable/events.sol @@ -3,7 +3,7 @@ pragma solidity ^0.7.6; contract Events { // TODO: Events go here event LogDeposit(address token, uint256 amount, address path); - event LogWithdraw(address token, uint256 amount, address origin); + event LogWithdraw(address token, uint256 amount, address path); event LogClaimReward( address token, uint256 amount, diff --git a/contracts/polygon/connectors/mstable/main.sol b/contracts/polygon/connectors/mstable/main.sol index 5f2b204f..3f57ad1a 100644 --- a/contracts/polygon/connectors/mstable/main.sol +++ b/contracts/polygon/connectors/mstable/main.sol @@ -39,7 +39,7 @@ abstract contract mStableResolver is Events, Helpers { * @param _minOut Minimum amount of token to mint */ - function deposit( + function depositViaMint( address _token, uint256 _amount, uint256 _minOut @@ -60,37 +60,127 @@ abstract contract mStableResolver is Events, Helpers { return _deposit(_token, mintedAmount, mUsdToken); } - // /** - // * @dev Deposit to Save via feeder pool - // * @notice Deposits token, requires _minOut for minting and _path - // * @param _token Address of token to deposit - // * @param _amount Amount of token to deposit - // * @param _minOut Minimum amount of token to mint - // * @param _path Feeder Pool address for _token - // */ + /** + * @dev Deposit to Save via feeder pool + * @notice Deposits token, requires _minOut for minting and _path + * @param _token Address of token to deposit + * @param _amount Amount of token to deposit + * @param _minOut Minimum amount of token to mint + * @param _path Feeder Pool address for _token + */ - // function deposit( - // address _token, - // uint256 _amount, - // 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, - // "Token is bAsset" - // ); + function depositViaSwap( + address _token, + uint256 _amount, + 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, + "Token is bAsset" + ); - // approve(TokenInterface(_token), _path, _amount); - // uint256 mintedAmount = IFeederPool(_path).swap( - // _token, - // mUsdToken, - // _amount, - // _minOut, - // address(this) - // ); - // return _deposit(_token, mintedAmount, _path); - // } + approve(TokenInterface(_token), _path, _amount); + uint256 mintedAmount = IFeederPool(_path).swap( + _token, + mUsdToken, + _amount, + _minOut, + address(this) + ); + return _deposit(_token, mintedAmount, _path); + } + + /** + * @dev Withdraw from Save to mUSD + * @notice Withdraws from Save Vault to mUSD + * @param _credits Credits to withdraw + */ + function withdraw(uint256 _credits) + external + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 amountWithdrawn = _withdraw(_credits); + + _eventName = "LogWithdraw()"; + _eventParam = abi.encode(mUsdToken, amountWithdrawn, imUsdToken); + } + + /** + * @dev Withdraw from Save to bAsset + * @notice Withdraws from Save Vault to bAsset + * @param _token bAsset to withdraw to + * @param _credits Credits to withdraw + * @param _minOut Minimum amount of token to mint + */ + + function withdrawViaRedeem( + address _token, + uint256 _credits, + uint256 _minOut + ) external returns (string memory _eventName, bytes memory _eventParam) { + require( + IMasset(mUsdToken).bAssetIndexes(_token) != 0, + "Token not a bAsset" + ); + + uint256 amountWithdrawn = _withdraw(_credits); + uint256 amountRedeemed = IMasset(mUsdToken).redeem( + _token, + amountWithdrawn, + _minOut, + address(this) + ); + + _eventName = "LogRedeem()"; + _eventParam = abi.encode(mUsdToken, amountRedeemed, _token); + } + + /** + * @dev Withdraw from Save via Feeder Pool + * @notice Withdraws from Save Vault to asset via Feeder Pool + * @param _token bAsset to withdraw to + * @param _credits Credits to withdraw + * @param _minOut Minimum amount of token to mint + * @param _path Feeder Pool address for _token + */ + + function withdrawViaSwap( + address _token, + uint256 _credits, + 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, + "Token is bAsset" + ); + + uint256 amountWithdrawn = _withdraw(_credits); + + approve(TokenInterface(mUsdToken), _path, amountWithdrawn); + uint256 amountRedeemed = IFeederPool(_path).swap( + mUsdToken, + _token, + amountWithdrawn, + _minOut, + address(this) + ); + + _eventName = "LogRedeem()"; + _eventParam = abi.encode(_token, amountRedeemed, _path); + } + + /** + * @dev Claims Rewards + * @notice Claims accrued rewards from the Vault + */ + + function claimRewards() external { + IStakingRewardsWithPlatformToken(imUsdVault).claimReward(); + } /*************************************** Internal @@ -125,19 +215,25 @@ abstract contract mStableResolver is Events, Helpers { } /** - * @dev Withdraw from Save + * @dev Withdraws from Save * @notice Withdraws token supported by mStable from Save - * @param _token Address of token to withdraw - * @param _amount Amount of token to withdraw + * @param _credits Credits to withdraw */ - // function withdraw(address _token, uint256 _amount) - // external - // returns (string memory _eventName, bytes memory _eventParam); + function _withdraw(uint256 _credits) + internal + returns (uint256 amountWithdrawn) + { + // 1. Withdraw from Vault + // approve(TokenInterface(imUsdVault), imUsdToken, _credits); + IStakingRewardsWithPlatformToken(imUsdVault).withdraw(_credits); - // TODO - // function to support via Feeders or separate function? - // blocked by new SaveUnwrapper upgrade + // 2. Withdraw from Save + approve(TokenInterface(imUsdToken), imUsdVault, _credits); + amountWithdrawn = ISavingsContractV2(imUsdToken).redeemCredits( + _credits + ); + } /** * @dev Swaps token supported by mStable for another token