-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathabaci.t.sol
More file actions
250 lines (200 loc) · 9.02 KB
/
Copy pathabaci.t.sol
File metadata and controls
250 lines (200 loc) · 9.02 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// abaci.t.sol -- tests for abaci.sol
// Copyright (C) 2021-2022 Dai Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity ^0.6.12;
import "ds-test/test.sol";
import "../abaci.sol";
interface Hevm {
function warp(uint256) external;
function store(address,bytes32,bytes32) external;
}
interface Vm {
function expectRevert(bytes calldata) external;
}
contract ClipperTest is DSTest {
Hevm hevm;
uint256 RAY = 10 ** 27;
// CHEAT_CODE = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D
bytes20 constant CHEAT_CODE =
bytes20(uint160(uint256(keccak256('hevm cheat code'))));
uint256 constant startTime = 604411200; // Used to avoid issues with `now`
function setUp() public {
hevm = Hevm(address(CHEAT_CODE));
hevm.warp(startTime);
}
function assertEqWithinTolerance(
uint256 x,
uint256 y,
uint256 tolerance) internal {
uint256 diff;
if (x >= y) {
diff = x - y;
} else {
diff = y - x;
}
assertTrue(diff <= tolerance);
}
function checkExpDecrease(
StairstepExponentialDecrease calc,
uint256 cut,
uint256 step,
uint256 top,
uint256 tic,
uint256 percentDecrease,
uint256 testTime,
uint256 tolerance
)
public
{
uint256 price;
uint256 lastPrice;
uint256 testPrice;
hevm.warp(startTime);
calc.file(bytes32("step"), step);
calc.file(bytes32("cut"), cut);
price = calc.price(top, now - tic);
assertEq(price, top);
for(uint256 i = 1; i < testTime; i += 1) {
hevm.warp(startTime + i);
lastPrice = price;
price = calc.price(top, now - tic);
// Stairstep calculation
if (i % step == 0) { testPrice = lastPrice * percentDecrease / RAY; }
else { testPrice = lastPrice; }
assertEqWithinTolerance(testPrice, price, tolerance);
}
}
function test_stairstep_exp_decrease() public {
StairstepExponentialDecrease calc = new StairstepExponentialDecrease();
uint256 tic = now; // Start of auction
uint256 percentDecrease;
uint256 step;
uint256 testTime = 10 minutes;
/*** Extreme high collateral price ($50m) ***/
uint256 tolerance = 100000000; // Tolerance scales with price
uint256 top = 50000000 * RAY;
// 1.1234567890% decrease every 1 second
// TODO: Check if there's a cleaner way to do this. I was getting rational_const errors.
percentDecrease = RAY - 1.1234567890E27 / 100;
step = 1;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 2.1234567890% decrease every 1 second
percentDecrease = RAY - 2.1234567890E27 / 100;
step = 1;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 1.1234567890% decrease every 5 seconds
percentDecrease = RAY - 1.1234567890E27 / 100;
step = 5;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 2.1234567890% decrease every 5 seconds
percentDecrease = RAY - 2.1234567890E27 / 100;
step = 5;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 1.1234567890% decrease every 5 minutes
percentDecrease = RAY - 1.1234567890E27 / 100;
step = 5 minutes;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
/*** Extreme low collateral price ($0.0000001) ***/
tolerance = 1; // Lowest tolerance is 1e-27
top = 1 * RAY / 10000000;
// 1.1234567890% decrease every 1 second
percentDecrease = RAY - 1.1234567890E27 / 100;
step = 1;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 2.1234567890% decrease every 1 second
percentDecrease = RAY - 2.1234567890E27 / 100;
step = 1;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 1.1234567890% decrease every 5 seconds
percentDecrease = RAY - 1.1234567890E27 / 100;
step = 5;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 2.1234567890% decrease every 5 seconds
percentDecrease = RAY - 2.1234567890E27 / 100;
step = 5;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
// 1.1234567890% decrease every 5 minutes
percentDecrease = RAY - 1.1234567890E27 / 100;
step = 5 minutes;
checkExpDecrease(calc, percentDecrease, step, top, tic, percentDecrease, testTime, tolerance);
}
function test_continuous_exp_decrease() public {
ExponentialDecrease calc = new ExponentialDecrease();
uint256 tHalf = 900;
uint256 cut = 0.999230132966E27; // ~15 half life, cut ~= e^(ln(1/2)/900)
calc.file("cut", cut);
uint256 top = 4000 * RAY;
uint256 expectedPrice = top;
uint256 tolerance = RAY / 1000; // 0.001, i.e 0.1%
for (uint256 i = 0; i < 5; i++) { // will cover initial value + four half-lives
assertEqWithinTolerance(calc.price(top, i*tHalf), expectedPrice, tolerance);
// each loop iteration advances one half-life, so expectedPrice decreases by a factor of 2
expectedPrice /= 2;
}
}
function test_linear_decrease() public {
hevm.warp(startTime);
LinearDecrease calc = new LinearDecrease();
calc.file(bytes32("tau"), 3600);
uint256 top = 1000 * RAY;
uint256 tic = now; // Start of auction
uint256 price = calc.price(top, now - tic);
assertEq(price, top);
hevm.warp(startTime + 360); // 6min in, 1/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100) * RAY);
hevm.warp(startTime + 360 * 2); // 12min in, 2/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 2) * RAY);
hevm.warp(startTime + 360 * 3); // 18min in, 3/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 3) * RAY);
hevm.warp(startTime + 360 * 4); // 24min in, 4/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 4) * RAY);
hevm.warp(startTime + 360 * 5); // 30min in, 5/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 5) * RAY);
hevm.warp(startTime + 360 * 6); // 36min in, 6/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 6) * RAY);
hevm.warp(startTime + 360 * 7); // 42min in, 7/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 7) * RAY);
hevm.warp(startTime + 360 * 8); // 48min in, 8/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 8) * RAY);
hevm.warp(startTime + 360 * 9); // 54min in, 9/10 done
price = calc.price(top, now - tic);
assertEq(price, (1000 - 100 * 9) * RAY);
hevm.warp(startTime + 360 * 10); // 60min in, 10/10 done
price = calc.price(top, now - tic);
assertEq(price, 0);
}
// --- Revert tests for zero-value parameter validation ---
// LinearDecrease must reject tau = 0 to prevent division-by-zero in price()
function test_linear_decrease_tau_zero_reverts() public {
LinearDecrease calc = new LinearDecrease();
Vm(address(CHEAT_CODE)).expectRevert(bytes("LinearDecrease/tau-is-zero"));
calc.file(bytes32("tau"), 0);
}
// StairstepExponentialDecrease must reject step = 0 to prevent division-by-zero in price()
function test_stairstep_step_zero_reverts() public {
StairstepExponentialDecrease calc = new StairstepExponentialDecrease();
Vm(address(CHEAT_CODE)).expectRevert(bytes("StairstepExponentialDecrease/step-is-zero"));
calc.file(bytes32("step"), 0);
}
}