fix: move VWAP bootstrap from forge script to bootstrap-common.sh
vm.warp in forge script --broadcast only affects the local simulation phase, not the actual Anvil node. The pool.observe([300,0]) call in recenter() therefore reverted with OLD when Forge pre-flighted the broadcast transactions on Anvil. Fix: - Remove the vm.warp + 2-recenter + SeedSwapper VWAP bootstrap from DeployLocal.sol (only contract deployment now, simpler and reliable). - Add bootstrap_vwap() to bootstrap-common.sh that uses Anvil RPC evm_increaseTime + evm_mine to advance chain time before each recenter, then executes a 0.5 ETH WETH->KRK seed swap between them. - Call bootstrap_vwap() before fund_liquidity_manager() in both containers/bootstrap.sh and ci-bootstrap.sh so the LM is seeded with thin positions (1 ETH) during bootstrap, ensuring the 0.5 ETH swap moves the price >400 ticks (amplitude gate). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
df2f0a87e5
commit
02b055ceb9
4 changed files with 73 additions and 100 deletions
|
|
@ -135,8 +135,8 @@ main() {
|
||||||
run_forge_script
|
run_forge_script
|
||||||
extract_addresses
|
extract_addresses
|
||||||
write_contracts_env
|
write_contracts_env
|
||||||
|
bootstrap_vwap
|
||||||
fund_liquidity_manager
|
fund_liquidity_manager
|
||||||
call_recenter
|
|
||||||
seed_application_state
|
seed_application_state
|
||||||
write_deployments_json
|
write_deployments_json
|
||||||
write_ponder_env
|
write_ponder_env
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ import { ERC1967Proxy } from "@openzeppelin/proxy/ERC1967/ERC1967Proxy.sol";
|
||||||
import "@uniswap-v3-core/interfaces/IUniswapV3Factory.sol";
|
import "@uniswap-v3-core/interfaces/IUniswapV3Factory.sol";
|
||||||
import "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";
|
import "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";
|
||||||
import "forge-std/Script.sol";
|
import "forge-std/Script.sol";
|
||||||
import "./DeployCommon.sol";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @title DeployLocal
|
* @title DeployLocal
|
||||||
|
|
@ -28,17 +27,6 @@ contract DeployLocal is Script {
|
||||||
address internal constant weth = 0x4200000000000000000000000000000000000006;
|
address internal constant weth = 0x4200000000000000000000000000000000000006;
|
||||||
address internal constant v3Factory = 0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24;
|
address internal constant v3Factory = 0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24;
|
||||||
|
|
||||||
// Seed amounts for VWAP bootstrap.
|
|
||||||
// seedLmEth: initial ETH sent to the LM to create thin bootstrap positions.
|
|
||||||
// seedSwapEth: ETH used for the seed buy. Must be large enough to move the
|
|
||||||
// Uniswap tick >400 ticks past the ANCHOR center (minAmplitude = 2*tickSpacing
|
|
||||||
// = 400 for the 1%-fee pool). The ANCHOR typically holds ~25% of seedLmEth as
|
|
||||||
// WETH across a ~7200-tick range; consuming half of that WETH (≈0.125 ETH)
|
|
||||||
// moves the price ~3600 ticks — well above the 400-tick threshold.
|
|
||||||
// 0.5 ether provides a 4× margin over the minimum needed.
|
|
||||||
uint256 internal constant SEED_LM_ETH = 1 ether;
|
|
||||||
uint256 internal constant SEED_SWAP_ETH = 0.5 ether;
|
|
||||||
|
|
||||||
// Deployed contracts
|
// Deployed contracts
|
||||||
Kraiken public kraiken;
|
Kraiken public kraiken;
|
||||||
Stake public stake;
|
Stake public stake;
|
||||||
|
|
@ -60,7 +48,7 @@ contract DeployLocal is Script {
|
||||||
|
|
||||||
// Deploy Kraiken token
|
// Deploy Kraiken token
|
||||||
kraiken = new Kraiken("Kraiken", "KRK");
|
kraiken = new Kraiken("Kraiken", "KRK");
|
||||||
console.log("\n[1/7] Kraiken deployed:", address(kraiken));
|
console.log("\n[1/6] Kraiken deployed:", address(kraiken));
|
||||||
|
|
||||||
// Determine token ordering
|
// Determine token ordering
|
||||||
token0isWeth = address(weth) < address(kraiken);
|
token0isWeth = address(weth) < address(kraiken);
|
||||||
|
|
@ -68,7 +56,7 @@ contract DeployLocal is Script {
|
||||||
|
|
||||||
// Deploy Stake contract
|
// Deploy Stake contract
|
||||||
stake = new Stake(address(kraiken), feeDest);
|
stake = new Stake(address(kraiken), feeDest);
|
||||||
console.log("\n[2/7] Stake deployed:", address(stake));
|
console.log("\n[2/6] Stake deployed:", address(stake));
|
||||||
|
|
||||||
// Set staking pool in Kraiken
|
// Set staking pool in Kraiken
|
||||||
kraiken.setStakingPool(address(stake));
|
kraiken.setStakingPool(address(stake));
|
||||||
|
|
@ -79,9 +67,9 @@ contract DeployLocal is Script {
|
||||||
address liquidityPool = factory.getPool(weth, address(kraiken), FEE);
|
address liquidityPool = factory.getPool(weth, address(kraiken), FEE);
|
||||||
if (liquidityPool == address(0)) {
|
if (liquidityPool == address(0)) {
|
||||||
liquidityPool = factory.createPool(weth, address(kraiken), FEE);
|
liquidityPool = factory.createPool(weth, address(kraiken), FEE);
|
||||||
console.log("\n[3/7] Uniswap pool created:", liquidityPool);
|
console.log("\n[3/6] Uniswap pool created:", liquidityPool);
|
||||||
} else {
|
} else {
|
||||||
console.log("\n[3/7] Using existing pool:", liquidityPool);
|
console.log("\n[3/6] Using existing pool:", liquidityPool);
|
||||||
}
|
}
|
||||||
pool = IUniswapV3Pool(liquidityPool);
|
pool = IUniswapV3Pool(liquidityPool);
|
||||||
|
|
||||||
|
|
@ -103,74 +91,22 @@ contract DeployLocal is Script {
|
||||||
bytes memory params = abi.encodeWithSignature("initialize(address,address)", address(kraiken), address(stake));
|
bytes memory params = abi.encodeWithSignature("initialize(address,address)", address(kraiken), address(stake));
|
||||||
ERC1967Proxy proxy = new ERC1967Proxy(address(optimizerImpl), params);
|
ERC1967Proxy proxy = new ERC1967Proxy(address(optimizerImpl), params);
|
||||||
address optimizerAddress = address(proxy);
|
address optimizerAddress = address(proxy);
|
||||||
console.log("\n[4/7] Optimizer deployed:", optimizerAddress);
|
console.log("\n[4/6] Optimizer deployed:", optimizerAddress);
|
||||||
|
|
||||||
// Deploy LiquidityManager
|
// Deploy LiquidityManager
|
||||||
liquidityManager = new LiquidityManager(v3Factory, weth, address(kraiken), optimizerAddress);
|
liquidityManager = new LiquidityManager(v3Factory, weth, address(kraiken), optimizerAddress);
|
||||||
console.log("\n[5/7] LiquidityManager deployed:", address(liquidityManager));
|
console.log("\n[5/6] LiquidityManager deployed:", address(liquidityManager));
|
||||||
|
|
||||||
// Configure contracts
|
// Configure contracts
|
||||||
kraiken.setLiquidityManager(address(liquidityManager));
|
kraiken.setLiquidityManager(address(liquidityManager));
|
||||||
console.log(" LiquidityManager set in Kraiken");
|
console.log(" LiquidityManager set in Kraiken");
|
||||||
|
|
||||||
console.log("\n[6/7] Configuration complete");
|
// Set the real feeDestination.
|
||||||
|
|
||||||
// =====================================================================
|
|
||||||
// [7/7] VWAP Bootstrap -> seed trade during deployment
|
|
||||||
//
|
|
||||||
// The cumulativeVolume==0 path in recenter() records VWAP from whatever
|
|
||||||
// price exists at the time of the first fee event. An attacker who
|
|
||||||
// front-runs deployment with a whale buy inflates that anchor.
|
|
||||||
//
|
|
||||||
// Fix: execute a small buy BEFORE handing control to users so that
|
|
||||||
// cumulativeVolume>0 by the time the protocol is live.
|
|
||||||
//
|
|
||||||
// Sequence:
|
|
||||||
// 1. Warp 301 seconds forward so the pool's TWAP oracle has enough history
|
|
||||||
// for _isPriceStable() to succeed (requires >= 300s of observations).
|
|
||||||
// 2. Fund LM with SEED_LM_ETH and call recenter() -> places thin initial
|
|
||||||
// positions; no fees collected yet, so cumulativeVolume stays 0.
|
|
||||||
// 3. Execute seed buy via SeedSwapper -> generates a non-zero WETH fee
|
|
||||||
// in the anchor position and moves the tick >400 (minimum amplitude).
|
|
||||||
// 4. Warp 301 more seconds so TWAP catches up to the post-buy price and
|
|
||||||
// cooldown (60s) elapses between the two recenters.
|
|
||||||
// 5. Call recenter() again -> cumulativeVolume==0 triggers the bootstrap
|
|
||||||
// path (shouldRecordVWAP=true); ethFee>0 → _recordVolumeAndPrice fires
|
|
||||||
// → cumulativeVolume>0. VWAP is now anchored to the real launch price.
|
|
||||||
// =====================================================================
|
|
||||||
console.log("\n[7/7] Bootstrapping VWAP with seed trade...");
|
|
||||||
|
|
||||||
// Step 1: Advance time so TWAP oracle has sufficient history.
|
|
||||||
// Track ts explicitly — Forge resets block.timestamp after state-changing calls,
|
|
||||||
// so block.timestamp + 301 would warp to the same value if used in Step 4.
|
|
||||||
uint256 ts = block.timestamp + 301;
|
|
||||||
vm.warp(ts);
|
|
||||||
|
|
||||||
// Step 2: Fund LM and place initial bootstrap positions.
|
|
||||||
(bool funded,) = address(liquidityManager).call{ value: SEED_LM_ETH }("");
|
|
||||||
require(funded, "Failed to fund LM for seed bootstrap");
|
|
||||||
liquidityManager.recenter();
|
|
||||||
console.log(" First recenter complete -> positions placed, cumulativeVolume still 0");
|
|
||||||
|
|
||||||
// Step 3: Seed buy -> generates a non-zero fee in the anchor position.
|
|
||||||
SeedSwapper seedSwapper = new SeedSwapper(weth, address(pool), token0isWeth);
|
|
||||||
seedSwapper.executeSeedBuy{ value: SEED_SWAP_ETH }(sender);
|
|
||||||
console.log(" Seed buy executed -> fee generated in anchor position");
|
|
||||||
|
|
||||||
// Step 4: Warp forward so TWAP settles at post-buy price and cooldown elapses.
|
|
||||||
ts += 301;
|
|
||||||
vm.warp(ts);
|
|
||||||
|
|
||||||
// Step 5: Second recenter records VWAP (bootstrap path + ethFee > 0).
|
|
||||||
liquidityManager.recenter();
|
|
||||||
require(liquidityManager.cumulativeVolume() > 0, "VWAP bootstrap failed: cumulativeVolume is 0");
|
|
||||||
console.log(" Second recenter complete -> VWAP bootstrapped");
|
|
||||||
console.log(" cumulativeVolume:", liquidityManager.cumulativeVolume());
|
|
||||||
console.log(" VWAP (X96):", liquidityManager.getVWAP());
|
|
||||||
|
|
||||||
// Set the real feeDestination now that bootstrap is complete.
|
|
||||||
liquidityManager.setFeeDestination(feeDest);
|
liquidityManager.setFeeDestination(feeDest);
|
||||||
|
|
||||||
|
console.log("\n[6/6] Configuration complete");
|
||||||
console.log(" feeDestination set to", feeDest);
|
console.log(" feeDestination set to", feeDest);
|
||||||
|
console.log(" VWAP bootstrap will be performed by the bootstrap script");
|
||||||
|
|
||||||
// Print deployment summary
|
// Print deployment summary
|
||||||
console.log("\n=== Deployment Summary ===");
|
console.log("\n=== Deployment Summary ===");
|
||||||
|
|
@ -181,10 +117,10 @@ contract DeployLocal is Script {
|
||||||
console.log("Optimizer:", optimizerAddress);
|
console.log("Optimizer:", optimizerAddress);
|
||||||
|
|
||||||
console.log("\n=== Next Steps ===");
|
console.log("\n=== Next Steps ===");
|
||||||
console.log("VWAP is already bootstrapped. To go live:");
|
console.log("1. bootstrap-common.sh bootstrap_vwap() advances chain time and seeds VWAP.");
|
||||||
console.log("1. Fund LiquidityManager with operational ETH (current balance includes seed):");
|
console.log("2. Fund LiquidityManager with operational ETH:");
|
||||||
console.log(" cast send", address(liquidityManager), "--value 10ether");
|
console.log(" cast send", address(liquidityManager), "--value 10ether");
|
||||||
console.log("2. recenter() is now permissionless - any address (e.g. txnBot) can call it.");
|
console.log("3. recenter() is permissionless - any address (e.g. txnBot) can call it.");
|
||||||
console.log(" TWAP manipulation protection is always enforced (no bypass path).");
|
console.log(" TWAP manipulation protection is always enforced (no bypass path).");
|
||||||
|
|
||||||
vm.stopBroadcast();
|
vm.stopBroadcast();
|
||||||
|
|
|
||||||
|
|
@ -101,34 +101,71 @@ fund_liquidity_manager() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
call_recenter() {
|
bootstrap_vwap() {
|
||||||
local recenter_pk="$DEPLOYER_PK"
|
# Idempotency guard: if a previous run already bootstrapped VWAP, skip.
|
||||||
local recenter_addr="$DEPLOYER_ADDR"
|
|
||||||
if [[ -n "${TXNBOT_ADDRESS:-}" ]]; then
|
|
||||||
recenter_pk="$TXNBOT_PRIVATE_KEY"
|
|
||||||
recenter_addr="$TXNBOT_ADDRESS"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If the deploy script already bootstrapped VWAP (cumulativeVolume > 0), positions
|
|
||||||
# are in place at the post-seed-buy tick. Calling recenter() now would fail with
|
|
||||||
# "amplitude not reached" because currentTick == anchorCenterTick. Skip it.
|
|
||||||
local cumvol
|
local cumvol
|
||||||
cumvol="$(cast call --rpc-url "$ANVIL_RPC" \
|
cumvol="$(cast call --rpc-url "$ANVIL_RPC" \
|
||||||
"$LIQUIDITY_MANAGER" "cumulativeVolume()(uint256)" 2>/dev/null || echo "0")"
|
"$LIQUIDITY_MANAGER" "cumulativeVolume()(uint256)" 2>/dev/null || echo "0")"
|
||||||
# cast call with a typed (uint256) selector returns a plain decimal string for
|
|
||||||
# non-zero values (e.g. "140734553600000") and "0" for zero. A simple != "0"
|
|
||||||
# check is sufficient; note that the output may include a scientific-notation
|
|
||||||
# annotation (e.g. "140734553600000 [1.407e14]") which is also != "0", so we
|
|
||||||
# do NOT attempt to parse it further with cast to-dec (which would fail on the
|
|
||||||
# annotation and incorrectly fall back to "0").
|
|
||||||
if [[ "$cumvol" != "0" && -n "$cumvol" ]]; then
|
if [[ "$cumvol" != "0" && -n "$cumvol" ]]; then
|
||||||
bootstrap_log "VWAP already bootstrapped by deploy script (cumulativeVolume=$cumvol) -- skipping initial recenter"
|
bootstrap_log "VWAP already bootstrapped (cumulativeVolume=$cumvol) -- skipping"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
bootstrap_log "Calling recenter() via $recenter_addr"
|
local recenter_pk="${TXNBOT_PRIVATE_KEY:-$DEPLOYER_PK}"
|
||||||
|
|
||||||
|
# Fund LM with 1 ETH (thin bootstrap positions; 0.5 ETH seed swap moves >400 ticks)
|
||||||
|
bootstrap_log "Funding LM with 1 ETH for VWAP bootstrap..."
|
||||||
|
cast send --rpc-url "$ANVIL_RPC" --private-key "$DEPLOYER_PK" \
|
||||||
|
"$LIQUIDITY_MANAGER" --value 1ether >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
# Advance Anvil time 301s so TWAP oracle has sufficient history for _isPriceStable()
|
||||||
|
cast rpc --rpc-url "$ANVIL_RPC" evm_increaseTime 301 >>"$LOG_FILE" 2>&1
|
||||||
|
cast rpc --rpc-url "$ANVIL_RPC" evm_mine >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
# First recenter: places initial bootstrap positions; no fees yet, cumulativeVolume stays 0
|
||||||
|
bootstrap_log "First recenter (places bootstrap positions)..."
|
||||||
cast send --rpc-url "$ANVIL_RPC" --private-key "$recenter_pk" \
|
cast send --rpc-url "$ANVIL_RPC" --private-key "$recenter_pk" \
|
||||||
"$LIQUIDITY_MANAGER" "recenter()" >>"$LOG_FILE" 2>&1
|
"$LIQUIDITY_MANAGER" "recenter()" >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
# Seed buy: wrap 0.5 ETH to WETH and swap WETH->KRK
|
||||||
|
# Generates a non-zero WETH fee in the anchor position and moves price >400 ticks.
|
||||||
|
# sqrtPriceLimitX96 is direction-dependent: MIN+1 when WETH<KRK (token0), MAX-1 otherwise.
|
||||||
|
bootstrap_log "Executing seed buy (0.5 ETH WETH->KRK)..."
|
||||||
|
cast send --rpc-url "$ANVIL_RPC" --private-key "$DEPLOYER_PK" \
|
||||||
|
"$WETH" "deposit()" --value 0.5ether >>"$LOG_FILE" 2>&1
|
||||||
|
cast send --rpc-url "$ANVIL_RPC" --private-key "$DEPLOYER_PK" \
|
||||||
|
"$WETH" "approve(address,uint256)" "$SWAP_ROUTER" "$MAX_UINT" >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
local weth_addr kraiken_addr sqrt_limit
|
||||||
|
weth_addr=$(echo "$WETH" | tr '[:upper:]' '[:lower:]' | sed 's/^0x//')
|
||||||
|
kraiken_addr=$(echo "$KRAIKEN" | tr '[:upper:]' '[:lower:]' | sed 's/^0x//')
|
||||||
|
if [[ "$weth_addr" < "$kraiken_addr" ]]; then
|
||||||
|
sqrt_limit=4295128740 # WETH=token0, zeroForOne=true, price decreases
|
||||||
|
else
|
||||||
|
sqrt_limit=1461446703485210103287273052203988822378723970341 # WETH=token1, price increases
|
||||||
|
fi
|
||||||
|
|
||||||
|
cast send --legacy --gas-limit 300000 --rpc-url "$ANVIL_RPC" --private-key "$DEPLOYER_PK" \
|
||||||
|
"$SWAP_ROUTER" "exactInputSingle((address,address,uint24,address,uint256,uint256,uint160))" \
|
||||||
|
"($WETH,$KRAIKEN,10000,$DEPLOYER_ADDR,500000000000000000,0,$sqrt_limit)" >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
# Advance time 301s so TWAP settles at post-buy price and cooldown (60s) elapses
|
||||||
|
cast rpc --rpc-url "$ANVIL_RPC" evm_increaseTime 301 >>"$LOG_FILE" 2>&1
|
||||||
|
cast rpc --rpc-url "$ANVIL_RPC" evm_mine >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
# Second recenter: cumulativeVolume==0 path fires (bootstrap), ethFee>0 -> records VWAP
|
||||||
|
bootstrap_log "Second recenter (records VWAP)..."
|
||||||
|
cast send --rpc-url "$ANVIL_RPC" --private-key "$recenter_pk" \
|
||||||
|
"$LIQUIDITY_MANAGER" "recenter()" >>"$LOG_FILE" 2>&1
|
||||||
|
|
||||||
|
# Verify VWAP bootstrap succeeded
|
||||||
|
cumvol="$(cast call --rpc-url "$ANVIL_RPC" \
|
||||||
|
"$LIQUIDITY_MANAGER" "cumulativeVolume()(uint256)" 2>/dev/null || echo "0")"
|
||||||
|
if [[ "$cumvol" == "0" || -z "$cumvol" ]]; then
|
||||||
|
bootstrap_log "ERROR: VWAP bootstrap failed -- cumulativeVolume is 0"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
bootstrap_log "VWAP bootstrapped (cumulativeVolume=$cumvol)"
|
||||||
}
|
}
|
||||||
|
|
||||||
seed_application_state() {
|
seed_application_state() {
|
||||||
|
|
|
||||||
|
|
@ -54,12 +54,12 @@ write_deployments_json "$ONCHAIN_DIR/deployments-local.json"
|
||||||
echo "=== deployments-local.json written ==="
|
echo "=== deployments-local.json written ==="
|
||||||
cat "$ONCHAIN_DIR/deployments-local.json"
|
cat "$ONCHAIN_DIR/deployments-local.json"
|
||||||
|
|
||||||
|
echo "=== Bootstrapping VWAP ==="
|
||||||
|
bootstrap_vwap
|
||||||
|
|
||||||
echo "=== Funding LiquidityManager ==="
|
echo "=== Funding LiquidityManager ==="
|
||||||
fund_liquidity_manager
|
fund_liquidity_manager
|
||||||
|
|
||||||
echo "=== Calling recenter() to seed liquidity ==="
|
|
||||||
call_recenter
|
|
||||||
|
|
||||||
echo "=== Seeding application state (initial swap) ==="
|
echo "=== Seeding application state (initial swap) ==="
|
||||||
seed_application_state
|
seed_application_state
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue