0%

thlm | 质押合约

新质押合约代码。

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Pausable} from "@openzeppelin/contracts/security/Pausable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract GameMember is Ownable, Pausable, ReentrancyGuard {

address public daoAddress;
address public GAMETokenAddress;
address public RecordOwner;
uint256 public deductionPercent;
bool public emergencyUnstakeFlag;

struct StakeInfo {
uint256 stakeLevel;
uint256 stakeAmount;
uint256 stakeTimestamp;
}

mapping(address => StakeInfo) internal _stakeRecords;

mapping(uint256 /* level */ => uint256 /* time */) internal _stakeLevels;

event GameStake(address user, uint256 oldLevel, uint256 newLevel, uint256 oldStakeAmount, uint256 newStakeAmount,
uint256 oldStakeTimestamp,uint256 newStakeTimestamp,
uint256 oldUnstakeTimestamp, uint256 newUnstakeTimestamp);

event GameUnstake(address user, uint256 level, uint256 unstakeAmount, uint256 userGetAmount, uint256 unstakeTimestamp);
event GameRecordStake(address user, uint256 level,uint256 stakeAmount, uint256 stakeTimestamp);

event LevelUpdated(uint256 level, uint256 stakeTime);

modifier onlyEOA() {
require(msg.sender.code.length == 0, "only EOA");
_;
}

modifier onlyRecordOwner() {
require(msg.sender == RecordOwner, "only RecordOwner");
_;
}

constructor(address ownerAddress,address RecordOwner_,address THMLTokenAddress_, address daoAddress_) Ownable(ownerAddress) {
GAMETokenAddress = THMLTokenAddress_;
RecordOwner = RecordOwner_;
daoAddress = daoAddress_;

emergencyUnstakeFlag = false;
deductionPercent = 10;

_stakeLevels[1] = 1; // 1 second
_stakeLevels[2] = 30 days;
_stakeLevels[3] = 90 days;
_stakeLevels[4] = 180 days;
}


function updateDeductionPercent(uint256 percent) public onlyOwner {
require(percent >= 0 && percent <= 100, "percent must be between 0 and 100");
deductionPercent = percent;
}

function updateEmergeceUnstakeFlag(bool flag) public onlyOwner {
emergencyUnstakeFlag = flag;
}

function updateDaoAddress(address daoAddress_) public onlyOwner {
daoAddress = daoAddress_;
}

/* 初始设置如下:
_stakeLevels[1] = 1; // 1 秒 , 活期
_stakeLevels[2] = 30 days;
_stakeLevels[3] = 90 days;
_stakeLevels[4] = 180 days;
设置时,需要满足level 越大, 时间越长的规则
*/
function updateStakeLevelInfo(uint256 level, uint256 time) public onlyOwner {
require(time > 0, "time must be greater than 0");
_stakeLevels[level] = time;
emit LevelUpdated(level, time);
}

function getUserStakeInfo(address user) public view returns(StakeInfo memory) {
StakeInfo memory stakeRecord = _stakeRecords[user];
return stakeRecord;
}

function getUnstakeTime(uint256 stakeTime, uint256 level) public view returns(uint256) {
return stakeTime + _stakeLevels[level];
}

function getStakeTime(uint256 level) public view returns(uint256) {
return _stakeLevels[level];
}

function pause() public onlyOwner {
_pause();
}

function unpause() public onlyOwner {
_unpause();
}

function RecordStake(address[] memory recordAddress,StakeInfo[] memory recordInfos) public onlyRecordOwner{
for (uint256 i = 0; i < recordAddress.length; i++) {
StakeInfo memory stakeRecord = recordInfos[i];
_stakeRecords[recordAddress[i]] = stakeRecord;
emit GameRecordStake(recordAddress[i],stakeRecord.stakeLevel,stakeRecord.stakeAmount,stakeRecord.stakeTimestamp);
}
}

function renounceRecordOwner() public onlyRecordOwner {
RecordOwner = address(0);
}

function stake(uint256 level, uint256 amount) public nonReentrant whenNotPaused onlyEOA {
require(_stakeLevels[level] > 0, "invalid level");

StakeInfo storage stakeRecord = _stakeRecords[msg.sender];
uint256 oldLevel = stakeRecord.stakeLevel;
uint256 oldAmount = stakeRecord.stakeAmount;
uint256 oldStakeTimestamp = stakeRecord.stakeTimestamp;
uint256 oldUnstakeTimestamp = getUnstakeTime(stakeRecord.stakeTimestamp, stakeRecord.stakeLevel);
if(oldUnstakeTimestamp > block.timestamp) {
require(level >= stakeRecord.stakeLevel, "new level shoule be greater than or equal to current level");
if(level > stakeRecord.stakeLevel) {
stakeRecord.stakeTimestamp = block.timestamp;
stakeRecord.stakeLevel = level;
}

stakeRecord.stakeAmount += amount;
}
else {
stakeRecord.stakeAmount += amount;
stakeRecord.stakeLevel = level;
stakeRecord.stakeTimestamp = block.timestamp;
}
if(amount > 0) {
IERC20(GAMETokenAddress).transferFrom(msg.sender, address(this), amount);
}

uint256 newUnstakeTimestamp = getUnstakeTime(stakeRecord.stakeTimestamp, stakeRecord.stakeLevel);
emit GameStake(msg.sender, oldLevel, level, oldAmount, stakeRecord.stakeAmount,
oldStakeTimestamp, stakeRecord.stakeTimestamp,
oldUnstakeTimestamp,newUnstakeTimestamp);
}

function queryUnstake(address user) public view returns(uint256 userGetAmount, uint256 userLostAmount) {
StakeInfo memory stakeRecord = _stakeRecords[user];
uint256 unstatkeAmount = stakeRecord.stakeAmount;
userGetAmount = unstatkeAmount;
userLostAmount = 0;
if(stakeRecord.stakeAmount > 0) {
uint256 unstakeTimestamp = getUnstakeTime(stakeRecord.stakeTimestamp, stakeRecord.stakeLevel);
if( unstakeTimestamp > block.timestamp) {
uint256 leftTime = unstakeTimestamp - block.timestamp;
uint256 maxStakeTime = _stakeLevels[stakeRecord.stakeLevel];
if(leftTime > maxStakeTime) {
leftTime = maxStakeTime;
}
userLostAmount = unstatkeAmount * deductionPercent * leftTime /(maxStakeTime * 100);
userGetAmount = unstatkeAmount - userLostAmount;
}
}
}

function unstake() public nonReentrant whenNotPaused {
StakeInfo storage stakeRecord = _stakeRecords[msg.sender];
uint256 unstatkeAmount = stakeRecord.stakeAmount;
require(stakeRecord.stakeAmount > 0, "not stake yet");
(uint256 userGetAmount, uint256 userLostAmount) = queryUnstake(msg.sender);
IERC20(GAMETokenAddress).transfer(msg.sender, userGetAmount);
if(userLostAmount > 0) {
IERC20(GAMETokenAddress).transfer(daoAddress, userLostAmount);
}

stakeRecord.stakeAmount = 0;
stakeRecord.stakeTimestamp = 0;
emit GameUnstake(msg.sender, stakeRecord.stakeLevel, unstatkeAmount, userGetAmount, block.timestamp);
}

/*
* 紧急情况下,管理员可以通过修改设置,让用户可以提取所有的资金
*/
function emergencyUnstake() public nonReentrant whenNotPaused {
require(emergencyUnstakeFlag, "emergency unstake is not allowed");

StakeInfo storage stakeRecord = _stakeRecords[msg.sender];
uint256 unstatkeAmount = stakeRecord.stakeAmount;
uint256 userGetAmount = unstatkeAmount;
require(stakeRecord.stakeAmount > 0, "not stake yet");

IERC20(GAMETokenAddress).transfer(msg.sender, unstatkeAmount);
stakeRecord.stakeAmount = 0;
stakeRecord.stakeTimestamp = 0;

emit GameUnstake(msg.sender, stakeRecord.stakeLevel, unstatkeAmount, userGetAmount, block.timestamp);
}
}
请我喝杯咖啡吧~