Verified contractExactMIT
FeeHook
Address0x84319955aa550dbedac99bb87dcbf5a48ac4c0cc
- Balance
0.001566 ETH
- Transactions
37
- Compiler
solc 0.8.26
- Same code
Only this contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,325,594
- Creation transaction
- 0x2e41e36eb8…09582278be
25 files
- src/FeeHook.solmain27.69 KB
- lib/openzeppelin-contracts/contracts/access/Ownable.sol3.1 KB
- lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol2.48 KB
- lib/openzeppelin-contracts/contracts/utils/Context.sol963 B
- lib/v4-core/src/interfaces/IExtsload.sol1.02 KB
- lib/v4-core/src/interfaces/IExttload.sol699 B
- lib/v4-core/src/interfaces/IHooks.sol8.3 KB
- lib/v4-core/src/interfaces/IPoolManager.sol13.45 KB
- lib/v4-core/src/interfaces/IProtocolFees.sol2.49 KB
- lib/v4-core/src/interfaces/external/IERC20Minimal.sol3.04 KB
- lib/v4-core/src/interfaces/external/IERC6909Claims.sol3.16 KB
- lib/v4-core/src/libraries/CustomRevert.sol5.17 KB
- lib/v4-core/src/libraries/Hooks.sol16.49 KB
- lib/v4-core/src/libraries/LPFeeLibrary.sol3.6 KB
- lib/v4-core/src/libraries/ParseBytes.sol1.2 KB
- lib/v4-core/src/libraries/SafeCast.sol2.27 KB
- lib/v4-core/src/types/BalanceDelta.sol2.34 KB
- lib/v4-core/src/types/BeforeSwapDelta.sol1.47 KB
- lib/v4-core/src/types/Currency.sol5.46 KB
- lib/v4-core/src/types/PoolId.sol539 B
- lib/v4-core/src/types/PoolKey.sol805 B
- src/interfaces/IBountyEngine.sol5.91 KB
- src/interfaces/IBurnEngine.sol4.01 KB
- src/interfaces/IRewardsDistributor.sol4.42 KB
- src/interfaces/IWordToken.sol6.56 KB
src/FeeHook.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {SafeCast} from "v4-core/src/libraries/SafeCast.sol";
import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary, toBeforeSwapDelta} from "v4-core/src/types/BeforeSwapDelta.sol";
import {Currency, CurrencyLibrary} from "v4-core/src/types/Currency.sol";
import {PoolId, PoolIdLibrary} from "v4-core/src/types/PoolId.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {IBountyEngine} from "./interfaces/IBountyEngine.sol";
import {IBurnEngine} from "./interfaces/IBurnEngine.sol";
import {IRewardsDistributor} from "./interfaces/IRewardsDistributor.sol";
import {IWordToken} from "./interfaces/IWordToken.sol";
/// @title FeeHook — Uniswap V4 hook: 1% ETH-side fee skim, launch guard, dynamic fee router
/// @notice Attached to the canonical native-ETH/WORD pool with `beforeSwap` + `afterSwap` +
/// return-delta permissions. On every swap it skims `feeBps` (launch 100 = 1%, hard
/// ceiling 200) of the ETH side — ETH in on buys, ETH out on sells — and accrues the
/// ETH in this contract. A public, permissionless `flush()` routes the accrued balance
/// with the split chosen PER FLUSH by whether burnable WORD exists (Option B):
/// **burnable excess present → three-way** (launch 50% RewardsDistributor / 25%
/// BountyEngine / 25% BurnEngine); **no excess → two-way** (default 70% rewards / 30%
/// bounty) so the burn slice folds into the holder/bounty streams rather than idling.
/// The floor is dynamic (`WordBank.totalAlive() × 1000e18`, read via
/// `IWordToken.burnableExcess()`), so excess appears whenever an unbind frees WORD and
/// is consumed as the BurnEngine buys and burns it — the split toggles between the two
/// modes over the protocol's life and never permanently "completes". Both split
/// configurations are admin-reconfigurable within hardcoded bounds and are BOTH live
/// permanently; the hook selects between them automatically.
///
/// Launch protection: all swaps revert until the one-time `enableTrading()`; for at
/// most 1 hour after it (earlier if the one-time `sunsetGuard()` fires) any single buy
/// whose WORD output exceeds 10,000e18 (1% of the 1,000,000e18 liquidity seed) reverts.
/// A pool-wide per-block buy cap was considered and DELIBERATELY REJECTED (architecture
/// §3): a fixed per-block quota is a race bots win against retail, a worse DoS than the
/// whale problem it addresses. The per-swap cap stands alone.
///
/// @dev The hook holds NO game, token, or swap-execution logic (system invariant 8). It
/// never calls WordBank and never executes the buyback — it only takes ETH from the
/// PoolManager and forwards it. The BurnEngine runs its buyback in its own transaction.
///
/// Skim accrual strategy (agent-5 micro-decision a): ACCUMULATE + PUBLIC FLUSH. The
/// per-swap cost is a single `poolManager.take` of native ETH to this contract (~10k
/// gas); the three external `deposit()` calls (~30k+ gas and three CALLs with value)
/// are deferred to `flush()`, which anyone may call at any time, so no stream ever
/// depends on an admin and swappers never pay routing gas. Split changes flush first,
/// so fees accrued under an old split are always routed at that old split.
///
/// Skim placement: ETH is ALWAYS currency0 of the canonical pool (native ETH sorts
/// first). When ETH is the swap's SPECIFIED currency (exact-input buy, exact-output
/// sell) the fee is taken in `beforeSwap` via the specified-currency return delta and
/// the basis is |amountSpecified|. When ETH is UNSPECIFIED (exact-output buy,
/// exact-input sell) the fee is taken in `afterSwap` via the unspecified-currency
/// return delta and the basis is the pool's ETH delta. The basis is therefore always
/// "the ETH amount known at the skim point"; the two bases differ only second-order
/// (1% of 1%) and each case is exact against its own basis (tested).
///
/// Guard boundary (documented choice): a buy of EXACTLY 10,000e18 WORD passes;
/// strictly more reverts. The BurnEngine is exempt from the guard (its buyback is a
/// buy and must never brick on it — architecture §6); it is identified as the swap
/// *sender* because it swaps the PoolManager directly, not through a router.
contract FeeHook is IHooks, Ownable2Step {
using CurrencyLibrary for Currency;
using PoolIdLibrary for PoolKey;
using SafeCast for uint256;
// ─────────────────────────────────── constants ─────────────────────────────────────
/// @notice Basis-point denominator for the fee rate and all splits.
uint256 public constant BPS = 10_000;
/// @notice Hard ceiling on the swap fee rate (2%). The admin can tune but never exceed.
uint16 public constant MAX_FEE_BPS = 200;
/// @notice Launch-guard per-buy WORD output cap: 1% of the 1,000,000e18 liquidity seed.
uint128 public constant BUY_CAP = 10_000e18;
/// @notice Hardcoded guard auto-expiry after `enableTrading()` — the backstop that kills
/// the cap even if the admin key goes dark.
uint256 public constant GUARD_DURATION = 1 hours;
// Three-way split bounds (active while burning) — architecture NORMATIVE.
uint16 public constant BURN_PHASE_REWARDS_MIN_BPS = 4000;
uint16 public constant BURN_PHASE_REWARDS_MAX_BPS = 6000;
uint16 public constant BURN_PHASE_BOUNTY_MIN_BPS = 1500;
uint16 public constant BURN_PHASE_BOUNTY_MAX_BPS = 3500;
uint16 public constant BURN_PHASE_BURN_MIN_BPS = 1500;
uint16 public constant BURN_PHASE_BURN_MAX_BPS = 3500;
// Two-way split bounds (after burn completion) — architecture NORMATIVE.
uint16 public constant POST_BURN_REWARDS_MIN_BPS = 5000;
uint16 public constant POST_BURN_REWARDS_MAX_BPS = 8000;
uint16 public constant POST_BURN_BOUNTY_MIN_BPS = 2000;
uint16 public constant POST_BURN_BOUNTY_MAX_BPS = 5000;
// Transient-storage slot for the flush reentrancy lock. Arbitrary fixed slot; this is the
// contract's only transient-storage use, so there is nothing to collide with.
bytes32 private constant FLUSH_LOCK_SLOT = 0x1f3c9647dbcc7da785a80f81f6f7d09ae62fa4a54a0bcef4e2962426c87a8113;
// ──────────────────────────────────── storage ──────────────────────────────────────
/// @notice The Uniswap V4 PoolManager.
IPoolManager public immutable poolManager;
/// @notice Holder fee-share stream (50% at launch; default 70% post-burn).
IRewardsDistributor public immutable rewardsDistributor;
/// @notice Bounty treasury stream (25% at launch; default 30% post-burn).
IBountyEngine public immutable bountyEngine;
/// @notice Buy-and-burn stream — receives the burn slice only on flushes where burnable
/// excess exists. Never retires (the dynamic floor descends as NFTs unbind).
IBurnEngine public immutable burnEngine;
/// @notice The WORD token — read on each flush for `burnableExcess()`, which selects the
/// routing mode. ETH is currency0; WORD is currency1 of the canonical pool.
IWordToken public immutable wordToken;
/// @notice The canonical pool's id — the only pool this hook serves.
PoolId public immutable canonicalPoolId;
/// @notice Swap fee rate in bps of the ETH side (launch 100 = 1%; 1..MAX_FEE_BPS).
uint16 public feeBps = 100;
// ── three-way split (used on flushes where burnable excess exists) ──
/// @notice Rewards slice of the three-way split, bps. Launch 5000. setBurnPhaseSplit.
uint16 public rewardsBps = 5000;
/// @notice Bounty slice of the three-way split, bps. Launch 2500. setBurnPhaseSplit.
uint16 public bountyBps = 2500;
/// @notice Burn slice of the three-way split, bps. Launch 2500. setBurnPhaseSplit.
uint16 public burnBps = 2500;
// ── two-way split (used on flushes with no burnable excess; burn slice folds in) ──
/// @notice Rewards slice of the two-way split, bps. Default 7000. setPostBurnSplit.
uint16 public postRewardsBps = 7000;
/// @notice Bounty slice of the two-way split, bps. Default 3000. setPostBurnSplit.
uint16 public postBountyBps = 3000;
/// @notice Timestamp of the one-time `enableTrading()`; 0 while trading is gated.
uint64 public tradingEnabledAt;
/// @notice True once the admin has fired the one-time `sunsetGuard()`.
bool public guardSunset;
// ──────────────────────────────────── events ───────────────────────────────────────
/// @notice Trading was enabled (one-time). The guard window runs from this moment.
event TradingEnabled(uint256 timestamp);
/// @notice The admin sunset the launch guard ahead of its 1-hour auto-expiry.
event GuardSunset(uint256 timestamp);
/// @notice Accrued ETH was routed. `toBurn` is 0 on flushes with no burnable excess
/// (two-way mode); positive when excess exists (three-way mode).
event Flushed(address indexed caller, uint256 toRewards, uint256 toBounty, uint256 toBurn);
/// @notice A swap was skimmed. `ethSpecified` tells which basis applied (see contract @dev).
event FeeSkimmed(PoolId indexed poolId, uint256 ethAmount, bool ethSpecified);
/// @notice Admin changed the fee rate.
event FeeBpsSet(uint16 feeBps);
/// @notice Admin changed the three-way (excess) split.
event BurnPhaseSplitSet(uint16 rewardsBps, uint16 bountyBps, uint16 burnBps);
/// @notice Admin changed the two-way (no-excess) split.
event PostBurnSplitSet(uint16 rewardsBps, uint16 bountyBps);
// ──────────────────────────────────── errors ───────────────────────────────────────
/// @notice Caller is not the PoolManager.
error NotPoolManager();
/// @notice This hook only serves the canonical ETH/WORD pool.
error NotCanonicalPool();
/// @notice A hook callback this contract does not use was invoked.
error HookNotImplemented();
/// @notice Swaps are gated until the one-time enableTrading().
error TradingNotEnabled();
/// @notice Trading was already enabled (enableTrading is one-time).
error TradingAlreadyEnabled();
/// @notice The guard was already sunset (sunsetGuard is one-time).
error GuardAlreadySunset();
/// @notice Launch guard: a single buy's WORD output exceeded BUY_CAP.
error BuyExceedsLaunchCap(uint256 wordOut);
/// @notice Fee rate outside (0, MAX_FEE_BPS].
error FeeOutOfBounds();
/// @notice Split shares outside their hardcoded bounds or not summing to 100%.
error SplitOutOfBounds();
/// @notice Reentrant flush.
error FlushReentered();
/// @notice Zero address where a real address is required.
error ZeroAddress();
// ──────────────────────────────────── modifiers ────────────────────────────────────
modifier onlyPoolManager() {
if (msg.sender != address(poolManager)) revert NotPoolManager();
_;
}
// ───────────────────────────────── construction ────────────────────────────────────
/// @param manager The V4 PoolManager.
/// @param word The WORD token (currency1 of the canonical pool; ETH is currency0).
/// @param lpFee The canonical pool's LP fee (pips), part of its PoolKey.
/// @param tickSpacing The canonical pool's tick spacing, part of its PoolKey.
/// @param rewards The RewardsDistributor.
/// @param bounty The BountyEngine.
/// @param burn The BurnEngine.
/// @param admin The protocol admin (bounded powers only — invariant 7).
constructor(
IPoolManager manager,
address word,
uint24 lpFee,
int24 tickSpacing,
IRewardsDistributor rewards,
IBountyEngine bounty,
IBurnEngine burn,
address admin
) Ownable(admin) {
if (
address(manager) == address(0) || word == address(0) || address(rewards) == address(0)
|| address(bounty) == address(0) || address(burn) == address(0)
) revert ZeroAddress();
poolManager = manager;
rewardsDistributor = rewards;
bountyEngine = bounty;
burnEngine = burn;
wordToken = IWordToken(word);
canonicalPoolId = PoolKey({
currency0: CurrencyLibrary.ADDRESS_ZERO,
currency1: Currency.wrap(word),
fee: lpFee,
tickSpacing: tickSpacing,
hooks: IHooks(address(this))
}).toId();
// The deployed address must encode exactly our four permission flags (CREATE2-mined).
Hooks.validateHookPermissions(
IHooks(address(this)),
Hooks.Permissions({
beforeInitialize: false,
afterInitialize: false,
beforeAddLiquidity: false,
afterAddLiquidity: false,
beforeRemoveLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: true,
afterSwapReturnDelta: true,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
})
);
}
/// @notice Receives the skimmed ETH from `poolManager.take`. Donations are accepted too —
/// anything here is protocol revenue at the next flush.
receive() external payable {}
// ────────────────────────────── swap-path callbacks ────────────────────────────────
/// @inheritdoc IHooks
/// @dev Gate check, canonical-pool check, and the ETH-specified skim (exact-input buys,
/// exact-output sells). Fee basis: |amountSpecified|.
function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata)
external
onlyPoolManager
returns (bytes4, BeforeSwapDelta, uint24)
{
if (PoolId.unwrap(key.toId()) != PoolId.unwrap(canonicalPoolId)) revert NotCanonicalPool();
if (tradingEnabledAt == 0) revert TradingNotEnabled();
// Specified currency is currency0 (= ETH) iff exact-input direction matches zeroForOne.
bool ethSpecified = (params.amountSpecified < 0) == params.zeroForOne;
if (!ethSpecified) return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
uint256 ethAmount =
params.amountSpecified < 0 ? uint256(-params.amountSpecified) : uint256(params.amountSpecified);
uint256 fee = ethAmount * feeBps / BPS;
if (fee == 0) return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
// Take the fee now; the positive specified-currency return delta charges it to the swap.
poolManager.take(key.currency0, address(this), fee);
emit FeeSkimmed(canonicalPoolId, fee, true);
return (IHooks.beforeSwap.selector, toBeforeSwapDelta(fee.toInt128(), 0), 0);
}
/// @inheritdoc IHooks
/// @dev Launch-guard post-check on buys, and the ETH-unspecified skim (exact-output buys,
/// exact-input sells). Fee basis: the pool's ETH delta for this swap.
function afterSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta delta,
bytes calldata
) external onlyPoolManager returns (bytes4, int128) {
// Anti-whale guard: buys only (zeroForOne = ETH in, WORD out). delta.amount1() is
// exactly the WORD credited to the swapper — the skim never touches the WORD side.
// Boundary: exactly BUY_CAP passes, strictly more reverts. BurnEngine exempt.
if (params.zeroForOne && sender != address(burnEngine) && guardActive()) {
int128 wordOut = delta.amount1();
if (wordOut > 0 && uint128(wordOut) > BUY_CAP) revert BuyExceedsLaunchCap(uint128(wordOut));
}
bool ethSpecified = (params.amountSpecified < 0) == params.zeroForOne;
if (ethSpecified) return (IHooks.afterSwap.selector, 0);
int128 ethDelta = delta.amount0();
uint256 ethAmount = ethDelta < 0 ? uint256(uint128(-ethDelta)) : uint256(uint128(ethDelta));
uint256 fee = ethAmount * feeBps / BPS;
if (fee == 0) return (IHooks.afterSwap.selector, 0);
poolManager.take(key.currency0, address(this), fee);
emit FeeSkimmed(canonicalPoolId, fee, false);
return (IHooks.afterSwap.selector, fee.toInt128());
}
// ─────────────────────────────────── routing ───────────────────────────────────────
/// @notice Routes this contract's entire ETH balance, choosing the split PER FLUSH from
/// live burnable excess. Permissionless by design — no fee stream ever depends on
/// an admin. **Burnable excess present** → three-way rewards/bounty/burn
/// (remainder-to-burn). **No excess** → two-way rewards/bounty (remainder-to-bounty),
/// and the BurnEngine receives nothing — the burn slice folds into the holder and
/// bounty streams so no fee ever idles waiting for a burn that cannot happen yet.
/// The selection is automatic and reversible: excess reappears whenever an unbind
/// lowers the floor, so the hook flips back to three-way on the next flush.
/// @dev `wordToken.burnableExcess()` reads `totalSupply − WordBank.totalAlive()×1000e18`.
/// A view-only external read inside the reentrancy-locked flush; the recipients are
/// trusted protocol contracts. In both modes the slices sum to exactly 100% of the
/// routed balance (the last slice takes the integer-division remainder).
function flush() public {
// Transient reentrancy lock: recipients are trusted protocol contracts, but a
// reentrant flush would mis-split the precomputed slices, so it is excluded outright.
bool locked;
assembly ("memory-safe") {
locked := tload(FLUSH_LOCK_SLOT)
}
if (locked) revert FlushReentered();
assembly ("memory-safe") {
tstore(FLUSH_LOCK_SLOT, 1)
}
uint256 balance = address(this).balance;
if (balance != 0) {
uint256 toRewards;
uint256 toBounty;
uint256 toBurn;
if (wordToken.burnableExcess() > 0) {
// Three-way: there is WORD to buy and burn.
toRewards = balance * rewardsBps / BPS;
toBounty = balance * bountyBps / BPS;
toBurn = balance - toRewards - toBounty; // remainder → burn
} else {
// Two-way: nothing burnable — fold the burn slice into rewards/bounty.
toRewards = balance * postRewardsBps / BPS;
toBounty = balance - toRewards; // remainder → bounty
}
if (toRewards != 0) rewardsDistributor.deposit{value: toRewards}();
if (toBounty != 0) bountyEngine.deposit{value: toBounty}();
if (toBurn != 0) burnEngine.deposit{value: toBurn}();
emit Flushed(msg.sender, toRewards, toBounty, toBurn);
}
assembly ("memory-safe") {
tstore(FLUSH_LOCK_SLOT, 0)
}
}
/// @notice ETH accrued and not yet flushed.
function pendingFees() external view returns (uint256) {
return address(this).balance;
}
// ─────────────────────────────── launch lifecycle ──────────────────────────────────
/// @notice One-time trading enable. Records `tradingEnabledAt` — the anchor for the
/// guard's 1-hour auto-expiry. Not re-disableable, not re-callable.
function enableTrading() external onlyOwner {
if (tradingEnabledAt != 0) revert TradingAlreadyEnabled();
tradingEnabledAt = uint64(block.timestamp);
emit TradingEnabled(block.timestamp);
}
/// @notice One-time, admin-only early end to the launch guard. Irreversible; the
/// 1-hour auto-expiry stands regardless, so the guard dies even if this is
/// never called.
function sunsetGuard() external onlyOwner {
if (tradingEnabledAt == 0) revert TradingNotEnabled();
if (guardSunset) revert GuardAlreadySunset();
guardSunset = true;
emit GuardSunset(block.timestamp);
}
/// @notice True while the anti-whale buy cap is enforced: trading enabled, not sunset,
/// and within 1 hour of `tradingEnabledAt`.
function guardActive() public view returns (bool) {
return tradingEnabledAt != 0 && !guardSunset && block.timestamp < uint256(tradingEnabledAt) + GUARD_DURATION;
}
// ──────────────────────────────── bounded admin ────────────────────────────────────
/// @notice Sets the fee rate. Bounded (0, MAX_FEE_BPS] — the admin can tune within the
/// 2% ceiling but can neither exceed it nor zero the streams out entirely.
/// @dev Flushes first so fees accrued at the old rate/split are routed before the
/// change takes effect.
function setFeeBps(uint16 newFeeBps) external onlyOwner {
if (newFeeBps == 0 || newFeeBps > MAX_FEE_BPS) revert FeeOutOfBounds();
flush();
feeBps = newFeeBps;
emit FeeBpsSet(newFeeBps);
}
/// @notice Sets the three-way (burnable-excess) split. Bounds: rewards 4000–6000, bounty
/// 1500–3500, burn 1500–3500, sum exactly 10000. Permanently live — applied on any
/// flush where burnable excess exists.
/// @dev Flushes first so pending fees route at the old configuration.
function setBurnPhaseSplit(uint16 newRewardsBps, uint16 newBountyBps, uint16 newBurnBps) external onlyOwner {
flush();
if (
newRewardsBps < BURN_PHASE_REWARDS_MIN_BPS || newRewardsBps > BURN_PHASE_REWARDS_MAX_BPS
|| newBountyBps < BURN_PHASE_BOUNTY_MIN_BPS || newBountyBps > BURN_PHASE_BOUNTY_MAX_BPS
|| newBurnBps < BURN_PHASE_BURN_MIN_BPS || newBurnBps > BURN_PHASE_BURN_MAX_BPS
|| uint256(newRewardsBps) + newBountyBps + newBurnBps != BPS
) revert SplitOutOfBounds();
rewardsBps = newRewardsBps;
bountyBps = newBountyBps;
burnBps = newBurnBps;
emit BurnPhaseSplitSet(newRewardsBps, newBountyBps, newBurnBps);
}
/// @notice Sets the two-way (no-excess) split. Bounds: rewards 5000–8000, bounty 2000–5000,
/// sum exactly 10000. Permanently live — applied on any flush with no burnable
/// excess (the burn slice folds into these two streams).
/// @dev Flushes first so pending fees route at the old configuration.
function setPostBurnSplit(uint16 newRewardsBps, uint16 newBountyBps) external onlyOwner {
flush();
if (
newRewardsBps < POST_BURN_REWARDS_MIN_BPS || newRewardsBps > POST_BURN_REWARDS_MAX_BPS
|| newBountyBps < POST_BURN_BOUNTY_MIN_BPS || newBountyBps > POST_BURN_BOUNTY_MAX_BPS
|| uint256(newRewardsBps) + newBountyBps != BPS
) revert SplitOutOfBounds();
postRewardsBps = newRewardsBps;
postBountyBps = newBountyBps;
emit PostBurnSplitSet(newRewardsBps, newBountyBps);
}
// ───────────────────────────── unused hook callbacks ───────────────────────────────
// No permission flags are set for these; the PoolManager never calls them. They revert
// so a misconfigured external caller fails loudly.
/// @inheritdoc IHooks
function beforeInitialize(address, PoolKey calldata, uint160) external pure returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterInitialize(address, PoolKey calldata, uint160, int24) external pure returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeAddLiquidity(address, PoolKey calldata, IPoolManager.ModifyLiquidityParams calldata, bytes calldata)
external
pure
returns (bytes4)
{
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterAddLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
BalanceDelta,
bytes calldata
) external pure returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeRemoveLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata
) external pure returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterRemoveLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
BalanceDelta,
bytes calldata
) external pure returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) external pure returns (bytes4) {
revert HookNotImplemented();
}
/// @inheritdoc IHooks
function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) external pure returns (bytes4) {
revert HookNotImplemented();
}
}
lib/openzeppelin-contracts/contracts/access/Ownable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (access/Ownable2Step.sol)
pragma solidity ^0.8.20;
import {Ownable} from "./Ownable.sol";
/**
* @dev Contract module which provides access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* This extension of the {Ownable} contract includes a two-step mechanism to transfer
* ownership, where the new owner must call {acceptOwnership} in order to replace the
* old one. This can help prevent common mistakes, such as transfers of ownership to
* incorrect accounts, or to contracts that are unable to interact with the
* permission system.
*
* The initial owner is specified at deployment time in the constructor for `Ownable`. This
* can later be changed with {transferOwnership} and {acceptOwnership}.
*
* This module is used through inheritance. It will make available all functions
* from parent (Ownable).
*/
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
/**
* @dev Returns the address of the pending owner.
*/
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
/**
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
* Can only be called by the current owner.
*
* Setting `newOwner` to the zero address is allowed; this can be used to cancel an initiated ownership transfer.
*/
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
/**
* @dev The new owner accepts the ownership transfer.
*/
function acceptOwnership() public virtual {
address sender = _msgSender();
if (pendingOwner() != sender) {
revert OwnableUnauthorizedAccount(sender);
}
_transferOwnership(sender);
}
}
lib/openzeppelin-contracts/contracts/utils/Context.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
lib/v4-core/src/interfaces/IExtsload.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @notice Interface for functions to access any storage slot in a contract
interface IExtsload {
/// @notice Called by external contracts to access granular pool state
/// @param slot Key of slot to sload
/// @return value The value of the slot as bytes32
function extsload(bytes32 slot) external view returns (bytes32 value);
/// @notice Called by external contracts to access granular pool state
/// @param startSlot Key of slot to start sloading from
/// @param nSlots Number of slots to load into return value
/// @return values List of loaded values.
function extsload(bytes32 startSlot, uint256 nSlots) external view returns (bytes32[] memory values);
/// @notice Called by external contracts to access sparse pool state
/// @param slots List of slots to SLOAD from.
/// @return values List of loaded values.
function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory values);
}
lib/v4-core/src/interfaces/IExttload.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/// @notice Interface for functions to access any transient storage slot in a contract
interface IExttload {
/// @notice Called by external contracts to access transient storage of the contract
/// @param slot Key of slot to tload
/// @return value The value of the slot as bytes32
function exttload(bytes32 slot) external view returns (bytes32 value);
/// @notice Called by external contracts to access sparse transient pool state
/// @param slots List of slots to tload
/// @return values List of loaded values
function exttload(bytes32[] calldata slots) external view returns (bytes32[] memory values);
}
lib/v4-core/src/interfaces/IHooks.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {PoolKey} from "../types/PoolKey.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {IPoolManager} from "./IPoolManager.sol";
import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
/// @notice V4 decides whether to invoke specific hooks by inspecting the least significant bits
/// of the address that the hooks contract is deployed to.
/// For example, a hooks contract deployed to address: 0x0000000000000000000000000000000000002400
/// has the lowest bits '10 0100 0000 0000' which would cause the 'before initialize' and 'after add liquidity' hooks to be used.
/// See the Hooks library for the full spec.
/// @dev Should only be callable by the v4 PoolManager.
interface IHooks {
/// @notice The hook called before the state of a pool is initialized
/// @param sender The initial msg.sender for the initialize call
/// @param key The key for the pool being initialized
/// @param sqrtPriceX96 The sqrt(price) of the pool as a Q64.96
/// @return bytes4 The function selector for the hook
function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96) external returns (bytes4);
/// @notice The hook called after the state of a pool is initialized
/// @param sender The initial msg.sender for the initialize call
/// @param key The key for the pool being initialized
/// @param sqrtPriceX96 The sqrt(price) of the pool as a Q64.96
/// @param tick The current tick after the state of a pool is initialized
/// @return bytes4 The function selector for the hook
function afterInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, int24 tick)
external
returns (bytes4);
/// @notice The hook called before liquidity is added
/// @param sender The initial msg.sender for the add liquidity call
/// @param key The key for the pool
/// @param params The parameters for adding liquidity
/// @param hookData Arbitrary data handed into the PoolManager by the liquidity provider to be passed on to the hook
/// @return bytes4 The function selector for the hook
function beforeAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external returns (bytes4);
/// @notice The hook called after liquidity is added
/// @param sender The initial msg.sender for the add liquidity call
/// @param key The key for the pool
/// @param params The parameters for adding liquidity
/// @param delta The caller's balance delta after adding liquidity; the sum of principal delta, fees accrued, and hook delta
/// @param feesAccrued The fees accrued since the last time fees were collected from this position
/// @param hookData Arbitrary data handed into the PoolManager by the liquidity provider to be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BalanceDelta The hook's delta in token0 and token1. Positive: the hook is owed/took currency, negative: the hook owes/sent currency
function afterAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) external returns (bytes4, BalanceDelta);
/// @notice The hook called before liquidity is removed
/// @param sender The initial msg.sender for the remove liquidity call
/// @param key The key for the pool
/// @param params The parameters for removing liquidity
/// @param hookData Arbitrary data handed into the PoolManager by the liquidity provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function beforeRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external returns (bytes4);
/// @notice The hook called after liquidity is removed
/// @param sender The initial msg.sender for the remove liquidity call
/// @param key The key for the pool
/// @param params The parameters for removing liquidity
/// @param delta The caller's balance delta after removing liquidity; the sum of principal delta, fees accrued, and hook delta
/// @param feesAccrued The fees accrued since the last time fees were collected from this position
/// @param hookData Arbitrary data handed into the PoolManager by the liquidity provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BalanceDelta The hook's delta in token0 and token1. Positive: the hook is owed/took currency, negative: the hook owes/sent currency
function afterRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) external returns (bytes4, BalanceDelta);
/// @notice The hook called before a swap
/// @param sender The initial msg.sender for the swap call
/// @param key The key for the pool
/// @param params The parameters for the swap
/// @param hookData Arbitrary data handed into the PoolManager by the swapper to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BeforeSwapDelta The hook's delta in specified and unspecified currencies. Positive: the hook is owed/took currency, negative: the hook owes/sent currency
/// @return uint24 Optionally override the lp fee, only used if three conditions are met: 1. the Pool has a dynamic fee, 2. the value's 2nd highest bit is set (23rd bit, 0x400000), and 3. the value is less than or equal to the maximum fee (1 million)
function beforeSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external returns (bytes4, BeforeSwapDelta, uint24);
/// @notice The hook called after a swap
/// @param sender The initial msg.sender for the swap call
/// @param key The key for the pool
/// @param params The parameters for the swap
/// @param delta The amount owed to the caller (positive) or owed to the pool (negative)
/// @param hookData Arbitrary data handed into the PoolManager by the swapper to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return int128 The hook's delta in unspecified currency. Positive: the hook is owed/took currency, negative: the hook owes/sent currency
function afterSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external returns (bytes4, int128);
/// @notice The hook called before donate
/// @param sender The initial msg.sender for the donate call
/// @param key The key for the pool
/// @param amount0 The amount of token0 being donated
/// @param amount1 The amount of token1 being donated
/// @param hookData Arbitrary data handed into the PoolManager by the donor to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function beforeDonate(
address sender,
PoolKey calldata key,
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external returns (bytes4);
/// @notice The hook called after donate
/// @param sender The initial msg.sender for the donate call
/// @param key The key for the pool
/// @param amount0 The amount of token0 being donated
/// @param amount1 The amount of token1 being donated
/// @param hookData Arbitrary data handed into the PoolManager by the donor to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function afterDonate(
address sender,
PoolKey calldata key,
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external returns (bytes4);
}
lib/v4-core/src/interfaces/IPoolManager.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Currency} from "../types/Currency.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "./IHooks.sol";
import {IERC6909Claims} from "./external/IERC6909Claims.sol";
import {IProtocolFees} from "./IProtocolFees.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {PoolId} from "../types/PoolId.sol";
import {IExtsload} from "./IExtsload.sol";
import {IExttload} from "./IExttload.sol";
/// @notice Interface for the PoolManager
interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload, IExttload {
/// @notice Thrown when a currency is not netted out after the contract is unlocked
error CurrencyNotSettled();
/// @notice Thrown when trying to interact with a non-initialized pool
error PoolNotInitialized();
/// @notice Thrown when unlock is called, but the contract is already unlocked
error AlreadyUnlocked();
/// @notice Thrown when a function is called that requires the contract to be unlocked, but it is not
error ManagerLocked();
/// @notice Pools are limited to type(int16).max tickSpacing in #initialize, to prevent overflow
error TickSpacingTooLarge(int24 tickSpacing);
/// @notice Pools must have a positive non-zero tickSpacing passed to #initialize
error TickSpacingTooSmall(int24 tickSpacing);
/// @notice PoolKey must have currencies where address(currency0) < address(currency1)
error CurrenciesOutOfOrderOrEqual(address currency0, address currency1);
/// @notice Thrown when a call to updateDynamicLPFee is made by an address that is not the hook,
/// or on a pool that does not have a dynamic swap fee.
error UnauthorizedDynamicLPFeeUpdate();
/// @notice Thrown when trying to swap amount of 0
error SwapAmountCannotBeZero();
///@notice Thrown when native currency is passed to a non native settlement
error NonzeroNativeValue();
/// @notice Thrown when `clear` is called with an amount that is not exactly equal to the open currency delta.
error MustClearExactPositiveDelta();
/// @notice Emitted when a new pool is initialized
/// @param id The abi encoded hash of the pool key struct for the new pool
/// @param currency0 The first currency of the pool by address sort order
/// @param currency1 The second currency of the pool by address sort order
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks
/// @param hooks The hooks contract address for the pool, or address(0) if none
/// @param sqrtPriceX96 The price of the pool on initialization
/// @param tick The initial tick of the pool corresponding to the initialized price
event Initialize(
PoolId indexed id,
Currency indexed currency0,
Currency indexed currency1,
uint24 fee,
int24 tickSpacing,
IHooks hooks,
uint160 sqrtPriceX96,
int24 tick
);
/// @notice Emitted when a liquidity position is modified
/// @param id The abi encoded hash of the pool key struct for the pool that was modified
/// @param sender The address that modified the pool
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param liquidityDelta The amount of liquidity that was added or removed
/// @param salt The extra data to make positions unique
event ModifyLiquidity(
PoolId indexed id, address indexed sender, int24 tickLower, int24 tickUpper, int256 liquidityDelta, bytes32 salt
);
/// @notice Emitted for swaps between currency0 and currency1
/// @param id The abi encoded hash of the pool key struct for the pool that was modified
/// @param sender The address that initiated the swap call, and that received the callback
/// @param amount0 The delta of the currency0 balance of the pool
/// @param amount1 The delta of the currency1 balance of the pool
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of the price of the pool after the swap
/// @param fee The swap fee in hundredths of a bip
event Swap(
PoolId indexed id,
address indexed sender,
int128 amount0,
int128 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick,
uint24 fee
);
/// @notice Emitted for donations
/// @param id The abi encoded hash of the pool key struct for the pool that was donated to
/// @param sender The address that initiated the donate call
/// @param amount0 The amount donated in currency0
/// @param amount1 The amount donated in currency1
event Donate(PoolId indexed id, address indexed sender, uint256 amount0, uint256 amount1);
/// @notice All interactions on the contract that account deltas require unlocking. A caller that calls `unlock` must implement
/// `IUnlockCallback(msg.sender).unlockCallback(data)`, where they interact with the remaining functions on this contract.
/// @dev The only functions callable without an unlocking are `initialize` and `updateDynamicLPFee`
/// @param data Any data to pass to the callback, via `IUnlockCallback(msg.sender).unlockCallback(data)`
/// @return The data returned by the call to `IUnlockCallback(msg.sender).unlockCallback(data)`
function unlock(bytes calldata data) external returns (bytes memory);
/// @notice Initialize the state for a given pool ID
/// @dev A swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee
/// @param key The pool key for the pool to initialize
/// @param sqrtPriceX96 The initial square root price
/// @return tick The initial tick of the pool
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);
struct ModifyLiquidityParams {
// the lower and upper tick of the position
int24 tickLower;
int24 tickUpper;
// how to modify the liquidity
int256 liquidityDelta;
// a value to set if you want unique liquidity positions at the same range
bytes32 salt;
}
/// @notice Modify the liquidity for the given pool
/// @dev Poke by calling with a zero liquidityDelta
/// @param key The pool to modify liquidity in
/// @param params The parameters for modifying the liquidity
/// @param hookData The data to pass through to the add/removeLiquidity hooks
/// @return callerDelta The balance delta of the caller of modifyLiquidity. This is the total of both principal, fee deltas, and hook deltas if applicable
/// @return feesAccrued The balance delta of the fees generated in the liquidity range. Returned for informational purposes
/// @dev Note that feesAccrued can be artificially inflated by a malicious actor and integrators should be careful using the value
/// For pools with a single liquidity position, actors can donate to themselves to inflate feeGrowthGlobal (and consequently feesAccrued)
/// atomically donating and collecting fees in the same unlockCallback may make the inflated value more extreme
function modifyLiquidity(PoolKey memory key, ModifyLiquidityParams memory params, bytes calldata hookData)
external
returns (BalanceDelta callerDelta, BalanceDelta feesAccrued);
struct SwapParams {
/// Whether to swap token0 for token1 or vice versa
bool zeroForOne;
/// The desired input amount if negative (exactIn), or the desired output amount if positive (exactOut)
int256 amountSpecified;
/// The sqrt price at which, if reached, the swap will stop executing
uint160 sqrtPriceLimitX96;
}
/// @notice Swap against the given pool
/// @param key The pool to swap in
/// @param params The parameters for swapping
/// @param hookData The data to pass through to the swap hooks
/// @return swapDelta The balance delta of the address swapping
/// @dev Swapping on low liquidity pools may cause unexpected swap amounts when liquidity available is less than amountSpecified.
/// Additionally note that if interacting with hooks that have the BEFORE_SWAP_RETURNS_DELTA_FLAG or AFTER_SWAP_RETURNS_DELTA_FLAG
/// the hook may alter the swap input/output. Integrators should perform checks on the returned swapDelta.
function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData)
external
returns (BalanceDelta swapDelta);
/// @notice Donate the given currency amounts to the in-range liquidity providers of a pool
/// @dev Calls to donate can be frontrun adding just-in-time liquidity, with the aim of receiving a portion donated funds.
/// Donors should keep this in mind when designing donation mechanisms.
/// @dev This function donates to in-range LPs at slot0.tick. In certain edge-cases of the swap algorithm, the `sqrtPrice` of
/// a pool can be at the lower boundary of tick `n`, but the `slot0.tick` of the pool is already `n - 1`. In this case a call to
/// `donate` would donate to tick `n - 1` (slot0.tick) not tick `n` (getTickAtSqrtPrice(slot0.sqrtPriceX96)).
/// Read the comments in `Pool.swap()` for more information about this.
/// @param key The key of the pool to donate to
/// @param amount0 The amount of currency0 to donate
/// @param amount1 The amount of currency1 to donate
/// @param hookData The data to pass through to the donate hooks
/// @return BalanceDelta The delta of the caller after the donate
function donate(PoolKey memory key, uint256 amount0, uint256 amount1, bytes calldata hookData)
external
returns (BalanceDelta);
/// @notice Writes the current ERC20 balance of the specified currency to transient storage
/// This is used to checkpoint balances for the manager and derive deltas for the caller.
/// @dev This MUST be called before any ERC20 tokens are sent into the contract, but can be skipped
/// for native tokens because the amount to settle is determined by the sent value.
/// However, if an ERC20 token has been synced and not settled, and the caller instead wants to settle
/// native funds, this function can be called with the native currency to then be able to settle the native currency
function sync(Currency currency) external;
/// @notice Called by the user to net out some value owed to the user
/// @dev Will revert if the requested amount is not available, consider using `mint` instead
/// @dev Can also be used as a mechanism for free flash loans
/// @param currency The currency to withdraw from the pool manager
/// @param to The address to withdraw to
/// @param amount The amount of currency to withdraw
function take(Currency currency, address to, uint256 amount) external;
/// @notice Called by the user to pay what is owed
/// @return paid The amount of currency settled
function settle() external payable returns (uint256 paid);
/// @notice Called by the user to pay on behalf of another address
/// @param recipient The address to credit for the payment
/// @return paid The amount of currency settled
function settleFor(address recipient) external payable returns (uint256 paid);
/// @notice WARNING - Any currency that is cleared, will be non-retrievable, and locked in the contract permanently.
/// A call to clear will zero out a positive balance WITHOUT a corresponding transfer.
/// @dev This could be used to clear a balance that is considered dust.
/// Additionally, the amount must be the exact positive balance. This is to enforce that the caller is aware of the amount being cleared.
function clear(Currency currency, uint256 amount) external;
/// @notice Called by the user to move value into ERC6909 balance
/// @param to The address to mint the tokens to
/// @param id The currency address to mint to ERC6909s, as a uint256
/// @param amount The amount of currency to mint
/// @dev The id is converted to a uint160 to correspond to a currency address
/// If the upper 12 bytes are not 0, they will be 0-ed out
function mint(address to, uint256 id, uint256 amount) external;
/// @notice Called by the user to move value from ERC6909 balance
/// @param from The address to burn the tokens from
/// @param id The currency address to burn from ERC6909s, as a uint256
/// @param amount The amount of currency to burn
/// @dev The id is converted to a uint160 to correspond to a currency address
/// If the upper 12 bytes are not 0, they will be 0-ed out
function burn(address from, uint256 id, uint256 amount) external;
/// @notice Updates the pools lp fees for the a pool that has enabled dynamic lp fees.
/// @dev A swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee
/// @param key The key of the pool to update dynamic LP fees for
/// @param newDynamicLPFee The new dynamic pool LP fee
function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external;
}
lib/v4-core/src/interfaces/IProtocolFees.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Currency} from "../types/Currency.sol";
import {PoolId} from "../types/PoolId.sol";
import {PoolKey} from "../types/PoolKey.sol";
/// @notice Interface for all protocol-fee related functions in the pool manager
interface IProtocolFees {
/// @notice Thrown when protocol fee is set too high
error ProtocolFeeTooLarge(uint24 fee);
/// @notice Thrown when collectProtocolFees or setProtocolFee is not called by the controller.
error InvalidCaller();
/// @notice Thrown when collectProtocolFees is attempted on a token that is synced.
error ProtocolFeeCurrencySynced();
/// @notice Emitted when the protocol fee controller address is updated in setProtocolFeeController.
event ProtocolFeeControllerUpdated(address indexed protocolFeeController);
/// @notice Emitted when the protocol fee is updated for a pool.
event ProtocolFeeUpdated(PoolId indexed id, uint24 protocolFee);
/// @notice Given a currency address, returns the protocol fees accrued in that currency
/// @param currency The currency to check
/// @return amount The amount of protocol fees accrued in the currency
function protocolFeesAccrued(Currency currency) external view returns (uint256 amount);
/// @notice Sets the protocol fee for the given pool
/// @param key The key of the pool to set a protocol fee for
/// @param newProtocolFee The fee to set
function setProtocolFee(PoolKey memory key, uint24 newProtocolFee) external;
/// @notice Sets the protocol fee controller
/// @param controller The new protocol fee controller
function setProtocolFeeController(address controller) external;
/// @notice Collects the protocol fees for a given recipient and currency, returning the amount collected
/// @dev This will revert if the contract is unlocked
/// @param recipient The address to receive the protocol fees
/// @param currency The currency to withdraw
/// @param amount The amount of currency to withdraw
/// @return amountCollected The amount of currency successfully withdrawn
function collectProtocolFees(address recipient, Currency currency, uint256 amount)
external
returns (uint256 amountCollected);
/// @notice Returns the current protocol fee controller address
/// @return address The current protocol fee controller address
function protocolFeeController() external view returns (address);
}
lib/v4-core/src/interfaces/external/IERC20Minimal.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title Minimal ERC20 interface for Uniswap
/// @notice Contains a subset of the full ERC20 interface that is used in Uniswap V3
interface IERC20Minimal {
/// @notice Returns an account's balance in the token
/// @param account The account for which to look up the number of tokens it has, i.e. its balance
/// @return The number of tokens held by the account
function balanceOf(address account) external view returns (uint256);
/// @notice Transfers the amount of token from the `msg.sender` to the recipient
/// @param recipient The account that will receive the amount transferred
/// @param amount The number of tokens to send from the sender to the recipient
/// @return Returns true for a successful transfer, false for an unsuccessful transfer
function transfer(address recipient, uint256 amount) external returns (bool);
/// @notice Returns the current allowance given to a spender by an owner
/// @param owner The account of the token owner
/// @param spender The account of the token spender
/// @return The current allowance granted by `owner` to `spender`
function allowance(address owner, address spender) external view returns (uint256);
/// @notice Sets the allowance of a spender from the `msg.sender` to the value `amount`
/// @param spender The account which will be allowed to spend a given amount of the owners tokens
/// @param amount The amount of tokens allowed to be used by `spender`
/// @return Returns true for a successful approval, false for unsuccessful
function approve(address spender, uint256 amount) external returns (bool);
/// @notice Transfers `amount` tokens from `sender` to `recipient` up to the allowance given to the `msg.sender`
/// @param sender The account from which the transfer will be initiated
/// @param recipient The recipient of the transfer
/// @param amount The amount of the transfer
/// @return Returns true for a successful transfer, false for unsuccessful
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/// @notice Event emitted when tokens are transferred from one address to another, either via `#transfer` or `#transferFrom`.
/// @param from The account from which the tokens were sent, i.e. the balance decreased
/// @param to The account to which the tokens were sent, i.e. the balance increased
/// @param value The amount of tokens that were transferred
event Transfer(address indexed from, address indexed to, uint256 value);
/// @notice Event emitted when the approval amount for the spender of a given owner's tokens changes.
/// @param owner The account that approved spending of its tokens
/// @param spender The account for which the spending allowance was modified
/// @param value The new allowance from the owner to the spender
event Approval(address indexed owner, address indexed spender, uint256 value);
}
lib/v4-core/src/interfaces/external/IERC6909Claims.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @notice Interface for claims over a contract balance, wrapped as a ERC6909
interface IERC6909Claims {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event OperatorSet(address indexed owner, address indexed operator, bool approved);
event Approval(address indexed owner, address indexed spender, uint256 indexed id, uint256 amount);
event Transfer(address caller, address indexed from, address indexed to, uint256 indexed id, uint256 amount);
/*//////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Owner balance of an id.
/// @param owner The address of the owner.
/// @param id The id of the token.
/// @return amount The balance of the token.
function balanceOf(address owner, uint256 id) external view returns (uint256 amount);
/// @notice Spender allowance of an id.
/// @param owner The address of the owner.
/// @param spender The address of the spender.
/// @param id The id of the token.
/// @return amount The allowance of the token.
function allowance(address owner, address spender, uint256 id) external view returns (uint256 amount);
/// @notice Checks if a spender is approved by an owner as an operator
/// @param owner The address of the owner.
/// @param spender The address of the spender.
/// @return approved The approval status.
function isOperator(address owner, address spender) external view returns (bool approved);
/// @notice Transfers an amount of an id from the caller to a receiver.
/// @param receiver The address of the receiver.
/// @param id The id of the token.
/// @param amount The amount of the token.
/// @return bool True, always, unless the function reverts
function transfer(address receiver, uint256 id, uint256 amount) external returns (bool);
/// @notice Transfers an amount of an id from a sender to a receiver.
/// @param sender The address of the sender.
/// @param receiver The address of the receiver.
/// @param id The id of the token.
/// @param amount The amount of the token.
/// @return bool True, always, unless the function reverts
function transferFrom(address sender, address receiver, uint256 id, uint256 amount) external returns (bool);
/// @notice Approves an amount of an id to a spender.
/// @param spender The address of the spender.
/// @param id The id of the token.
/// @param amount The amount of the token.
/// @return bool True, always
function approve(address spender, uint256 id, uint256 amount) external returns (bool);
/// @notice Sets or removes an operator for the caller.
/// @param operator The address of the operator.
/// @param approved The approval status.
/// @return bool True, always
function setOperator(address operator, bool approved) external returns (bool);
}
lib/v4-core/src/libraries/CustomRevert.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title Library for reverting with custom errors efficiently
/// @notice Contains functions for reverting with custom errors with different argument types efficiently
/// @dev To use this library, declare `using CustomRevert for bytes4;` and replace `revert CustomError()` with
/// `CustomError.selector.revertWith()`
/// @dev The functions may tamper with the free memory pointer but it is fine since the call context is exited immediately
library CustomRevert {
/// @dev ERC-7751 error for wrapping bubbled up reverts
error WrappedError(address target, bytes4 selector, bytes reason, bytes details);
/// @dev Reverts with the selector of a custom error in the scratch space
function revertWith(bytes4 selector) internal pure {
assembly ("memory-safe") {
mstore(0, selector)
revert(0, 0x04)
}
}
/// @dev Reverts with a custom error with an address argument in the scratch space
function revertWith(bytes4 selector, address addr) internal pure {
assembly ("memory-safe") {
mstore(0, selector)
mstore(0x04, and(addr, 0xffffffffffffffffffffffffffffffffffffffff))
revert(0, 0x24)
}
}
/// @dev Reverts with a custom error with an int24 argument in the scratch space
function revertWith(bytes4 selector, int24 value) internal pure {
assembly ("memory-safe") {
mstore(0, selector)
mstore(0x04, signextend(2, value))
revert(0, 0x24)
}
}
/// @dev Reverts with a custom error with a uint160 argument in the scratch space
function revertWith(bytes4 selector, uint160 value) internal pure {
assembly ("memory-safe") {
mstore(0, selector)
mstore(0x04, and(value, 0xffffffffffffffffffffffffffffffffffffffff))
revert(0, 0x24)
}
}
/// @dev Reverts with a custom error with two int24 arguments
function revertWith(bytes4 selector, int24 value1, int24 value2) internal pure {
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(fmp, selector)
mstore(add(fmp, 0x04), signextend(2, value1))
mstore(add(fmp, 0x24), signextend(2, value2))
revert(fmp, 0x44)
}
}
/// @dev Reverts with a custom error with two uint160 arguments
function revertWith(bytes4 selector, uint160 value1, uint160 value2) internal pure {
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(fmp, selector)
mstore(add(fmp, 0x04), and(value1, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(fmp, 0x24), and(value2, 0xffffffffffffffffffffffffffffffffffffffff))
revert(fmp, 0x44)
}
}
/// @dev Reverts with a custom error with two address arguments
function revertWith(bytes4 selector, address value1, address value2) internal pure {
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(fmp, selector)
mstore(add(fmp, 0x04), and(value1, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(fmp, 0x24), and(value2, 0xffffffffffffffffffffffffffffffffffffffff))
revert(fmp, 0x44)
}
}
/// @notice bubble up the revert message returned by a call and revert with a wrapped ERC-7751 error
/// @dev this method can be vulnerable to revert data bombs
function bubbleUpAndRevertWith(
address revertingContract,
bytes4 revertingFunctionSelector,
bytes4 additionalContext
) internal pure {
bytes4 wrappedErrorSelector = WrappedError.selector;
assembly ("memory-safe") {
// Ensure the size of the revert data is a multiple of 32 bytes
let encodedDataSize := mul(div(add(returndatasize(), 31), 32), 32)
let fmp := mload(0x40)
// Encode wrapped error selector, address, function selector, offset, additional context, size, revert reason
mstore(fmp, wrappedErrorSelector)
mstore(add(fmp, 0x04), and(revertingContract, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(
add(fmp, 0x24),
and(revertingFunctionSelector, 0xffffffff00000000000000000000000000000000000000000000000000000000)
)
// offset revert reason
mstore(add(fmp, 0x44), 0x80)
// offset additional context
mstore(add(fmp, 0x64), add(0xa0, encodedDataSize))
// size revert reason
mstore(add(fmp, 0x84), returndatasize())
// revert reason
returndatacopy(add(fmp, 0xa4), 0, returndatasize())
// size additional context
mstore(add(fmp, add(0xa4, encodedDataSize)), 0x04)
// additional context
mstore(
add(fmp, add(0xc4, encodedDataSize)),
and(additionalContext, 0xffffffff00000000000000000000000000000000000000000000000000000000)
)
revert(fmp, add(0xe4, encodedDataSize))
}
}
}
lib/v4-core/src/libraries/Hooks.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {SafeCast} from "./SafeCast.sol";
import {LPFeeLibrary} from "./LPFeeLibrary.sol";
import {BalanceDelta, toBalanceDelta, BalanceDeltaLibrary} from "../types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "../types/BeforeSwapDelta.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {ParseBytes} from "./ParseBytes.sol";
import {CustomRevert} from "./CustomRevert.sol";
/// @notice V4 decides whether to invoke specific hooks by inspecting the least significant bits
/// of the address that the hooks contract is deployed to.
/// For example, a hooks contract deployed to address: 0x0000000000000000000000000000000000002400
/// has the lowest bits '10 0100 0000 0000' which would cause the 'before initialize' and 'after add liquidity' hooks to be used.
library Hooks {
using LPFeeLibrary for uint24;
using Hooks for IHooks;
using SafeCast for int256;
using BeforeSwapDeltaLibrary for BeforeSwapDelta;
using ParseBytes for bytes;
using CustomRevert for bytes4;
uint160 internal constant ALL_HOOK_MASK = uint160((1 << 14) - 1);
uint160 internal constant BEFORE_INITIALIZE_FLAG = 1 << 13;
uint160 internal constant AFTER_INITIALIZE_FLAG = 1 << 12;
uint160 internal constant BEFORE_ADD_LIQUIDITY_FLAG = 1 << 11;
uint160 internal constant AFTER_ADD_LIQUIDITY_FLAG = 1 << 10;
uint160 internal constant BEFORE_REMOVE_LIQUIDITY_FLAG = 1 << 9;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_FLAG = 1 << 8;
uint160 internal constant BEFORE_SWAP_FLAG = 1 << 7;
uint160 internal constant AFTER_SWAP_FLAG = 1 << 6;
uint160 internal constant BEFORE_DONATE_FLAG = 1 << 5;
uint160 internal constant AFTER_DONATE_FLAG = 1 << 4;
uint160 internal constant BEFORE_SWAP_RETURNS_DELTA_FLAG = 1 << 3;
uint160 internal constant AFTER_SWAP_RETURNS_DELTA_FLAG = 1 << 2;
uint160 internal constant AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 1;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 0;
struct Permissions {
bool beforeInitialize;
bool afterInitialize;
bool beforeAddLiquidity;
bool afterAddLiquidity;
bool beforeRemoveLiquidity;
bool afterRemoveLiquidity;
bool beforeSwap;
bool afterSwap;
bool beforeDonate;
bool afterDonate;
bool beforeSwapReturnDelta;
bool afterSwapReturnDelta;
bool afterAddLiquidityReturnDelta;
bool afterRemoveLiquidityReturnDelta;
}
/// @notice Thrown if the address will not lead to the specified hook calls being called
/// @param hooks The address of the hooks contract
error HookAddressNotValid(address hooks);
/// @notice Hook did not return its selector
error InvalidHookResponse();
/// @notice Additional context for ERC-7751 wrapped error when a hook call fails
error HookCallFailed();
/// @notice The hook's delta changed the swap from exactIn to exactOut or vice versa
error HookDeltaExceedsSwapAmount();
/// @notice Utility function intended to be used in hook constructors to ensure
/// the deployed hooks address causes the intended hooks to be called
/// @param permissions The hooks that are intended to be called
/// @dev permissions param is memory as the function will be called from constructors
function validateHookPermissions(IHooks self, Permissions memory permissions) internal pure {
if (
permissions.beforeInitialize != self.hasPermission(BEFORE_INITIALIZE_FLAG)
|| permissions.afterInitialize != self.hasPermission(AFTER_INITIALIZE_FLAG)
|| permissions.beforeAddLiquidity != self.hasPermission(BEFORE_ADD_LIQUIDITY_FLAG)
|| permissions.afterAddLiquidity != self.hasPermission(AFTER_ADD_LIQUIDITY_FLAG)
|| permissions.beforeRemoveLiquidity != self.hasPermission(BEFORE_REMOVE_LIQUIDITY_FLAG)
|| permissions.afterRemoveLiquidity != self.hasPermission(AFTER_REMOVE_LIQUIDITY_FLAG)
|| permissions.beforeSwap != self.hasPermission(BEFORE_SWAP_FLAG)
|| permissions.afterSwap != self.hasPermission(AFTER_SWAP_FLAG)
|| permissions.beforeDonate != self.hasPermission(BEFORE_DONATE_FLAG)
|| permissions.afterDonate != self.hasPermission(AFTER_DONATE_FLAG)
|| permissions.beforeSwapReturnDelta != self.hasPermission(BEFORE_SWAP_RETURNS_DELTA_FLAG)
|| permissions.afterSwapReturnDelta != self.hasPermission(AFTER_SWAP_RETURNS_DELTA_FLAG)
|| permissions.afterAddLiquidityReturnDelta != self.hasPermission(AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG)
|| permissions.afterRemoveLiquidityReturnDelta
!= self.hasPermission(AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG)
) {
HookAddressNotValid.selector.revertWith(address(self));
}
}
/// @notice Ensures that the hook address includes at least one hook flag or dynamic fees, or is the 0 address
/// @param self The hook to verify
/// @param fee The fee of the pool the hook is used with
/// @return bool True if the hook address is valid
function isValidHookAddress(IHooks self, uint24 fee) internal pure returns (bool) {
// The hook can only have a flag to return a hook delta on an action if it also has the corresponding action flag
if (!self.hasPermission(BEFORE_SWAP_FLAG) && self.hasPermission(BEFORE_SWAP_RETURNS_DELTA_FLAG)) return false;
if (!self.hasPermission(AFTER_SWAP_FLAG) && self.hasPermission(AFTER_SWAP_RETURNS_DELTA_FLAG)) return false;
if (!self.hasPermission(AFTER_ADD_LIQUIDITY_FLAG) && self.hasPermission(AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG))
{
return false;
}
if (
!self.hasPermission(AFTER_REMOVE_LIQUIDITY_FLAG)
&& self.hasPermission(AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG)
) return false;
// If there is no hook contract set, then fee cannot be dynamic
// If a hook contract is set, it must have at least 1 flag set, or have a dynamic fee
return address(self) == address(0)
? !fee.isDynamicFee()
: (uint160(address(self)) & ALL_HOOK_MASK > 0 || fee.isDynamicFee());
}
/// @notice performs a hook call using the given calldata on the given hook that doesn't return a delta
/// @return result The complete data returned by the hook
function callHook(IHooks self, bytes memory data) internal returns (bytes memory result) {
bool success;
assembly ("memory-safe") {
success := call(gas(), self, 0, add(data, 0x20), mload(data), 0, 0)
}
// Revert with FailedHookCall, containing any error message to bubble up
if (!success) CustomRevert.bubbleUpAndRevertWith(address(self), bytes4(data), HookCallFailed.selector);
// The call was successful, fetch the returned data
assembly ("memory-safe") {
// allocate result byte array from the free memory pointer
result := mload(0x40)
// store new free memory pointer at the end of the array padded to 32 bytes
mstore(0x40, add(result, and(add(returndatasize(), 0x3f), not(0x1f))))
// store length in memory
mstore(result, returndatasize())
// copy return data to result
returndatacopy(add(result, 0x20), 0, returndatasize())
}
// Length must be at least 32 to contain the selector. Check expected selector and returned selector match.
if (result.length < 32 || result.parseSelector() != data.parseSelector()) {
InvalidHookResponse.selector.revertWith();
}
}
/// @notice performs a hook call using the given calldata on the given hook
/// @return int256 The delta returned by the hook
function callHookWithReturnDelta(IHooks self, bytes memory data, bool parseReturn) internal returns (int256) {
bytes memory result = callHook(self, data);
// If this hook wasn't meant to return something, default to 0 delta
if (!parseReturn) return 0;
// A length of 64 bytes is required to return a bytes4, and a 32 byte delta
if (result.length != 64) InvalidHookResponse.selector.revertWith();
return result.parseReturnDelta();
}
/// @notice modifier to prevent calling a hook if they initiated the action
modifier noSelfCall(IHooks self) {
if (msg.sender != address(self)) {
_;
}
}
/// @notice calls beforeInitialize hook if permissioned and validates return value
function beforeInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96) internal noSelfCall(self) {
if (self.hasPermission(BEFORE_INITIALIZE_FLAG)) {
self.callHook(abi.encodeCall(IHooks.beforeInitialize, (msg.sender, key, sqrtPriceX96)));
}
}
/// @notice calls afterInitialize hook if permissioned and validates return value
function afterInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96, int24 tick)
internal
noSelfCall(self)
{
if (self.hasPermission(AFTER_INITIALIZE_FLAG)) {
self.callHook(abi.encodeCall(IHooks.afterInitialize, (msg.sender, key, sqrtPriceX96, tick)));
}
}
/// @notice calls beforeModifyLiquidity hook if permissioned and validates return value
function beforeModifyLiquidity(
IHooks self,
PoolKey memory key,
IPoolManager.ModifyLiquidityParams memory params,
bytes calldata hookData
) internal noSelfCall(self) {
if (params.liquidityDelta > 0 && self.hasPermission(BEFORE_ADD_LIQUIDITY_FLAG)) {
self.callHook(abi.encodeCall(IHooks.beforeAddLiquidity, (msg.sender, key, params, hookData)));
} else if (params.liquidityDelta <= 0 && self.hasPermission(BEFORE_REMOVE_LIQUIDITY_FLAG)) {
self.callHook(abi.encodeCall(IHooks.beforeRemoveLiquidity, (msg.sender, key, params, hookData)));
}
}
/// @notice calls afterModifyLiquidity hook if permissioned and validates return value
function afterModifyLiquidity(
IHooks self,
PoolKey memory key,
IPoolManager.ModifyLiquidityParams memory params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) internal returns (BalanceDelta callerDelta, BalanceDelta hookDelta) {
if (msg.sender == address(self)) return (delta, BalanceDeltaLibrary.ZERO_DELTA);
callerDelta = delta;
if (params.liquidityDelta > 0) {
if (self.hasPermission(AFTER_ADD_LIQUIDITY_FLAG)) {
hookDelta = BalanceDelta.wrap(
self.callHookWithReturnDelta(
abi.encodeCall(
IHooks.afterAddLiquidity, (msg.sender, key, params, delta, feesAccrued, hookData)
),
self.hasPermission(AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG)
)
);
callerDelta = callerDelta - hookDelta;
}
} else {
if (self.hasPermission(AFTER_REMOVE_LIQUIDITY_FLAG)) {
hookDelta = BalanceDelta.wrap(
self.callHookWithReturnDelta(
abi.encodeCall(
IHooks.afterRemoveLiquidity, (msg.sender, key, params, delta, feesAccrued, hookData)
),
self.hasPermission(AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG)
)
);
callerDelta = callerDelta - hookDelta;
}
}
}
/// @notice calls beforeSwap hook if permissioned and validates return value
function beforeSwap(IHooks self, PoolKey memory key, IPoolManager.SwapParams memory params, bytes calldata hookData)
internal
returns (int256 amountToSwap, BeforeSwapDelta hookReturn, uint24 lpFeeOverride)
{
amountToSwap = params.amountSpecified;
if (msg.sender == address(self)) return (amountToSwap, BeforeSwapDeltaLibrary.ZERO_DELTA, lpFeeOverride);
if (self.hasPermission(BEFORE_SWAP_FLAG)) {
bytes memory result = callHook(self, abi.encodeCall(IHooks.beforeSwap, (msg.sender, key, params, hookData)));
// A length of 96 bytes is required to return a bytes4, a 32 byte delta, and an LP fee
if (result.length != 96) InvalidHookResponse.selector.revertWith();
// dynamic fee pools that want to override the cache fee, return a valid fee with the override flag. If override flag
// is set but an invalid fee is returned, the transaction will revert. Otherwise the current LP fee will be used
if (key.fee.isDynamicFee()) lpFeeOverride = result.parseFee();
// skip this logic for the case where the hook return is 0
if (self.hasPermission(BEFORE_SWAP_RETURNS_DELTA_FLAG)) {
hookReturn = BeforeSwapDelta.wrap(result.parseReturnDelta());
// any return in unspecified is passed to the afterSwap hook for handling
int128 hookDeltaSpecified = hookReturn.getSpecifiedDelta();
// Update the swap amount according to the hook's return, and check that the swap type doesn't change (exact input/output)
if (hookDeltaSpecified != 0) {
bool exactInput = amountToSwap < 0;
amountToSwap += hookDeltaSpecified;
if (exactInput ? amountToSwap > 0 : amountToSwap < 0) {
HookDeltaExceedsSwapAmount.selector.revertWith();
}
}
}
}
}
/// @notice calls afterSwap hook if permissioned and validates return value
function afterSwap(
IHooks self,
PoolKey memory key,
IPoolManager.SwapParams memory params,
BalanceDelta swapDelta,
bytes calldata hookData,
BeforeSwapDelta beforeSwapHookReturn
) internal returns (BalanceDelta, BalanceDelta) {
if (msg.sender == address(self)) return (swapDelta, BalanceDeltaLibrary.ZERO_DELTA);
int128 hookDeltaSpecified = beforeSwapHookReturn.getSpecifiedDelta();
int128 hookDeltaUnspecified = beforeSwapHookReturn.getUnspecifiedDelta();
if (self.hasPermission(AFTER_SWAP_FLAG)) {
hookDeltaUnspecified += self.callHookWithReturnDelta(
abi.encodeCall(IHooks.afterSwap, (msg.sender, key, params, swapDelta, hookData)),
self.hasPermission(AFTER_SWAP_RETURNS_DELTA_FLAG)
).toInt128();
}
BalanceDelta hookDelta;
if (hookDeltaUnspecified != 0 || hookDeltaSpecified != 0) {
hookDelta = (params.amountSpecified < 0 == params.zeroForOne)
? toBalanceDelta(hookDeltaSpecified, hookDeltaUnspecified)
: toBalanceDelta(hookDeltaUnspecified, hookDeltaSpecified);
// the caller has to pay for (or receive) the hook's delta
swapDelta = swapDelta - hookDelta;
}
return (swapDelta, hookDelta);
}
/// @notice calls beforeDonate hook if permissioned and validates return value
function beforeDonate(IHooks self, PoolKey memory key, uint256 amount0, uint256 amount1, bytes calldata hookData)
internal
noSelfCall(self)
{
if (self.hasPermission(BEFORE_DONATE_FLAG)) {
self.callHook(abi.encodeCall(IHooks.beforeDonate, (msg.sender, key, amount0, amount1, hookData)));
}
}
/// @notice calls afterDonate hook if permissioned and validates return value
function afterDonate(IHooks self, PoolKey memory key, uint256 amount0, uint256 amount1, bytes calldata hookData)
internal
noSelfCall(self)
{
if (self.hasPermission(AFTER_DONATE_FLAG)) {
self.callHook(abi.encodeCall(IHooks.afterDonate, (msg.sender, key, amount0, amount1, hookData)));
}
}
function hasPermission(IHooks self, uint160 flag) internal pure returns (bool) {
return uint160(address(self)) & flag != 0;
}
}
lib/v4-core/src/libraries/LPFeeLibrary.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {CustomRevert} from "./CustomRevert.sol";
/// @notice Library of helper functions for a pools LP fee
library LPFeeLibrary {
using LPFeeLibrary for uint24;
using CustomRevert for bytes4;
/// @notice Thrown when the static or dynamic fee on a pool exceeds 100%.
error LPFeeTooLarge(uint24 fee);
/// @notice An lp fee of exactly 0b1000000... signals a dynamic fee pool. This isn't a valid static fee as it is > MAX_LP_FEE
uint24 public constant DYNAMIC_FEE_FLAG = 0x800000;
/// @notice the second bit of the fee returned by beforeSwap is used to signal if the stored LP fee should be overridden in this swap
// only dynamic-fee pools can return a fee via the beforeSwap hook
uint24 public constant OVERRIDE_FEE_FLAG = 0x400000;
/// @notice mask to remove the override fee flag from a fee returned by the beforeSwaphook
uint24 public constant REMOVE_OVERRIDE_MASK = 0xBFFFFF;
/// @notice the lp fee is represented in hundredths of a bip, so the max is 100%
uint24 public constant MAX_LP_FEE = 1000000;
/// @notice returns true if a pool's LP fee signals that the pool has a dynamic fee
/// @param self The fee to check
/// @return bool True of the fee is dynamic
function isDynamicFee(uint24 self) internal pure returns (bool) {
return self == DYNAMIC_FEE_FLAG;
}
/// @notice returns true if an LP fee is valid, aka not above the maximum permitted fee
/// @param self The fee to check
/// @return bool True of the fee is valid
function isValid(uint24 self) internal pure returns (bool) {
return self <= MAX_LP_FEE;
}
/// @notice validates whether an LP fee is larger than the maximum, and reverts if invalid
/// @param self The fee to validate
function validate(uint24 self) internal pure {
if (!self.isValid()) LPFeeTooLarge.selector.revertWith(self);
}
/// @notice gets and validates the initial LP fee for a pool. Dynamic fee pools have an initial fee of 0.
/// @dev if a dynamic fee pool wants a non-0 initial fee, it should call `updateDynamicLPFee` in the afterInitialize hook
/// @param self The fee to get the initial LP from
/// @return initialFee 0 if the fee is dynamic, otherwise the fee (if valid)
function getInitialLPFee(uint24 self) internal pure returns (uint24) {
// the initial fee for a dynamic fee pool is 0
if (self.isDynamicFee()) return 0;
self.validate();
return self;
}
/// @notice returns true if the fee has the override flag set (2nd highest bit of the uint24)
/// @param self The fee to check
/// @return bool True of the fee has the override flag set
function isOverride(uint24 self) internal pure returns (bool) {
return self & OVERRIDE_FEE_FLAG != 0;
}
/// @notice returns a fee with the override flag removed
/// @param self The fee to remove the override flag from
/// @return fee The fee without the override flag set
function removeOverrideFlag(uint24 self) internal pure returns (uint24) {
return self & REMOVE_OVERRIDE_MASK;
}
/// @notice Removes the override flag and validates the fee (reverts if the fee is too large)
/// @param self The fee to remove the override flag from, and then validate
/// @return fee The fee without the override flag set (if valid)
function removeOverrideFlagAndValidate(uint24 self) internal pure returns (uint24 fee) {
fee = self.removeOverrideFlag();
fee.validate();
}
}
lib/v4-core/src/libraries/ParseBytes.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @notice Parses bytes returned from hooks and the byte selector used to check return selectors from hooks.
/// @dev parseSelector also is used to parse the expected selector
/// For parsing hook returns, note that all hooks return either bytes4 or (bytes4, 32-byte-delta) or (bytes4, 32-byte-delta, uint24).
library ParseBytes {
function parseSelector(bytes memory result) internal pure returns (bytes4 selector) {
// equivalent: (selector,) = abi.decode(result, (bytes4, int256));
assembly ("memory-safe") {
selector := mload(add(result, 0x20))
}
}
function parseFee(bytes memory result) internal pure returns (uint24 lpFee) {
// equivalent: (,, lpFee) = abi.decode(result, (bytes4, int256, uint24));
assembly ("memory-safe") {
lpFee := mload(add(result, 0x60))
}
}
function parseReturnDelta(bytes memory result) internal pure returns (int256 hookReturn) {
// equivalent: (, hookReturnDelta) = abi.decode(result, (bytes4, int256));
assembly ("memory-safe") {
hookReturn := mload(add(result, 0x40))
}
}
}
lib/v4-core/src/libraries/SafeCast.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {CustomRevert} from "./CustomRevert.sol";
/// @title Safe casting methods
/// @notice Contains methods for safely casting between types
library SafeCast {
using CustomRevert for bytes4;
error SafeCastOverflow();
/// @notice Cast a uint256 to a uint160, revert on overflow
/// @param x The uint256 to be downcasted
/// @return y The downcasted integer, now type uint160
function toUint160(uint256 x) internal pure returns (uint160 y) {
y = uint160(x);
if (y != x) SafeCastOverflow.selector.revertWith();
}
/// @notice Cast a uint256 to a uint128, revert on overflow
/// @param x The uint256 to be downcasted
/// @return y The downcasted integer, now type uint128
function toUint128(uint256 x) internal pure returns (uint128 y) {
y = uint128(x);
if (x != y) SafeCastOverflow.selector.revertWith();
}
/// @notice Cast a int128 to a uint128, revert on overflow or underflow
/// @param x The int128 to be casted
/// @return y The casted integer, now type uint128
function toUint128(int128 x) internal pure returns (uint128 y) {
if (x < 0) SafeCastOverflow.selector.revertWith();
y = uint128(x);
}
/// @notice Cast a int256 to a int128, revert on overflow or underflow
/// @param x The int256 to be downcasted
/// @return y The downcasted integer, now type int128
function toInt128(int256 x) internal pure returns (int128 y) {
y = int128(x);
if (y != x) SafeCastOverflow.selector.revertWith();
}
/// @notice Cast a uint256 to a int256, revert on overflow
/// @param x The uint256 to be casted
/// @return y The casted integer, now type int256
function toInt256(uint256 x) internal pure returns (int256 y) {
y = int256(x);
if (y < 0) SafeCastOverflow.selector.revertWith();
}
/// @notice Cast a uint256 to a int128, revert on overflow
/// @param x The uint256 to be downcasted
/// @return The downcasted integer, now type int128
function toInt128(uint256 x) internal pure returns (int128) {
if (x >= 1 << 127) SafeCastOverflow.selector.revertWith();
return int128(int256(x));
}
}
lib/v4-core/src/types/BalanceDelta.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {SafeCast} from "../libraries/SafeCast.sol";
/// @dev Two `int128` values packed into a single `int256` where the upper 128 bits represent the amount0
/// and the lower 128 bits represent the amount1.
type BalanceDelta is int256;
using {add as +, sub as -, eq as ==, neq as !=} for BalanceDelta global;
using BalanceDeltaLibrary for BalanceDelta global;
using SafeCast for int256;
function toBalanceDelta(int128 _amount0, int128 _amount1) pure returns (BalanceDelta balanceDelta) {
assembly ("memory-safe") {
balanceDelta := or(shl(128, _amount0), and(sub(shl(128, 1), 1), _amount1))
}
}
function add(BalanceDelta a, BalanceDelta b) pure returns (BalanceDelta) {
int256 res0;
int256 res1;
assembly ("memory-safe") {
let a0 := sar(128, a)
let a1 := signextend(15, a)
let b0 := sar(128, b)
let b1 := signextend(15, b)
res0 := add(a0, b0)
res1 := add(a1, b1)
}
return toBalanceDelta(res0.toInt128(), res1.toInt128());
}
function sub(BalanceDelta a, BalanceDelta b) pure returns (BalanceDelta) {
int256 res0;
int256 res1;
assembly ("memory-safe") {
let a0 := sar(128, a)
let a1 := signextend(15, a)
let b0 := sar(128, b)
let b1 := signextend(15, b)
res0 := sub(a0, b0)
res1 := sub(a1, b1)
}
return toBalanceDelta(res0.toInt128(), res1.toInt128());
}
function eq(BalanceDelta a, BalanceDelta b) pure returns (bool) {
return BalanceDelta.unwrap(a) == BalanceDelta.unwrap(b);
}
function neq(BalanceDelta a, BalanceDelta b) pure returns (bool) {
return BalanceDelta.unwrap(a) != BalanceDelta.unwrap(b);
}
/// @notice Library for getting the amount0 and amount1 deltas from the BalanceDelta type
library BalanceDeltaLibrary {
/// @notice A BalanceDelta of 0
BalanceDelta public constant ZERO_DELTA = BalanceDelta.wrap(0);
function amount0(BalanceDelta balanceDelta) internal pure returns (int128 _amount0) {
assembly ("memory-safe") {
_amount0 := sar(128, balanceDelta)
}
}
function amount1(BalanceDelta balanceDelta) internal pure returns (int128 _amount1) {
assembly ("memory-safe") {
_amount1 := signextend(15, balanceDelta)
}
}
}
lib/v4-core/src/types/BeforeSwapDelta.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Return type of the beforeSwap hook.
// Upper 128 bits is the delta in specified tokens. Lower 128 bits is delta in unspecified tokens (to match the afterSwap hook)
type BeforeSwapDelta is int256;
// Creates a BeforeSwapDelta from specified and unspecified
function toBeforeSwapDelta(int128 deltaSpecified, int128 deltaUnspecified)
pure
returns (BeforeSwapDelta beforeSwapDelta)
{
assembly ("memory-safe") {
beforeSwapDelta := or(shl(128, deltaSpecified), and(sub(shl(128, 1), 1), deltaUnspecified))
}
}
/// @notice Library for getting the specified and unspecified deltas from the BeforeSwapDelta type
library BeforeSwapDeltaLibrary {
/// @notice A BeforeSwapDelta of 0
BeforeSwapDelta public constant ZERO_DELTA = BeforeSwapDelta.wrap(0);
/// extracts int128 from the upper 128 bits of the BeforeSwapDelta
/// returned by beforeSwap
function getSpecifiedDelta(BeforeSwapDelta delta) internal pure returns (int128 deltaSpecified) {
assembly ("memory-safe") {
deltaSpecified := sar(128, delta)
}
}
/// extracts int128 from the lower 128 bits of the BeforeSwapDelta
/// returned by beforeSwap and afterSwap
function getUnspecifiedDelta(BeforeSwapDelta delta) internal pure returns (int128 deltaUnspecified) {
assembly ("memory-safe") {
deltaUnspecified := signextend(15, delta)
}
}
}
lib/v4-core/src/types/Currency.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IERC20Minimal} from "../interfaces/external/IERC20Minimal.sol";
import {CustomRevert} from "../libraries/CustomRevert.sol";
type Currency is address;
using {greaterThan as >, lessThan as <, greaterThanOrEqualTo as >=, equals as ==} for Currency global;
using CurrencyLibrary for Currency global;
function equals(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) == Currency.unwrap(other);
}
function greaterThan(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) > Currency.unwrap(other);
}
function lessThan(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) < Currency.unwrap(other);
}
function greaterThanOrEqualTo(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) >= Currency.unwrap(other);
}
/// @title CurrencyLibrary
/// @dev This library allows for transferring and holding native tokens and ERC20 tokens
library CurrencyLibrary {
/// @notice Additional context for ERC-7751 wrapped error when a native transfer fails
error NativeTransferFailed();
/// @notice Additional context for ERC-7751 wrapped error when an ERC20 transfer fails
error ERC20TransferFailed();
/// @notice A constant to represent the native currency
Currency public constant ADDRESS_ZERO = Currency.wrap(address(0));
function transfer(Currency currency, address to, uint256 amount) internal {
// altered from https://github.com/transmissions11/solmate/blob/44a9963d4c78111f77caa0e65d677b8b46d6f2e6/src/utils/SafeTransferLib.sol
// modified custom error selectors
bool success;
if (currency.isAddressZero()) {
assembly ("memory-safe") {
// Transfer the ETH and revert if it fails.
success := call(gas(), to, amount, 0, 0, 0, 0)
}
// revert with NativeTransferFailed, containing the bubbled up error as an argument
if (!success) {
CustomRevert.bubbleUpAndRevertWith(to, bytes4(0), NativeTransferFailed.selector);
}
} else {
assembly ("memory-safe") {
// Get a pointer to some free memory.
let fmp := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(fmp, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
mstore(add(fmp, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to" argument.
mstore(add(fmp, 36), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.
success :=
and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
// We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
// Counterintuitively, this call must be positioned second to the or() call in the
// surrounding and() call or else returndatasize() will be zero during the computation.
call(gas(), currency, 0, fmp, 68, 0, 32)
)
// Now clean the memory we used
mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `to` were stored here
mstore(add(fmp, 0x20), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
mstore(add(fmp, 0x40), 0) // 4 bytes of `amount` were stored here
}
// revert with ERC20TransferFailed, containing the bubbled up error as an argument
if (!success) {
CustomRevert.bubbleUpAndRevertWith(
Currency.unwrap(currency), IERC20Minimal.transfer.selector, ERC20TransferFailed.selector
);
}
}
}
function balanceOfSelf(Currency currency) internal view returns (uint256) {
if (currency.isAddressZero()) {
return address(this).balance;
} else {
return IERC20Minimal(Currency.unwrap(currency)).balanceOf(address(this));
}
}
function balanceOf(Currency currency, address owner) internal view returns (uint256) {
if (currency.isAddressZero()) {
return owner.balance;
} else {
return IERC20Minimal(Currency.unwrap(currency)).balanceOf(owner);
}
}
function isAddressZero(Currency currency) internal pure returns (bool) {
return Currency.unwrap(currency) == Currency.unwrap(ADDRESS_ZERO);
}
function toId(Currency currency) internal pure returns (uint256) {
return uint160(Currency.unwrap(currency));
}
// If the upper 12 bytes are non-zero, they will be zero-ed out
// Therefore, fromId() and toId() are not inverses of each other
function fromId(uint256 id) internal pure returns (Currency) {
return Currency.wrap(address(uint160(id)));
}
}
lib/v4-core/src/types/PoolId.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {PoolKey} from "./PoolKey.sol";
type PoolId is bytes32;
/// @notice Library for computing the ID of a pool
library PoolIdLibrary {
/// @notice Returns value equal to keccak256(abi.encode(poolKey))
function toId(PoolKey memory poolKey) internal pure returns (PoolId poolId) {
assembly ("memory-safe") {
// 0xa0 represents the total size of the poolKey struct (5 slots of 32 bytes)
poolId := keccak256(poolKey, 0xa0)
}
}
}
lib/v4-core/src/types/PoolKey.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Currency} from "./Currency.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {PoolIdLibrary} from "./PoolId.sol";
using PoolIdLibrary for PoolKey global;
/// @notice Returns the key for identifying a pool
struct PoolKey {
/// @notice The lower currency of the pool, sorted numerically
Currency currency0;
/// @notice The higher currency of the pool, sorted numerically
Currency currency1;
/// @notice The pool LP fee, capped at 1_000_000. If the highest bit is 1, the pool has a dynamic fee and must be exactly equal to 0x800000
uint24 fee;
/// @notice Ticks that involve positions must be a multiple of tick spacing
int24 tickSpacing;
/// @notice The hooks of the pool
IHooks hooks;
}
src/interfaces/IBountyEngine.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title IBountyEngine — daily commit-reveal sentence bounties
/// @notice FROZEN at interfaces-v1. Do not edit without overseer approval and a tag bump.
/// @dev Implemented by agent 4 (bounty). Consumed by:
/// - FeeHook (agent 5): deposit (the bounty slice: 25% at launch, default 30% post-burn).
/// - RewardsDistributor (agent 3): deposit (dust sweep destination).
/// - Frontend (agent 8): the full game console drives off this surface + events.
/// Template administration and tier-menu administration are BountyEngine-internal
/// admin surface, intentionally absent here (shape is agent 4's discretion within
/// the normative bounds: MAX 7 slots, tiers 0.1–0.5 ETH, admin sets menus never draws).
interface IBountyEngine {
// ──────────────────────────────────── events ───────────────────────────────────────
/// @notice ETH arrived in the treasury (from FeeHook flush, dust sweep, or donation).
event TreasuryDeposit(address indexed from, uint256 amount);
/// @notice A holder committed; the reveal becomes possible after targetBlock.
event Committed(uint256 indexed eventId, address indexed committer, uint256 targetBlock);
/// @notice The sentence was generated and the bounty locked. `words` are the resolved
/// strings at reveal time (frontend renders history from this without re-reading).
event SentenceGenerated(
uint256 indexed eventId,
uint256[] tokenIds,
string[] words,
uint256 templateId,
uint256 amount,
uint256 sharePerWord,
uint256 deadline
);
/// @notice An unrevealed commit lapsed past the blockhash window; bond forfeited to treasury.
event CommitExpired(uint256 indexed eventId, address indexed committer, uint256 bondForfeited);
/// @notice A word's share was claimed.
event BountyClaimed(uint256 indexed eventId, uint256 indexed tokenId, address indexed to, uint256 amount);
/// @notice Post-deadline unclaimed remainder returned to the free treasury.
event EventSwept(uint256 indexed eventId, uint256 amountReturned);
// ─────────────────────────────────── mutating ──────────────────────────────────────
/// @notice Accepts ETH into the treasury. Permissionless (FeeHook is the primary source).
function deposit() external payable;
/// @notice Opens a generation event. Caller must hold ≥1 Word NFT; at most one event per
/// 24h cycle; requires free treasury ≥ the minimum tier and no pending commit.
/// @dev msg.value MUST equal the 0.01 ETH bond exactly. Stores
/// targetBlock = block.number + REVEAL_DELAY (15 blocks).
function commit() external payable;
/// @notice Resolves a pending commit. Callable by ANYONE once block.number > targetBlock,
/// while targetBlock is within the 256-block blockhash window. Atomically: picks a
/// feasible template, fills slots from the alive registry (deduplicated), draws a
/// tier among only affordable tiers, locks the funds, pays the caller the 2%
/// reveal reward, refunds the committer's bond, emits SentenceGenerated.
function reveal() external;
/// @notice Clears a commit whose targetBlock fell out of the blockhash window unrevealed.
/// Callable by anyone; forfeits the bond to the treasury; re-enables commit.
function expireCommit() external;
/// @notice Claims one word's share. Requires: tokenId in the event's word set; before the
/// 7-day deadline; wordBank.ownerOf(tokenId) == msg.sender (claim-time ownership);
/// not already claimed.
function claim(uint256 eventId, uint256 tokenId) external;
/// @notice Batched claim for multi-word winners. Same checks per id; any failure reverts
/// the whole batch.
function claimMany(uint256 eventId, uint256[] calldata tokenIds) external;
/// @notice Returns the post-deadline unclaimed remainder of an event's locked funds to the
/// free treasury. Callable by anyone. Shares of words burned before claiming fall
/// through to this — never redistributed (per-event share math is immutable).
function sweep(uint256 eventId) external;
// ──────────────────────────────────── views ────────────────────────────────────────
/// @notice Single-call buyer/frontend check: true iff tokenId is in the event's set,
/// unclaimed, before the deadline, and the token is still alive.
function isClaimable(uint256 eventId, uint256 tokenId) external view returns (bool);
/// @notice ETH reserved for revealed-but-unswept events. Free treasury =
/// address(this).balance - lockedFunds(). Invariant: lockedFunds() ≤ balance.
function lockedFunds() external view returns (uint256);
/// @notice Full event record for frontends and integration tests.
/// @return tokenIds the words in sentence order (empty if eventId unknown/unrevealed)
/// @return sharePerWord immutable per-word payout for this event
/// @return deadline claim cutoff timestamp
/// @return swept whether the post-deadline sweep has run
function eventInfo(uint256 eventId)
external
view
returns (uint256[] memory tokenIds, uint256 sharePerWord, uint256 deadline, bool swept);
}
src/interfaces/IBurnEngine.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title IBurnEngine — buy-and-burn, the deflationary third fee destination
/// @notice v3 IN PROGRESS (interfaces-v3, 2026-06-13). Do not edit without overseer approval and
/// a tag bump. Lands together with WordToken/IWordToken v3 (agent 1) and the
/// BurnEngine/FeeHook rewire (agent 5).
/// @dev Implemented by agent 5 (hook-locker — it owns pool-facing infra and the V4 swap
/// expertise this needs). Consumed by:
/// - FeeHook (agent 5): deposit (the burn slice). The FeeHook no longer reads any
/// completion flag from the engine — it picks the fee split per flush from
/// `WordToken.burnableExcess()` directly (dynamic routing, Option B).
/// - Frontend (agent 9): burn progress / supply countdown, executeBuyback button.
/// Holds no NFT, bounty, or rewards logic. Buys WORD on the canonical pool and burns it
/// via WordToken.burn, respecting the DYNAMIC backing floor
/// (`WordBank.totalAlive() × 1000e18`). Buyback runs in its OWN transaction — never
/// inside a swap callback — to avoid reentering the V4 PoolManager.
///
/// v3 change (2026-06-13): the burn floor is dynamic and there is NO permanent
/// completion — burning pauses at the floor and resumes whenever an unbind lowers it.
/// Removed `burnComplete()` and the `BurnEngineRetired` event accordingly.
interface IBurnEngine {
// ──────────────────────────────────── events ───────────────────────────────────────
/// @notice ETH arrived (from the FeeHook flush, or anyone — donations are fine).
event Deposited(address indexed from, uint256 amount);
/// @notice A buyback executed: `ethSpent` bought `wordBought`, all of which was burned;
/// `keeperTip` paid to `caller`.
event BuybackExecuted(address indexed caller, uint256 ethSpent, uint256 wordBought, uint256 keeperTip);
/// @notice Admin updated the bounded max-slippage used to derive the buyback min-out.
event MaxSlippageSet(uint256 bps);
// ─────────────────────────────────── mutating ──────────────────────────────────────
/// @notice Accepts ETH to be used for buy-and-burn. Permissionless (FeeHook is the source).
function deposit() external payable;
/// @notice Spends accrued ETH to buy WORD on the canonical pool and burn it. Permissionless.
/// @dev MUST: size the spend so WORD bought never overshoots `WordToken.burnableExcess()`
/// (the live `totalSupply - currentBurnFloor()`); enforce an on-chain slippage guard
/// (min-out from pool price within the admin-bounded max-slippage, hardcoded ceiling);
/// burn 100% of WORD bought; pay the caller the bounded keeper tip. When there is no
/// burnable excess (supply is at the live floor) it cleanly reverts/no-ops; it resumes
/// automatically once a later unbind lowers the floor and frees new excess. There is
/// no permanent retirement.
/// @param maxEthToSpend Caller-supplied upper bound on ETH spent this call (rate-limit /
/// price-impact control); the engine spends min(this, its sizing cap, its balance).
function executeBuyback(uint256 maxEthToSpend) external;
// ──────────────────────────────────── views ────────────────────────────────────────
/// @notice ETH currently held and available for buybacks.
function pendingEth() external view returns (uint256);
}
src/interfaces/IRewardsDistributor.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title IRewardsDistributor — equal fee-share across alive NFTs (accumulator pattern)
/// @notice FROZEN at interfaces-v1. Do not edit without overseer approval and a tag bump.
/// @dev Implemented by agent 3 (rewards). Consumed by:
/// - WordBank (agent 1): register on mint, settleAndClose on unbind.
/// - FeeHook (agent 5): deposit (the rewards slice: 50% at launch, default 70% post-burn).
/// - BurnEngine (agent 5): deposit (residual-ETH sweep at retirement).
/// - Frontend (agent 8): pendingRewards, claimRewards, events.
/// All accounting is keyed by tokenId, never by owner — rewards travel with the NFT.
interface IRewardsDistributor {
// ──────────────────────────────────── events ───────────────────────────────────────
/// @notice ETH arrived and was spread over `totalAlive` shares.
event Deposited(address indexed from, uint256 amount, uint256 accRewardPerNFT);
/// @notice Token registered at mint with its debt checkpointed to the current accumulator.
event Registered(uint256 indexed tokenId);
/// @notice Pending rewards paid to the token's owner via claimRewards.
event Claimed(uint256 indexed tokenId, address indexed to, uint256 amount);
/// @notice Token force-settled and permanently closed during unbind.
event SettledAndClosed(uint256 indexed tokenId, address indexed to, uint256 amount);
/// @notice Provable rounding dust swept to the BountyEngine treasury.
event DustSwept(address indexed to, uint256 amount);
// ─────────────────────────────────── mutating ──────────────────────────────────────
/// @notice Accepts ETH and accrues it equally to all currently-alive tokens:
/// accRewardPerNFT += amount * 1e18 / wordBank.totalAlive().
/// @dev Called by the FeeHook's flush; MUST be permissionless (donations are fine).
/// Behavior at totalAlive == 0 is the implementer's documented choice (charter).
function deposit() external payable;
/// @notice Registers a freshly minted token. WordBank-only, called inside every mint.
/// @dev MUST set rewardDebt[tokenId] = accRewardPerNFT so mid-stream mints cannot
/// claim fees that arrived before they existed. MUST revert on re-registration
/// and on previously closed ids.
function register(uint256 tokenId) external;
/// @notice Force-settles and permanently closes a token during unbind. WordBank-only.
/// @param to Recipient of the pending payout (the burner — claim-time owner).
/// @dev WordBank calls this BEFORE removing the token from the alive registry, so the
/// token receives its full share of every deposit up to the burn and survivors
/// split everything after (settle-before-decrement, system invariant 3).
/// Closed ids can never accrue, claim, or re-register.
function settleAndClose(uint256 tokenId, address to) external;
/// @notice Batched pull-payment claim. For each id: requires
/// wordBank.ownerOf(tokenId) == msg.sender; pays pending; advances rewardDebt.
/// @dev No deadline — this stream accrues continuously. MUST skip nothing silently:
/// a non-owned or closed id reverts the whole batch.
function claimRewards(uint256[] calldata tokenIds) external;
// ──────────────────────────────────── views ────────────────────────────────────────
/// @notice Exact pending (unclaimed) rewards for a token, in wei. Returns 0 for closed
/// ids. The buyer-due-diligence view: marketplaces price listings off this.
function pendingRewards(uint256 tokenId) external view returns (uint256);
/// @notice Current accumulator value (1e18-scaled rewards per alive NFT), for frontends
/// and invariant tests.
function accRewardPerNFT() external view returns (uint256);
}
src/interfaces/IWordToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title IWordToken — WORD ERC-20, 11,000,000e18 minted cap, burnable to a dynamic backing floor
/// @notice v3 IN PROGRESS (interfaces-v3, 2026-06-13). Do not edit without overseer approval and
/// a tag bump. This file lands together with WordToken (agent 1) and the BurnEngine/FeeHook
/// rewire (agent 5); the tag bumps to v3 once both are in.
/// @dev Implemented by agent 1 (token-bank). Consumed by WordBank (backing mints, unbind
/// release), the BurnEngine (buy-and-burn), and deployment scripts (liquidity mint).
/// No transfer hooks, no burn-on-transfer — bound backing is physically held by
/// WordBank, so liquid WORD is a plain ERC-20. The ONLY burn path is `burn`, callable
/// solely by the BurnEngine and floored at the LIVE backing requirement so the NFT
/// backing is never eroded. Interface is self-contained (no OZ import) so the freeze
/// does not depend on library paths; the implementation should use OZ v5.
///
/// v2 change (2026-06-12): added the buy-and-burn surface (`burn`, `BURN_FLOOR`,
/// `burner`, `burnedTotal`, `burnComplete`).
/// v3 change (2026-06-13): the burn floor is now DYNAMIC — it tracks the live backing
/// requirement `WordBank.totalAlive() * 1000e18` instead of a fixed 10,000,000e18.
/// Unbinding an NFT lowers `totalAlive` and frees its 1,000 WORD into circulation, so
/// that freed WORD becomes burnable. There is no permanent completion: burning pauses
/// when `totalSupply == currentBurnFloor()` and resumes when a later unbind lowers the
/// floor. Removed `BURN_FLOOR()`/`burnComplete()`/`BurnComplete`; added
/// `currentBurnFloor()`/`burnableExcess()`. Post-seal invariant:
/// `totalAlive*1000e18 <= totalSupply <= 11,000,000e18`, both supply and floor
/// monotonically non-increasing.
interface IWordToken {
// ───────────────────────────── standard ERC-20 surface ─────────────────────────────
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
// ───────────────────────────── WORDBANK-specific surface ───────────────────────────
/// @notice Emitted when the admin mints from the liquidity allotment.
event LiquidityMinted(address indexed to, uint256 amount, uint256 liquidityMintedTotal);
/// @notice Emitted exactly once, when minting is permanently sealed.
event MintingSealed(uint256 finalTotalSupply);
/// @notice Emitted on every buy-and-burn.
event Burned(address indexed burner, uint256 amount, uint256 newTotalSupply);
/// @notice Mints backing tokens. Callable ONLY by WordBank, ONLY during the NFT mint
/// phase, always 1,000e18 per NFT minted, always to the WordBank itself.
/// @dev Cumulative backing mints can never exceed 10,000,000e18 (10,000 × 1,000e18).
function mint(address to, uint256 amount) external;
/// @notice Mints from the admin liquidity allotment for seeding the canonical pool.
/// @dev Admin-only. Cumulative cap 1,000,000e18 — reverts beyond it, even for the admin.
function mintLiquidity(address to, uint256 amount) external;
/// @notice Permanently seals all minting. Callable once, only after the NFT mint phase has
/// closed AND the liquidity allotment is fully minted. After this, totalSupply()
/// starts at 11_000_000e18 and only ever decreases via `burn` toward the live
/// backing floor — it can never rise again.
function sealMinting() external;
/// @notice Cumulative amount minted from the liquidity allotment (≤ 1,000,000e18).
function liquidityMinted() external view returns (uint256);
/// @notice True once minting is permanently sealed.
function mintingSealed() external view returns (bool);
// ───────────────────────────── buy-and-burn surface (v3) ───────────────────────────
/// @notice The only address permitted to call `burn` — the BurnEngine.
function burner() external view returns (address);
/// @notice The live supply floor: `WordBank.totalAlive() * 1000e18`. Burning can never
/// drop totalSupply below this, so every alive NFT's 1,000-WORD backing is always
/// fully covered. The floor falls as NFTs unbind (each unbind frees 1,000 WORD into
/// circulation and lowers totalAlive), which is exactly what makes that freed WORD
/// burnable.
function currentBurnFloor() external view returns (uint256);
/// @notice WORD currently burnable: `totalSupply - currentBurnFloor()`, or 0 when supply is
/// already at the floor. The amount the BurnEngine may still buy and destroy.
function burnableExcess() external view returns (uint256);
/// @notice Burns `amount` WORD from the caller's balance. Callable ONLY by `burner`.
/// @dev MUST revert if `totalSupply() - amount < currentBurnFloor()` (equivalently,
/// `amount > burnableExcess()`). There is NO permanent completion: when supply
/// reaches the floor, burning simply pauses until a later unbind lowers the floor,
/// at which point `burn` works again. Burns only from the burner's own balance.
function burn(uint256 amount) external;
/// @notice Cumulative WORD burned via buy-and-burn.
function burnedTotal() external view returns (uint256);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"solady/=lib/solady/src/",
"v4-core/=lib/v4-core/",
"v4-periphery/=lib/v4-periphery/",
"forge-std/=lib/forge-std/src/",
"@uniswap/v4-core/=lib/v4-core/",
"solmate/=lib/v4-core/lib/solmate/",
"@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
"ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"hardhat/=lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"permit2/=lib/v4-periphery/lib/permit2/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.