Lesson 9 — The same hook on Monad
Sources: Monad docs: Differences between Monad and Ethereum, Opcode Pricing and Foundry (read 2026-11-15), Uniswap v4 deployments (Monad: 143), fabrknt/shadowbook
5d566ad, Foundry 1.9.0.This lesson was written with an AI (Claude). Every measurement in it comes from the commands shown, run in a Linux codespace with Foundry 1.9.0, with the output pasted as it came. The gas schedule (what an opcode costs) comes from Monad's docs.
About 15 minutes to read, 40 if you run everything.
Same hook, 17% more gas on Monad
Shadowbook's hook runs on Monad without changing a line. All 49 tests pass.
The only thing that changes is gas.
Measure one swap through the hook, with every account cold, and you get 323,357 on Ethereum's schedule and 378,710 on Monad's. That's 17% more.
"Isn't Monad supposed to be the fast, cheap one?" you might ask.
Most of the extra is the five accounts the swap touches for the first time. On Monad that costs 10,100 gas each instead of 2,600.
Meanwhile, the hook's own fifteen storage slots sit in one 128-slot page, so they cost less than on Ethereum.
One more thing. Monad charges the gas limit, not the gas used. If a wallet pads the limit, the swapper pays for the padding.
In this lesson you'll produce these numbers yourself and take them apart:
- Where Uniswap v4 is on Monad, and what you can check from a terminal
- The differences from Ethereum that matter to a hook: cold accounts, storage pages, the gas limit, the contract size limit
- How to run any Foundry suite under Monad's rules (
--network monad), and how to measure one swap with every account cold - Why the same swap costs more on Monad, and which part of a hook's design moves that number
Yes, Monad has a v4 PoolManager
Uniswap lists a v4 deployment for Monad mainnet (chain 143).
The PoolManager is at 0x188d586ddcf52439676ca21a244753fa19f9ea8e. The PositionManager, Quoter, StateView and Universal Router sit beside it.
A pool on Monad is the same thing as in lesson 1: a PoolKey held by that one PoolManager. Only the address you call changes.
Check that it's really there (Foundry's cast, Monad's public RPC):
cast chain-id --rpc-url https://rpc.monad.xyz
cast code 0x188d586ddcf52439676ca21a244753fa19f9ea8e --rpc-url https://rpc.monad.xyz | wc -c
143
48021
That's 48,021 characters of hex, 0x and a newline included: 24,009 bytes of code.
(Note: we haven't compared that bytecode with v4-core v4.0.0. If you deploy against it, do that comparison first.)
What changes, and what doesn't
Monad runs EVM bytecode as it is.
So a hook written for Ethereum deploys unchanged, and mining its address (lesson 3) works the same way, because CREATE2 does.
What changes is what things cost, plus a few limits. Here are the ones a hook author runs into, from Monad's docs:
| Ethereum | Monad | Where a hook meets it | |
|---|---|---|---|
First access to an account (CALL, BALANCE, EXTCODESIZE, …) | 2,600 | 10,100 | the PoolManager calling the hook, the hook calling a token, an oracle, a keeper's contract |
| First read of storage | 2,100 per slot | 8,100 per page of 128 consecutive slots | the hook's own state, and every token balance it moves |
| Later reads in the same slot / page | 100 | 100 | |
| Memory expansion | quadratic | linear, 8 MB per transaction | rarely, for a hook |
| What the sender pays | gas used | the gas limit | every swap through the pool |
| Contract size limit | 24 KB | 128 KB | a hook that has to be split on Ethereum |
ecRecover | 3,000 | 6,000 | a hook that checks signed quotes |
Two of these drive the numbers later on.
The first is pages.
A slot's page is slot >> 7. State variables declared one after another, the fields of a struct and the elements of an array are consecutive slots, so they share a page. Monad charges the cold read once per page.
A mapping is the opposite. Each key lands somewhere of its own, so each key is its own page.
An ERC-20 balance is a mapping entry. So every balance a swap moves is a fresh page.
The second is the gas limit.
Why does Monad charge the limit instead of the gas used?
Monad executes blocks after consensus has ordered them. If it refunded unused gas the way Ethereum does, that would open a denial-of-service hole. So the sender pays gas_bid * gas_limit.
A router or front end that pads the limit "to be safe" makes every swap cost more. Estimate, then set the limit close to the estimate. That's all it takes.
First, run the suite under Monad's rules
With Foundry 1.8 or later, just pass --network monad (or set network = "monad" in foundry.toml). It then runs with Monad's gas model, opcode prices, storage pages and size limits.
Releases before 1.8 don't have it, so check with forge --version.
git clone --recurse-submodules https://github.com/fabrknt/shadowbook
cd shadowbook && git checkout --recurse-submodules 5d566ad
forge test
forge test --network monad
Ran 2 test suites in 1.82s (2.43s CPU time): 49 tests passed, 0 failed, 0 skipped (49 total tests)
Ran 2 test suites in 1.84s (2.55s CPU time): 49 tests passed, 0 failed, 0 skipped (49 total tests)
It passes on both. What differs is the gas each test reports.
Across the 45 tests that report a single gas figure, Monad's schedule costs between 0.96 and 1.99 times as much, 1.30 at the median.
- The biggest jump is
test_constructor_rejectsBadConfig(1.99×). It tries several deployments of the hook with a bad configuration. - Next is
test_withdraw(1.84×). - One goes slightly down:
test_onlyOwnerInitializes_andOnlyOnePool(0.96×).
But a test's figure covers a whole test, several calls in one.
If you want to know about one swap, you have to measure one swap.
Measure one swap, with every account cold
Put this file next to Shadowbook's tests as test/MonadGas.t.sol.
It does three things:
- Reuses
ShadowbookTest's setup - Marks every account the swap touches cold (as at the start of a real transaction; exercise 4 shows forge already starts each test that way)
- Sells 1 HYPE at the bid, and prints the gas and every account and storage slot the swap touched
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.26;
import {console2} from "forge-std/console2.sol";
import {Vm} from "forge-std/Vm.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {ShadowbookTest} from "./Shadowbook.t.sol";
contract MonadGasMeasure is ShadowbookTest {
function test_measure_oneSwap() public {
address[5] memory touched = [
address(manager), address(hook), address(swapRouter),
Currency.unwrap(currency0), Currency.unwrap(currency1)
];
for (uint256 i; i < touched.length; i++) vm.cool(touched[i]);
vm.startStateDiffRecording();
uint256 g = gasleft();
_swap(key, true, -1e18); // sell 1 HYPE at the bid
uint256 used = g - gasleft();
Vm.AccountAccess[] memory acc = vm.stopAndReturnStateDiff();
console2.log("GAS", used);
for (uint256 i; i < acc.length; i++) {
console2.log("ACCOUNT", acc[i].account, uint256(acc[i].kind));
for (uint256 j; j < acc[i].storageAccesses.length; j++) {
Vm.StorageAccess memory s = acc[i].storageAccesses[j];
console2.log("SLOT", s.account, uint256(s.slot), s.isWrite);
}
}
}
}
forge test --match-test test_measure_oneSwap -vv | grep GAS
forge test --network monad --match-test test_measure_oneSwap -vv | grep GAS
GAS 323357
GAS 378710
Those are the two numbers from the top.
(Note: the figure is the swap call alone. It leaves out the 21,000 every transaction pays, and the calldata.)
Where do the 55,353 extra gas come from?
Let's count what the swap touched.
Take the ACCOUNT and SLOT lines (the same on both schedules), group them by account, and work out each slot's page with slot >> 7:
| Account | Distinct slots | Pages |
|---|---|---|
| the swap router | 0 | 0 |
the PoolManager | 4 | 3 |
| token0 (HYPE-like) | 3 | 3 |
| token1 (USDC-like) | 3 | 3 |
the Shadowbook hook (0x4444…2888) | 15 | 1 |
| total | 25 | 10 |
Now hold that up against the price table from earlier.
- Accounts: +37,500. Reaching the five accounts cold costs 5 × (10,100 − 2,600) = 37,500 more.
- The hook's storage: −23,400. Its fifteen slots are one page, so the first reads cost 8,100 on Monad against 15 × 2,100 = 31,500 on Ethereum. Monad is cheaper here.
- Balances and the PoolManager: +51,900. These slots are spread out: ten slots on nine pages, 9 × 8,100 = 72,900 against 10 × 2,100 = 21,000.
Add up the read side alone, and it doesn't match the measured +55,353 exactly.
That's because writes are priced differently again (on Monad a page pays its write cost once, and a second write in the same page costs 100).
The measurement is the total. The table shows where it comes from.
So what should you watch in your own hook?
Count the accounts.
Every contract a swap reaches costs 10,100 the first time. The measured swap reached five: the router, the PoolManager, the two tokens and the hook.
A hook that also calls an oracle, a registry or a second hook adds one more for each. Shadowbook calls none of them. It keeps its price in its own storage, written by a keeper (lesson 7).
Pack the hook's state.
Shadowbook's fifteen slots fit in one page. That's why its own storage is cheaper on Monad.
Values a swap reads together belong in consecutive slots (a struct, or adjacent state variables), not one mapping entry each.
The balances aren't yours to pack.
Each token balance is its own page, inside the token's contract. The hook can't change that.
(Note: Shadowbook takes its input as ERC-6909 claims in the PoolManager (lesson 7). We didn't measure a design that settles another way, so we won't say which costs less.)
Set the gas limit from an estimate.
On Monad, the swapper pays the limit.
Size isn't the constraint it is on Ethereum.
Shadowbook's runtime code is 20,271 bytes. That leaves only 4,305 under Ethereum's 24 KB limit. Monad allows 128 KB.
forge build --sizes | grep -E "Contract|Shadowbook "
| Contract | Runtime Size (B) | Initcode Size (B) | Runtime Margin (B) | Initcode Margin (B) |
| Shadowbook | 20,271 | 24,469 | 4,305 | 24,683 |
Exercise
-
Run
forge testandforge test --network monadon Shadowbook at5d566adand confirm 49 tests pass in both. -
Add
test/MonadGas.t.solabove and reproduce323357and378710. -
Count the slots and pages yourself from the
SLOTlines:forge test --network monad --match-test test_measure_oneSwap -vv | grep SLOT. Which account owns fifteen slots, and how many pages are they? -
Predict first. Comment out the
vm.coolloop and run both again.setUpalready touched every one of those accounts, so do the figures fall?We guessed they would.
They don't:
323360and378710, within 3 gas of the cooled run. Forge starts each test call with fresh access lists, sosetUp's warmth doesn't carry into the test. The loop only makes that explicit.Check a guess like ours before you write it down.
What to check
- You can name the
PoolManageraddress on Monad and checked it has code. - You can say what costs 10,100 on Monad and 2,600 on Ethereum, and what a storage page is.
- You reproduced the two figures for one swap, and can say why the hook's own storage is cheaper on Monad while the swap as a whole is dearer.
- You know why a padded gas limit costs the swapper on Monad.
Next: would your tests notice a missing check?
The tests here are the ones the course wrote.
On Monad, as anywhere, the next question is: would they notice if a check went missing?
Lesson 8's exercise deleted one and watched a bare vm.expectRevert() keep passing.
The Monad Foundation's Ultrafuzz writes fuzz tests for Solidity contracts with AI agents. How much those tests catch is something to measure, not assume.