Lesson 4 — Who may create the pool and add liquidity
Sources: Uniswap/v4-core
v4.0.0, psyto/summon-stocksb1dd190, fabrknt/shadowbook7d07439.From this lesson on, Summon is read at
b1dd190, its main branch since 2026-11-14. It isd4debb7plus the permission self-check from Lesson 3's dated note (getHookPermissionsandvalidateHookPermissionsin SummonHook's constructor, one more test, 58 in all). Lessons 1–3 stay ond4debb7. The files this lesson cites other thanSummonHook.soland the test file are identical at both commits.About 20 minutes to read, 35 if you run everything.
Comment out one line, and a stranger creates the pool before the round can
The PoolManager doesn't ask who you are.
Anyone may initialize a pool for any valid key. Anyone may add liquidity to any pool. The first initialize for a key wins, and every later one reverts.
"So who can say no?" Only the hook named in the key.
It's called with the address that called the PoolManager, before the pool is created and before each liquidity addition.
SummonHook says no unless that address is the round its factory registered for that exact key.
So the pool is created by the round, and the LP position belongs to the round's backers alone.
In this lesson we'll read that check and compare it with Shadowbook's two hooks. Then we'll remove it in a scratch copy, to see which tests notice and what an outsider could then do.
- What
initializeandmodifyLiquiditycheck about the caller (nothing), and what they pass to the hook (sender) - How SummonHook finds the one round allowed for a pool key: the factory's
roundForPoolregistry - Why that matters for Summon in particular: a pool key known in advance, and a re-pricing step that assumes the pool holds only the backers' position
- Three answers to "who may": Summon, Shadowbook and DirectionalFee
Does the PoolManager check who's calling? No
Look again at initialize from Lesson 1 (PoolManager.sol L116-L141).
Every check is on the key: tick spacing, currency order, a consistent hooks address.
None is on msg.sender. And as Lesson 2 quoted, initialize doesn't even need an unlock.
What stops a second initialize is the pool's own state (libraries/Pool.sol L100-L107):
function initialize(State storage self, uint160 sqrtPriceX96, uint24 lpFee) internal returns (int24 tick) {
if (self.slot0.sqrtPriceX96() != 0) PoolAlreadyInitialized.selector.revertWith();
tick = TickMath.getTickAtSqrtPrice(sqrtPriceX96);
// the initial protocolFee is 0 so doesn't need to be set
self.slot0 = Slot0.wrap(bytes32(0)).setSqrtPriceX96(sqrtPriceX96).setTick(tick).setLpFee(lpFee);
}
So a pool key is first come, first served.
And the first caller also picks the starting price.
modifyLiquidity is the same. It needs the lock, but not a particular caller.
It records the position under the caller's address (PoolManager.sol L157-L166): owner: msg.sender.
That protects a position from being removed by anyone else. It doesn't stop anyone from adding a position of their own next to it.
What the PoolManager does pass on is who called it.
Before adding liquidity, it calls the hook with its own msg.sender (libraries/Hooks.sol L194-L205):
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)));
}
}
beforeInitialize passes it the same way (L177-L181, quoted in Lesson 3).
The interface calls this argument "The initial msg.sender for the initialize call" (IHooks.sol L17). It also says a hook "Should only be callable by the v4 PoolManager" (L14).
That has two consequences for a hook that gates on sender:
senderis whoever called thePoolManager. When a user goes through a router or a position manager, that's the router, not the user. Gating onsenderworks when the party you trust calls thePoolManageritself, asSummonRounddoes inside its ownunlock(Lesson 2).senderis only an argument. Anyone can call the hook directly and pass any address. So the hook must also check thatmsg.senderis thePoolManager.
Why can't Summon leave it open?
The pool key is known before the pool exists.
A round builds its key from the token, USDC, two constants and the hook (src/SummonRound.sol L216-L221, read in Lesson 1).
Nothing in it depends on the round. So the key is public from the moment the round opens.
That can be weeks before the fill that seeds the pool (a round may run up to 30 days).
If anyone could initialize it, an outsider could create the pool first, at a price of their choosing.
The round's fill would then revert inside pm.initialize with PoolAlreadyInitialized. So would the fill of every later round for the same stock, because each builds the same key.
The exercise runs exactly this.
The LP position is meant to be the backers' alone.
The README states it: "The pool can only be created by the round, and the backers own the LP position together." (README L24-L25).
The round's re-pricing after a split, which Lesson 5 reads, relies on it. Its comment begins (src/SummonRound.sol L427-L428): "realign: the pool is empty now, so a swap moves its price and moves no tokens."
That's true only if the round's own position was the only one.
How does the hook know which round? It asks the factory's registry
The factory keeps one mapping, from a key's hash to the round that may use it (src/SummonFactory.sol L60):
mapping(bytes32 keyHash => address) public roundForPool;
It writes the entry when it creates a round (L152-L154):
latestRound[token] = address(round);
openedAt[address(round)] = uint64(block.timestamp);
roundForPool[keccak256(abi.encode(round.poolKey()))] = address(round);
"Can't someone just open a second round and overwrite it?"
Anyone may open a round, but only for a token the owner listed. And not while another round for it is running or has filled (L106-L108): "Anyone may open a round for a listed token, unless one is still running or one already filled (the token then has its Summon pool, and the pool key can only be initialized once)."
The same comment adds one exception, which the factory's constants spell out (L39-L45). An open round at least a day old that is behind its pledge pace (or that nobody has pledged to) may be replaced. The factory cancels it in the same transaction.
Either way, a later round for the same token overwrites the entry. So at any time, the key points at one round.
The hook is told where the factory is once, by its deployer (src/SummonHook.sol L55-L60): "One-time wiring; the factory needs the hook's address and the hook needs the factory's."
Then both callbacks run the same check (L62-L74 and L116-L124):
function beforeInitialize(address sender, PoolKey calldata key, uint160) external view returns (bytes4) {
_requireRound(sender, key);
return IHooks.beforeInitialize.selector;
}
function beforeAddLiquidity(address sender, PoolKey calldata key, IPoolManager.ModifyLiquidityParams calldata, bytes calldata)
external
view
returns (bytes4)
{
_requireRound(sender, key);
return IHooks.beforeAddLiquidity.selector;
}
...
function _roundOf(PoolKey calldata key) internal view returns (address) {
return address(factory) == address(0) ? address(0) : factory.roundForPool(keccak256(abi.encode(key)));
}
function _requireRound(address sender, PoolKey calldata key) internal view {
if (msg.sender != poolManager) revert NotPoolManager();
address round = _roundOf(key);
if (round == address(0) || round != sender) revert NotTheRound(sender);
}
Read it against the two points above.
The msg.sender check makes sender trustworthy.
The lookup hashes the key the hook was called with, keccak256(abi.encode(key)). That's the same hash the factory stored (and, as Lesson 1 showed, the pool's id).
A key with any field changed, a different fee or tick spacing for instance, hashes to an entry nobody wrote. round == address(0) refuses it.
Both callbacks are view. The hook stores nothing per pool. It asks the factory each time.
(Note: SummonHook has no beforeRemoveLiquidity bit. It doesn't need one. Only the position's owner can remove it, and the owner is the round.)
Three answers to "who may"
Summon (SummonHook) | Shadowbook | DirectionalFee | |
|---|---|---|---|
| create the pool | only the round registered for the key | only the owner, once | only the owner, once, and only with a dynamic fee |
| add liquidity | only that round | nobody | anyone |
| how the hook knows | looks the key up in the factory | stores the pool id on its first initialize | stores the pool id on its first initialize |
Shadowbook checks the owner and binds itself to one pool.
(src/Shadowbook.sol L216-L230):
function beforeInitialize(address sender, PoolKey calldata key, uint160) external onlyPoolManager returns (bytes4) {
if (sender != owner) revert NotOwner();
if (bound) revert AlreadyBound();
(poolId, bound) = (key.toId(), true);
return IHooks.beforeInitialize.selector;
}
function beforeAddLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata)
external
view
onlyPoolManager
returns (bytes4)
{
revert NoExternalLiquidity();
}
Why refuse all liquidity? Because its pool isn't a curve.
The hook fills every swap from its own inventory (Lesson 2's last section). So a position in the pool would never trade.
DirectionalFee's beforeInitialize is the same, plus a fee check.
(src/DirectionalFee.sol L191-L197). It doesn't take the BEFORE_ADD_LIQUIDITY bit at all.
Its README: "One pool, initialized by the owner, which must use the dynamic-fee flag. Anyone can add liquidity; the hook only sets the fee." (README L141-L142).
"Then why does Summon need a registry at all?"
Shadowbook's and DirectionalFee's owner check is simpler because each hook serves one pool for one owner.
Summon has one hook for every stock's pool, and a new round per stock. So the hook needs a lookup from key to round.
Which tests guard the rule?
Two tests in Summon's suite cover it (test/SummonRound.t.sol L657-L661 and L1000-L1009):
function test_onlyTheRoundInitializesItsPool() public {
PoolKey memory key = round.poolKey();
vm.expectRevert(); // the PoolManager wraps the hook's NotTheRound
pm.initialize(key, 79228162514264337593543950336);
}
...
function test_onlyTheRoundAddsLiquidity() public {
(, PoolKey memory key,) = _summoned();
PoolModifyLiquidityTest lp = new PoolModifyLiquidityTest(IPoolManager(address(pm)));
usdc.mint(address(this), 1_000e6);
token.mint(address(this), 100e8);
usdc.approve(address(lp), type(uint256).max);
token.approve(address(lp), type(uint256).max);
vm.expectRevert(); // the PoolManager wraps the hook's NotTheRound
lp.modifyLiquidity(key, IPoolManager.ModifyLiquidityParams(-887220, 887220, 1e9, 0), "");
}
In the second, the sender the hook sees is PoolModifyLiquidityTest, v4-core's test router, not the test contract. The router is what calls the PoolManager.
Note also the bare vm.expectRevert(). It passes on any revert.
The comment says why the authors didn't name the error: the PoolManager wraps it. Lesson 5 shows how to match the wrapped error exactly.
Exercise: remove the check, in a scratch copy
Start from the directory that holds your checkouts (the one you cloned into in Lesson 1).
If your summon-stocks checkout predates b1dd190, the fetch brings it in:
git -C summon-stocks fetch origin
git -C summon-stocks checkout b1dd190 && git -C summon-stocks submodule update --init --recursive
rm -rf /tmp/summon-scratch
cp -R summon-stocks /tmp/summon-scratch && cd /tmp/summon-scratch
rm -rf cache/fuzz # no fuzz failures replayed from earlier runs
forge test --mc SummonRoundTest # 52 passed
A. Anyone may initialize.
Comment out the check in beforeInitialize (line 63 at b1dd190):
sed -i '' '63s|^ _requireRound| // _requireRound|' src/SummonHook.sol # GNU sed: sed -i without ''
git diff
forge test --mc SummonRoundTest
What we got (forge 1.7.1, macOS, 2026-11-14): the diff's two changed lines, then the failing tests and the summary, with the timing cut (…):
- _requireRound(sender, key);
+ // _requireRound(sender, key);
[FAIL: next call did not revert as expected] test_hookRejectsDirectCalls() (gas: 22833)
[FAIL: next call did not revert as expected] test_onlyTheRoundInitializesItsPool() (gas: 55466)
Suite result: FAILED. 50 passed; 2 failed; 0 skipped; …
Two tests fail, not one.
The msg.sender check lives inside _requireRound too. So a direct call to beforeInitialize (test_hookRejectsDirectCalls, L663-L667) no longer reverts either.
B. What an outsider can then do.
Keep the edit from A and add a test of your own.
It inherits the suite's setUp and helpers. It lets another address initialize the round's pool first (bob here; any address but the round will do), then fills the round as usual:
cat > test/FirstToInitialize.t.sol <<'EOF'
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.26;
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {SummonRound} from "../src/SummonRound.sol";
import {SummonRoundTest} from "./SummonRound.t.sol";
contract FirstToInitializeTest is SummonRoundTest {
function test_aStrangerCannotInitializeFirst() public {
PoolKey memory key = round.poolKey();
// a stranger tries to create the round's pool first, at a price of their choosing
vm.prank(bob);
try pm.initialize(key, 79228162514264337593543950336) {} catch {}
// the round must still fill and seed its own pool
_start();
_fillAfter(60);
assertEq(uint8(round.state()), uint8(SummonRound.State.Filled));
}
}
EOF
forge test --mt test_aStrangerCannotInitializeFirst
(Note: a contract that inherits a test contract runs all of its tests again. That's why we use --mt to run just the new one.)
With the check removed (the compiler also warns that sender and key are now unused):
Ran 1 test for test/FirstToInitialize.t.sol:FirstToInitializeTest
[FAIL: PoolAlreadyInitialized()] test_aStrangerCannotInitializeFirst() (gas: 861429)
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; …
The stranger's initialize went through at a 1:1 price.
The round's fill reverted inside its own pm.initialize.
Restore the hook and run it again:
git checkout -- src
forge test --mt test_aStrangerCannotInitializeFirst
Ran 1 test for test/FirstToInitialize.t.sol:FirstToInitializeTest
[PASS] test_aStrangerCannotInitializeFirst() (gas: 1134040)
Suite result: ok. 1 passed; 0 failed; 0 skipped; …
Now the stranger's call reverts (the try … catch swallows it), and the round creates the pool.
Remove the file: rm test/FirstToInitialize.t.sol.
C. Anyone may add liquidity.
This time, comment out the check in beforeAddLiquidity instead (line 72):
sed -i '' '72s|^ _requireRound| // _requireRound|' src/SummonHook.sol
forge test --mc SummonRoundTest
[FAIL: next call did not revert as expected] test_onlyTheRoundAddsLiquidity() (gas: 3021892)
Suite result: FAILED. 51 passed; 1 failed; 0 skipped; …
What would this do to the re-pricing? We tried that too.
In the same scratch copy, we wrote a test that added an outsider's position through PoolModifyLiquidityTest, then called round.realign() after a 2:1 multiplier change. It failed with CurrencyNotSettled().
Its trace shows why:
- The round's price-moving swap (
amountSpecified: -1) now traded against the outsider's liquidity. - It took one unit of USDC and left the price where it was.
- The round's callback never pays for that swap, so the
unlockended with a debt.
Undo with git checkout -- src. forge test is back to 58 passing.
What to check
- You can say what
initializeandmodifyLiquiditycheck about the caller, and why a secondinitializefor the same key reverts. - You can say what
senderis when a user adds liquidity through a router, and why the hook also checksmsg.sender. - You can follow a pool key from
SummonFactory.openRoundtoSummonHook._requireRound, and say why a key with a different fee is refused. - You saw two tests fail with the
beforeInitializecheck removed, and your own test fail withPoolAlreadyInitialized()and pass once it was restored. - For a hook of your own, you can fill in the table's three rows.
Next, Lesson 5 reads the third callback, beforeSwap: how the pool stops trading when the stock does.