Skip to content

Commit 2822bc5

Browse files
committed
some cleanup and natspec
1 parent 5d1aae1 commit 2822bc5

File tree

9 files changed

+36
-20
lines changed

9 files changed

+36
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The following are the high-level steps required to use Maglev:
5050
* Deploy the desired Maglev contract, choosing parameters such as the vaults, debt limits, and the desired `fee`
5151
* Note that the Maglev contract must be created after the funds are deposited, because its constructor will read the current debts and balances to setup its reserves cache
5252
* Install the Maglev contract as an operator for your account
53-
* Invoke the `configure()` function on the Maglev contract
53+
* Invoke the `activate()` function on the Maglev contract
5454
* This function can be invoked by anyone, and it is harmless to re-invoke it
5555

5656
At this point, anyone can invoke `swap()` on the Maglev contract, and this will perform borrowing and transferring activity between the two vaults.

src/MaglevBase.sol

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
1515
address public immutable myAccount;
1616
uint256 public immutable feeMultiplier;
1717

18-
uint112 internal reserve0;
19-
uint112 internal reserve1;
18+
uint112 public reserve0;
19+
uint112 public reserve1;
2020
uint32 internal locked; // uses single storage slot, accessible via getReserves()
2121

2222
error Locked();
@@ -80,8 +80,10 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
8080

8181
// Owner functions
8282

83-
/// @dev Call *after* installing as operator
84-
function configure() external {
83+
/// @notice Approve the vaults to access the Maglev instance's tokens,
84+
/// and enables vaults as collateral. Must call *after* installing
85+
/// the Maglev instance as an operator.
86+
function activate() external {
8587
IERC20(asset0).approve(vault0, type(uint256).max);
8688
IERC20(asset1).approve(vault1, type(uint256).max);
8789

@@ -91,6 +93,10 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
9193

9294
// Swapper interface
9395

96+
/// @notice Optimistically sends the requested amounts of tokens to the `to`
97+
/// address, invokes `uniswapV2Call` callback on `to` (if `data` provided), and
98+
/// then verifies that a sufficient amount of tokens were transferred to
99+
/// satisfy the swapping curve invariant.
94100
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data)
95101
external
96102
callThroughEVC
@@ -137,16 +143,19 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
137143
}
138144
}
139145

146+
/// @notice Convenience function for when both reserve values are needed.
140147
function getReserves() public view returns (uint112, uint112, uint32) {
141148
return (reserve0, reserve1, locked);
142149
}
143150

151+
/// @notice How much `tokenOut` can I get for `amountIn` of `tokenIn`?
144152
function quoteExactInput(address tokenIn, address tokenOut, uint256 amountIn) external view returns (uint256) {
145-
return _computeQuote(tokenIn, tokenOut, amountIn, true);
153+
return computeFullQuote(tokenIn, tokenOut, amountIn, true);
146154
}
147155

156+
/// @notice How much `tokenIn` do I need to get `amountOut` of `tokenOut`?
148157
function quoteExactOutput(address tokenIn, address tokenOut, uint256 amountOut) external view returns (uint256) {
149-
return _computeQuote(tokenIn, tokenOut, amountOut, false);
158+
return computeFullQuote(tokenIn, tokenOut, amountOut, false);
150159
}
151160

152161
// Internal utilities
@@ -205,7 +214,9 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
205214
}
206215
}
207216

208-
function _computeQuote(address tokenIn, address tokenOut, uint256 amount, bool exactIn)
217+
/// @dev This function handles quoting, including swap fees. Curve-specific
218+
/// quote calculations are delegated to computeQuote().
219+
function computeFullQuote(address tokenIn, address tokenOut, uint256 amount, bool exactIn)
209220
internal
210221
view
211222
returns (uint256)
@@ -236,8 +247,10 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
236247
return quote;
237248
}
238249

