@juicedollar/jusd 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +356 -0
  3. package/contracts/Equity.sol +457 -0
  4. package/contracts/JuiceDollar.sol +363 -0
  5. package/contracts/Leadrate.sol +79 -0
  6. package/contracts/MintingHubV2/MintingHub.sol +445 -0
  7. package/contracts/MintingHubV2/Position.sol +810 -0
  8. package/contracts/MintingHubV2/PositionFactory.sol +69 -0
  9. package/contracts/MintingHubV2/PositionRoller.sol +159 -0
  10. package/contracts/MintingHubV2/interface/IMintingHub.sol +26 -0
  11. package/contracts/MintingHubV2/interface/IPosition.sol +90 -0
  12. package/contracts/MintingHubV2/interface/IPositionFactory.sol +20 -0
  13. package/contracts/Savings.sol +141 -0
  14. package/contracts/SavingsVaultJUSD.sol +140 -0
  15. package/contracts/StablecoinBridge.sol +109 -0
  16. package/contracts/StartUSD.sol +16 -0
  17. package/contracts/gateway/CoinLendingGateway.sol +223 -0
  18. package/contracts/gateway/FrontendGateway.sol +224 -0
  19. package/contracts/gateway/MintingHubGateway.sol +87 -0
  20. package/contracts/gateway/SavingsGateway.sol +51 -0
  21. package/contracts/gateway/interface/ICoinLendingGateway.sol +73 -0
  22. package/contracts/gateway/interface/IFrontendGateway.sol +49 -0
  23. package/contracts/gateway/interface/IMintingHubGateway.sol +12 -0
  24. package/contracts/impl/ERC3009.sol +171 -0
  25. package/contracts/interface/IJuiceDollar.sol +54 -0
  26. package/contracts/interface/ILeadrate.sol +7 -0
  27. package/contracts/interface/IReserve.sol +9 -0
  28. package/contracts/interface/ISavingsJUSD.sol +49 -0
  29. package/contracts/test/FreakToken.sol +25 -0
  30. package/contracts/test/Math.sol +339 -0
  31. package/contracts/test/MockEquity.sol +15 -0
  32. package/contracts/test/PositionExpirationTest.sol +75 -0
  33. package/contracts/test/PositionRollingTest.sol +65 -0
  34. package/contracts/test/TestFlashLoan.sol +84 -0
  35. package/contracts/test/TestFlashLoanGateway.sol +49 -0
  36. package/contracts/test/TestMathUtil.sol +40 -0
  37. package/contracts/test/TestToken.sol +45 -0
  38. package/contracts/test/TestWcBTC.sol +35 -0
  39. package/contracts/utils/MathUtil.sol +61 -0
  40. package/dist/index.d.mts +8761 -0
  41. package/dist/index.d.ts +8761 -0
  42. package/dist/index.js +11119 -0
  43. package/dist/index.mjs +11073 -0
  44. package/exports/abis/MintingHubV2/PositionFactoryV2.ts +90 -0
  45. package/exports/abis/MintingHubV2/PositionRoller.ts +183 -0
  46. package/exports/abis/MintingHubV2/PositionV2.ts +999 -0
  47. package/exports/abis/core/CoinLendingGateway.ts +427 -0
  48. package/exports/abis/core/Equity.ts +1286 -0
  49. package/exports/abis/core/FrontendGateway.ts +906 -0
  50. package/exports/abis/core/JuiceDollar.ts +1366 -0
  51. package/exports/abis/core/MintingHubGateway.ts +865 -0
  52. package/exports/abis/core/SavingsGateway.ts +559 -0
  53. package/exports/abis/core/SavingsVaultJUSD.ts +920 -0
  54. package/exports/abis/utils/ERC20.ts +310 -0
  55. package/exports/abis/utils/ERC20PermitLight.ts +520 -0
  56. package/exports/abis/utils/Leadrate.ts +175 -0
  57. package/exports/abis/utils/MintingHubV2.ts +682 -0
  58. package/exports/abis/utils/Ownable.ts +76 -0
  59. package/exports/abis/utils/Savings.ts +453 -0
  60. package/exports/abis/utils/StablecoinBridge.ts +209 -0
  61. package/exports/abis/utils/StartUSD.ts +315 -0
  62. package/exports/abis/utils/UniswapV3Pool.ts +638 -0
  63. package/exports/address.config.ts +48 -0
  64. package/exports/index.ts +28 -0
  65. package/package.json +87 -0
