wip: withdraw from vautl

This commit is contained in:
Dimitri 2021-12-29 20:07:27 +07:00
parent e892220020
commit 093d7b30c6
2 changed files with 136 additions and 40 deletions

View File

@ -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,

View File

@ -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