Index / NthBuyPot
pattern · MIT · last updated 2026-08-27
NthBuyPot
Deterministic Nth-buy jackpot — potBps per buy, counter advances once per block, claim-backed.
First-party teaching hook inspired by Spark Nth-buy Pot. afterInitialize requires native currency0. On exact-input buys, afterSwap pulls potBps of token output into the hook (afterSwapReturnDelta) and increments buyCount at most once per block; when buyCount reaches nthBuy, winner is set from abi.encode(trader) in hookData (else swap sender). claim() sends accumulated token1 to the winner. No random draw — public counter, no permissionless flush. Exact-input buys only. Experimental. Forge tests in test/NthBuyPot.t.sol. Listing is not an audit.
Permission bits
These bits must match the deployed address. Confirm on-chain before you route.
afterInitializeafterSwapafterSwapReturnDelta
Solidity
Excerpt from contracts/NthBuyPot.sol. Copy the full file to implement this. Not an audit.
// SPDX-License-Identifier: MIT
// v4hooks first-party — excerpt. Full file: source.url
function _afterSwap(
address sender,
PoolKey calldata key,
SwapParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) internal override returns (bytes4, int128) {
if (!params.zeroForOne || params.amountSpecified >= 0) return (this.afterSwap.selector, 0);
PotState storage state = pots[key.toId()];
if (block.number != state.lastCountBlock) {
state.lastCountBlock = block.number;
state.buyCount++;
if (state.buyCount == nthBuy) state.winner = traderOf(sender, hookData);
}
uint256 contribution = (uint256(int256(delta.amount1())) * potBps) / 10_000;
key.currency1.take(poolManager, address(this), contribution, false);
return (this.afterSwap.selector, contribution.toInt128());
}Spec
| Kind | pattern |
|---|---|
| Status | experimental |
| License | MIT |
| Source | https://github.com/CryptoGnome/v4hooks |
| Categories | Launchpads |
| Properties | Custom swap dataVanilla swap |
| Chains | Ethereum |
FAQ
Can I paste this into production? The snippet is an excerpt. Use the full file at the source URL, match flags to the address, and treat this page as a map, not a guarantee.
How do I build this safely? Start with secure v4 hooks and the OpenZeppelin hooks guide.