dsa-connectors/contracts/polygon/connectors/mstable/helpers.sol

117 lines
3.6 KiB
Solidity
Raw Normal View History

2021-12-29 10:37:38 +00:00
pragma solidity ^0.7.6;
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
2022-01-24 15:55:44 +00:00
import { ISavingsContractV2, IStakingRewardsWithPlatformToken } from "./interface.sol";
import { TokenInterface } from "../../common/interfaces.sol";
2021-12-29 10:37:38 +00:00
abstract contract Helpers is DSMath, Basic {
address internal constant mUsdToken =
0xE840B73E5287865EEc17d250bFb1536704B43B21;
address internal constant imUsdToken =
0x5290Ad3d83476CA6A2b178Cd9727eE1EF72432af;
address internal constant imUsdVault =
0x32aBa856Dc5fFd5A56Bcd182b13380e5C855aa29;
2022-01-24 15:55:44 +00:00
/***************************************
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)
2022-01-27 12:34:47 +00:00
* @param _stake stake token in Vault?
2022-01-24 15:55:44 +00:00
* @return _eventName Event name
* @return _eventParam Event parameters
*/
function _deposit(
address _token,
uint256 _amount,
2022-01-27 12:34:47 +00:00
address _path,
bool _stake
2022-01-24 15:55:44 +00:00
) internal returns (string memory _eventName, bytes memory _eventParam) {
// 1. Deposit mUSD to Save
approve(TokenInterface(mUsdToken), imUsdToken, _amount);
uint256 credits = ISavingsContractV2(imUsdToken).depositSavings(
_amount
);
2022-01-27 12:34:47 +00:00
if (_stake) {
// 2. Stake imUSD to Vault
approve(TokenInterface(imUsdToken), imUsdVault, credits);
IStakingRewardsWithPlatformToken(imUsdVault).stake(credits);
}
2022-01-24 15:55:44 +00:00
// 3. Log Events
2022-01-27 12:34:47 +00:00
_eventName = "LogDeposit(address,uint256,address,bool)";
_eventParam = abi.encode(_token, _amount, _path, _stake);
2022-01-24 15:55:44 +00:00
}
/**
* @dev Withdraws from Save
* @notice Withdraws token supported by mStable from Save
* @param _credits Credits to withdraw
2022-01-27 12:34:47 +00:00
* @param _unstake unstake from Vault?
2022-01-24 15:55:44 +00:00
* @return amountWithdrawn Amount withdrawn in mUSD
*/
2022-01-27 12:34:47 +00:00
function _withdraw(uint256 _credits, bool _unstake)
2022-01-24 15:55:44 +00:00
internal
returns (uint256 amountWithdrawn)
{
2022-01-27 12:34:47 +00:00
uint256 credits;
2022-01-24 15:55:44 +00:00
// 1. Withdraw from Vault
2022-01-27 12:34:47 +00:00
if (_unstake) {
credits = _credits == uint256(-1)
? TokenInterface(imUsdVault).balanceOf(address(this))
: _credits;
IStakingRewardsWithPlatformToken(imUsdVault).withdraw(credits);
}
2022-01-24 15:55:44 +00:00
// 2. Withdraw from Save
2022-01-27 12:34:47 +00:00
credits = _credits == uint256(-1)
? TokenInterface(imUsdToken).balanceOf(address(this))
: _credits;
2022-01-24 15:55:44 +00:00
approve(TokenInterface(imUsdToken), imUsdVault, _credits);
2022-01-27 12:34:47 +00:00
amountWithdrawn = ISavingsContractV2(imUsdToken).redeemCredits(credits);
2022-01-24 15:55:44 +00:00
}
/**
* @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));
}
2021-12-29 10:37:38 +00:00
}