Skip to content

Commit bfbeed1

Browse files
committed
cleanup
1 parent 07e07c6 commit bfbeed1

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

src/MaglevBase.sol

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.27;
33

4-
import {console} from "forge-std/Test.sol";
54
import {Ownable, Context} from "openzeppelin-contracts/access/Ownable.sol";
65
import {EVCUtil} from "evc/utils/EVCUtil.sol";
76
import {IEVC} from "evc/interfaces/IEthereumVaultConnector.sol";
@@ -22,17 +21,17 @@ abstract contract MaglevBase is EVCUtil, Ownable {
2221

2322
uint112 public reserve0;
2423
uint112 public reserve1;
25-
uint32 private locked;
24+
uint32 public locked; // 0=unlocked, 1=reentrancy guard, 2=paused
2625

27-
error Reentrancy();
26+
error Locked();
2827
error Overflow();
2928
error UnsupportedPair();
3029
error BadFee();
3130
error InsufficientReserves();
3231
error InsufficientCash();
3332

3433
modifier nonReentrant() {
35-
require(locked == 0, Reentrancy());
34+
require(locked == 0, Locked());
3635
locked = 1;
3736
_;
3837
locked = 0;
@@ -76,6 +75,16 @@ abstract contract MaglevBase is EVCUtil, Ownable {
7675
IEVC(evc).enableCollateral(myAccount, vault1);
7776
}
7877

78+
function pause() external onlyOwner {
79+
require(locked == 0, Locked());
80+
locked = 2;
81+
}
82+
83+
function unpause() external onlyOwner {
84+
require(locked == 2, Locked());
85+
locked = 0;
86+
}
87+
7988
// Swapper interface
8089

8190
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data)

src/MaglevConstantSum.sol

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ pragma solidity ^0.8.27;
44
import {MaglevBase} from "./MaglevBase.sol";
55

66
contract MaglevConstantSum is MaglevBase {
7-
uint256 public immutable priceA;
8-
uint256 public immutable priceB;
7+
uint256 public immutable priceX;
8+
uint256 public immutable priceY;
99

1010
error KNotSatisfied();
1111

1212
struct ConstantSumParams {
13-
uint256 priceA;
14-
uint256 priceB;
13+
uint256 priceX;
14+
uint256 priceY;
1515
}
1616

1717
constructor(BaseParams memory baseParams, ConstantSumParams memory params) MaglevBase(baseParams) {
18-
priceA = params.priceA;
19-
priceB = params.priceB;
18+
priceX = params.priceX;
19+
priceY = params.priceY;
2020
}
2121

2222
function k(uint256 r0, uint256 r1) public view returns (uint256) {
23-
return (r0 * priceA) + (r1 * priceB);
23+
return (r0 * priceX) + (r1 * priceY);
2424
}
2525

2626
function verify(uint256 newReserve0, uint256 newReserve1) internal view virtual override {
@@ -29,9 +29,8 @@ contract MaglevConstantSum is MaglevBase {
2929
require(kAfter >= kBefore, KNotSatisfied());
3030
}
3131

32-
// FIXME: incorporate priceA and priceB
33-
3432
function computeQuote(uint256 amount, bool, bool) internal view virtual override returns (uint256) {
33+
// FIXME: use priceX and priceY
3534
return amount;
3635
}
3736
}

src/MaglevEulerSwap.sol

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.27;
33

4-
import {console} from "forge-std/Test.sol";
54
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
65
import {MaglevBase} from "./MaglevBase.sol";
76

87
contract MaglevEulerSwap is MaglevBase {
9-
uint256 public _px;
10-
uint256 public _py;
11-
uint256 public _cx;
12-
uint256 public _cy;
8+
uint256 public priceX;
9+
uint256 public priceY;
10+
uint256 public concentrationX;
11+
uint256 public concentrationY;
1312

1413
error KNotSatisfied();
1514
error ReservesZero();
@@ -27,27 +26,32 @@ contract MaglevEulerSwap is MaglevBase {
2726
}
2827

2928
function setEulerSwapParams(EulerSwapParams memory params) public onlyOwner {
30-
_px = params.px;
31-
_py = params.py;
32-
_cx = params.cx;
33-
_cy = params.cy;
29+
priceX = params.px;
30+
priceY = params.py;
31+
concentrationX = params.cx;
32+
concentrationY = params.cy;
3433
}
3534

35+
// Due to rounding, computeQuote() may underestimate the amount required to
36+
// pass the verify() function. In order to prevent swaps from failing, quotes
37+
// are inflated by this compensation factor. FIXME: solve the rounding.
38+
uint256 private constant roundingCompensation = 1.0000000000001e18;
39+
3640
function verify(uint256 newReserve0, uint256 newReserve1) internal view virtual override {
3741
int256 delta = 0;
3842

3943
if (newReserve0 >= initialReserve0) {
40-
delta = int256(newReserve0) - int256(fy(newReserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy));
44+
delta = int256(newReserve0)
45+
- int256(fy(newReserve1, priceX, priceY, initialReserve0, initialReserve1, concentrationX, concentrationY));
4146
} else {
42-
delta = int256(newReserve1) - int256(fx(newReserve0, _px, _py, initialReserve0, initialReserve1, _cx, _cy));
47+
delta = int256(newReserve1)
48+
- int256(fx(newReserve0, priceX, priceY, initialReserve0, initialReserve1, concentrationX, concentrationY));
4349
}
4450

4551
// if delta is >= zero, then point is on or above the curve
4652
require(delta >= 0, KNotSatisfied());
4753
}
4854

49-
uint256 private constant roundingCompensation = 1.0000000000001e18;
50-
5155
function computeQuote(uint256 amount, bool exactIn, bool asset0IsInput)
5256
internal
5357
view
@@ -72,11 +76,31 @@ contract MaglevEulerSwap is MaglevBase {
7276

7377
if (dx != 0) {
7478
reserve0New += dx;
75-
reserve1New = int256(fx(uint256(reserve0New), _px, _py, initialReserve0, initialReserve1, _cx, _cy));
79+
reserve1New = int256(
80+
fx(
81+
uint256(reserve0New),
82+
priceX,
83+
priceY,
84+
initialReserve0,
85+
initialReserve1,
86+
concentrationX,
87+
concentrationY
88+
)
89+
);
7690
}
7791
if (dy != 0) {
7892
reserve1New += dy;
79-
reserve0New = int256(fy(uint256(reserve1New), _px, _py, initialReserve0, initialReserve1, _cx, _cy));
93+
reserve0New = int256(
94+
fy(
95+
uint256(reserve1New),
96+
priceX,
97+
priceY,
98+
initialReserve0,
99+
initialReserve1,
100+
concentrationX,
101+
concentrationY
102+
)
103+
);
80104
}
81105

82106
dx = reserve0New - int256(uint256(reserve0));

test/ConstantSum.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ contract ConstantSumTest is MaglevTestBase {
1919
createMaglev(50e18, 50e18, 0, 1, 1);
2020
}
2121

22-
function createMaglev(uint112 debtLimit0, uint112 debtLimit1, uint256 fee, uint256 priceA, uint256 priceB)
22+
function createMaglev(uint112 debtLimit0, uint112 debtLimit1, uint256 fee, uint256 priceX, uint256 priceY)
2323
internal
2424
{
2525
vm.prank(owner);
2626
maglev = new Maglev(
27-
getMaglevBaseParams(debtLimit0, debtLimit1, fee), Maglev.ConstantSumParams({priceA: priceA, priceB: priceB})
27+
getMaglevBaseParams(debtLimit0, debtLimit1, fee), Maglev.ConstantSumParams({priceX: priceX, priceY: priceY})
2828
);
2929

3030
vm.prank(holder);

0 commit comments

Comments
 (0)