Hooks.wtf

Core contracts

InternalSwapPool.sol

InternalSwapPool queues launched-token fees and converts them to USDC through temporary liquidity. Its findings cover conversion pricing, stale fee splits, callback gas, settlement utilities and stranded ETH.

Reviewed source: src/core/InternalSwapPool.sol at commit bcd45b2.

Findings

IDSeverityFinding
DICES-05MediumQueued fees are converted at a spot price that any caller can push down first
DICES-08LowThe swap pool keeps the fee split it was deployed with
DICES-10InformationalA conversion position is opened on every buy; callback gas is high
DICES-11InformationalProduction code uses a settler from v4-core's test utilities
DICES-13InformationalETH sent to the swap pool is stuck

[MEDIUM] DICES-05 · Queued fees are converted at a spot price that any caller can push down first

Affected modules: DicesHook.sol, InternalSwapPool.sol.

Severity: Medium. Difficulty: Medium (needs a prior large buy; nothing else). Type: economic, transaction ordering.

Affected code: src/core/InternalSwapPool.sol:155-212, :226-279; src/DicesHook.sol:474-476.

Description

On an exact-input buy the fee is charged in the coin and queued in the swap pool. On the next buy, the swap pool places the whole queue as liquidity in one tick spacing next to the pool's price at that moment:

// src/core/InternalSwapPool.sol:168
(uint160 sqrtPriceX96, int24 tick,,) = poolManager.getSlot0(poolId);

There is no reference price, no minimum price and no cap on how much is offered in one swap. Whoever makes that next buy purchases the queued coins at the current spot price, and the USDC they pay is what the platform, the creator and the buy-and-burn recipient receive.

A caller can lower that price first. It does not need to own any coins. Inside one unlock, Uniswap v4 lets a contract sell coins it only owes and buy the same number back before settling:

  1. Sell c coins (exact input). The price falls. The caller is owed USDC and owes c coins.
  2. Buy exactly c coins (exact output). The swap pool has just placed the queue at the lowered price, so part of the c coins comes from the queue cheaply and the rest from the pool on the way back up.
  3. The coin debt is now zero. The USDC left over is taken as profit.

Both swaps pay the 1.25% fee. The manoeuvre pays when the queue is worth more than those fees, which in practice means directly after one large exact-input buy.

Impact

Measured after a single 30,000 USDC exact-input buy (market cap about $159k, 8.40M coins queued, worth 1,338 USDC at spot):

Ordinary conversion by a 5,000 USDC buyerZero-capital caller sells and re-buys 436.7M coins in one unlock
Caller's profitnone470.38 USDC, with no coins, no USDC and no approvals
Credited to recipients for the queue1,349.45 USDC188.58 USDC
Credited to recipients in total1,349.45 USDC826.41 USDC, of which 637.83 is the fee on the caller's own two swaps
Pool's USDCunchangedfalls from 30,000.00 to 28,703.19

Who bears the cost depends on what happens next. If ordinary buying follows, the fee recipients are 523.04 USDC short (39%) and holders are unaffected. If nobody buys, the cost falls on holders instead: the large buyer could realise 1,175.89 USDC less by selling everything, because the queue was in effect sold into the pool.

How large the preceding buy has to be, for a pool at launch:

One exact-input buy from launchBest result for the zero-capital callerShare of the queue's spot value
1,000 to 12,500 USDCa loss
13,500 USDC (price × 4.65)+1.58 USDC
20,000 USDC+107.46 USDC16%
30,000 USDC+470.38 USDC35%
34,000 USDC+674.96 USDC41%
50,000 USDC+2,024.95 USDC57%
75,000 USDC+5,361.49 USDC67%

The threshold is a single buy that multiplies the price by about 4.5, whatever the fee rate. Ordinary flow does not create it: after one further 5,000 USDC buy has converted the queue, every size of the manoeuvre loses. An exact-output buy queues nothing, so it cannot be targeted.

Things that limit the severity:

  • The amount is bounded by one buy's queued fee. No pool liquidity or escrow balance can be taken, and the escrow stays fully funded throughout.
  • The state it needs comes from someone else's large buy. An attacker cannot create it at a profit, because it would pay the fee it then partly recovers.

Things that raise it:

  • Once the state exists, anyone can take the profit in one transaction with no capital, so it will be taken.
  • The large buyer can do it to its own queue in the same transaction as its buy, which turns it into a standing discount on the buy fee for large buys.
  • The launch block is where large single buys are most likely, and the guard meant to restrict that block does not (DICES-06).

A holder can run the same manoeuvre with coins it owns and end with the same USDC and more coins (1,783,265 more in the test above). That version is not profitable in money terms: the holder realises 675.31 USDC less if it then sells everything.

The independent verifier who established the zero-capital form rated it Low, and Medium if large first-block buys are expected to be routine. It is rated Medium here because that is the expected pattern on a launchpad and because the profit needs no capital or position.

Evidence