@@ -0,0 +1,339 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // OpenZeppelin Contracts (last updated v4.8.0) (utils/math/Math.sol)
3
+
4
+ pragma solidity ^0.8.0;
5
+
6
+ /**
7
+ * @dev Standard math utilities missing in the Solidity language.
8
+ */
9
+ library Math {
10
+ enum Rounding {
11
+ Down, // Toward negative infinity
12
+ Up, // Toward infinity
13
+ Zero // Toward zero
14
+ }
15
+
16
+ /**
17
+ * @dev Returns the largest of two numbers.
18
+ */
19
+ function max(uint256 a, uint256 b) internal pure returns (uint256) {
20
+ return a > b ? a : b;
21
+ }
22
+
23
+ /**
24
+ * @dev Returns the smallest of two numbers.
25
+ */
26
+ function min(uint256 a, uint256 b) internal pure returns (uint256) {
27
+ return a < b ? a : b;
28
+ }
29
+
30
+ /**
31
+ * @dev Returns the average of two numbers. The result is rounded towards
32
+ * zero.
33
+ */
34
+ function average(uint256 a, uint256 b) internal pure returns (uint256) {
35
+ // (a + b) / 2 can overflow.
36
+ return (a & b) + (a ^ b) / 2;
37
+ }
38
+
39
+ /**
40
+ * @dev Returns the ceiling of the division of two numbers.
41
+ *
42
+ * This differs from standard division with `/` in that it rounds up instead
43
+ * of rounding down.
44
+ */
45
+ function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
46
+ // (a + b - 1) / b can overflow on addition, so we distribute.
47
+ return a == 0 ? 0 : (a - 1) / b + 1;
48
+ }
49
+
50
+ /**
51
+ * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
52
+ * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
53
+ * with further edits by Uniswap Labs also under MIT license.
54
+ */
55
+ function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
56
+ unchecked {
57
+ // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
58
+ // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
59
+ // variables such that product = prod1 * 2^256 + prod0.
60
+ uint256 prod0; // Least significant 256 bits of the product
61
+ uint256 prod1; // Most significant 256 bits of the product
62
+ assembly {
63
+ let mm := mulmod(x, y, not(0))
64
+ prod0 := mul(x, y)
65
+ prod1 := sub(sub(mm, prod0), lt(mm, prod0))
66
+ }
67
+
68
+ // Handle non-overflow cases, 256 by 256 division.
69
+ if (prod1 == 0) {
70
+ // Solidity will revert if denominator == 0, unlike the div opcode on its own.
71
+ // The surrounding unchecked block does not change this fact.
72
+ // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
73
+ return prod0 / denominator;
74
+ }
75
+
76
+ // Make sure the result is less than 2^256. Also prevents denominator == 0.
77
+ require(denominator > prod1, "Math: mulDiv overflow");
78
+
79
+ ///////////////////////////////////////////////
80
+ // 512 by 256 division.
81
+ ///////////////////////////////////////////////
82
+
83
+ // Make division exact by subtracting the remainder from [prod1 prod0].
84
+ uint256 remainder;
85
+ assembly {
86
+ // Compute remainder using mulmod.
87
+ remainder := mulmod(x, y, denominator)
88
+
89
+ // Subtract 256 bit number from 512 bit number.
90
+ prod1 := sub(prod1, gt(remainder, prod0))
91
+ prod0 := sub(prod0, remainder)
92
+ }
93
+
94
+ // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
95
+ // See https://cs.stackexchange.com/q/138556/92363.
96
+
97
+ // Does not overflow because the denominator cannot be zero at this stage in the function.
98
+ uint256 twos = denominator & (~denominator + 1);
99
+ assembly {
100
+ // Divide denominator by twos.
101
+ denominator := div(denominator, twos)
102
+
103
+ // Divide [prod1 prod0] by twos.
104
+ prod0 := div(prod0, twos)
105
+
106
+ // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
107
+ twos := add(div(sub(0, twos), twos), 1)
108
+ }
109
+
110
+ // Shift in bits from prod1 into prod0.
111
+ prod0 |= prod1 * twos;
112
+
113
+ // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
114
+ // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
115
+ // four bits. That is, denominator * inv = 1 mod 2^4.
116
+ uint256 inverse = (3 * denominator) ^ 2;
117
+
118
+ // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
119
+ // in modular arithmetic, doubling the correct bits in each step.
120
+ inverse *= 2 - denominator * inverse; // inverse mod 2^8
121
+ inverse *= 2 - denominator * inverse; // inverse mod 2^16
122
+ inverse *= 2 - denominator * inverse; // inverse mod 2^32
123
+ inverse *= 2 - denominator * inverse; // inverse mod 2^64
124
+ inverse *= 2 - denominator * inverse; // inverse mod 2^128
125
+ inverse *= 2 - denominator * inverse; // inverse mod 2^256
126
+
127
+ // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
128
+ // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
129
+ // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
130
+ // is no longer required.
131
+ result = prod0 * inverse;
132
+ return result;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
138
+ */
139
+ function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
140
+ uint256 result = mulDiv(x, y, denominator);
141
+ if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
142
+ result += 1;
143
+ }
144
+ return result;
145
+ }
146
+
147
+ /**
148
+ * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
149
+ *
150
+ * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
151
+ */
152
+ function sqrt(uint256 a) internal pure returns (uint256) {
153
+ if (a == 0) {
154
+ return 0;
155
+ }
156
+
157
+ // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
158
+ //
159
+ // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
160
+ // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
161
+ //
162
+ // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
163
+ // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
164
+ // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
165
+ //
166
+ // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
167
+ uint256 result = 1 << (log2(a) >> 1);
168
+
169
+ // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
170
+ // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
171
+ // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
172
+ // into the expected uint128 result.
173
+ unchecked {
174
+ result = (result + a / result) >> 1;
175
+ result = (result + a / result) >> 1;
176
+ result = (result + a / result) >> 1;
177
+ result = (result + a / result) >> 1;
178
+ result = (result + a / result) >> 1;
179
+ result = (result + a / result) >> 1;
180
+ result = (result + a / result) >> 1;
181
+ return min(result, a / result);
182
+ }
183
+ }
184
+
185
+ /**
186
+ * @notice Calculates sqrt(a), following the selected rounding direction.
187
+ */
188
+ function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
189
+ unchecked {
190
+ uint256 result = sqrt(a);
191
+ return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
192
+ }
193
+ }
194
+
195
+ /**
196
+ * @dev Return the log in base 2, rounded down, of a positive value.
197
+ * Returns 0 if given 0.
198
+ */
199
+ function log2(uint256 value) internal pure returns (uint256) {
200
+ uint256 result = 0;
201
+ unchecked {
202
+ if (value >> 128 > 0) {
203
+ value >>= 128;
204
+ result += 128;
205
+ }
206
+ if (value >> 64 > 0) {
207
+ value >>= 64;
208
+ result += 64;
209
+ }
210
+ if (value >> 32 > 0) {
211
+ value >>= 32;
212
+ result += 32;
213
+ }
214
+ if (value >> 16 > 0) {
215
+ value >>= 16;
216
+ result += 16;
217
+ }
218
+ if (value >> 8 > 0) {
219
+ value >>= 8;
220
+ result += 8;
221
+ }
222
+ if (value >> 4 > 0) {
223
+ value >>= 4;
224
+ result += 4;
225
+ }
226
+ if (value >> 2 > 0) {
227
+ value >>= 2;
228
+ result += 2;
229
+ }
230
+ if (value >> 1 > 0) {
231
+ result += 1;
232
+ }
233
+ }
234
+ return result;
235
+ }
236
+
237
+ /**
238
+ * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
239
+ * Returns 0 if given 0.
240
+ */
241
+ function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
242
+ unchecked {
243
+ uint256 result = log2(value);
244
+ return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
245
+ }
246
+ }
247
+
248
+ /**
249
+ * @dev Return the log in base 10, rounded down, of a positive value.
250
+ * Returns 0 if given 0.
251
+ */
252
+ function log10(uint256 value) internal pure returns (uint256) {
253
+ uint256 result = 0;
254
+ unchecked {
255
+ if (value >= 10 ** 64) {
256
+ value /= 10 ** 64;
257
+ result += 64;
258
+ }
259
+ if (value >= 10 ** 32) {
260
+ value /= 10 ** 32;
261
+ result += 32;
262
+ }
263
+ if (value >= 10 ** 16) {
264
+ value /= 10 ** 16;
265
+ result += 16;
266
+ }
267
+ if (value >= 10 ** 8) {
268
+ value /= 10 ** 8;
269
+ result += 8;
270
+ }
271
+ if (value >= 10 ** 4) {
272
+ value /= 10 ** 4;
273
+ result += 4;
274
+ }
275
+ if (value >= 10 ** 2) {
276
+ value /= 10 ** 2;
277
+ result += 2;
278
+ }
279
+ if (value >= 10 ** 1) {
280
+ result += 1;
281
+ }
282
+ }
283
+ return result;
284
+ }
285
+
286
+ /**
287
+ * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
288
+ * Returns 0 if given 0.
289
+ */
290
+ function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
291
+ unchecked {
292
+ uint256 result = log10(value);
293
+ return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
294
+ }
295
+ }
296
+
297
+ /**
298
+ * @dev Return the log in base 256, rounded down, of a positive value.
299
+ * Returns 0 if given 0.
300
+ *
301
+ * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
302
+ */
303
+ function log256(uint256 value) internal pure returns (uint256) {
304
+ uint256 result = 0;
305
+ unchecked {
306
+ if (value >> 128 > 0) {
307
+ value >>= 128;
308
+ result += 16;
309
+ }
310
+ if (value >> 64 > 0) {
311
+ value >>= 64;
312
+ result += 8;
313
+ }
314
+ if (value >> 32 > 0) {
315
+ value >>= 32;
316
+ result += 4;
317
+ }
318
+ if (value >> 16 > 0) {
319
+ value >>= 16;
320
+ result += 2;
321
+ }
322
+ if (value >> 8 > 0) {
323
+ result += 1;
324
+ }
325
+ }
326
+ return result;
327
+ }
328
+
329
+ /**
330
+ * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
331
+ * Returns 0 if given 0.
332
+ */
333
+ function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
334
+ unchecked {
335
+ uint256 result = log256(value);
336
+ return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
337
+ }
338
+ }
339
+ }
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {Equity} from "../Equity.sol";
5
+ import {JuiceDollar} from "../JuiceDollar.sol";
6
+
7
+ contract MockEquity is Equity {
8
+ constructor(JuiceDollar JUSD_) Equity(JUSD_) {
9
+ require(block.chainid == 31337, "MockEquity: TEST ONLY");
10
+ }
11
+
12
+ function mintForTesting(address to, uint256 amount) external {
13
+ _mint(to, amount);
14
+ }
15
+ }
@@ -0,0 +1,75 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {Position} from "../MintingHubV2/Position.sol";
5
+ import {MintingHubGateway} from "../gateway/MintingHubGateway.sol";
6
+ import {IMintingHubGateway} from "../gateway/interface/IMintingHubGateway.sol";
7
+ import {IJuiceDollar} from "../interface/IJuiceDollar.sol";
8
+ import {TestToken} from "./TestToken.sol";
9
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
10
+
11
+ contract PositionExpirationTest {
12
+ MintingHubGateway public hub;
13
+ TestToken public col;
14
+ IJuiceDollar public jusd;
15
+ bytes32 public frontendCode;
16
+
17
+ constructor(address hub_) {
18
+ hub = MintingHubGateway(hub_);
19
+ col = new TestToken("Some Collateral", "COL", uint8(0));
20
+ jusd = hub.JUSD();
21
+ }
22
+
23
+ function openPositionFor(address owner, bytes32 frontendCode_) public returns (address) {
24
+ frontendCode = frontendCode_;
25
+ col.mint(address(this), 100);
26
+ col.approve(address(hub), 100);
27
+ jusd.approve(address(hub), hub.OPENING_FEE());
28
+ address pos;
29
+ if (IERC165(hub).supportsInterface(type(IMintingHubGateway).interfaceId)) {
30
+ pos = hub.openPosition(
31
+ address(col),
32
+ 10,
33
+ 100 /* collateral */,
34
+ 1000000 * 10 ** 18,
35
+ 7 days,
36
+ 30 days,
37
+ 1 days,
38
+ 50000,
39
+ 1000 * 10 ** 36 /* price */,
40
+ 200000,
41
+ frontendCode
42
+ );
43
+ } else {
44
+ pos = hub.openPosition(
45
+ address(col),
46
+ 10,
47
+ 100 /* collateral */,
48
+ 1000000 * 10 ** 18,
49
+ 7 days,
50
+ 30 days,
51
+ 1 days,
52
+ 50000,
53
+ 1000 * 10 ** 36 /* price */,
54
+ 200000
55
+ );
56
+ }
57
+ Position(pos).transferOwnership(owner);
58
+ return pos;
59
+ }
60
+
61
+ function approveJUSD(address spender, uint256 amount) external {
62
+ jusd.approve(spender, amount);
63
+ }
64
+
65
+ function forceBuy(address pos, uint256 amount) public {
66
+ uint256 price = hub.expiredPurchasePrice(Position(pos));
67
+ uint256 balanceBefore = jusd.balanceOf(address(this));
68
+ uint256 colBalBefore = col.balanceOf(address(this));
69
+ amount = hub.buyExpiredCollateral(Position(pos), amount);
70
+ uint256 balanceAfter = jusd.balanceOf(address(this));
71
+ uint256 colBalAfter = col.balanceOf(address(this));
72
+ require(colBalAfter - colBalBefore == amount, "collateral amount");
73
+ require((balanceBefore - balanceAfter) == ((amount * price) / 10 ** 18), "price paid");
74
+ }
75
+ }
@@ -0,0 +1,65 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ // import "./Strings.sol";
5
+ import "./TestToken.sol";
6
+ import "../Equity.sol";
7
+ import "../MintingHubV2/Position.sol";
8
+ import "../MintingHubV2/MintingHub.sol";
9
+ import "../StablecoinBridge.sol";
10
+ import "../MintingHubV2/interface/IPosition.sol";
11
+ import "../interface/IReserve.sol";
12
+ import "../interface/IJuiceDollar.sol";
13
+
14
+ contract PositionRollingTest {
15
+ MintingHub hub;
16
+ TestToken col;
17
+ IJuiceDollar jusd;
18
+ PositionRoller roller;
19
+
20
+ IPosition public p1;
21
+ IPosition public p2;
22
+
23
+ constructor(address hub_) {
24
+ hub = MintingHub(hub_);
25
+ col = new TestToken("Some Collateral", "COL", uint8(0));
26
+ jusd = hub.JUSD();
27
+ roller = hub.ROLLER();
28
+ }
29
+
30
+ function openTwoPositions() public {
31
+ jusd.approve(address(hub), hub.OPENING_FEE());
32
+ p1 = IPosition(openPosition(100, uint40(3 days)));
33
+ jusd.approve(address(hub), hub.OPENING_FEE());
34
+ p2 = IPosition(openPosition(10, uint40(7 days)));
35
+ }
36
+
37
+ function mintFromFirstPosition(uint256 amount) public {
38
+ p1.mint(address(this), amount);
39
+ }
40
+
41
+ function openPosition(uint256 collateral, uint40 initializationDelay) public returns (address) {
42
+ col.mint(address(this), collateral);
43
+ col.approve(address(hub), collateral);
44
+ return
45
+ hub.openPosition(
46
+ address(col),
47
+ 10,
48
+ collateral,
49
+ 1000000 * 10 ** 18,
50
+ initializationDelay,
51
+ 30 days,
52
+ 1 days,
53
+ 50000,
54
+ 1000 * 10 ** 36,
55
+ 200000
56
+ );
57
+ }
58
+
59
+ function roll() public {
60
+ col.approve(address(roller), col.balanceOf(address(p1))); // approve full balance
61
+ roller.rollFully(p1, p2);
62
+ require(p1.getDebt() == 0);
63
+ require(jusd.balanceOf(address(this)) == 0);
64
+ }
65
+ }
@@ -0,0 +1,84 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
+
6
+ interface IEquity is IERC20 {
7
+ function invest(uint256 amount, uint256 expected) external returns (uint256);
8
+ function investFor(address investor, uint256 amount, uint256 expected) external returns (uint256);
9
+ function redeem(address target, uint256 shares) external returns (uint256);
10
+ function redeemExpected(address target, uint256 shares, uint256 expectedProceeds) external returns (uint256);
11
+ function redeemFrom(
12
+ address owner,
13
+ address target,
14
+ uint256 shares,
15
+ uint256 expectedProceeds
16
+ ) external returns (uint256);
17
+ }
18
+
19
+ /**
20
+ * @title TestFlashLoan
21
+ * @notice Helper contract to test same-block flash loan protection in Equity contract
22
+ */
23
+ contract TestFlashLoan {
24
+ IERC20 public immutable jusd;
25
+ IEquity public immutable equity;
26
+
27
+ constructor(address _jusd, address _equity) {
28
+ jusd = IERC20(_jusd);
29
+ equity = IEquity(_equity);
30
+ }
31
+
32
+ /**
33
+ * @notice Attempts to invest and redeem in the same block
34
+ * @dev Should revert with SameBlockRedemption
35
+ */
36
+ function attemptInvestAndRedeem(uint256 amount) external returns (uint256) {
37
+ uint256 shares = equity.invest(amount, 0);
38
+ uint256 proceeds = equity.redeem(address(this), shares);
39
+ jusd.transfer(msg.sender, proceeds);
40
+ return proceeds;
41
+ }
42
+
43
+ /**
44
+ * @notice Attempts to redeem shares that were received in the current block
45
+ * @dev Should revert with SameBlockRedemption
46
+ */
47
+ function attemptReceiveAndRedeem() external {
48
+ uint256 msgSenderShares = equity.balanceOf(msg.sender);
49
+ require(msgSenderShares > 0, "Message sender has no shares");
50
+ equity.transferFrom(msg.sender, address(this), msgSenderShares);
51
+ equity.redeem(address(this), msgSenderShares);
52
+ }
53
+
54
+ /**
55
+ * @notice Attempts redeemExpected in same block as receiving shares
56
+ * @dev Should revert with SameBlockRedemption
57
+ */
58
+ function attemptRedeemExpected() external {
59
+ uint256 msgSenderShares = equity.balanceOf(msg.sender);
60
+ require(msgSenderShares > 0, "Message sender has no shares");
61
+ equity.transferFrom(msg.sender, address(this), msgSenderShares);
62
+ equity.redeemExpected(address(this), msgSenderShares, 0);
63
+ }
64
+
65
+ /**
66
+ * @notice Attempts invest and redeemExpected in the same block
67
+ * @dev Should revert with SameBlockRedemption
68
+ */
69
+ function attemptInvestAndRedeemExpected(uint256 amount) external returns (uint256) {
70
+ uint256 shares = equity.invest(amount, 0);
71
+ uint256 proceeds = equity.redeemExpected(address(this), shares, 0);
72
+ return proceeds;
73
+ }
74
+
75
+ /**
76
+ * @notice Attempts investFor and redeemFrom on behalf of another investor in the same block
77
+ * @dev Should revert with SameBlockRedemption
78
+ */
79
+ function attemptInvestForAndRedeemFrom(uint256 amount, address investor) external returns (uint256) {
80
+ uint256 shares = equity.investFor(investor, amount, 0);
81
+ uint256 proceeds = equity.redeemFrom(investor, address(this), shares, 0);
82
+ return proceeds;
83
+ }
84
+ }
@@ -0,0 +1,49 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
+
6
+ interface IFrontendGateway {
7
+ function invest(uint256 amount, uint256 expectedShares, bytes32 frontendCode) external returns (uint256);
8
+ function redeem(address target, uint256 shares, uint256 expectedProceeds, bytes32 frontendCode) external returns (uint256);
9
+ }
10
+
11
+ interface IEquity is IERC20 {
12
+ function approve(address spender, uint256 amount) external returns (bool);
13
+ }
14
+
15
+ /**
16
+ * @title TestFlashLoanGateway
17
+ * @notice Helper contract to test same-block flash loan protection via FrontendGateway
18
+ */
19
+ contract TestFlashLoanGateway {
20
+ IERC20 public immutable jusd;
21
+ IEquity public immutable equity;
22
+ IFrontendGateway public immutable gateway;
23
+
24
+ constructor(address _jusd, address _equity, address _gateway) {
25
+ jusd = IERC20(_jusd);
26
+ equity = IEquity(_equity);
27
+ gateway = IFrontendGateway(_gateway);
28
+ }
29
+
30
+ /**
31
+ * @notice Attempts to invest and immediately redeem via FrontendGateway in the same transaction
32
+ * @dev Should revert with SameBlockRedemption
33
+ */
34
+ function attemptInvestAndRedeemViaGateway(
35
+ uint256 amount,
36
+ bytes32 frontendCode
37
+ ) external returns (uint256) {
38
+ jusd.approve(address(gateway), amount);
39
+ jusd.approve(address(equity), amount);
40
+
41
+ uint256 shares = gateway.invest(amount, 0, frontendCode);
42
+
43
+ equity.approve(address(gateway), shares);
44
+ uint256 proceeds = gateway.redeem(address(this), shares, 0, frontendCode);
45
+
46
+ jusd.transfer(msg.sender, proceeds);
47
+ return proceeds;
48
+ }
49
+ }
@@ -0,0 +1,40 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import "../utils/MathUtil.sol";
5
+
6
+ contract TestMathUtil is MathUtil {
7
+ uint256 public result;
8
+
9
+ constructor() {
10
+ result = 1;
11
+ }
12
+
13
+ function calculateShares(
14
+ uint256 totalShares,
15
+ uint256 capitalBefore,
16
+ uint256 investment
17
+ ) external pure returns (uint256) {
18
+ uint256 newTotalShares = _mulD18(totalShares, _tenthRoot(_divD18(capitalBefore + investment, capitalBefore)));
19
+ return newTotalShares - totalShares;
20
+ }
21
+
22
+ function mulD18(uint256 _a, uint256 _b) external pure returns (uint256) {
23
+ return _mulD18(_a, _b);
24
+ }
25
+
26
+ function divD18(uint256 _a, uint256 _b) external pure returns (uint256) {
27
+ return _divD18(_a, _b);
28
+ }
29
+
30
+ function power10(uint256 _x) external pure returns (uint256) {
31
+ return _power10(_x);
32
+ }
33
+
34
+ function tenthRoot(uint256 a, bool recordResult) external {
35
+ uint256 r = _tenthRoot(a);
36
+ if (recordResult) {
37
+ result = r;
38
+ }
39
+ }
40
+ }