-
Notifications
You must be signed in to change notification settings - Fork 1
/
PuzzleBox.sol
336 lines (303 loc) · 9.58 KB
/
PuzzleBox.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
// Proxy contract for a puzzlebox.
contract PuzzleBoxProxy {
event Lock(bytes4 selector, bool isLocked);
PuzzleBox private immutable _logic;
mapping (bytes4 selector => bool isLocked) public isFunctionLocked;
address public owner;
constructor(PuzzleBox logic) {
_logic = logic;
owner = msg.sender;
}
// Allow/disallow a function selector from being called.
function lock(bytes4 selector, bool isLocked)
external
{
require(msg.sender == owner, 'not owner');
isFunctionLocked[selector] = isLocked;
emit Lock(selector, isLocked);
}
fallback(bytes calldata callData)
external
payable
returns (bytes memory returnData)
{
require(!isFunctionLocked[msg.sig], 'function is locked');
bool s;
(s, returnData) = address(_logic).delegatecall(callData);
if (!s) {
assembly { revert(add(returnData, 0x20), mload(returnData)) }
}
}
}
// Logic contract for a puzzlebox.
contract PuzzleBox {
event Operate(address operator);
event Drip(uint256 dripId, uint256 fee);
event Spread(uint256 amount, uint256 remaining);
event Zip();
event Torch(uint256[] dripIds);
event Burned(uint256 dripId);
event Creep();
event Open(address winner);
bool public isInitialized;
address public admin;
address payable public operator;
bytes32 public friendshipHash;
uint256 public lastDripId;
uint256 public dripCount;
uint256 public dripFee;
uint256 public leakCount;
mapping (uint256 dripId => bool isValid) public isValidDripId;
mapping (bytes signature => uint256 blockNumber) public signatureConsumedAt;
modifier onlyAdmin() {
require(admin == address(0) || msg.sender == admin, 'not admin');
_;
}
modifier noContractCaller() {
require(msg.sender.code.length == 0, 'cannot be called by a contract');
_;
}
modifier operation() {
require(msg.sender == operator, 'only current operator');
_;
operator = payable(address(uint160(0xDEAD)));
}
modifier maxDripCount(uint256 maxDripped) {
require(dripCount <= maxDripped, 'too much outstanding drip');
_;
}
modifier minTotalDripped(uint256 minTotal) {
require(lastDripId >= minTotal, 'not enough dripped');
_;
}
modifier maxCallDataSize(uint256 maxBytes) {
require(msg.data.length <= maxBytes, 'call data too large');
_;
}
modifier maxBalance(uint256 max) {
require(address(this).balance <= max, 'puzzlebox has too much balance');
_;
}
modifier burnDripId(uint256 dripId) {
_burnDrip(dripId);
_;
}
function initialize(
uint256 initialDripFee,
address payable[] calldata friends,
uint256[] calldata friendsCutBps,
uint256 adminSigNonce,
bytes calldata adminSig
)
external
payable
{
require(!isInitialized, 'already initialized');
isInitialized = true;
dripFee = initialDripFee;
befriend(friends, friendsCutBps);
admin = _consumeSignature(bytes32(adminSigNonce), adminSig);
operator = payable(address(0));
}
// Register fee recipient and cut (in bps) for spread().
function befriend(
address payable[] calldata friends,
uint256[] calldata friendsCutBps
)
public
onlyAdmin
{
friendshipHash = _getFriendshipHash(friends, friendsCutBps);
}
// Become the operator for drip() and receive this contract's entire balance.
function operate()
external
noContractCaller
{
require(operator == address(0), 'already being operated');
operator = payable(msg.sender);
_transferEth(operator, address(this).balance);
emit Operate(operator);
}
// Mint drips for an exponentially increasing fee.
function drip()
external payable
operation
returns (uint256 dripId)
{
require(msg.value >= dripFee, 'insufficient fee');
// Refund excess.
if (msg.value > dripFee) {
_transferEth(payable(msg.sender), msg.value - dripFee);
}
// Drip.
++dripCount;
dripId = ++lastDripId;
isValidDripId[dripId] = true;
emit Drip(dripId, dripFee);
// Double fee for next drip.
dripFee *= 2;
}
// Pay out a portion of accumulated fees to friends set by befriend().
function spread(
address payable[] calldata friends,
uint256[] calldata friendsCutBps
)
external
burnDripId(3)
{
require(friendshipHash == _getFriendshipHash(friends, friendsCutBps), 'not my friends');
uint256 total = 0;
for (uint256 i; i < friends.length; ++i) {
uint256 feeBps = friendsCutBps[i] > 1e4 ? 1e4 : friendsCutBps[i];
uint256 amount = feeBps * address(this).balance / 1e4;
total += amount;
_transferEth(friends[i], amount);
}
emit Spread(total, address(this).balance);
}
// Perform a gas constrained call to leak().
function zip()
external
burnDripId(1)
{
this.leak{gas: 12_000}();
emit Zip();
}
function leak()
external
{
unchecked {
payable(address(uint160(address(this)) + uint160(++leakCount))).transfer(1);
}
}
// Burn an encoded list of drip IDs.
function torch(bytes calldata encodedDripIds)
external
burnDripId(5)
maxCallDataSize(300)
{
uint256[] memory dripIds = abi.decode(encodedDripIds, (uint256[]));
for (uint256 i; i < dripIds.length; ++i) {
_burnDrip(dripIds[i]);
}
emit Torch(dripIds);
}
// Recursively calls creepForward().
function creep()
external
burnDripId(10)
{
// Succeed only if creepForward is called 7 times.
require(this.creepForward{value: address(this).balance}() == 7, 'too creepy');
emit Creep();
}
function creepForward()
external payable
returns (uint256 count)
{
unchecked {
count = 1;
if (msg.value != 0) {
try this.creepForward{value: msg.value - 1}() returns (uint256 count_) {
count += count_;
} catch {}
}
}
}
// Consume an unused signature generated by the admin and open the box.
function open(uint256 nonce, bytes calldata adminSig)
external
maxBalance(0)
maxDripCount(0)
minTotalDripped(10)
{
require(admin == _consumeSignature(bytes32(nonce), adminSig), 'not signed by admin');
// Congrats ;-)
emit Open(msg.sender);
}
function _consumeSignature(bytes32 h, bytes memory sig)
internal
returns (address signer)
{
require(signatureConsumedAt[sig] == 0, 'signature already used');
signatureConsumedAt[sig] = block.number;
signer = _recoverPackedSignature(h, sig);
}
function _recoverPackedSignature(bytes32 h, bytes memory sig)
internal
pure
returns (address)
{
require(sig.length == 65, 'invalid packed signature');
// unpack signature into r, s, v components
bytes32 r; bytes32 s; uint8 v;
assembly {
r := mload(add(sig, 0x20))
s := mload(add(sig, 0x40))
v := shr(248, mload(add(sig, 0x60)))
}
return ecrecover(h, v, r, s);
}
function _transferEth(address payable to, uint256 amount)
private
{
(bool s,) = to.call{value: amount}("");
require(s, 'transfer failed');
}
function _burnDrip(uint256 dripId)
internal
{
require(isValidDripId[dripId], 'missing drip id');
isValidDripId[dripId] = false;
--dripCount;
emit Burned(dripId);
}
function _getFriendshipHash(
address payable[] calldata friends,
uint256[] calldata friendsBps
)
internal
pure
returns (bytes32)
{
return keccak256(abi.encodePacked('fees', friends, friendsBps));
}
}
// Contract for instantiating puzzleboxes.
contract PuzzleBoxFactory {
PuzzleBox public immutable logic = new PuzzleBox();
function createPuzzleBox()
external
payable
returns (PuzzleBox puzzle)
{
PuzzleBoxProxy proxy = new PuzzleBoxProxy(logic);
proxy.lock(PuzzleBox.torch.selector, true);
puzzle = PuzzleBox(payable(proxy));
{
address payable[] memory friends = new address payable[](2);
uint256[] memory friendsCutBps = new uint256[](friends.length);
friends[0] = payable(0x416e59DaCfDb5D457304115bBFb9089531D873B7);
friends[1] = payable(0xC817dD2a5daA8f790677e399170c92AabD044b57);
friendsCutBps[0] = 0.015e4;
friendsCutBps[1] = 0.0075e4;
puzzle.initialize{value: 1337}(
// initialDripFee
100,
friends,
friendsCutBps,
// adminSigNonce
0xc8f549a7e4cb7e1c60d908cc05ceff53ad731e6ea0736edf7ffeea588dfb42d8,
// adminSig
(
hex"c8f549a7e4cb7e1c60d908cc05ceff53ad731e6ea0736edf7ffeea588dfb42d8"
hex"625cb970c2768fefafc3512a3ad9764560b330dcafe02714654fe48dd069b6df"
hex"1c"
)
);
}
}
}