dsa-connectors-old/contracts/connectors/staking.sol

204 lines
6.2 KiB
Solidity
Raw Permalink Normal View History

pragma solidity ^0.6.0;
2020-07-13 15:31:59 +00:00
pragma experimental ABIEncoderV2;
// import files from common directory
2020-07-07 15:10:27 +00:00
import { TokenInterface } from "../common/interfaces.sol";
import { Stores } from "../common/stores.sol";
2020-07-07 15:05:19 +00:00
import { DSMath } from "../common/math.sol";
interface IStakingRewards {
function stake(uint256 amount) external;
2020-07-07 14:43:14 +00:00
function withdraw(uint256 amount) external;
function getReward() external;
2020-07-13 15:31:59 +00:00
function balanceOf(address) external view returns(uint);
}
interface SynthetixMapping {
2020-07-19 10:43:58 +00:00
struct StakingData {
address stakingPool;
address stakingToken;
2020-07-19 15:01:00 +00:00
address rewardToken;
}
2020-07-13 15:31:59 +00:00
function stakingMapping(bytes32) external view returns(StakingData memory);
2020-07-19 10:43:58 +00:00
}
2020-07-19 10:43:58 +00:00
contract StakingHelper is DSMath, Stores {
/**
2020-07-19 10:43:58 +00:00
* @dev Return InstaDApp Staking Mapping Addresses
*/
2020-07-14 17:34:52 +00:00
function getMappingAddr() internal virtual view returns (address) {
2020-07-17 12:33:47 +00:00
return 0x772590F33eD05b0E83553650BF9e75A04b337526; // InstaMapping Address
}
2020-07-07 15:05:19 +00:00
/**
* @dev Convert String to bytes32.
*/
function stringToBytes32(string memory str) internal pure returns (bytes32 result) {
require(bytes(str).length != 0, "string-empty");
// solium-disable-next-line security/no-inline-assembly
assembly {
2020-07-14 17:34:52 +00:00
result := mload(add(str, 32))
2020-07-13 15:31:59 +00:00
}
}
2020-07-13 15:31:59 +00:00
/**
* @dev Get staking data
*/
function getStakingData(string memory stakingName)
internal
view
returns (
IStakingRewards stakingContract,
TokenInterface stakingToken,
2020-07-19 15:01:00 +00:00
TokenInterface rewardToken,
bytes32 stakingType
)
{
stakingType = stringToBytes32(stakingName);
SynthetixMapping.StakingData memory stakingData = SynthetixMapping(getMappingAddr()).stakingMapping(stakingType);
2020-07-14 17:34:52 +00:00
require(stakingData.stakingPool != address(0) && stakingData.stakingToken != address(0), "Wrong Staking Name");
stakingContract = IStakingRewards(stakingData.stakingPool);
stakingToken = TokenInterface(stakingData.stakingToken);
2020-07-19 15:01:00 +00:00
rewardToken = TokenInterface(stakingData.rewardToken);
}
}
2020-07-19 10:43:58 +00:00
contract Staking is StakingHelper {
2020-07-07 15:05:19 +00:00
event LogDeposit(
2020-07-19 15:01:00 +00:00
address indexed stakingToken,
bytes32 indexed stakingType,
2020-07-07 15:05:19 +00:00
uint256 amount,
uint getId,
uint setId
);
2020-07-14 17:34:52 +00:00
2020-07-07 15:05:19 +00:00
event LogWithdraw(
2020-07-19 15:01:00 +00:00
address indexed stakingToken,
bytes32 indexed stakingType,
2020-07-07 15:05:19 +00:00
uint256 amount,
uint getId,
uint setId
);
2020-07-19 10:43:58 +00:00
2020-07-09 11:21:39 +00:00
event LogClaimedReward(
2020-07-19 15:01:00 +00:00
address indexed rewardToken,
bytes32 indexed stakingType,
2020-07-09 11:21:39 +00:00
uint256 rewardAmt,
uint setId
);
/**
2020-07-07 15:05:19 +00:00
* @dev Deposit Token.
2020-07-14 17:34:52 +00:00
* @param stakingPoolName staking token address.
* @param amt staking token amount.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
2020-07-14 17:34:52 +00:00
*/
2020-07-07 15:05:19 +00:00
function deposit(
2020-07-13 15:31:59 +00:00
string calldata stakingPoolName,
2020-07-07 15:05:19 +00:00
uint amt,
uint getId,
uint setId
) external payable {
2020-07-07 15:05:19 +00:00
uint _amt = getUint(getId, amt);
2020-07-19 15:01:00 +00:00
(
IStakingRewards stakingContract,
TokenInterface stakingToken,
,
bytes32 stakingType
) = getStakingData(stakingPoolName);
2020-07-13 15:31:59 +00:00
_amt = _amt == uint(-1) ? stakingToken.balanceOf(address(this)) : _amt;
2020-07-13 15:31:59 +00:00
stakingToken.approve(address(stakingContract), _amt);
2020-07-07 15:05:19 +00:00
stakingContract.stake(_amt);
2020-07-09 11:21:39 +00:00
2020-07-07 15:44:33 +00:00
setUint(setId, _amt);
2020-07-13 15:31:59 +00:00
emit LogDeposit(address(stakingToken), stakingType, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogDeposit(address,bytes32,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(address(stakingToken), stakingType, _amt, getId, setId);
emitEvent(_eventCode, _eventParam);
}
/**
2020-07-07 15:05:19 +00:00
* @dev Withdraw Token.
2020-07-14 17:34:52 +00:00
* @param stakingPoolName staking token address.
* @param amt staking token amount.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setIdAmount Set token amount at this ID in `InstaMemory` Contract.
* @param setIdReward Set reward amount at this ID in `InstaMemory` Contract.
2020-07-14 17:34:52 +00:00
*/
2020-07-07 15:05:19 +00:00
function withdraw(
2020-07-13 15:31:59 +00:00
string calldata stakingPoolName,
2020-07-07 15:05:19 +00:00
uint amt,
uint getId,
2020-07-09 11:21:39 +00:00
uint setIdAmount,
uint setIdReward
) external payable {
2020-07-07 15:05:19 +00:00
uint _amt = getUint(getId, amt);
2020-07-19 15:01:00 +00:00
(
IStakingRewards stakingContract,
TokenInterface stakingToken,
TokenInterface rewardToken,
bytes32 stakingType
) = getStakingData(stakingPoolName);
2020-07-13 15:31:59 +00:00
_amt = _amt == uint(-1) ? stakingContract.balanceOf(address(this)) : _amt;
2020-07-19 15:01:00 +00:00
uint intialBal = rewardToken.balanceOf(address(this));
2020-07-13 15:31:59 +00:00
stakingContract.withdraw(_amt);
stakingContract.getReward();
2020-07-19 15:01:00 +00:00
uint finalBal = rewardToken.balanceOf(address(this));
2020-07-07 15:05:19 +00:00
uint rewardAmt = sub(finalBal, intialBal);
2020-07-09 11:21:39 +00:00
setUint(setIdAmount, _amt);
setUint(setIdReward, rewardAmt);
2020-07-13 15:31:59 +00:00
emit LogWithdraw(address(stakingToken), stakingType, _amt, getId, setIdAmount);
bytes32 _eventCodeWithdraw = keccak256("LogWithdraw(address,bytes32,uint256,uint256,uint256)");
2020-07-20 16:53:54 +00:00
bytes memory _eventParamWithdraw = abi.encode(address(stakingToken), stakingType, _amt, getId, setIdAmount);
2020-07-09 11:21:39 +00:00
emitEvent(_eventCodeWithdraw, _eventParamWithdraw);
2020-07-19 15:01:00 +00:00
emit LogClaimedReward(address(rewardToken), stakingType, rewardAmt, setIdReward);
2020-07-13 15:31:59 +00:00
bytes32 _eventCodeReward = keccak256("LogClaimedReward(address,bytes32,uint256,uint256)");
2020-07-20 16:53:54 +00:00
bytes memory _eventParamReward = abi.encode(address(rewardToken), stakingType, rewardAmt, setIdReward);
2020-07-09 11:21:39 +00:00
emitEvent(_eventCodeReward, _eventParamReward);
}
/**
* @dev Claim Reward.
2020-07-14 17:34:52 +00:00
* @param stakingPoolName staking token address.
* @param setId Set reward amount at this ID in `InstaMemory` Contract.
2020-07-14 17:34:52 +00:00
*/
2020-07-09 11:21:39 +00:00
function claimReward(
2020-07-13 15:31:59 +00:00
string calldata stakingPoolName,
2020-07-09 11:21:39 +00:00
uint setId
) external payable {
2020-07-19 15:01:00 +00:00
(
IStakingRewards stakingContract,
,
TokenInterface rewardToken,
bytes32 stakingType
) = getStakingData(stakingPoolName);
uint intialBal = rewardToken.balanceOf(address(this));
2020-07-09 11:21:39 +00:00
stakingContract.getReward();
2020-07-19 15:01:00 +00:00
uint finalBal = rewardToken.balanceOf(address(this));
2020-07-09 11:21:39 +00:00
uint rewardAmt = sub(finalBal, intialBal);
setUint(setId, rewardAmt);
2020-07-19 15:01:00 +00:00
emit LogClaimedReward(address(rewardToken), stakingType, rewardAmt, setId);
2020-07-13 15:31:59 +00:00
bytes32 _eventCode = keccak256("LogClaimedReward(address,bytes32,uint256,uint256)");
2020-07-19 15:01:00 +00:00
bytes memory _eventParam = abi.encode(address(rewardToken), stakingType, rewardAmt, setId);
emitEvent(_eventCode, _eventParam);
}
}
2020-07-19 10:43:58 +00:00
contract ConnectStaking is Staking {
2020-07-19 15:01:00 +00:00
string public name = "Staking-v1.1";
}