Two sets of tests, written separately:

  • The original reviewer's tests, in poc-tests.md: testPoC8_queuedFeeInventoryIsSoldAtAPriceTheBuyerCanPushDown, testPoC8_baseline_inventoryValueAtTheUnmanipulatedPrice, testPoC8b_aDifferentHolderCapturesAnotherBuyersQueuedFee and testPoC8c_measuresOfGainAndWhoPays. These cover the holder version.
  • The independent verifier's, in poc-tests-verify05.md: eleven tests including test_2_zeroInventoryVariant_paysOutUsdc, the precondition sweeps (test_3a, test_3b, test_5c), the other currency ordering (test_4) and a run without extra USDC in the PoolManager that also asserts escrow solvency (test_5b). The original reviewer re-ran all eleven; the figures above are from that recorded run.

Recommendation

Give the conversion a price it cannot be pushed below. In rising order of effort:

  • Skip conversion when the spot tick is below the pool's time-weighted average. LiveCalculator already keeps one per pool, and a price pushed down inside one transaction is by construction below it.
  • Record the price at which each fee was charged and do not convert below it.
  • Cap the share of the queue offered in a single swap, which shrinks the prize without removing it.
  • Charge buy fees in USDC as well, as AGENT.md still describes. No queue forms and the swap pool is not needed.

Whichever is chosen, add a test that a sell followed by a buy in one unlock cannot leave the caller with more USDC than it started with.

The guidelines review flagged the same code as "offered at spot with no reference price" (F-15) without establishing whether it paid.

Recorded test source and output: Original proofs and independent verification.

Source finding: findings.md, DICES-05.

[LOW] DICES-08 · The swap pool keeps the fee split it was deployed with

Affected modules: DicesHook.sol, InternalSwapPool.sol, FeeCalculator.sol.

Severity: Low. Type: configuration.

Affected code: src/core/InternalSwapPool.sol:75, :265; src/DicesHook.sol:115, :752.

Description

DicesHook.feeCalculator is replaceable. InternalSwapPool.feeCalculator is immutable and the hook's reference to the swap pool has no setter. USDC-denominated fees are split by the first; coin-denominated fees are split by the second when they are converted. After a replacement the two disagree.

Impact

A change of split by the owner applies to fees from exact-output buys and exact-input sells and not to fees from exact-input buys and exact-output sells. Recipients are paid by two rules with no on-chain signal that this is so. No funds are lost: every unit still reaches one of the three recipients.

Evidence

testPoC5_swapPoolKeepsTheOldSplitAfterTheCalculatorIsReplaced passes: after the owner installs a split that pays everything to the protocol, the next conversion still credits the creator 0.30 USDC and the buy-and-burn recipient the same.

Recommendation

Have the swap pool read dicesHook.feeCalculator() at conversion time, or move the split into one place. test/CalculatorUpdates.t.sol currently pins the immutability, so the test will need to change with it.

Reported independently by the token-integration (TI-10), Uniswap (V4SF-04), guidelines (F-06) and code-maturity (F7) reviews.

Recorded test source and output: poc-tests.md.

Source finding: findings.md, DICES-08.

[INFORMATIONAL] DICES-10 · A conversion position is opened on every buy; callback gas is high

Affected modules: DicesHook.sol, InternalSwapPool.sol.

Severity: Informational.

MINIMUM_INTERNAL_SWAP_POOL_SIZE is 1e6 (src/core/InternalSwapPool.sol:90) and is compared with an 18-decimal coin balance, so it filters nothing: once any fee is queued, every buy adds and removes a liquidity position. Rounding makes an untouched position cost one raw unit, so the swap pool also runs sync, a coin transfer and settle on every such buy. The position state is kept in ordinary storage (:80, :212, :234) although it never outlives the swap.

Measured from test traces (test environment, cold storage included):

CallbackMedianMaximumUniswap skill's hard ceiling
beforeSwap44,558202,036150,000
afterSwap210,011397,604100,000 (300,000 with external calls)

Recommendation: express the minimum in whole coins or in USDC value; use transient storage for the position state; credit three recipients with one token transfer rather than three. Detail in uniswap-v4-security-foundations.md, section 7.

Source finding: findings.md, DICES-10.

[INFORMATIONAL] DICES-11 · Production code uses a settler from v4-core's test utilities

Affected modules: InternalSwapPool.sol, CurrencySettler.sol.

Severity: Informational.

src/core/InternalSwapPool.sol:9 imports @uniswap/v4-core/test/utils/CurrencySettler.sol, which calls transfer without checking the returned boolean. The repository has its own CurrencySettler (src/libraries/CurrencySettler.sol) that uses SafeERC20; the hook uses that one. Not exploitable today, because the only token transferred this way is DicesCoin, which reverts on failure. Recommendation: use the in-repo library in both places.

Source finding: findings.md, DICES-11.

[INFORMATIONAL] DICES-13 · ETH sent to the swap pool is stuck

Affected modules: InternalSwapPool.sol.

Severity: Informational.

InternalSwapPool.receive() accepts ETH from anyone (src/core/InternalSwapPool.sol:363-365). The only code that could send it on is the native branch of _creditFee (:291-292), which a USDC pool never reaches (testPoC13_ethSentToTheSwapPoolStaysThere). Recommendation: remove receive() and the native branches, or add a sweep to the fee recipient.

Recorded test source and output: poc-tests.md.

Source finding: findings.md, DICES-13.

Shared findings

Previous
DicesCoin.sol