-
Notifications
You must be signed in to change notification settings - Fork 0
/
StakingPool.sol
184 lines (162 loc) · 5.49 KB
/
StakingPool.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Enjinstarter
pragma solidity ^0.8.0;
import "./AdminPrivileges.sol";
import "./interfaces/IStakingPool.sol";
/**
* @title StakingPool
* @author Tim Loh
* @notice Contains the staking pool configs used by StakingService
*/
contract StakingPool is AdminPrivileges, IStakingPool {
struct StakingPoolInfo {
uint256 stakeDurationDays;
address stakeTokenAddress;
uint256 stakeTokenDecimals;
address rewardTokenAddress;
uint256 rewardTokenDecimals;
uint256 poolAprWei; // pool APR in Wei
bool isOpen; // true if staking pool allows staking
bool isActive; // true if staking pool allows claim rewards and unstake
bool isInitialized; // true if staking pool has been initialized
}
uint256 public constant TOKEN_MAX_DECIMALS = 18;
mapping(bytes32 => StakingPoolInfo) private _stakingPools;
/**
* @inheritdoc IStakingPool
*/
function closeStakingPool(bytes32 poolId)
external
virtual
override
onlyRole(CONTRACT_ADMIN_ROLE)
{
require(_stakingPools[poolId].isInitialized, "SPool: uninitialized");
require(_stakingPools[poolId].isOpen, "SPool: closed");
_stakingPools[poolId].isOpen = false;
emit StakingPoolClosed(poolId, msg.sender);
}
/**
* @inheritdoc IStakingPool
*/
function createStakingPool(
bytes32 poolId,
uint256 stakeDurationDays,
address stakeTokenAddress,
uint256 stakeTokenDecimals,
address rewardTokenAddress,
uint256 rewardTokenDecimals,
uint256 poolAprWei
) external virtual override onlyRole(CONTRACT_ADMIN_ROLE) {
require(stakeDurationDays > 0, "SPool: stake duration");
require(stakeTokenAddress != address(0), "SPool: stake token");
require(
stakeTokenDecimals <= TOKEN_MAX_DECIMALS,
"SPool: stake decimals"
);
require(rewardTokenAddress != address(0), "SPool: reward token");
require(
rewardTokenDecimals <= TOKEN_MAX_DECIMALS,
"SPool: reward decimals"
);
require(
stakeTokenAddress != rewardTokenAddress ||
stakeTokenDecimals == rewardTokenDecimals,
"SPool: decimals different"
);
require(poolAprWei > 0, "SPool: pool APR");
require(!_stakingPools[poolId].isInitialized, "SPool: exists");
_stakingPools[poolId] = StakingPoolInfo({
stakeDurationDays: stakeDurationDays,
stakeTokenAddress: stakeTokenAddress,
stakeTokenDecimals: stakeTokenDecimals,
rewardTokenAddress: rewardTokenAddress,
rewardTokenDecimals: rewardTokenDecimals,
poolAprWei: poolAprWei,
isOpen: true,
isActive: true,
isInitialized: true
});
emit StakingPoolCreated(
poolId,
msg.sender,
stakeDurationDays,
stakeTokenAddress,
stakeTokenDecimals,
rewardTokenAddress,
rewardTokenDecimals,
poolAprWei
);
}
/**
* @inheritdoc IStakingPool
*/
function openStakingPool(bytes32 poolId)
external
virtual
override
onlyRole(CONTRACT_ADMIN_ROLE)
{
require(_stakingPools[poolId].isInitialized, "SPool: uninitialized");
require(!_stakingPools[poolId].isOpen, "SPool: opened");
_stakingPools[poolId].isOpen = true;
emit StakingPoolOpened(poolId, msg.sender);
}
/**
* @inheritdoc IStakingPool
*/
function resumeStakingPool(bytes32 poolId)
external
virtual
override
onlyRole(CONTRACT_ADMIN_ROLE)
{
require(_stakingPools[poolId].isInitialized, "SPool: uninitialized");
require(!_stakingPools[poolId].isActive, "SPool: active");
_stakingPools[poolId].isActive = true;
emit StakingPoolResumed(poolId, msg.sender);
}
/**
* @inheritdoc IStakingPool
*/
function suspendStakingPool(bytes32 poolId)
external
virtual
override
onlyRole(CONTRACT_ADMIN_ROLE)
{
require(_stakingPools[poolId].isInitialized, "SPool: uninitialized");
require(_stakingPools[poolId].isActive, "SPool: suspended");
_stakingPools[poolId].isActive = false;
emit StakingPoolSuspended(poolId, msg.sender);
}
/**
* @inheritdoc IStakingPool
*/
function getStakingPoolInfo(bytes32 poolId)
external
view
virtual
override
returns (
uint256 stakeDurationDays,
address stakeTokenAddress,
uint256 stakeTokenDecimals,
address rewardTokenAddress,
uint256 rewardTokenDecimals,
uint256 poolAprWei,
bool isOpen,
bool isActive
)
{
require(_stakingPools[poolId].isInitialized, "SPool: uninitialized");
stakeDurationDays = _stakingPools[poolId].stakeDurationDays;
stakeTokenAddress = _stakingPools[poolId].stakeTokenAddress;
stakeTokenDecimals = _stakingPools[poolId].stakeTokenDecimals;
rewardTokenAddress = _stakingPools[poolId].rewardTokenAddress;
rewardTokenDecimals = _stakingPools[poolId].rewardTokenDecimals;
poolAprWei = _stakingPools[poolId].poolAprWei;
isOpen = _stakingPools[poolId].isOpen;
isActive = _stakingPools[poolId].isActive;
}
}