-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyTest.t.sol
More file actions
351 lines (281 loc) · 11.3 KB
/
StrategyTest.t.sol
File metadata and controls
351 lines (281 loc) · 11.3 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import {Strings} from "openzeppelin-contracts/utils/Strings.sol";
// Interfaces
import {ISsovV3} from "../src/interfaces/ISsovV3.sol";
import {IERC20} from "../src/interfaces/IERC20.sol";
import {ISim} from "../src/interfaces/ISim.sol";
// Contracts
import {Simulate} from "../src/simulate/Simulate.sol";
import {Strategy} from "../src/strategy/Strategy.sol";
contract StrategyTest is Test {
address ssov;
Simulate sim;
Strategy strat;
uint256 epoch;
// Deposit variables
uint256[] public depositBlockNumbers;
uint256[] public depositStrikeIndexes;
uint256[] public depositAmounts;
// Purchase variables
uint256[] public purchaseBlockNumbers;
uint256[] public purchaseStrikeIndexes;
uint256[] public purchaseAmounts;
function setUp() public {
/*=== USER INPUT REQUIRED ===*/
// Select the SSOV V3 and epoch to test.
ssov = 0x10FD85ec522C245a63239b9FC64434F58520bd1f; // WEEKLY DPX CALLS
// Choose an epoch to test
epoch = 1;
// Input deposit parameters: block number, strike index and amount
depositBlockNumbers = [22962396, 23020472, 23244639];
depositStrikeIndexes = [3, 2, 1];
depositAmounts = [7.782e18, 11.427e18, 0.15e18];
// Input purchase parameters: block number, strike index and amount
purchaseBlockNumbers = [23165341, 23377592, 23402497];
purchaseStrikeIndexes = [0, 0, 0];
purchaseAmounts = [1e18, 6e18, 6e18];
/* === END USER INPUT ===*/
// Deploying contracts to Arbitrum fork & setting their state as persistent.
setupFork();
deploySimulateAndStrategy();
}
/*=== STRATEGY BACKTEST ===*/
// Description:
// User inputs SSOV strategy (desired deposits or purchases during an epoch for given block numbers, strike indexes and amounts).
// `testStrategy()` then simulates an approximate performance of this strategy based on past SSOV epochs (forked Arbitrum state).
// If the desired purchase amount in `purchaseAmounts` for the given block number & strike index exceeds the available collateral,
// the purchase amount will be adjusted to the available amount (emits a log when this occurs).
// To run in CL:
// forge test --match-test testStrategy -vvv
// Returns:
//
function testStrategy() public {
require(
((purchaseBlockNumbers.length == purchaseStrikeIndexes.length) &&
(purchaseBlockNumbers.length == purchaseAmounts.length)),
"PurchaseArrayLengthsNotEqual"
);
require(
((depositBlockNumbers.length == depositStrikeIndexes.length) &&
(depositBlockNumbers.length == depositAmounts.length)),
"DepositArrayLengthsNotEqual"
);
// PURCHASE -> SETTLE LOGIC
if (purchaseBlockNumbers.length != 0) {
purchaseCollateralChecker();
emit log_string(" ");
emit log_string(" ");
strat.createPurchases(
purchaseBlockNumbers,
purchaseStrikeIndexes,
purchaseAmounts
);
(
uint256[] memory purchaseIds,
uint256[] memory premiums,
uint256[] memory purchaseFees
) = strat.executePurchases();
uint256[] memory netPnls = strat.executeSettle(purchaseIds);
purchaseSummary(netPnls, premiums, purchaseFees);
}
emit log_string(" ");
emit log_string(" ");
// DEPOSIT -> WITHDRAW LOGIC
if (depositBlockNumbers.length != 0) {
strat.createDeposits(
depositBlockNumbers,
depositStrikeIndexes,
depositAmounts
);
uint256[] memory depositIds = strat.executeDeposits();
strat.executeWithdraw(depositIds);
depositSummary(depositIds);
}
}
/*=== SUMARRY FUNCTIONS ===*/
function purchaseSummary(
uint256[] memory netPnls,
uint256[] memory premiums,
uint256[] memory purchaseFees
) public {
emit log_string(
"##################### PURCHASES #####################"
);
emit log_string(" ");
for (uint256 i; i < purchaseBlockNumbers.length; ++i) {
emit log_string(concatenate("INDEX ", Strings.toString(i)));
emit log_string(
"===================================================="
);
emit log_named_string(
"block number",
Strings.toString(purchaseBlockNumbers[i])
);
emit log_named_string(
"strike index",
Strings.toString(purchaseStrikeIndexes[i])
);
emit log_named_string(
"strike",
Strings.toString(
ISsovV3(ssov).getEpochData(epoch).strikes[
purchaseStrikeIndexes[i]
]
)
);
emit log_named_string(
"amount",
Strings.toString(purchaseAmounts[i])
);
emit log_string(
"****************************************************"
);
emit log_named_int(
"dpx net pnl (units)",
int256(netPnls[i]) -
int256(premiums[i]) -
int256(purchaseFees[i])
);
emit log_string(" ");
}
}
function depositSummary(uint256[] memory depositIds) public {
emit log_string("##################### DEPOSITS #####################");
emit log_string(" ");
for (uint256 i; i < depositBlockNumbers.length; ++i) {
emit log_string(concatenate("INDEX ", Strings.toString(i)));
emit log_string(
"===================================================="
);
emit log_named_string(
"block number",
Strings.toString(depositBlockNumbers[i])
);
emit log_named_string(
"strike index",
Strings.toString(depositStrikeIndexes[i])
);
emit log_named_string(
"strike",
Strings.toString(
ISsovV3(ssov).getEpochData(epoch).strikes[
depositStrikeIndexes[i]
]
)
);
emit log_named_string(
"amount",
Strings.toString(depositAmounts[i])
);
emit log_string(
"****************************************************"
);
(
uint256 collateralTokenWithdrawAmount,
uint256[] memory rewardTokenWithdrawAmounts
) = strat.returnWithdrawDetails(depositIds[i]);
emit log_named_string(
"collateralTokenWithdrawAmount",
Strings.toString(collateralTokenWithdrawAmount)
);
for (uint256 x; x < rewardTokenWithdrawAmounts.length; ++x) {
string memory symbol = IERC20(
ISsovV3(ssov).getEpochData(epoch).rewardTokensToDistribute[
x
]
).symbol();
emit log_named_string(
concatenate("reward token ", symbol),
Strings.toString(rewardTokenWithdrawAmounts[x])
);
}
emit log_named_int(
"net dpx return (units)",
int256(collateralTokenWithdrawAmount) -
int256(depositAmounts[i]) +
int256(rewardTokenWithdrawAmounts[0])
);
emit log_string(" ");
}
}
/*=== CHECK OPTIONS AVAILABLE FOR PURCHASE ===*/
// To run in CL:
// forge test --match-test testOptionsAvailableForPurchase -vvv
// Returns:
// strike indexes available for the epoch (corresponding strike with 1e8 precision)
// available collateral for purchase at each strike index for a given epoch and given block (1e18 precision)
// Run this function whenever you want to verify the number of options available for purchase.
function testOptionsAvailableForPurchase() public {
for (uint256 i; i < purchaseBlockNumbers.length; ++i) {
setupForkBlockSpecified(purchaseBlockNumbers[i]);
emit log_string(
"*******************************************************************************"
);
emit log_string(
concatenate(
"Block Number: ",
Strings.toString(purchaseBlockNumbers[i])
)
);
strat.optionsAvailableForPurchase();
}
}
/*=== HELPER FUNCTIONS ===*/
function setupFork() public returns (uint256 id) {
id = vm.createSelectFork(vm.rpcUrl("arbitrum"));
assertEq(vm.activeFork(), id);
}
function setupForkBlockSpecified(uint256 blk) public returns (uint256 id) {
id = vm.createSelectFork(vm.rpcUrl("arbitrum"), blk);
assertEq(vm.activeFork(), id);
}
function deploySimulateAndStrategy() public {
sim = new Simulate();
strat = new Strategy(address(sim), ssov, epoch);
vm.makePersistent(address(sim), address(strat));
}
function concatenate(string memory _a, string memory _b)
public
pure
returns (string memory)
{
return string(abi.encodePacked(_a, _b));
}
function purchaseCollateralChecker() public {
for (uint256 i; i < purchaseBlockNumbers.length; ++i) {
setupForkBlockSpecified(purchaseBlockNumbers[i]);
uint256 strike = ISsovV3(ssov).getEpochData(epoch).strikes[
purchaseStrikeIndexes[i]
];
uint256 availableCollateral = ISsovV3(ssov)
.getEpochStrikeData(epoch, strike)
.totalCollateral -
ISsovV3(ssov)
.getEpochStrikeData(epoch, strike)
.activeCollateral;
uint256 requiredCollateral = ISsovV3(ssov).isPut()
? (purchaseAmounts[i] *
strike *
ISsovV3(ssov).collateralPrecision() *
ISsovV3(ssov).getEpochData(epoch).collateralExchangeRate) /
(1e34)
: (purchaseAmounts[i] *
ISsovV3(ssov).getEpochData(epoch).collateralExchangeRate *
ISsovV3(ssov).collateralPrecision()) / (1e26);
if (requiredCollateral > availableCollateral) {
string memory temp = concatenate(
"Purchase amount at index ",
Strings.toString(i)
);
temp = concatenate(temp, " changed from ");
temp = concatenate(temp, Strings.toString(purchaseAmounts[i]));
temp = concatenate(temp, " to ");
purchaseAmounts[i] = availableCollateral;
temp = concatenate(temp, Strings.toString(purchaseAmounts[i]));
emit log_string(temp);
}
}
}
}