fix: Remove recenterAccess — make recenter() public with TWAP enforcement (#706)
This commit is contained in:
parent
860b56f216
commit
1a410a30b7
13 changed files with 94 additions and 180 deletions
|
|
@ -163,7 +163,7 @@ contract FitnessEvaluator is Test {
|
|||
|
||||
/// @dev Account 8 — adversary (10 000 ETH in Anvil; funded via vm.deal here)
|
||||
uint256 internal constant ADV_PK = 0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97;
|
||||
/// @dev Account 2 — recenter caller (granted recenterAccess in bootstrap)
|
||||
/// @dev Account 2 — recenter caller (recenter() is now permissionless)
|
||||
uint256 internal constant RECENTER_PK = 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a;
|
||||
|
||||
// ─── Runtime state ────────────────────────────────────────────────────────
|
||||
|
|
@ -248,7 +248,7 @@ contract FitnessEvaluator is Test {
|
|||
bytes32 ERC1967_IMPL = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
|
||||
vm.store(optProxy, ERC1967_IMPL, bytes32(uint256(uint160(IMPL_SLOT))));
|
||||
|
||||
// Bootstrap: fund LM, set recenterAccess, initial recenter.
|
||||
// Bootstrap: fund LM, initial recenter.
|
||||
if (!_bootstrap()) {
|
||||
console.log(string.concat('{"candidate_id":"', candidateId, '","fitness":0,"error":"bootstrap_failed"}'));
|
||||
continue;
|
||||
|
|
@ -367,18 +367,14 @@ contract FitnessEvaluator is Test {
|
|||
* @notice Bootstrap LM state for a candidate evaluation (mirrors fitness.sh bootstrap).
|
||||
*
|
||||
* Steps (same order as fitness.sh):
|
||||
* a. Grant recenterAccess to recenterAddr (impersonate feeDestination).
|
||||
* b. Fund adversary account and wrap ETH → WETH.
|
||||
* c. Transfer 1000 WETH to LM.
|
||||
* d. Wrap 9000 WETH for adversary trades + set approvals.
|
||||
* e. Initial recenter (succeeds immediately: recenterAccess set, no ANCHOR liquidity yet).
|
||||
* a. Fund adversary account and wrap ETH → WETH.
|
||||
* b. Transfer 1000 WETH to LM.
|
||||
* c. Wrap 9000 WETH for adversary trades + set approvals.
|
||||
* d. Initial recenter (callable by anyone: cooldown passes because block.timestamp on a
|
||||
* Base fork is a large value >> 60; TWAP passes because the pool has existing history).
|
||||
*/
|
||||
function _bootstrap() internal returns (bool) {
|
||||
// a. Grant recenterAccess (feeDestination call, no ETH needed with gas_price=0).
|
||||
vm.prank(FEE_DEST);
|
||||
LiquidityManager(payable(lmAddr)).setRecenterAccess(recenterAddr);
|
||||
|
||||
// b. Fund adversary with ETH.
|
||||
// a. Fund adversary with ETH.
|
||||
vm.deal(advAddr, 10_000 ether);
|
||||
|
||||
// c. Wrap 1000 ETH → WETH and send to LM.
|
||||
|
|
@ -397,8 +393,8 @@ contract FitnessEvaluator is Test {
|
|||
IERC20(krkAddr).approve(NPM_ADDR, type(uint256).max);
|
||||
vm.stopPrank();
|
||||
|
||||
// e. Initial recenter: no ANCHOR position exists yet so amplitude check is skipped;
|
||||
// recenterAccess is set so TWAP stability check is also skipped.
|
||||
// d. Initial recenter: no ANCHOR position exists yet so amplitude check is skipped.
|
||||
// Cooldown passes (Base fork timestamp >> 60). TWAP passes (existing pool history).
|
||||
// If all retries fail, revert with a clear message — silent failure would make every
|
||||
// candidate score identically (all lm_eth_total = free WETH only, no positions).
|
||||
bool recentered = false;
|
||||
|
|
|
|||
|
|
@ -406,12 +406,6 @@ contract LiquidityManagerTest is UniSwapHelper {
|
|||
_skipAutoSetup = true;
|
||||
}
|
||||
|
||||
/// @notice Grant recenter access for testing (commonly needed)
|
||||
function _grantRecenterAccess() internal {
|
||||
vm.prank(feeDestination);
|
||||
lm.setRecenterAccess(RECENTER_CALLER);
|
||||
}
|
||||
|
||||
/// @notice Setup with custom parameters but standard flow
|
||||
function _setupCustom(bool token0IsWeth, uint256 accountBalance) internal {
|
||||
disableAutoSetup();
|
||||
|
|
@ -450,10 +444,6 @@ contract LiquidityManagerTest is UniSwapHelper {
|
|||
vm.prank(account);
|
||||
weth.deposit{ value: 15_000 ether }();
|
||||
|
||||
// Grant recenter access
|
||||
vm.prank(feeDestination);
|
||||
lm.setRecenterAccess(RECENTER_CALLER);
|
||||
|
||||
// Setup approvals without creating blocking positions
|
||||
vm.startPrank(account);
|
||||
weth.approve(address(lm), type(uint256).max);
|
||||
|
|
@ -947,58 +937,25 @@ contract LiquidityManagerTest is UniSwapHelper {
|
|||
}
|
||||
|
||||
// =========================================================
|
||||
// COVERAGE TESTS: onlyFeeDestination, revokeRecenterAccess,
|
||||
// open recenter path, VWAP else branch,
|
||||
// COVERAGE TESTS: cooldown check, TWAP oracle path, VWAP else branch,
|
||||
// optimizer fallback, _getKraikenToken/_getWethToken
|
||||
// =========================================================
|
||||
|
||||
/**
|
||||
* @notice Calling an onlyFeeDestination function from a non-fee address must revert
|
||||
*/
|
||||
function testOnlyFeeDestinationReverts() public {
|
||||
address nonFee = makeAddr("notFeeDestination");
|
||||
vm.expectRevert("only callable by feeDestination");
|
||||
vm.prank(nonFee);
|
||||
lm.setRecenterAccess(nonFee);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice feeDestination can revoke recenter access (covers revokeRecenterAccess body)
|
||||
*/
|
||||
function testRevokeRecenterAccess() public {
|
||||
assertEq(lm.recenterAccess(), RECENTER_CALLER, "precondition: access should be set");
|
||||
|
||||
vm.prank(feeDestination);
|
||||
lm.revokeRecenterAccess();
|
||||
|
||||
assertEq(lm.recenterAccess(), address(0), "recenterAccess should be revoked");
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Open recenter (no access restriction) must fail with cooldown if called too soon
|
||||
* @notice recenter() must fail with cooldown if called too soon after the last recenter
|
||||
*/
|
||||
function testOpenRecenterCooldown() public {
|
||||
vm.prank(feeDestination);
|
||||
lm.revokeRecenterAccess();
|
||||
|
||||
// Immediately try to recenter without waiting — should hit cooldown check
|
||||
vm.expectRevert("recenter cooldown");
|
||||
lm.recenter();
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice After cooldown, open recenter calls _isPriceStable (covering _getPool) then
|
||||
* hits amplitude check (covers the open-recenter else branch, lines 141-142, 265-266)
|
||||
* @dev PriceOracle._isPriceStable has a 60,000-second fallback interval.
|
||||
* setUp warps ~18,000s so the pool's history is only ~18,000s.
|
||||
* We warp an additional 61,000s so pool history > 60,000s for the fallback to succeed.
|
||||
* @notice After cooldown, recenter() calls _isPriceStable (covering _getPool) then
|
||||
* hits amplitude check when price has not moved since last recenter
|
||||
*/
|
||||
function testOpenRecenterOracleCheck() public {
|
||||
vm.prank(feeDestination);
|
||||
lm.revokeRecenterAccess();
|
||||
|
||||
// Warp enough seconds so pool.observe([300,0]) and its fallback ([60000,0]) both succeed.
|
||||
// Pool was initialized at timestamp 1; after setUp + this warp: ~79,001s of history.
|
||||
// Warp enough seconds for cooldown + TWAP window (300s).
|
||||
vm.warp(block.timestamp + 61_000);
|
||||
|
||||
// _isPriceStable (→ _getPool) is called; price unchanged → stable.
|
||||
|
|
@ -1133,6 +1090,9 @@ contract LiquidityManagerTest is UniSwapHelper {
|
|||
// Move price up with a buy so the second recenter satisfies amplitude requirement
|
||||
buyRaw(10 ether);
|
||||
|
||||
// Warp past cooldown interval; also lets TWAP settle at the post-buy price.
|
||||
vm.warp(block.timestamp + 301);
|
||||
|
||||
// Second recenter: _scrapePositions() burns positions and collects principal KRK
|
||||
// into the LM's balance. _setPositions() then calls _getOutstandingSupply().
|
||||
// Without the fix: outstandingSupply() already excludes balanceOf(lm), and
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@ contract SupplyCorruptionTest is UniSwapHelper {
|
|||
performSwap(5 ether, true);
|
||||
|
||||
console.log("Performed 5 ETH swap to move price");
|
||||
vm.warp(block.timestamp + 301); // TWAP catches up to post-swap price; cooldown passes
|
||||
|
||||
// Call recenter
|
||||
vm.prank(RECENTER_CALLER);
|
||||
|
|
@ -126,6 +127,7 @@ contract SupplyCorruptionTest is UniSwapHelper {
|
|||
console.log("Initial supply:", initialTotalSupply);
|
||||
|
||||
// Perform multiple recenter cycles
|
||||
uint256 ts = block.timestamp; // track time explicitly to avoid Forge block.timestamp reset
|
||||
for (uint256 i = 0; i < 3; i++) {
|
||||
// Swap to move price
|
||||
vm.deal(account, 2 ether);
|
||||
|
|
@ -133,6 +135,8 @@ contract SupplyCorruptionTest is UniSwapHelper {
|
|||
weth.deposit{ value: 2 ether }();
|
||||
|
||||
performSwap(2 ether, true);
|
||||
ts += 301; // TWAP catches up; cooldown passes
|
||||
vm.warp(ts);
|
||||
|
||||
vm.prank(RECENTER_CALLER);
|
||||
lm.recenter();
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
|
||||
// ---- step 2: first buy + recenter → bootstrap ----
|
||||
buyRaw(25 ether); // push price up enough to satisfy amplitude check
|
||||
vm.warp(block.timestamp + 301); // TWAP catches up to post-buy price; cooldown passes
|
||||
|
||||
vm.prank(RECENTER_CALLER);
|
||||
lm.recenter(); // cumulativeVolume == 0 → shouldRecordVWAP = true (bootstrap path)
|
||||
|
|
@ -78,8 +79,11 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
|
||||
// ---- step 3: continued buy-only cycles ----
|
||||
uint256 successfulBuyCycles;
|
||||
uint256 ts = block.timestamp; // track explicitly to avoid Forge block.timestamp reset
|
||||
for (uint256 i = 0; i < 10; i++) {
|
||||
buyRaw(25 ether);
|
||||
ts += 301; // TWAP catches up; cooldown passes
|
||||
vm.warp(ts);
|
||||
vm.prank(RECENTER_CALLER);
|
||||
// Recenter may fail if amplitude isn't reached; that's fine.
|
||||
try lm.recenter() {
|
||||
|
|
@ -114,12 +118,16 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
|
||||
// Bootstrap via first buy-recenter
|
||||
buyRaw(25 ether);
|
||||
vm.warp(block.timestamp + 301); // TWAP catches up; cooldown passes
|
||||
vm.prank(RECENTER_CALLER);
|
||||
lm.recenter();
|
||||
|
||||
// Run several buy cycles
|
||||
uint256 ts = block.timestamp; // track explicitly to avoid Forge block.timestamp reset
|
||||
for (uint256 i = 0; i < 6; i++) {
|
||||
buyRaw(25 ether);
|
||||
ts += 301; // TWAP catches up; cooldown passes
|
||||
vm.warp(ts);
|
||||
vm.prank(RECENTER_CALLER);
|
||||
try lm.recenter() { } catch { }
|
||||
}
|
||||
|
|
@ -160,6 +168,7 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
assertEq(lm.cumulativeVolume(), 0, "no VWAP data before first fees");
|
||||
|
||||
buyRaw(25 ether);
|
||||
vm.warp(block.timestamp + 301); // TWAP catches up to post-buy price; cooldown passes
|
||||
|
||||
vm.prank(RECENTER_CALLER);
|
||||
lm.recenter();
|
||||
|
|
@ -188,7 +197,10 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
vm.prank(RECENTER_CALLER);
|
||||
lm.recenter();
|
||||
|
||||
uint256 ts = block.timestamp; // track explicitly to avoid Forge block.timestamp reset
|
||||
buyRaw(25 ether);
|
||||
ts += 301; // TWAP catches up to post-buy price; cooldown passes
|
||||
vm.warp(ts);
|
||||
vm.prank(RECENTER_CALLER);
|
||||
lm.recenter();
|
||||
|
||||
|
|
@ -199,6 +211,8 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
}
|
||||
|
||||
// Recenter with price now lower (sell direction) — must not revert
|
||||
ts += 301; // TWAP catches up to post-sell price; cooldown passes
|
||||
vm.warp(ts);
|
||||
vm.prank(RECENTER_CALLER);
|
||||
try lm.recenter() {
|
||||
// success — sell-direction recenter works
|
||||
|
|
@ -239,6 +253,7 @@ contract VWAPFloorProtectionTest is UniSwapHelper {
|
|||
// 25 ether against a 100 ETH LM pool reliably satisfies the amplitude check
|
||||
// (same amount used across other bootstrap tests in this file).
|
||||
buyRaw(25 ether);
|
||||
vm.warp(block.timestamp + 301); // TWAP catches up to post-buy price; cooldown passes
|
||||
|
||||
// Step 3: Second recenter — bootstrap path records VWAP.
|
||||
vm.prank(RECENTER_CALLER);
|
||||
|
|
|
|||
|
|
@ -132,10 +132,6 @@ contract TestEnvironment is TestConstants {
|
|||
// Configure permissions
|
||||
_configurePermissions();
|
||||
|
||||
// Grant recenter access to specified caller
|
||||
vm.prank(feeDestination);
|
||||
lm.setRecenterAccess(recenterCaller);
|
||||
|
||||
return (factory, pool, weth, harberg, stake, lm, optimizer, token0isWeth);
|
||||
}
|
||||
|
||||
|
|
@ -172,11 +168,14 @@ contract TestEnvironment is TestConstants {
|
|||
|
||||
/**
|
||||
* @notice Create and initialize the Uniswap pool
|
||||
* @dev Warp 301 seconds after pool init so _isPriceStable()'s 300-second TWAP window
|
||||
* has sufficient history for any subsequent recenter() call.
|
||||
*/
|
||||
function _createAndInitializePool() internal {
|
||||
pool = IUniswapV3Pool(factory.createPool(address(weth), address(harberg), FEE));
|
||||
token0isWeth = address(weth) < address(harberg);
|
||||
pool.initializePoolFor1Cent(token0isWeth);
|
||||
vm.warp(block.timestamp + 301);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -248,10 +247,6 @@ contract TestEnvironment is TestConstants {
|
|||
// Configure permissions
|
||||
_configurePermissions();
|
||||
|
||||
// Grant recenter access to specified caller
|
||||
vm.prank(feeDestination);
|
||||
lm.setRecenterAccess(recenterCaller);
|
||||
|
||||
return (factory, pool, weth, harberg, stake, lm, optimizer, token0isWeth);
|
||||
}
|
||||
|
||||
|
|
@ -299,10 +294,6 @@ contract TestEnvironment is TestConstants {
|
|||
harberg.setLiquidityManager(address(lm));
|
||||
vm.deal(address(lm), INITIAL_LM_ETH_BALANCE);
|
||||
|
||||
// feeDestination IS address(lm), so prank as lm to grant recenter access
|
||||
vm.prank(address(lm));
|
||||
lm.setRecenterAccess(recenterCaller);
|
||||
|
||||
return (factory, pool, weth, harberg, stake, lm, optimizer, token0isWeth);
|
||||
}
|
||||
|
||||
|
|
@ -357,10 +348,6 @@ contract TestEnvironment is TestConstants {
|
|||
// Configure permissions
|
||||
_configurePermissions();
|
||||
|
||||
// Grant recenter access to specified caller
|
||||
vm.prank(feeDestination);
|
||||
lm.setRecenterAccess(recenterCaller);
|
||||
|
||||
return (factory, pool, weth, harberg, stake, lm, optimizer, token0isWeth);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue