10/On-chain controls

A fee that follows the toxicity score

ParityLvrShieldHook is a Uniswap v4 dynamic-fee hook that charges 5 bps when Parity scores the flow as calm and up to 100 bps when it is toxic, reverts swaps while the mandate is paused, and charges the maximum fee if the agent stops reporting. The contract is contracts/uniswap-v4/src/ParityLvrShieldHook.sol; the agent drives it from apps/agent/src/hookOracle.ts.

The fee curve

v4 LP fees are in hundredths of a bip, so 1,000,000 is 100% and 500 is 5 bps. The hook uses fee = BASE_FEE + score × MAX_TOXICITY_SLOPE with BASE_FEE = 500 and MAX_TOXICITY_SLOPE = 95, which is the same curve the quant kernel reports as recommendedDynamicFeeBps.

StateLP feeIn bps
Score 05005 bps
Score 101,45014.5 bps
Score 505,25052.5 bps
Score 909,05090.5 bps
Score 10010,000100 bps
Oracle stale10,000 (MAX_FEE)100 bps
paused == trueSwap reverts with MandatePausedn/a

beforeSwap returns fee | LPFeeLibrary.OVERRIDE_FEE_FLAG, so the fee applies to that swap only and no separate updateDynamicLPFee call is needed. beforeInitialize rejects pools that were not created with the dynamic-fee flag. Those two callbacks are the only permissions, checked against the address bits in the constructor.

Staleness fail-safe

Every setToxicityOracle call records lastUpdatedBlock. When more than maxStalenessBlocks blocks pass without one, beforeSwap charges MAX_FEE and emits StaleOracleFailSafe. The deploy script defaults the window to about 30 minutes: 1800 blocks on Unichain, 150 on Ethereum and Sepolia. If the agent crashes in a volatile market, LPs are not left charging 5 bps to arbitrageurs.

  • currentFee() is what a swap would pay in this block, including the fail-safe.
  • previewDynamicFee() is the curve for the stored score only, ignoring staleness.
  • isStale() says which one applies.

Who can do what

FunctionCaller
setToxicityOracle(score, paused)guardianOracle (the agent) or owner
setGuardianOracle, setMaxStalenessBlocks, transferOwnershipowner only
beforeInitialize, beforeSwapthe PoolManager only

How the agent drives it

HookOraclePublisher.maybePublish(score, paused) runs every cycle. It rounds the score to an integer from 0 to 100 and sends setToxicityOracle only when the score moved by at least HOOK_MIN_SCORE_DELTA (default 5), the pause flag flipped, or HOOK_HEARTBEAT_MS (default 600000, ten minutes) passed since the last write. The pause flag is true whenever the ENS mandate is inactive or revoked. The result (published, skipped, disabled or error, with the transaction hash and explorer link) is in hook on GET /status.

ChainChain idOfficial PoolManager
Unichain (default)1300x1F98400000000000000000000000000000000004
Ethereum10x000000000004444c5dc75cB358380D2e3dE08A90
Unichain Sepolia13010x00B036B58a818B1BC34d502D3fE730Db729e62AC
Sepolia111551110xE03A1074c86CFeDd5C142C4F04F1a1536e203543

The publisher needs UNISWAP_V4_HOOK_ADDRESS and HOOK_ORACLE_PRIVATE_KEY (the hook's guardianOracle or owner); without them it reports disabled and sends nothing. HOOK_CHAIN_ID defaults to 130, and HOOK_RPC_URL is optional for the four chains above.

Deploying

1. Mine and deploy the hook

script/DeployParityHook.s.sol mines a CREATE2 salt so the hook address has exactly the BEFORE_INITIALIZE and BEFORE_SWAP bits in its low 14 bits, and deploys through the CREATE2 deployer 0x4e59b44847b379578588920cA78FbF26c0B4956C. It needs HOOK_OWNER and HOOK_ORACLE, and on chains 1 and 130 it refuses to run without CONFIRM_MAINNET=true.

2. Seed a demo pool

script/SeedParityPool.s.sol deploys two mock tokens, pHBAR and pUSDC, and v4-core's test routers, initialises a dynamic-fee pool wired to the hook, adds liquidity from −600 to 600 ticks, can push a demo score, and makes one swap so a SwapShielded event appears on-chain.

contracts/uniswap-v4 · Unichain mainnetbash
cd contracts/uniswap-v4
forge build && forge test -vv

CONFIRM_MAINNET=true forge script script/DeployParityHook.s.sol:DeployParityHook \
  --rpc-url https://mainnet.unichain.org --private-key $DEPLOYER_PRIVATE_KEY --broadcast

CONFIRM_MAINNET=true HOOK_ADDRESS=0x<hook> DEMO_TOXICITY_SCORE=40 \
forge script script/SeedParityPool.s.sol:SeedParityPool \
  --rpc-url https://mainnet.unichain.org --private-key $DEPLOYER_PRIVATE_KEY --broadcast