Flash Accounting
Deferring balance checks to the end of a transaction so a contract can move funds through several steps at once.
Basically
Flash accounting cuts gas costs by waiting until the end of a transaction to move tokens. It adds up the changes first, so only the final amounts need to be sent.
Key Takeaways
- Flash accounting defers all token transfers to the end of a transaction. The protocol tracks internal balance changes, called deltas, and settles only the net amounts, which cuts gas costs significantly.
- Uniswap V4 pioneered flash accounting through EIP-1153 transient storage and the singleton contract pattern. All liquidity pools sit in one contract, and transient storage opcodes cost 100 gas each instead of up to 20,000 gas for regular storage writes.
- Multi-hop DEX swaps benefit most from flash accounting. A three-hop trade needs only two transfers, one in and one out, instead of three separate token transfers in older designs, saving roughly 50% on gas.
What Is Flash Accounting?
Flash accounting is a gas optimisation technique used by decentralised exchanges. It tracks token balance changes internally as debits and credits throughout a transaction, defers actual token transfers until the very end, and reverts the entire transaction if the net balances do not resolve to zero.
Uniswap V4 introduced the concept, drawing on the same atomic-transaction principle behind flash loans. Ethereum transactions are all-or-nothing, so flash accounting can defer settlement as long as all balances net out before the transaction ends, just as a flash loan allows uncollateralised borrowing when repayment occurs within the same transaction.
Traditional AMM designs such as Uniswap V3 transfer tokens between separate pool contracts at every step of a swap, and each ERC-20 transfer costs significant gas. Flash accounting eliminates these intermediate transfers by keeping all pools inside a single contract and replacing real token movements with cheap internal bookkeeping.
How It Works
Flash accounting relies on two architectural innovations working together. The two innovations are the singleton contract pattern and EIP-1153 transient storage.
The Singleton Contract
Earlier AMM designs used a factory contract to deploy a new smart contract for each trading pair. Uniswap V3, for example, creates a separate pool contract for every token pair and fee tier, so a multi-hop swap such as ETH to USDC to DAI requires tokens to physically move between different contracts.
Uniswap V4 replaced this with a singleton architecture, where a single PoolManager contract holds the state for all pools. The protocol can track balance changes across multiple pools using internal variables rather than moving tokens between contracts, while pool creation becomes a simple state update rather than a contract deployment, reducing pool creation gas by approximately 99%.
The Delta System
The delta system sits at the core of flash accounting and uses signed integers to track how much each party owes or is owed. The process follows a strict pattern.
Negative deltas mean the caller owes tokens to the PoolManager, while positive deltas mean the PoolManager owes tokens to the caller. Only the final net amounts ever trigger real ERC-20 transfers.
Transient Storage (EIP-1153)
EIP-1153 makes flash accounting practical and was activated in Ethereum's Dencun hard fork in March 2024. The EIP introduced two new opcodes.
Transient storage persists across internal calls within a transaction but is automatically erased when the transaction ends. Flash accounting needs exactly this temporary bookkeeping, which must zero out by transaction end, because tracking deltas before EIP-1153 would have required regular SSTORE operations costing up to 20,000 gas per write and made the entire approach uneconomical.
Concrete Example
A simple example uses a swap of 100 USDC for 0.03 ETH. The example separates the swap itself from the token transfers.
The swap itself in step 2 moves zero tokens and only updates two integers in transient storage. The actual ERC-20 transfers happen once each in steps 3 and 4, whereas the swap step itself would have triggered token transfers in a traditional AMM.
Multi-Hop Swaps
Multi-hop swaps compound the gas savings. The route runs from ETH to USDC to DAI.
Gas Savings
Flash accounting delivers measurable gas reductions. The reductions apply across different operation types.
A three-hop swap in Uniswap V4 costs approximately 145,000 gas, compared with roughly 260,000 gas in V3, a reduction of about 44%. Each additional hop eliminated saves the gas cost of one full ERC-20 transfer, making complex routing through multiple pools significantly cheaper.
Flash Accounting vs. Flash Loans
Flash loans and flash accounting share a core design principle because both exploit the atomic nature of blockchain transactions. Both revert entirely if an end-of-transaction invariant is not met, but they serve different purposes.
Uniswap V4's take() function can drive a delta negative, effectively enabling flash-loan-like behaviour within the flash accounting system. A caller can take tokens first and settle later, as long as everything nets to zero by the time unlock() returns.
Efficient Multi-Hop Routing
DEX aggregators and smart order routers frequently split trades across multiple pools and paths to minimise slippage. Flash accounting means complex routes through many pools incur only two token transfers regardless of the number of intermediate hops, making sophisticated routing strategies economically viable.
Batched Operations
Liquidity providers can add or remove liquidity across multiple pools in a single transaction, with only the net token movements settled at the end. The approach is particularly useful for rebalancing positions across concentrated liquidity ranges.
Atomic Multi-Pool Arbitrage
Arbitrageurs can execute complex arbitrage paths across many pools within a single callback, with intermediate balances netting out. The reduced gas cost lowers the minimum profitable arbitrage, which improves price efficiency across pools.
Custom Logic via Hooks
Uniswap V4 introduced a hook system that lets developers attach custom logic to pool events before and after swaps and liquidity changes. Flash accounting makes hooks more practical by reducing the gas overhead of the additional computation they introduce.
Adoption Beyond Uniswap
Singleton contracts, flash accounting and EIP-1153 transient storage have become an emerging pattern for next-generation AMMs. Several next-generation AMMs are adopting the combination.
The convergence suggests flash accounting is becoming a standard design pattern for efficient on-chain trading. It also points beyond a protocol-specific optimisation.
Complexity of the Callback Pattern
Flash accounting requires callers to interact through an unlock/callback pattern rather than simple function calls. Developers must correctly manage deltas within the callback, ensuring every credit is matched by a debit. Bugs in delta management can cause transactions to revert unexpectedly or, in the worst case, leave tokens stuck.
Reentrancy Surface
The callback pattern introduces a reentrancy surface because the PoolManager calls external code, specifically the caller's callback, while in an unlocked state. The delta-zero invariant provides a strong safety guarantee, but custom hooks and periphery contracts must still be carefully audited for reentrancy vulnerabilities.
EVM Dependency
Flash accounting in its current form depends on EIP-1153 transient storage, which is only available on Ethereum and EVM-compatible chains that have adopted the Dencun opcodes. Chains without transient storage support would need to use regular storage, negating much of the gas benefit.
Centralization of Pool State
The singleton pattern concentrates all pool state in a single contract. The design enables flash accounting, but a critical bug in the PoolManager could affect every pool simultaneously. The factory model has a different risk profile because each pool contract is isolated.
Why It Matters
Gas costs are one of the primary barriers to using on-chain decentralised exchanges. Flash accounting addresses this directly by eliminating redundant token transfers, making complex swaps significantly cheaper. As DeFi routing becomes more sophisticated, with trades splitting across many pools and paths, the savings from flash accounting grow proportionally.
The broader ecosystem pattern demonstrates how new EVM primitives like transient storage can unlock architectural improvements that were previously impractical. Rapid adoption by multiple major protocols, including Uniswap, Balancer and PancakeSwap, signals that deferred settlement is becoming a foundational design pattern for on-chain trading infrastructure.
Bitcoin layer-2 solutions like Spark take a fundamentally different approach to transaction efficiency by moving activity off-chain entirely and avoiding on-chain gas costs altogether. Flash accounting optimises within the constraints of on-chain execution, while off-chain protocols sidestep those constraints by settling only when necessary.
The glossary entry is for informational purposes only and does not constitute financial or investment advice. You should always do your own research before using any protocol or technology.