dsa-connectors/contracts/mainnet/connectors/erc20_staking/main.sol

120 lines
3.7 KiB
Solidity
Raw Normal View History

2021-06-10 11:56:39 +00:00
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
2021-06-10 17:57:27 +00:00
/**
* @title Token Staking.
* @dev Stake ERC20 for earning rewards.
*/
2021-06-10 11:56:39 +00:00
import { TokenInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
2021-06-15 15:51:19 +00:00
import { IStakingRewards, StakingERC20Mapping } from "./interface.sol";
2021-06-10 11:56:39 +00:00
2021-06-10 18:12:39 +00:00
contract Main is Helpers, Events {
2021-06-10 11:56:39 +00:00
/**
2021-06-10 17:57:27 +00:00
* @dev Deposit ERC20.
* @notice Deposit Tokens to staking pool.
* @param stakingPoolName staking pool name.
2021-06-10 11:56:39 +00:00
* @param amt staking token amount.
2021-06-10 17:57:27 +00:00
* @param getId ID to retrieve amount.
* @param setId ID stores the amount of staked tokens.
2021-06-10 11:56:39 +00:00
*/
function deposit(
string calldata stakingPoolName,
uint amt,
uint getId,
uint setId
2021-06-10 17:57:27 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
2021-06-10 11:56:39 +00:00
uint _amt = getUint(getId, amt);
(
IStakingRewards stakingContract,
TokenInterface stakingToken,
,
bytes32 stakingType
) = getStakingData(stakingPoolName);
_amt = _amt == uint(-1) ? stakingToken.balanceOf(address(this)) : _amt;
stakingToken.approve(address(stakingContract), _amt);
stakingContract.stake(_amt);
setUint(setId, _amt);
2021-06-10 17:57:27 +00:00
_eventName = "LogDeposit(address,bytes32,uint256,uint256,uint256)";
_eventParam = abi.encode(address(stakingToken), stakingType, _amt, getId, setId);
2021-06-10 11:56:39 +00:00
}
/**
2021-06-10 17:57:27 +00:00
* @dev Withdraw ERC20.
* @notice Withdraw Tokens from the staking pool.
* @param stakingPoolName staking pool name.
2021-06-10 11:56:39 +00:00
* @param amt staking token amount.
2021-06-10 17:57:27 +00:00
* @param getId ID to retrieve amount.
* @param setIdAmount ID stores the amount of stake tokens withdrawn.
* @param setIdReward ID stores the amount of reward tokens claimed.
2021-06-10 11:56:39 +00:00
*/
function withdraw(
string calldata stakingPoolName,
uint amt,
uint getId,
uint setIdAmount,
uint setIdReward
2021-06-10 17:57:27 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
2021-06-10 11:56:39 +00:00
uint _amt = getUint(getId, amt);
(
IStakingRewards stakingContract,
TokenInterface stakingToken,
TokenInterface rewardToken,
bytes32 stakingType
) = getStakingData(stakingPoolName);
_amt = _amt == uint(-1) ? stakingContract.balanceOf(address(this)) : _amt;
uint intialBal = rewardToken.balanceOf(address(this));
stakingContract.withdraw(_amt);
stakingContract.getReward();
2021-06-10 18:12:39 +00:00
uint rewardAmt = sub(rewardToken.balanceOf(address(this)), intialBal);
2021-06-10 11:56:39 +00:00
setUint(setIdAmount, _amt);
setUint(setIdReward, rewardAmt);
2021-06-10 18:12:39 +00:00
{
2021-06-10 17:57:27 +00:00
_eventName = "LogWithdrawAndClaimedReward(address,bytes32,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(address(stakingToken), stakingType, _amt, rewardAmt, getId, setIdAmount, setIdReward);
2021-06-10 18:12:39 +00:00
}
2021-06-10 11:56:39 +00:00
}
/**
* @dev Claim Reward.
2021-06-10 17:57:27 +00:00
* @notice Claim Pending Rewards of tokens staked.
* @param stakingPoolName staking pool name.
* @param setId ID stores the amount of reward tokens claimed.
2021-06-10 11:56:39 +00:00
*/
function claimReward(
string calldata stakingPoolName,
uint setId
2021-06-10 17:57:27 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
2021-06-10 11:56:39 +00:00
(
IStakingRewards stakingContract,
,
TokenInterface rewardToken,
bytes32 stakingType
) = getStakingData(stakingPoolName);
uint intialBal = rewardToken.balanceOf(address(this));
stakingContract.getReward();
uint finalBal = rewardToken.balanceOf(address(this));
uint rewardAmt = sub(finalBal, intialBal);
setUint(setId, rewardAmt);
2021-06-10 17:57:27 +00:00
_eventName = "LogClaimedReward(address,bytes32,uint256,uint256)";
_eventParam = abi.encode(address(rewardToken), stakingType, rewardAmt, setId);
2021-06-10 11:56:39 +00:00
}
2021-06-10 17:57:27 +00:00
}
2021-06-10 18:12:39 +00:00
contract connectV2StakeERC20 is Main {
2021-06-10 17:57:27 +00:00
string public constant name = "Stake-ERC20-v1.0";
2021-06-10 11:56:39 +00:00
}