Implementing Gas Fee Calculation and Settings in Mobile Crypto Wallet
Gas fee is not just a number in the UI. It's a balance between confirmation speed and cost. A mobile wallet must offer sensible defaults and give control to those who want it.
Where to Get Current gasPrice
For legacy transactions (pre-EIP-1559) or networks without EIP-1559 support (BNB Chain by default): eth_gasPrice returns median gasPrice from recent blocks. This is the starting point.
For Ethereum Mainnet and EIP-1559 networks—eth_feeHistory(10, "latest", [10,50,90]) gives percentile-distribution of priority fee over the last 10 blocks. This is the foundation for slow/average/fast.
// Android — web3j: get feeHistory
val feeHistory = web3j.ethFeeHistory(10, DefaultBlockParameterName.LATEST, listOf(10.0, 50.0, 90.0)).send()
val baseFee = feeHistory.feeHistory.baseFeePerGas.last() // in wei
val slowPriorityFee = feeHistory.feeHistory.reward[0][0] // 10th percentile
val avgPriorityFee = feeHistory.feeHistory.reward[0][1] // 50th percentile
val fastPriorityFee = feeHistory.feeHistory.reward[0][2] // 90th percentile
For Polygon with its gas spikes, use Gas Station API: https://gasstation.polygon.technology/v2—accounts for network specifics.
Three Modes: Slow / Average / Fast
Standard scheme:
| Mode | Base Fee | Priority Fee | Expected Time |
|---|---|---|---|
| Slow | base + 0% | 10th percentile | 3–5 min |
| Average | base + 10% | 50th percentile | ~30 sec |
| Fast | base + 20% | 90th percentile | ~15 sec |
maxFeePerGas = baseFee * multiplier + priorityFee. User picks a mode—app auto-recalculates total cost in USD.
Custom Mode for Advanced Users
"Custom" mode: input fields for maxFeePerGas and maxPriorityFeePerGas in Gwei. Validation required:
-
maxPriorityFeePerGas <= maxFeePerGas -
maxFeePerGas >= current baseFee(or transaction hangs) - Warning if values too low: "Transaction may not confirm for hours"
Gwei is more convenient than Wei. Conversion: 1 Gwei = 10^9 Wei.
Estimate gasLimit
eth_estimateGas is mandatory for ERC-20 transfers and contract calls. Returns an estimate—multiply by 1.2 for safety buffer. For ETH transfer—fixed 21,000 gas.
If eth_estimateGas returns execution reverted—transaction will definitely fail. Show the error to the user before confirmation; don't allow submit.
Timeline: 2–3 days: poll feeHistory, calculate three modes, UI mode selection with USD cost display, custom mode with validation, gasLimit estimation.







