eth_estimateL1Fee
Estimates the L1 data fee that would be paid for a given transaction on Optimism Layer 2. This is unique to Optimism's rollup architecture.
When to Use This Method
eth_estimateL1Fee
is essential for:
- Complete Fee Estimation - Calculate total transaction cost (L1 + L2 fees)
- Cost Optimization - Compare different transaction approaches
- User Experience - Show accurate fee estimates to users
- Rollup Economics - Understand L1 data costs for your application
Parameters
- Transaction Object
from
- Sender address (optional)to
- Destination addressgas
- Gas limit (optional)gasPrice
- Gas price (optional)value
- Value in wei (optional)data
- Transaction data (optional)
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": "eth_estimateL1Fee",
"params": [{
"from": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
"to": "0x4200000000000000000000000000000000000006",
"value": "0xde0b6b3a7640000",
"data": "0xa9059cbb0000000000000000000000008ba1f109551bd432803012645hac136c0000000000000000000000000000000000000000000000000de0b6b3a7640000"
}],
"id": 1
}'
// Estimate L1 fee for WETH transfer 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: 'eth_estimateL1Fee',
params: [{
from: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
to: "0x4200000000000000000000000000000000000006", // WETH on Optimism
value: "0x0",
data: "0xa9059cbb0000000000000000000000008ba1f109551bd432803012645hac136c0000000000000000000000000000000000000000000000000de0b6b3a7640000"
}],
id: 1
})
});
const data = await response.json();
const l1Fee = BigInt(data.result);
console.log('L1 data fee:', Number(l1Fee) / 1e18, 'ETH');
// Calculate total transaction cost
async function calculateTotalFee(txObject) {
// Get L2 execution fee
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: [txObject],
id: 2
})
});
const l2Data = await l2Response.json();
const l2Gas = BigInt(l2Data.result);
// Get gas price
const priceResponse = 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_gasPrice',
params: [],
id: 3
})
});
const priceData = await priceResponse.json();
const gasPrice = BigInt(priceData.result);
const l2Fee = l2Gas * gasPrice;
const totalFee = l1Fee + l2Fee;
return {
l1Fee: Number(l1Fee) / 1e18,
l2Fee: Number(l2Fee) / 1e18,
totalFee: Number(totalFee) / 1e18,
breakdown: {
l1Percentage: (Number(l1Fee) / Number(totalFee) * 100).toFixed(2),
l2Percentage: (Number(l2Fee) / Number(totalFee) * 100).toFixed(2)
}
};
}
Response Example
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x2386f26fc10000"
}
This represents the L1 data fee in wei. For a typical ERC-20 transfer, this might be around 0.001 ETH.
Understanding Optimism Fee Structure
Total Transaction Cost = L1 Data Fee + L2 Execution Fee
// Complete fee breakdown for Optimism transactions
async function getOptimismFeeBreakdown(txObject) {
// L1 data fee (varies based on calldata size and L1 gas price)
const l1FeeResponse = 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: [txObject],
id: 1
})
});
const l1Fee = BigInt((await l1FeeResponse.json()).result);
// L2 execution fee (much cheaper than Ethereum mainnet)
const l2GasResponse = 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: [txObject],
id: 2
})
});
const l2Gas = BigInt((await l2GasResponse.json()).result);
const gasPriceResponse = 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_gasPrice',
params: [],
id: 3
})
});
const gasPrice = BigInt((await gasPriceResponse.json()).result);
const l2Fee = l2Gas * gasPrice;
console.log('Optimism Fee Breakdown:');
console.log('L1 Data Fee:', Number(l1Fee) / 1e18, 'ETH');
console.log('L2 Execution Fee:', Number(l2Fee) / 1e18, 'ETH');
console.log('Total Fee:', Number(l1Fee + l2Fee) / 1e18, 'ETH');
console.log('L1 Fee %:', (Number(l1Fee) / Number(l1Fee + l2Fee) * 100).toFixed(1) + '%');
return {
l1Fee: l1Fee,
l2Fee: l2Fee,
totalFee: l1Fee + l2Fee
};
}
Optimizing L1 Fees
Reduce Calldata Size
// Techniques to minimize L1 data fees
class OptimismFeeOptimizer {
// Use shorter function selectors when possible
static optimizeCalldata(data) {
// Remove unnecessary zero bytes
// Use packed structs
// Minimize string lengths
return data;
}
// Batch multiple operations to amortize L1 costs
static async batchOperations(operations) {
// Bundle multiple calls into single transaction
const batchCalldata = this.encodeBatch(operations);
return {
to: BATCH_CONTRACT_ADDRESS,
data: batchCalldata
};
}
// Compare costs of different transaction approaches
static async compareFees(txOptions) {
const fees = [];
for (const tx of txOptions) {
const l1Fee = await this.estimateL1Fee(tx);
fees.push({
transaction: tx,
l1Fee: l1Fee,
description: tx.description
});
}
return fees.sort((a, b) => Number(a.l1Fee - b.l1Fee));
}
}
Need help? Contact our support team or check the Optimism documentation.