239-
/// @dev May be overridden by subclass if there is a more efficient method
240-
/// of computing quotes than binary search.
250+
/// @dev This function generates quotes given the specific installed curve
251+
/// instance. The base-class implementation is a binary search, however this
252+
/// may be overridden by a subclass if there is a more efficient method
253+
/// of computing quotes, or if a curve is non-convex.
241254
function computeQuote(uint256 amount, bool exactIn, bool asset0IsInput)
242255
internal
243256
view
@@ -281,6 +294,9 @@ abstract contract MaglevBase is IMaglevBase, EVCUtil {
281294
}
282295
}
283296

284-
/// @dev Must be implemented by sub-class
297+
/// @dev Function that defines the shape of the swapping curve. Returns true iff
298+
/// the provided reserve amounts are acceptable to the pool. Geometrically, this
299+
/// can be visualised as checking if a point is on or above/to the right of
300+
/// the swapping curve. This function must be implemented by a sub-class.
285301
function verify(uint256 newReserve0, uint256 newReserve1) internal view virtual returns (bool);
286302
}

src/MaglevEulerSwap.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ contract MaglevEulerSwap is IMaglevEulerSwap, MaglevBase {
3232
initialReserve1 = reserve1;
3333
}
3434

35-
function fx(uint256 xt, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c)
35+
function f(uint256 xt, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c)
3636
internal
3737
pure
3838
returns (uint256)
@@ -43,10 +43,10 @@ contract MaglevEulerSwap is IMaglevEulerSwap, MaglevBase {
4343
function verify(uint256 newReserve0, uint256 newReserve1) internal view virtual override returns (bool) {
4444
if (newReserve0 >= initialReserve0) {
4545
if (newReserve1 >= initialReserve1) return true;
46-
return newReserve0 >= fx(newReserve1, priceY, priceX, initialReserve1, initialReserve0, concentrationY);
46+
return newReserve0 >= f(newReserve1, priceY, priceX, initialReserve1, initialReserve0, concentrationY);
4747
} else {
4848
if (newReserve1 < initialReserve1) return false;
49-
return newReserve1 >= fx(newReserve0, priceX, priceY, initialReserve0, initialReserve1, concentrationX);
49+
return newReserve1 >= f(newReserve0, priceX, priceY, initialReserve0, initialReserve1, concentrationX);
5050
}
5151
}
5252
}

src/interfaces/IMaglevBase.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pragma solidity >=0.8.0;
33

44
interface IMaglevBase {
5-
function configure() external;
5+
function activate() external;
66
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data) external;
77
function quoteExactInput(address tokenIn, address tokenOut, uint256 amountIn) external view returns (uint256);
88
function quoteExactOutput(address tokenIn, address tokenOut, uint256 amountOut) external view returns (uint256);

test/ConstantProduct.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ contract ConstantProductTest is MaglevTestBase {
2727
evc.setAccountOperator(holder, address(maglev), true);
2828

2929
vm.prank(anyone);
30-
maglev.configure();
30+
maglev.activate();
3131
}
3232

3333
function test_basicExactIn() public monotonicHolderNAV {

test/ConstantSum.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ contract ConstantSumTest is MaglevTestBase {
3131
evc.setAccountOperator(holder, address(maglev), true);
3232

3333
vm.prank(anyone);
34-
maglev.configure();
34+
maglev.activate();
3535
}
3636

3737
function test_basicSwapReport() public monotonicHolderNAV {

test/EulerSwap.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ contract EulerSwapTest is MaglevTestBase {
3838
evc.setAccountOperator(holder, address(maglev), true);
3939

4040
vm.prank(anyone);
41-
maglev.configure();
41+
maglev.activate();
4242
}
4343

4444
function test_different_EVC() public {

test/PreserveNav.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ contract PreserveNav is MaglevTestBase {
3636
evc.setAccountOperator(holder, address(maglev), true);
3737

3838
vm.prank(anyone);
39-
maglev.configure();
39+
maglev.activate();
4040
}
4141

4242
function test_preserve_nav(

test/UniswapV2Call.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ contract UniswapV2CallTest is MaglevTestBase {
3737
evc.setAccountOperator(holder, address(maglev), true);
3838

3939
vm.prank(anyone);
40-
maglev.configure();
40+
maglev.activate();
4141
}
4242

4343
function test_callback() public {

0 commit comments

Comments
 (0)