Gas Management
Strategies for managing gas costs and choosing who pays for blockchain transactions.
Who Pays for Gas?
One of the key decisions in your integration is who pays for blockchain transaction fees (gas).
User Pays
User submits transaction with their wallet
- ✓ No cost to you
- Requires user wallet
- Higher UX friction
You Pay (Meta-Tx)
User signs message, you submit transaction
- ✓ Better UX
- ✓ User still verifiable
- You pay gas
You Pay (Backend)
Backend signs and submits everything
- ✓ Simplest UX
- ✓ No wallet needed
- You pay gas
Gas Costs on Nexera
Nexera is an EVM-compatible blockchain optimized for low gas costs.
Cost Estimate
~150,000 gas~1 gwei~$0.001 USDMonthly Cost Estimates
| Prompts/Month | Transactions | Est. Gas Cost |
|---|---|---|
| 1,000 | 1,000 | ~$1 USD |
| 10,000 | 10,000 | ~$10 USD |
| 100,000 | 100,000 | ~$100 USD |
| 1,000,000 | 1,000,000 | ~$1,000 USD |
Note: These are estimates. Actual costs depend on network congestion and gas prices at the time of transaction.
Sponsored Transactions (You Pay Gas)
If you're paying for gas, you need to manage a backend wallet with sufficient funds.
Setup
# Backend wallet private key (keep secret!)
PRIVATE_KEY=0x...
# RPC URL
RPC_URL=https://rpc.nexera.org
# Monitor this wallet's ETH balance
# Refill when balance drops below thresholdAuto-Refill Strategy
import { ethers } from 'ethers'
// Check balance periodically
async function monitorGasWallet() {
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL)
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider)
const balance = await provider.getBalance(wallet.address)
const balanceInEth = ethers.formatEther(balance)
const threshold = 0.1 // Refill when < 0.1 ETH
if (parseFloat(balanceInEth) < threshold) {
// Send alert
await sendAlert({
type: 'LOW_GAS_BALANCE',
wallet: wallet.address,
balance: balanceInEth,
threshold,
})
// Optionally: Auto-refill from treasury wallet
await refillGasWallet(wallet.address, 1.0) // Add 1 ETH
}
}
// Run every hour
setInterval(monitorGasWallet, 60 * 60 * 1000)Manual Refill
- Get wallet address from env
- Transfer ETH from your main wallet
- Monitor balance in dashboard
Auto Refill
- Set up treasury hot wallet
- Monitor gas wallet balance
- Auto-transfer when below threshold
Gas Optimization Strategies
1. Batch Transactions
If minting multiple prompts, batch them into a single transaction.
// Instead of:
for (const prompt of prompts) {
await contract.mint(...) // Separate tx for each
}
// Do this:
await contract.batchMint([
{ hash: hash1, author: author1, points: 25 },
{ hash: hash2, author: author2, points: 30 },
{ hash: hash3, author: author3, points: 20 },
]) // Single transaction2. Gas Price Management
Set appropriate gas prices based on urgency.
// Low priority (save costs)
const lowPriorityTx = await contract.mint(..., {
gasPrice: ethers.parseUnits('1', 'gwei'), // Low gas price
})
// High priority (fast confirmation)
const highPriorityTx = await contract.mint(..., {
gasPrice: ethers.parseUnits('5', 'gwei'), // Higher gas price
})3. Queue Non-Urgent Transactions
Accumulate prompts and submit during off-peak hours when gas is cheaper.
// Queue prompts
const queue: PendingMint[] = []
app.post('/api/prompts/mint', async (req, res) => {
const mintData = prepareMintData(req.body)
queue.push(mintData)
res.json({ success: true, queued: true })
})
// Process queue every hour or when queue size reaches threshold
setInterval(async () => {
if (queue.length === 0) return
const batch = queue.splice(0, 50) // Process 50 at a time
await contract.batchMint(batch)
}, 60 * 60 * 1000) // Every hourGas Estimation
Estimate gas costs before submitting transactions.
import { ethers } from 'ethers'
async function estimateGasCost(
promptHash: string,
author: string,
activityPoints: number
) {
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL)
const contract = new ethers.Contract(
process.env.PROMPT_MINER_ADDRESS!,
ABI,
provider
)
// Estimate gas units
const gasEstimate = await contract.mint.estimateGas(
promptHash,
author,
activityPoints,
nonce,
signature
)
// Get current gas price
const feeData = await provider.getFeeData()
const gasPrice = feeData.gasPrice || ethers.parseUnits('1', 'gwei')
// Calculate cost in ETH
const gasCostWei = gasEstimate * gasPrice
const gasCostEth = ethers.formatEther(gasCostWei)
// Get ETH price (from API or oracle)
const ethPriceUSD = await getEthPrice() // e.g., 2000
const gasCostUSD = parseFloat(gasCostEth) * ethPriceUSD
return {
gasUnits: gasEstimate.toString(),
gasPrice: ethers.formatUnits(gasPrice, 'gwei') + ' gwei',
costEth: gasCostEth,
costUSD: gasCostUSD.toFixed(4),
}
}
// Example output:
// {
// gasUnits: "150000",
// gasPrice: "1 gwei",
// costEth: "0.00015",
// costUSD: "0.30"
// }Monitoring & Alerts
Set up monitoring to track gas spending and prevent unexpected costs.
Metrics to Track
- • Gas wallet ETH balance
- • Daily/weekly/monthly gas expenditure
- • Average gas cost per transaction
- • Failed transactions (wasted gas)
- • Gas price trends
Alert Thresholds
- ⚠️ Balance below 0.1 ETH (refill soon)
- 🔴 Balance below 0.01 ETH (urgent refill)
- ⚠️ Daily gas spend exceeds budget
- ⚠️ Failed transaction rate > 5%
Best Practice: Set up alerts via email/Slack/PagerDuty when gas wallet balance is low. Running out of gas means your users can't mint prompts.
Testnet vs Mainnet
Nexera Testnet
- • Chain ID: 7357
- • RPC: https://rpc.testnet.nexera.org
- • Free test ETH from faucet
- • Use for development & testing
# Testnet config
CHAIN_ID=7357
RPC_URL=https://rpc.testnet.nexera.orgNexera Mainnet
- • Chain ID: 7358
- • RPC: https://rpc.nexera.org
- • Real ETH costs
- • Use for production
# Mainnet config
CHAIN_ID=7358
RPC_URL=https://rpc.nexera.orgAlways test thoroughly on testnet before deploying to mainnet. Testnet ETH is free, so you can experiment without cost.