rollup_gasPrices
Returns the current gas prices for both L1 data submission and L2 execution on Optimism Layer 2.
When to Use This Method
rollup_gasPrices
is essential for:
- Fee Estimation - Get both L1 and L2 gas price components
- Cost Analysis - Understand the breakdown of rollup fees
- Dynamic Pricing - Adjust transaction timing based on gas costs
- L1/L2 Cost Comparison - Compare relative costs of L1 vs L2 operations
Parameters
None. This method takes no parameters.
Implementation Examples
- cURL
- JavaScript
curl -X POST https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "rollup_gasPrices",
"params": [],
"id": 1
}'
// Get current rollup gas prices on Optimism
const response = await fetch('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'rollup_gasPrices',
params: [],
id: 1
})
});
const data = await response.json();
const gasPrices = data.result;
console.log('Optimism Gas Prices:');
console.log('L1 Gas Price:', parseInt(gasPrices.l1GasPrice, 16) / 1e9, 'gwei');
console.log('L2 Gas Price:', parseInt(gasPrices.l2GasPrice, 16) / 1e9, 'gwei');
// Calculate fee estimates with current prices
async function estimateFeesWithCurrentPrices(txData) {
const gasPrices = await getRollupGasPrices();
// Estimate L2 execution gas
const l2GasEstimate = await estimateGas(txData);
const l2Fee = BigInt(l2GasEstimate) * BigInt(gasPrices.l2GasPrice);
// Estimate L1 data fee (this uses L1 gas price internally)
const l1Fee = await estimateL1Fee(txData);
const totalFee = l1Fee + l2Fee;
return {
l1Fee: Number(l1Fee) / 1e18,
l2Fee: Number(l2Fee) / 1e18,
totalFee: Number(totalFee) / 1e18,
gasPrices: {
l1GasPrice: parseInt(gasPrices.l1GasPrice, 16),
l2GasPrice: parseInt(gasPrices.l2GasPrice, 16)
}
};
}
Response Example
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"l1GasPrice": "0x34630b8a00",
"l2GasPrice": "0x1dcd6500"
}
}
This shows:
- L1 Gas Price:
0x34630b8a00
(~14 gwei) - for L1 data availability - L2 Gas Price:
0x1dcd6500
(~0.5 gwei) - for L2 execution
Understanding Optimism Gas Price Structure
Dual Gas Price Model
Optimism uses a dual gas price model:
- L1 Gas Price: Cost of posting transaction data to Ethereum mainnet
- L2 Gas Price: Cost of executing transactions on the L2 network
// Monitor gas price trends on Optimism
class OptimismGasPriceMonitor {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = `https://api-optimism-mainnet-archive.n.dwellir.com/${apiKey}`;
}
async getCurrentPrices() {
const response = await fetch(this.baseUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'rollup_gasPrices',
params: [],
id: 1
})
});
const data = await response.json();
return {
l1GasPrice: parseInt(data.result.l1GasPrice, 16),
l2GasPrice: parseInt(data.result.l2GasPrice, 16),
timestamp: Date.now()
};
}
async trackPriceHistory(duration = 3600000) { // 1 hour
const history = [];
const startTime = Date.now();
while (Date.now() - startTime < duration) {
const prices = await this.getCurrentPrices();
history.push(prices);
console.log(`L1: ${(prices.l1GasPrice / 1e9).toFixed(2)} gwei, L2: ${(prices.l2GasPrice / 1e9).toFixed(2)} gwei`);
// Wait 5 minutes before next sample
await new Promise(resolve => setTimeout(resolve, 300000));
}
return history;
}
analyzePriceTrends(history) {
const l1Prices = history.map(h => h.l1GasPrice);
const l2Prices = history.map(h => h.l2GasPrice);
return {
l1Stats: {
min: Math.min(...l1Prices) / 1e9,
max: Math.max(...l1Prices) / 1e9,
avg: l1Prices.reduce((a, b) => a + b, 0) / l1Prices.length / 1e9
},
l2Stats: {
min: Math.min(...l2Prices) / 1e9,
max: Math.max(...l2Prices) / 1e9,
avg: l2Prices.reduce((a, b) => a + b, 0) / l2Prices.length / 1e9
}
};
}
}
Cost Optimization Strategies
// Optimize transaction costs based on gas prices
class OptimismCostOptimizer {
static async getOptimalTransactionTime(txData, maxWaitTime = 3600000) {
const monitor = new OptimismGasPriceMonitor();
let bestPrice = Infinity;
let bestTime = null;
const checkInterval = 300000; // 5 minutes
const checks = Math.floor(maxWaitTime / checkInterval);
for (let i = 0; i < checks; i++) {
const prices = await monitor.getCurrentPrices();
const estimatedCost = await this.estimateTotalCost(txData, prices);
if (estimatedCost < bestPrice) {
bestPrice = estimatedCost;
bestTime = new Date();
}
console.log(`Check ${i + 1}/${checks}: Cost ${estimatedCost.toFixed(6)} ETH`);
if (i < checks - 1) {
await new Promise(resolve => setTimeout(resolve, checkInterval));
}
}
return {
bestPrice: bestPrice,
bestTime: bestTime,
savingsOpportunity: bestPrice < await this.estimateTotalCost(txData)
};
}
static async estimateTotalCost(txData, gasPrices = null) {
if (!gasPrices) {
const monitor = new OptimismGasPriceMonitor();
gasPrices = await monitor.getCurrentPrices();
}
// Get L1 fee estimate
const l1Response = await fetch('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_estimateL1Fee',
params: [txData],
id: 1
})
});
const l1Fee = BigInt((await l1Response.json()).result);
// Get L2 gas estimate
const l2Response = await fetch('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_estimateGas',
params: [txData],
id: 2
})
});
const l2Gas = BigInt((await l2Response.json()).result);
const l2Fee = l2Gas * BigInt(gasPrices.l2GasPrice);
return Number(l1Fee + l2Fee) / 1e18;
}
}
Need help? Contact our support team or check the Optimism documentation.