harb/onchain/script/DeployCommon.sol
openhands 38bc0f7057 fix: address AI review findings on VWAP bootstrap PR
SPDX license:
- Restore GPL-3.0-or-later SPDX header to DeployBase.sol (removed by
  the em-dash sed fix in an earlier commit).

SeedSwapper deduplication:
- Extract SeedSwapper into onchain/script/DeployCommon.sol — a single
  canonical definition shared by both deploy scripts.  This eliminates
  duplicate Foundry artifacts (previously both DeployLocal.sol and
  DeployBase.sol produced a SeedSwapper artifact, causing ambiguity for
  verification and coverage tools).
- Remove inline SeedSwapper and redundant IWETH9 import from
  DeployLocal.sol and DeployBase.sol; add `import "./DeployCommon.sol"`.

SeedSwapper hardening (in DeployCommon.sol):
- Replace magic-literal price sentinels with named constants
  SQRT_PRICE_LIMIT_MIN / SQRT_PRICE_LIMIT_MAX.
- Wrap both weth.transfer() calls with require() so a non-standard
  WETH9 false-return is caught rather than silently ignored.
- Add post-swap WETH sweep in executeSeedBuy(): if the price limit is
  reached before the full input is spent, the residual WETH balance is
  returned to `recipient` instead of being stranded in the contract.

bootstrap-common.sh:
- Normalise cumulativeVolume output through `cast to-dec` before the
  string comparison, guarding against a future change in cast output
  format (decimal vs hex).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-13 00:12:39 +00:00

68 lines
3.3 KiB
Solidity

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.19;
import { IWETH9 } from "../src/interfaces/IWETH9.sol";
import "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";
/**
* @title SeedSwapper
* @notice One-shot helper deployed during Deploy*.run() to perform the initial seed buy.
* Executing a small buy before the protocol opens eliminates the cumulativeVolume==0
* front-run window: after the seed recenter, VWAP has a real anchor and the bootstrap
* path in LiquidityManager.recenter() is never reachable by external users.
*
* @dev Deployed by both DeployLocal and DeployBase; extracted here to avoid duplicate
* Foundry artifacts and drift between the two deploy scripts.
*/
contract SeedSwapper {
// TickMath sentinel values — the pool will consume as much input as available in-range.
uint160 private constant SQRT_PRICE_LIMIT_MIN = 4295128740; // TickMath.MIN_SQRT_RATIO + 1
uint160 private constant SQRT_PRICE_LIMIT_MAX = // TickMath.MAX_SQRT_RATIO - 1
1461446703485210103287273052203988822378723970341;
IWETH9 private immutable weth;
IUniswapV3Pool private immutable pool;
bool private immutable token0isWeth;
constructor(address _weth, address _pool, bool _token0isWeth) {
weth = IWETH9(_weth);
pool = IUniswapV3Pool(_pool);
token0isWeth = _token0isWeth;
}
/// @notice Wraps msg.value ETH to WETH, swaps it for KRK (buying KRK), and sweeps any
/// unconsumed WETH back to `recipient`. The fee generated by the swap is captured
/// in the LM's anchor position so the subsequent recenter() call will collect a
/// non-zero ethFee and record VWAP.
///
/// @dev If the pool exhausts in-range liquidity before spending all WETH (price-limit
/// sentinel reached), `wethDelta < msg.value` in the callback and the remainder
/// stays as WETH in this contract. The post-swap sweep returns it to `recipient`
/// so no funds are stranded.
function executeSeedBuy(address recipient) external payable {
weth.deposit{ value: msg.value }();
// zeroForOne=true when WETH is token0: sell token0(WETH) -> token1(KRK)
// zeroForOne=false when WETH is token1: sell token1(WETH) -> token0(KRK)
bool zeroForOne = token0isWeth;
uint160 priceLimit = zeroForOne ? SQRT_PRICE_LIMIT_MIN : SQRT_PRICE_LIMIT_MAX;
pool.swap(recipient, zeroForOne, int256(msg.value), priceLimit, "");
// Sweep any unconsumed WETH to recipient (occurs when price limit is reached
// before the full input is spent).
uint256 leftover = weth.balanceOf(address(this));
if (leftover > 0) {
require(weth.transfer(recipient, leftover), "SeedSwapper: WETH sweep failed");
}
}
/// @notice Uniswap V3 callback: pay the WETH owed for the seed buy.
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata) external {
require(msg.sender == address(pool), "SeedSwapper: only pool");
int256 wethDelta = token0isWeth ? amount0Delta : amount1Delta;
if (wethDelta > 0) {
require(weth.transfer(msg.sender, uint256(wethDelta)), "SeedSwapper: WETH transfer failed");
}
}
}