-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lock.sol
215 lines (184 loc) · 6.33 KB
/
Lock.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
pragma solidity >=0.7.0 <0.9.0;
contract Lock {
//合约拥有者账号地址
address private owner;
mapping(address => Record) records;
mapping(address => uint) balances;
address[] users;
bool _enabled = true;
//抵押状态
struct Record {
//抵押额度(wei)
uint value;
//单次提取时间(s)
uint64 slice;
//抵押起始时间(s)
uint64 startTime;
//总提取次数
uint64 count;
//已提取次数
uint64 freeCount;
//用户索引
uint index;
}
struct QueryResult {
address addr;
uint lockedAmount;
uint withdrawed;
uint64 startTime;
uint64 slice;
uint64 count;
}
constructor() {
owner = msg.sender;
}
event USDTLog(address addr, uint amount, string txid);
//直接抵押函数
function lockLinear(address addr, uint64 timeSlice, uint64 count, string calldata txid) public payable {
require(msg.value > 0, "value cannot be zero");
require(address(msg.sender) == address(tx.origin), "no contract");
require(_enabled, "is disabled");
require(count > 0 && count <= 36, "illegal count");
require(timeSlice > 0 && timeSlice < 36500, "illegal time");
require(records[addr].value == 0, "lock exist");
records[addr] = Record({
value : msg.value,
slice : timeSlice * (1 days),
startTime : uint64(block.timestamp),
count : count,
freeCount : 0,
index : users.length
});
users.push(addr);
emit USDTLog(addr, msg.value, txid);
}
//查询自身锁仓
function querySelf() view public returns (uint, QueryResult memory result) {
require(records[msg.sender].value > 0, "no record");
Record storage curRecord = records[msg.sender];
uint share = curRecord.value / curRecord.count;
result = QueryResult({
addr : msg.sender,
lockedAmount : curRecord.value,
withdrawed : share * curRecord.freeCount,
startTime : curRecord.startTime,
slice : curRecord.slice,
count : curRecord.count
});
return (block.timestamp, result);
}
//查询指定锁仓
function queryAny(address addr) view public onlyOwner returns (QueryResult memory result) {
require(records[addr].value > 0, "no record");
Record storage curRecord = records[addr];
uint share = curRecord.value / curRecord.count;
result = QueryResult({
addr : addr,
lockedAmount : curRecord.value,
withdrawed : share * curRecord.freeCount,
startTime : curRecord.startTime,
slice : curRecord.slice,
count : curRecord.count
});
return result;
}
//查询全部锁仓
function queryAll(uint start, uint size) view public onlyOwner returns (QueryResult[] memory) {
require(start + size <= users.length, "overflow");
QueryResult[] memory result = new QueryResult[](size);
uint end = start + size;
for (uint i = start; i < end; i++) {
Record storage curRecord = records[users[i]];
uint share = curRecord.value / curRecord.count;
result[i - start] = QueryResult({
addr : users[i],
lockedAmount : curRecord.value,
withdrawed : share * curRecord.freeCount,
startTime : curRecord.startTime,
slice : curRecord.slice,
count : curRecord.count
});
}
return result;
}
function getAllCount() onlyOwner public view returns (uint count) {
return users.length;
}
function deleteUser(address addr) private {
//删除用户
uint index = records[addr].index;
uint end = users.length - 1;
if (index < end) {
users[index] = users[end];
records[users[end]].index = index;
}
users.pop();
delete records[addr];
}
function settle_(address addr) private {
Record storage curRecord = records[addr];
uint share = curRecord.value / curRecord.count;
uint curTime = block.timestamp;
//抵押已到期
if (curTime >= curRecord.startTime + curRecord.slice * curRecord.count) {
//剩余抵押
uint amount = curRecord.value - share * curRecord.freeCount;
curRecord.freeCount = curRecord.count;
balances[addr] += amount;
deleteUser(addr);
return;
}
uint times = (curTime - uint(curRecord.startTime)) / curRecord.slice;
//按时间释放
if (times > curRecord.freeCount) {
uint amount = (times - curRecord.freeCount) * share;
curRecord.freeCount = uint64(times);
balances[addr] += amount;
}
}
//提取指定余额
function withdrawAmount(uint amount) public {
require(address(msg.sender) == address(tx.origin), "no contract");
if (records[msg.sender].value > 0) {
settle_(msg.sender);
}
require(balances[msg.sender] >= amount, "not enough");
balances[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
}
//提取全部余额
function withdrawAll() public {
require(address(msg.sender) == address(tx.origin), "no contract");
if (records[msg.sender].value > 0) {
settle_(msg.sender);
}
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}
function transferLock(address addr) public {
require(records[addr].value == 0, "lock exist");
require(addr != msg.sender, "no self");
users.push(addr);
records[addr] = records[msg.sender];
deleteUser(msg.sender);
}
//设置开始状态
function changeStatus(bool flag) public onlyOwner {
_enabled = flag;
}
function transferOwner(address paramOwner) public onlyOwner {
require(paramOwner != address(0));
owner = paramOwner;
}
modifier onlyOwner(){
require(msg.sender == owner, "only owner");
_;
}
function getOwner() public view returns (address) {
return owner;
}
function isEnabled() public view returns (bool) {
return _enabled;
}
}