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

Average gas per mint:~150,000 gas
Nexera gas price:~1 gwei
Cost per transaction:~$0.001 USD

Monthly Cost Estimates

Prompts/MonthTransactionsEst. Gas Cost
1,0001,000~$1 USD
10,00010,000~$10 USD
100,000100,000~$100 USD
1,000,0001,000,000~$1,000 USD

Note: These are estimates. Actual costs depend on network congestion and gas prices at the time of transaction.

Gas Optimization Strategies

1. Batch Transactions

If minting multiple prompts, batch them into a single transaction.

typescript
// 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 transaction

2. Gas Price Management

Set appropriate gas prices based on urgency.

typescript
// 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.

typescript
// 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 hour

Gas Estimation

Estimate gas costs before submitting transactions.

typescript
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
typescript
# Testnet config
CHAIN_ID=7357
RPC_URL=https://rpc.testnet.nexera.org

Nexera Mainnet

  • • Chain ID: 7358
  • • RPC: https://rpc.nexera.org
  • • Real ETH costs
  • • Use for production
typescript
# Mainnet config
CHAIN_ID=7358
RPC_URL=https://rpc.nexera.org

Always test thoroughly on testnet before deploying to mainnet. Testnet ETH is free, so you can experiment without cost.