This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontract.sol
227 lines (202 loc) · 6.18 KB
/
contract.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
216
217
218
219
220
221
222
223
224
225
226
227
pragma solidity ^0.5.0;
import "./friendship.sol";
import "./token_receiver.sol";
contract RedPackage is Friendship, Token {
struct Record {
address owner;
bool equalDivision;
bool onlyFriend;
address token;
uint256 amount;
uint256 remainAmount;
uint256 size;
uint256 remainSize;
uint256 timestamp;
uint256 expired;
uint256 id;
}
event Send(
address indexed sender,
address indexed token,
uint256 indexed value
);
event Receive(
address indexed receiver,
address indexed token,
uint256 indexed value
);
mapping(bytes32 => Record) records;
mapping(uint256 => mapping(address => bool)) grabbed;
uint256 internal nonce = 0;
constructor() public {}
function getRecord(bytes32 word)
public
view
returns (
address owner,
bool equalDivision,
bool onlyFriend,
address token,
uint256 amount,
uint256 remainAmount,
uint256 size,
uint256 remainSize,
uint256 timestamp,
uint256 expired
)
{
Record memory r = records[word];
return (r.owner, r.equalDivision, r.onlyFriend, r.token, r.amount, r.remainAmount, r.size, r.remainSize, r.timestamp, r.expired);
}
function IsWordExists(bytes32 word) internal view returns (bool) {
Record memory r = records[word];
return r.owner != address(0x0);
}
// Noitce for app implements
// If word length is more than 32bytes
// you can keccak256(word) at first
// Giving gives out ETH.
function Giving(
bytes32 word,
bool equalDivision,
bool onlyFriend,
uint256 size,
uint256 expireDays
) public payable {
require(
size > 0 && msg.value > 0 && msg.value > size && expireDays > 0,
"invalid data provided"
);
require(!IsWordExists(word), "Red package exists");
if (equalDivision) {
require(
msg.value % size == 0,
"Invalid value and size for equal division mode"
);
}
records[word] = Record(
msg.sender,
equalDivision,
onlyFriend,
address(0x0),
msg.value,
msg.value,
size,
size,
block.timestamp,
block.timestamp + expireDays * 1 days,
nonce
);
nonce++;
emit Send(msg.sender, address(0x0), msg.value);
}
function Giving(
bytes32 word,
address token,
uint256 value,
bool equalDivision,
bool onlyFriend,
uint256 size,
uint256 expireDays
) public {
uint256 balance = TokenReciever(tokens[msg.sender]).balanceOf(token);
require(balance >= value, "not sufficient funds");
require(
size > 0 && value > 0 && value > size && expireDays > 0,
"invalid data provided"
);
require(!IsWordExists(word), "Red package exists");
if (equalDivision) {
require(
value % size == 0,
"Invalid value and size for equal division mode"
);
}
records[word] = Record(
msg.sender,
equalDivision,
onlyFriend,
token,
value,
value,
size,
size,
block.timestamp,
block.timestamp + expireDays * 1 days,
nonce
);
nonce++;
SetLock(msg.sender, true);
emit Send(msg.sender, token, value);
}
function Revoke(bytes32 word) public {
Record storage r = records[word];
require(
r.owner == msg.sender,
"Red package not exists or you're not the owner"
);
require(r.expired < block.timestamp, "Only revoke expired one");
if (r.token == address(0x0)) {
msg.sender.transfer(r.amount);
} else {
SetLock(msg.sender, false);
}
delete records[word];
}
function CanGrab(bytes32 word) public view returns (bool has) {
Record storage r = records[word];
require(r.owner != address(0x0), "Red package not exists");
require(r.expired >= block.timestamp, "Red package expired");
if (r.onlyFriend) {
require(friendship[r.owner][msg.sender], "Only friend can grab");
}
return !grabbed[r.id][msg.sender];
}
function Grabbing(bytes32 word) public {
Record storage r = records[word];
require(r.owner != address(0x0), "Red package not exists");
require(r.expired >= block.timestamp, "Red package expired");
if (r.onlyFriend) {
require(friendship[r.owner][msg.sender], "Only friend can grab");
}
require(!grabbed[r.id][msg.sender], "can't grabbed twice");
uint256 value = 0;
if (r.equalDivision) {
value = uint256(r.amount) / uint256(r.size);
} else if (r.remainSize == 1) {
value = r.remainAmount;
} else {
bytes memory entropy = abi.encode(
msg.sender,
r.remainAmount,
r.remainSize,
block.timestamp
);
uint256 val = uint256(keccak256(entropy)) % r.remainAmount;
uint256 max = uint256(r.remainAmount) / uint256(r.remainSize);
if (val == 0) {
value = 1;
} else if (val > max) {
value = max;
} else {
value = val;
}
}
if (r.token == address(0x0)) {
msg.sender.transfer(value);
} else if (r.owner != msg.sender) {
SendToken(r.token, r.owner, msg.sender, value);
}
r.remainAmount -= value;
r.remainSize--;
if (r.remainSize == 0) {
delete records[word];
if (r.token != address(0x0)) {
SetLock(r.owner, false);
}
} else {
grabbed[r.id][msg.sender] = true;
}
emit Receive(msg.sender, r.token, value);
}
}