Gas Network is a decentralized ecosystem designed to unify multi-chain gas data and simplify fee management across every blockchain. Our core vision is to empower end users, wallets, and protocols with near-instant, accurate gas price data—bringing transparent, cost-effective “gas-aware” functionality to the entire Web3 world.
This post will demonstrate how Gas Network's onchain gas oracle can be used to optimize swap fees.
We'll first review how a swap's profit is calculated and then demonstrate how integrating Gas Network with Uniswap V4 hooks could improve profits for swaps and increase fees for pools.
At a high-level the profit for a swap is:
The above equation shows the direct impact of gas fees on profit, but doesn't demonstrate the impact of slippage and swap fees. Let's expand Qin and Qout to account for these, taking fees on the input token amount, Qin.
From equations (1), (2) and (3), we see fee_gas, fee_swap, and slippage all eat into a swap's profit.
Higher fee_gas decreases swap profit.
Higher fee_swap decreases swap profit.
Higher slippage(decreased liquidity) decreases swap profit.
When slippage and fees are too high, the profit is completely eliminated and the swap will not happen at all. This creates higher spreads and less revenue for arbitrageurs, builders and validators.
Selectively reducing swap fees could ensure swaps are still profitable under high gas price or slippage conditions. This can keep spreads tighter under volatile market conditions, something possibly of high interest to stablecoin providers, for example.
Another way to think about it: Reducing the swap fee effectively lowers the gas price and liquidity thresholds for which arbitrage swaps will happen. This allows swaps that wouldn't have previously happened to occur, theoretically increasing overall LP revenue.
With Uniswap V4, a pool can now set dynamic fees using hooks. These hooks can execute arbitrary code before and after a swap.
With Uniswap V4 hooks, a pool can adapt its fees to current liquidity and gas price conditions!
Liquidity amounts are already available onchain, so we'll show how a hook can also access the current gas price to make swap fee decisions. Specifically, we want this hook to know the minimum marketable gas price, as this is the threshold for whether a swap will occur. Swaps priced below this value are unable to occur and the spread will not be closed,
tx.gasprice
The EVM can access the effective gas price of a tx with tx.gasprice
. However this will not necessarily be the minimum marketable gas price. Reducing swap fees based off a tx's gas price could subsidize the swap when it's not necessary. A malicious tx originator could even set a high gas price to induce large swap fee reduction and reduce LP revenue. We want the hook to adapt to the current gas market(the minimum price to get included) and not refund over-priced txs.
Also, most arbitrage swaps will be MEV and pay the builder through a coinbase
transfer and not priority fees. In this case, tx.gasprice
will be equal to block.basefee
.
block.basefee
In the EVM there is already block.basefee
that represents the minimum, network-enforced price for a tx to be included.
But, there are some scenarios where block.basefee
will be inadequate to optimize the swap fee.
Remember under EIP-1559,
High priority fees can happens in periods of congestion, making block.basefee
less helpful for optimizing swap fees.
On some L2s, the priority fee can take up a significant portion of gas price in periods of non-congestion. Many L2s have pricing mechanisms that raise the minimum priority fee when L1 data costs are high, even if there is no congestion on the L2.
block.basefee
is inconsistent on L2sSome L2s use partial implementations of EIP-1559 or have custom fee pricing mechanisms. This results in block.basefee
having an insignificant, placeholder value that isn't very helpful in predicting total gas price.
Gas Network provides onchain access to Blocknative's proven minimum marketable gas price predictions. A hook can get the latest minimum market price, by integration with a Gas Network oracle contract.
To demonstrate how a Uniswap V4 pool creator can integrate a hook with Gas Network to optimize swap fees, we've created a sample pre-swap hook.
Uniswap V4 pre-swap hooks execute code in a beforeSwap
function, which is shown below. The entire hook source code can be found here.
This hook:
sets gasPriceThreshold
for lowering the swap fee and the resulting reducedFee
.
In practice, these values will be calculated dynamically and not constant! This code is just to demonstrate the capability and not provide optimal calculations.
queries Gas Network for the current gas price.
Note: This is curently two calls but we will potentially consolidate this to a single gas price call.
finally, reduces the swap fee if gas price is over a certain threshold.
This reduced swap fee will make swaps profitable, reducing spreads and improving prices.
Further work can be done to formulate the explicit profit function and exactly calculate the swap fee threshold that creates profitability, given slippage, gas price, hook gas usage, and swap amounts.
This work was inspired with a stable/stable pair in mind, where each token has a fixed par value of 1 and profit calculation is straightforward. Expanding it to other pairs would need some additional oracles or pools to calculate profit.
Oracle gas price performance needs to be measured with this use case in mind. There will be a LP revenue tradeoff between additional fees from previously unprofitable swaps and reduced fees due to overprediction of gas price.
We're interested to see what other uses cases people come up with to leverage the gas price oracles of Gas Network!
Blocknative Sample GasHook
Uniswap V4 Hooks
Uniswap V4 hook template
24/7.....Indonesia Jakarta Hoding Company 08975895112