Overview
OniSwap is an exchange on Solana where every pool carries a hook, a separate program that the pool calls before and after each swap. The hook can answer with the fee for that trade and record what happened in its own account. This page describes the shape we tested on a local validator on 17 November 2026, with the numbers we measured.
The hook interface
The pool calls the hook with a plain invoke, never with the pool's signature. The call carries exactly one account and a short instruction.
| Part | What it is |
|---|---|
| Account | The hook's own state account, writable, not a signer |
| Instruction data | A phase byte (before or after the swap) and the swap amount as an unsigned 64-bit integer |
| Answer | Optional return data with a fee in basis points. The pool rejects any fee above 10 percent |
call hook(state_account) data = [phase: u8, amount: u64]
answer return_data = fee_bps: u16 // the pool refuses fee_bps > 1000Attaching a hook
- The pool creator picks the hook when the pool is created. The pool stores the hook program id, the hook's state account and the vault, and no instruction can change them.
- Every swap checks that the hook program and the state account passed in are the stored ones, so a trader cannot swap in a different hook.
- The state account must be owned by the hook program, and it can never be the pool's own state or its vault.
- Pools are keyed by their two coins, their hook and their fee settings, so a hook only ever occupies its own key.
- A pool with no hook is a first-class pool and pays no hook cost.
Security shape
A program on Solana can only touch the accounts named in its own instruction. OniSwap hands the hook one account, so the hook has nothing else to work with. The pool state, the vault, the vault's authority, the token program and the trader's wallet all stay out of its reach.
| Attack by the hook | In OniSwap's shape | If the pool forwarded everything |
|---|---|---|
| Overwrite the reserves | Fails, the hook sees one account | Fails, the runtime blocks writes to accounts a program does not own |
| Move vault coins to an attacker | Fails, vault stays at 1,000,000,000 | Succeeds, 500,000,000 taken |
| Call back into the pool | Fails, the pool program is out of reach | Fails, the runtime rejects the reentry |
| Return a 100 percent fee | Refused, error 0x109 | Not tested |
The rule that follows is simple. The pool never signs for a hook and never forwards its vaults, its authority or the trader's accounts. All 17 tests and their logs are in the project's spike evidence.
Compute cost
| Swap | Compute units |
|---|---|
| Test swap with no hook | 825 |
| Test swap with a hook called before and after | 4,772 |
| The two hook calls alone | 3,947 |
| Swap in the tested security shape with an honest dynamic-fee hook | 5,439 |
The test swap moves no SPL tokens, so a production swap costs more. The hook pair stays in the same few thousand units either way, against a 1,400,000 unit ceiling per transaction.
What a hook can still do
A hook can stop swaps in its own pool. In our test an honest hook switched to an endless loop after creation, and every later swap failed. It cannot reach the coins, so the design keeps withdrawing liquidity free of any hook call, and shows each pool's hook upgrade authority. A looping hook fails only its own transaction; another pool swapped right after it in 5,439 units.