-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathETHSP.t.sol
297 lines (250 loc) · 9.41 KB
/
ETHSP.t.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
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.4;
import "solmate/test/utils/mocks/MockERC20.sol";
import "solmate/utils/SafeCastLib.sol";
import "../contracts/test/ERC20Wrapper.sol";
import "./Setup.sol";
using SafeCastLib for uint256;
function to128(uint256 x) pure returns (uint128 y) {
y = x.safeCastTo128();
}
contract ETHSP is Setup {
using { to128 } for uint256;
using NormalConfiguration for Configuration;
Configuration CONFIG;
address TOKEN_0;
address TOKEN_1;
address TOKEN_2;
address TOKEN_3;
uint64 TRANCHE_A_POOL;
uint64 TRANCHE_B_POOL;
address TRANCHE_A_TOKEN;
address TRANCHE_B_TOKEN;
address ETHSP_TOKEN;
address PRIMARY_MARKET_USER;
function create_actor() public {
PRIMARY_MARKET_USER = address(0x0444);
}
modifier usePrimaryMarketUser() {
vm.startPrank(PRIMARY_MARKET_USER);
_;
vm.stopPrank();
}
/// @dev ETHSP is a pool of two liquidity pool tokens.
function test_ethsp() public {
create_actor();
_create_tokens();
_create_pairs();
_create_tranche_pools();
_tokenize_tranches();
_tokenize_ethsp();
approve_tokens();
allocate();
issue();
rebalance();
}
function approve_tokens() public usePrimaryMarketUser {
// Approves tokens to subject (portfolio)
MockERC20(TOKEN_0).approve(address(subject()), type(uint256).max);
MockERC20(TOKEN_1).approve(address(subject()), type(uint256).max);
MockERC20(TOKEN_2).approve(address(subject()), type(uint256).max);
MockERC20(TOKEN_3).approve(address(subject()), type(uint256).max);
// Approve tokenized pools to subject (portfolio)
MockERC20(TRANCHE_A_TOKEN).approve(
address(subject()), type(uint256).max
);
MockERC20(TRANCHE_B_TOKEN).approve(
address(subject()), type(uint256).max
);
MockERC20(ETHSP_TOKEN).approve(address(subject()), type(uint256).max);
}
function allocate() public usePrimaryMarketUser {
// Allocate 10 liquidity to each tranche
uint256 amount = 10 ether;
bytes[] memory calls = new bytes[](2);
calls[0] = abi.encodeWithSelector(
IPortfolioActions.allocate.selector,
false,
PRIMARY_MARKET_USER,
TRANCHE_A_POOL,
amount,
type(uint128).max,
type(uint128).max
);
calls[1] = abi.encodeWithSelector(
IPortfolioActions.allocate.selector,
false,
PRIMARY_MARKET_USER,
TRANCHE_B_POOL,
amount,
type(uint128).max,
type(uint128).max
);
subject().multicall(calls);
}
function issue() public usePrimaryMarketUser {
// Gets amounts of liquidity to wrap into ETHSP token.
uint256 amount = 1 ether;
ERC1155(address(subject())).setApprovalForAll(ETHSP_TOKEN, true);
ERC1155(address(subject())).setApprovalForAll(ETHSP_TOKEN, true);
ERC20Wrapper(ETHSP_TOKEN).mint(PRIMARY_MARKET_USER, amount);
}
function rebalance() public usePrimaryMarketUser {
// Assume I receive 1 ETHSP token.
// For example, I purchase it on a DEX for $0.9,
// but the underlying assets are worth $1.0.
// I want to redeem it for the underlying assest and sell it into the market.
uint256[4] memory startBalances = [
MockERC20(TOKEN_0).balanceOf(PRIMARY_MARKET_USER),
MockERC20(TOKEN_1).balanceOf(PRIMARY_MARKET_USER),
MockERC20(TOKEN_2).balanceOf(PRIMARY_MARKET_USER),
MockERC20(TOKEN_3).balanceOf(PRIMARY_MARKET_USER)
];
// Redeem ETHSP
uint256 amount = 1 ether;
ERC20Wrapper(ETHSP_TOKEN).burn(PRIMARY_MARKET_USER, amount);
// Remove liquidity from both ERC115 pool tokens.
bytes[] memory calls = new bytes[](2);
calls[0] = abi.encodeWithSelector(
IPortfolioActions.deallocate.selector,
false,
TRANCHE_A_POOL,
amount,
0,
0
);
calls[1] = abi.encodeWithSelector(
IPortfolioActions.deallocate.selector,
false,
TRANCHE_B_POOL,
amount,
0,
0
);
subject().multicall(calls);
uint256[4] memory endBalances = [
MockERC20(TOKEN_0).balanceOf(PRIMARY_MARKET_USER),
MockERC20(TOKEN_1).balanceOf(PRIMARY_MARKET_USER),
MockERC20(TOKEN_2).balanceOf(PRIMARY_MARKET_USER),
MockERC20(TOKEN_3).balanceOf(PRIMARY_MARKET_USER)
];
uint256[4] memory deltas = [
endBalances[0] - startBalances[0],
endBalances[1] - startBalances[1],
endBalances[2] - startBalances[2],
endBalances[3] - startBalances[3]
];
console2.log("ethsp delta: -", amount);
for (uint256 i; i < deltas.length; i++) {
emit delta(i, deltas[i]);
}
// Send the tokens to pay the DEX, or anyone else, back!
}
event delta(uint256 i, uint256 amt);
function _create_tokens() internal {
// Basket A
TOKEN_0 = address(new MockERC20("Token0", "0", 18));
TOKEN_1 = address(new MockERC20("Token1", "1", 18));
// Basket B
TOKEN_2 = address(new MockERC20("Token2", "2", 18));
TOKEN_3 = address(new MockERC20("Token3", "3", 18));
// Labels
vm.label(TOKEN_0, "Token0");
vm.label(TOKEN_1, "Token1");
vm.label(TOKEN_2, "Token2");
vm.label(TOKEN_3, "Token3");
// Mint some tokens to primary market user.
MockERC20(TOKEN_0).mint(PRIMARY_MARKET_USER, 1_000_000_000 ether);
MockERC20(TOKEN_1).mint(PRIMARY_MARKET_USER, 1_000_000_000 ether);
MockERC20(TOKEN_2).mint(PRIMARY_MARKET_USER, 1_000_000_000 ether);
MockERC20(TOKEN_3).mint(PRIMARY_MARKET_USER, 1_000_000_000 ether);
}
function _create_pairs() internal {
// Create both pairs
uint24 TRANCHE_A_PAIR = subject().createPair(TOKEN_0, TOKEN_1);
uint24 TRANCHE_B_PAIR = subject().createPair(TOKEN_2, TOKEN_3);
}
function _create_tranche_pools() internal {
// Create both pools
uint256 volatility_bps = 1000;
uint256 duration_sec = SECONDS_PER_YEAR;
uint256 strike_price = 2000 ether;
uint256 last_price = 1800 ether;
uint256 fee_bps = 10;
uint256 priority_fee_bps = 0;
// Pool config is specific to the NormalStrategy.
PortfolioConfig memory POOL_CONFIG = PortfolioConfig({
strikePriceWad: strike_price.to128(),
volatilityBasisPoints: volatility_bps.safeCastTo32(),
durationSeconds: duration_sec.safeCastTo32(),
isPerpetual: false,
creationTimestamp: uint32(block.timestamp)
});
// Makes a defaut config which defines the Portfolio pool state.
CONFIG = configure();
// Edits the fee of the pool.
CONFIG = CONFIG.edit("feeBasisPoints", abi.encode(fee_bps)).edit(
"priorityFeeBasisPoints", abi.encode(priority_fee_bps)
);
// Adds the strategy specific arguments to the config, by computing the reserves and also encoding the strategy data.
CONFIG = CONFIG.combine(POOL_CONFIG);
// Updates the strategy to have the reserves match a price.
CONFIG = CONFIG.editStrategy("priceWad", abi.encode(last_price));
// Applies the respective tokens to each config.
Configuration memory TRANCHE_A_CONFIG = CONFIG.edit(
"asset", abi.encode(TOKEN_0)
).edit("quote", abi.encode(TOKEN_1));
TRANCHE_A_CONFIG.strategy = normalStrategy();
Configuration memory TRANCHE_B_CONFIG = CONFIG.edit(
"asset", abi.encode(TOKEN_2)
).edit("quote", abi.encode(TOKEN_3));
TRANCHE_B_CONFIG.strategy = normalStrategy();
// Creates the pools with the configs.
TRANCHE_A_POOL = TRANCHE_A_CONFIG.activate(
address(subject()), NormalConfiguration.validateNormalStrategy
);
TRANCHE_B_POOL = TRANCHE_B_CONFIG.activate(
address(subject()), NormalConfiguration.validateNormalStrategy
);
}
function _tokenize_tranches() public {
uint64[] memory a_pools = new uint64[](1);
a_pools[0] = TRANCHE_A_POOL;
TRANCHE_A_TOKEN = address(
new ERC20Wrapper(
address(subject()),
a_pools,
"ETHSP Tranche A",
"ETHSP-A"
)
);
uint64[] memory b_pools = new uint64[](1);
b_pools[0] = TRANCHE_B_POOL;
TRANCHE_B_TOKEN = address(
new ERC20Wrapper(
address(subject()),
b_pools,
"ETHSP Tranche B",
"ETHSP-B"
)
);
// Labels
vm.label(TRANCHE_A_TOKEN, "ETHSP Tranche A");
vm.label(TRANCHE_B_TOKEN, "ETHSP Tranche B");
}
function _tokenize_ethsp() public {
uint64[] memory ethsp_pools = new uint64[](2);
ethsp_pools[0] = TRANCHE_A_POOL;
ethsp_pools[1] = TRANCHE_B_POOL;
ETHSP_TOKEN = address(
new ERC20Wrapper(
address(subject()),
ethsp_pools,
"ETHSP",
"ETHSP"
)
);
// Labels
vm.label(ETHSP_TOKEN, "ETHSP");
}
}