-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathgst2_free_example.sol
78 lines (64 loc) · 2.46 KB
/
gst2_free_example.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
contract GST2 {
function freeUpTo(uint256 value) public returns (uint256 freed);
function freeFromUpTo(address from, uint256 value) public returns (uint256 freed);
}
contract GST2FreeExample {
function freeExample(uint num_tokens) public returns (uint freed) {
// we need at least
// num_tokens * (1148 + 5722 + 150) + 25710 gas before entering destroyChildren
// ^ mk_contract_address
// ^ solidity bug constant
// ^ cost of invocation
// ^ loop, etc...
// to be on the safe side, let's add another constant 2k gas
// for CALLing freeFrom, reading from storage, etc...
// so we get
// gas cost to freeFromUpTo n tokens <= 27710 + n * (1148 + 5722 + 150)
// Note that 27710 is sufficiently large that we always have enough
// gas left to update s_tail, balance, etc... after we are done
// with destroyChildren.
GST2 gst2 = GST2(0x0000000000b3F879cb30FE243b4Dfee438691c04);
uint safe_num_tokens = 0;
uint gas = msg.gas;
if (gas >= 27710) {
safe_num_tokens = (gas - 27710) / (1148 + 5722 + 150);
}
if (num_tokens > safe_num_tokens) {
num_tokens = safe_num_tokens;
}
if (num_tokens > 0) {
return gst2.freeUpTo(num_tokens);
} else {
return 0;
}
}
function freeFromExample(address from, uint num_tokens) public returns (uint freed) {
// we need at least
// num_tokens * (1148 + 5722 + 150) + 25710 gas before entering destroyChildren
// ^ mk_contract_address
// ^ solidity bug constant
// ^ cost of invocation
// ^ loop, etc...
// to be on the safe side, let's add another constant 2k gas
// for CALLing freeFrom, reading from storage, etc...
// so we get
// gas cost to freeFromUpTo n tokens <= 27710 + n * (1148 + 5722 + 150)
// Note that 27710 is sufficiently large that we always have enough
// gas left to update s_tail, balance, etc... after we are done
// with destroyChildren.
GST2 gst2 = GST2(0x0000000000b3F879cb30FE243b4Dfee438691c04);
uint safe_num_tokens = 0;
uint gas = msg.gas;
if (gas >= 27710) {
safe_num_tokens = (gas - 27710) / (1148 + 5722 + 150);
}
if (num_tokens > safe_num_tokens) {
num_tokens = safe_num_tokens;
}
if (num_tokens > 0) {
return gst2.freeFromUpTo(from, num_tokens);
} else {
return 0;
}
}
}