レッスン1 — Uniswap v4 の市場とは何か
出典: Uniswap/v4-core
v4.0.0、psyto/summon-stocksd4debb7、fabrknt/shadowbook7d07439。以下の行リンクはすべてこの版を指す。読了目安:約 10 分(手を動かすと 30 分)
v4 の市場は、5 つの値でできている
Uniswap v4 では PoolManager という 1 つのコントラクトが、すべてのプールを持っている。
プールの正体は PoolKey だけだ。2 つの通貨・手数料・tick spacing・hooks のアドレス、この 5 つでできている。
どれか 1 つを変えれば、別の市場になる。
そして、自分のルールを足す場所が hooks のアドレスだ。
このレッスンでは、v4-core でこれらを読む。次に、この形で市場を作っている 2 つの公開プロジェクトを見て、両方のテストを実際に走らせる。片方は 57 件、もう片方は 49 件だ。
- v4 がすべてのプールに 1 つの
PoolManagerを使う理由と、それが新しい市場にとって何を意味するか PoolKeyの 5 つのフィールドと、そこからプールの id がどう決まるか- hook が入る場所:キーの中の hooks アドレスと、
initializeがそれに対して行う検査 - 2 つのプロジェクトの使い方:Shadowbook(オフチェーンの価格で値付けするプール)と Summon(支援者が LP を持つプールつきで届くトークン化株式)
ファクトリーは、どこにもない
v4 には、プールごとにコントラクトをデプロイするファクトリーがない。
PoolManager が、すべてのプールの状態を 1 つの mapping に持っている(PoolManager.sol L77-L92)。
/// @title PoolManager
/// @notice Holds the state for all pools
contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claims, Extsload, Exttload {
...
mapping(PoolId id => Pool.State) internal _pools;
つまり v4 で「市場をデプロイする」とは、すでにある PoolManager の initialize を呼ぶことだ。
Summon がまさにそうしている。Base Sepolia で 0x05E7…3408 の PoolManager を使っていて(Deploy.s.sol L23)、自前のものはデプロイしていない。
プールは、キーそのもの
プールは 5 つの値で識別される(types/PoolKey.sol L11-L22)。
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;
}
プールの id は、キー全体のハッシュだ(types/PoolId.sol L10-L16)。keccak256(abi.encode(poolKey)) を、キーの 5 つの 32 バイトスロットに対して assembly で計算している。
「同じペアなら、同じ市場では?」と思うかもしれない。v4 ではそうならない。
同じペアでも、手数料・tick spacing・hooks アドレスのどれかが違えば、流動性の分かれた 2 つの市場になる。
新しい市場を作るときに効いてくる点が 2 つある。
1 つ目は currency0 < currency1。
そうでなければ、initialize は CurrenciesOutOfOrderOrEqual で revert する。ネイティブ通貨は address(0) なので、必ず currency0 になる。
2 つ目は、fee がフラグにもなること。
0x800000(LPFeeLibrary.DYNAMIC_FEE_FLAG、LPFeeLibrary.sol L15)は「固定の手数料なし」を意味する。
プールは手数料 0 で始まり(L51-L56)、hook が手数料を決める。
これらの規則を確かめているのが initialize だ(PoolManager.sol L116-L142、抜粋)。
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external noDelegateCall returns (int24 tick) {
if (key.tickSpacing > MAX_TICK_SPACING) TickSpacingTooLarge.selector.revertWith(key.tickSpacing);
if (key.tickSpacing < MIN_TICK_SPACING) TickSpacingTooSmall.selector.revertWith(key.tickSpacing);
if (key.currency0 >= key.currency1) { CurrenciesOutOfOrderOrEqual ... }
if (!key.hooks.isValidHookAddress(key.fee)) Hooks.HookAddressNotValid.selector.revertWith(address(key.hooks));
uint24 lpFee = key.fee.getInitialLPFee();
key.hooks.beforeInitialize(key, sqrtPriceX96);
PoolId id = key.toId();
tick = _pools[id].initialize(sqrtPriceX96, lpFee);
// event is emitted before the afterInitialize call to ensure events are always emitted in order
// emit all details of a pool key. poolkeys are not saved in storage and must always be provided by the caller
// the key's fee may be a static fee or a sentinel to denote a dynamic fee.
emit Initialize(id, key.currency0, key.currency1, key.fee, key.tickSpacing, key.hooks, sqrtPriceX96, tick);
key.hooks.afterInitialize(key, sqrtPriceX96, tick);
}
emit の上のコメントを見てほしい。プールのキーは、ストレージに保存されない。
だから呼び出し側が、毎回キー全体を渡す。
自分のルールを置く場所が、hook だ
最後のフィールド hooks は、コントラクトのアドレスだ。
PoolManager は、決まった時点でそれを呼び出す。initialize、流動性の追加と削除、swap、donate のそれぞれの前と後だ。
上の initialize にも、beforeInitialize と afterInitialize の 2 つが見える。
どの呼び出しが起きるかは、hooks のアドレス自体が決める。これはレッスン3 の主題だ。
ここでは、これだけ押さえておけば十分だ。v4 の市場とはプールのキーであり、その中の hooks アドレスが、作り手が市場を自分のものにするルールを置く場所だ。
この形で作られた 2 つの市場
次の 2 つは、どちらも公開されている初期段階のプロジェクトだ。まず、状態の記述を読んでほしい。
- Shadowbook: 「Status: early and unaudited; not yet deployed.」(初期段階で監査なし、まだデプロイされていない。README L15)
- Summon: 「testnet only (Base Sepolia), unaudited, not for real funds.」(テストネット(Base Sepolia)のみ、監査なし、実際の資金には使わない。README L5)
Shadowbook:オフチェーンの価格で値付けするプール
Shadowbook は「a prop AMM for Elysium (Kinetiq's Hyperliquid L2), built as a Uniswap v4 hook」(Elysium(Kinetiq の Hyperliquid L2)向けの prop AMM を Uniswap v4 の hook として作ったもの、README L3)だ。
hook は曲線ではなく、keeper が送る価格でプールに値を付ける。
なぜ新しい取引所ではなく、v4 のプールにしたのか。README はこう書いている(README L7-L8)。
Because it is an ordinary v4 pool, the aggregators and solvers already routing on Elysium reach it without a new integration.
(普通の v4 プールなので、Elysium で、すでに経路を探しているアグリゲーターやソルバーが、新しい組み込みなしにたどり着く。)
その裏づけの測定を、README の記述どおりに引く(README L25-L31)。
Measured on Elysium mainnet, 2026-11-10 UTC (scripts in
measure/):
- HYPE/USDC traded about $12M across v3/v4/DLMM pools.
- Pools charging 25–30 bp still filled over $3M of it, mostly small orders arriving through aggregators and solvers (KyberSwap, 0x, OKX DEX, Relay and others). A new v4 pool was reached by ERC-4337 and Relay flow within a minute of initialization, and by KyberSwap and a solver within the hour.
(Elysium メインネット、2026-11-10 UTC の測定。HYPE/USDC は v3/v4/DLMM のプール全体で約 1,200 万ドル取引された。25〜30 bp を取るプールでもそのうち 300 万ドル超が約定し、大半はアグリゲーターやソルバー(KyberSwap、0x、OKX DEX、Relay ほか)経由の小口だった。新しい v4 プールには、初期化から 1 分以内に ERC-4337 と Relay の注文が、1 時間以内に KyberSwap と 1 つのソルバーがたどり着いた。)
デモでは、ネイティブ HYPE / USDC のキーでプールを作る(script/Demo.s.sol L69-L70)。
PoolKey memory key = PoolKey(Currency.wrap(address(0)), Currency.wrap(address(usdc)), 0, 60, IHooks(at));
manager.initialize(key, 2 ** 96); // the curve never runs; any valid price will do
currency0 は address(0)、つまりネイティブ HYPE だ。
手数料はなぜ 0 なのか。README にあるとおり、「the curve never runs, so no LP fee is charged whatever the tier」(曲線が一度も動かないので、どの段階の手数料でも LP 手数料はかからない、README L205-L206)からだ。
Summon:支援者が LP を持つプールつきで、株が届く
Summon は Base 上の「a primary-market primitive for tokenized stocks」(トークン化株式の発行市場の部品、README L14)だ。
支援者がティッカーに USDC を約束する。株式トークンの保有者がそのラウンドを約定させると、「Part of the round seeds a Uniswap v4 pool. The pool can only be created by the round, and the backers own the LP position together.」(ラウンドの一部が Uniswap v4 のプールに初期流動性を入れる。プールを作れるのはラウンドだけで、LP ポジションは支援者が共同で持つ。README L24-L25)。
約定のあと、プールの hook は「stops swaps while the stock is halted, around a corporate action, or while the issuer pauses it, and charges 1.00% instead of 0.30% while the US session is closed」(株が売買停止中、コーポレートアクションの前後、発行体が一時停止している間は swap を止め、米国市場が閉まっている間は 0.30% ではなく 1.00% を取る、README L36-L38)ようになる。
各ラウンドは、自分のプールのキーを知っている(src/SummonRound.sol L216-L221)。
function poolKey() public view returns (PoolKey memory key) {
(Currency c0, Currency c1) = p.token < d.usdc
? (Currency.wrap(p.token), Currency.wrap(d.usdc))
: (Currency.wrap(d.usdc), Currency.wrap(p.token));
key = PoolKey(c0, c1, SummonPool.FEE, SummonPool.TICK_SPACING, IHooks(d.hook));
}
定数は src/SummonPool.sol L14-L17 にある。
int24 internal constant TICK_SPACING = 60;
uint24 internal constant FEE = LPFeeLibrary.DYNAMIC_FEE_FLAG;
uint24 internal constant OPEN_FEE = 3000; // 0.30%, while the listing exchange is open
uint24 internal constant CLOSED_FEE = 10_000; // 1.00%, while it is closed or the guard is quiet
株式トークンと USDC は、initialize の要求どおりアドレス順に並べられる。
そしてキーがプールの身元なので、ファクトリーはラウンドを開いた時点で「このキーはこのラウンドのもの」と登録できる(src/SummonFactory.sol L154):roundForPool[keccak256(abi.encode(round.poolKey()))] = address(round);。
hook は、あとで同じハッシュでラウンドを引く。
2 つのキーを並べる
| Shadowbook(デモ) | Summon | |
|---|---|---|
| 通貨 | ネイティブ HYPE(address(0))/ USDC | 株式トークン / USDC、アドレス順 |
| fee | 0(曲線が動かない) | 0x800000、動的: hook が 0.30% か 1.00% を決める |
| tickSpacing | 60 | 60 |
| hooks | Shadowbook の hook | SummonHook |
| 初期化できる者 | owner が 1 回だけ | そのキーに登録されたラウンドだけ |
演習:両プロジェクトを clone してテストを走らせる
Foundry が必要だ。
コマンドは 2 つの README にあるものだ。そこに、このレッスンが引く版への git checkout と、それに合わせたサブモジュールの更新を足している(Summon README L180-L182、Shadowbook README L243-L244)。
git clone --recursive https://github.com/psyto/summon-stocks
cd summon-stocks
git checkout d4debb7 # the version this lesson cites
git submodule update --init --recursive
forge test
cd ..
git clone https://github.com/fabrknt/shadowbook
cd shadowbook
git checkout 7d07439
forge install
forge test
実際の出力はこうだった(forge 1.7.1、macOS、2026-11-14。テストごとの行は省略)。
Summon は via_ir でビルドするので(README L187-L188)、初回のコンパイルに時間がかかる。
# summon-stocks
Compiling 93 files with Solc 0.8.26
Solc 0.8.26 finished in 20.50s
Compiler run successful!
Ran 6 tests for test/SummonPrice.t.sol:SummonPriceTest
Suite result: ok. 6 passed; 0 failed; 0 skipped; finished in 9.78ms (10.51ms CPU time)
Ran 51 tests for test/SummonRound.t.sol:SummonRoundTest
Suite result: ok. 51 passed; 0 failed; 0 skipped; finished in 9.75ms (36.38ms CPU time)
Ran 2 test suites in 13.61ms (19.53ms CPU time): 57 tests passed, 0 failed, 0 skipped (57 total tests)
# shadowbook
Compiling 88 files with Solc 0.8.26
Solc 0.8.26 finished in 1.18s
Compiler run successful!
Ran 21 tests for test/DirectionalFee.t.sol:DirectionalFeeTest
Suite result: ok. 21 passed; 0 failed; 0 skipped; finished in 197.52ms (559.46ms CPU time)
Ran 28 tests for test/Shadowbook.t.sol:ShadowbookTest
Suite result: ok. 28 passed; 0 failed; 0 skipped; finished in 197.48ms (206.61ms CPU time)
Ran 2 test suites in 205.43ms (394.99ms CPU time): 49 tests passed, 0 failed, 0 skipped (49 total tests)
時間は環境で変わる。件数は変わらないはずだ。
- Summon は 57 件。README の「57 tests, no keys, no network after the clone」のとおり。
- Shadowbook は 49 件。README では「28 Shadowbook + 21 DirectionalFee」。
次に、それぞれのテストの setUp を開く。プールのキーを作る行と、プールを初期化する行を探してほしい。
- Summon: どちらも、ラウンドが
fillの中で行う。setUpはfactory.openRoundでラウンドを開き(test/SummonRound.t.sol L124)、補助関数の_startと_fillAfter(L160-L178)がオークションを始めて約定させる。 - Shadowbook: v4 のテスト用ヘルパー
initPool(currency0, currency1, IHooks(address(hook)), 0, 60, SQRT_PRICE_1_1)(test/Shadowbook.t.sol L41)だ。
確認すること
- 引用した版で、両方のテストが 57 件と 49 件で通った。
-
PoolKeyの 5 つのフィールドを挙げられ、ネイティブ通貨が必ずcurrency0になる理由を言える。 - hook が手数料を決めない動的手数料プールが手数料 0 で取引される理由を説明できる(
getInitialLPFeeは0x800000に対して 0 を返す)。 - 上の表の各行は市場の作り手が選ぶことであり、それぞれについて、その選択をしているソースの行を両プロジェクトで指せる。
次のレッスン2 では PoolManager の unlock を開き、実際の swap とプールへの初期流動性の投入をその中で追う。