Skip to content

Instantly share code, notes, and snippets.

@0x7An
Created July 18, 2024 20:15
Show Gist options
  • Save 0x7An/45c3a2436796fc040bd7a2ac79a8640c to your computer and use it in GitHub Desktop.
Save 0x7An/45c3a2436796fc040bd7a2ac79a8640c to your computer and use it in GitHub Desktop.
transaction_costs
const axios = require('axios');
const INFURA_URL = '';
const TRANSACTION_HASHES = [
'0xa66a4c97e2a52935f83580f796928db8fce6a3f831267dc04bb07d5f778183fc',
'0x7be46bab99388776d7d4abfc5866b5fdab01d8eb93a09323c4b4427943961c6a',
'0xcbd03c8c881ca4b0c46bd6f4e80220edfb8e2a5d58b6b0d3990ef929b0f81ed0',
'0xe7ca3e7a7e838c10749ffdc885cc66757371888403460150576aa8693a60ae85',
'0xbc12ff2a4ad6193c88667b39aa5705c3613d176f0903444ede068e40c0bb34ee',
'0xffc2f213655f69295af4b4531447678ecf724af4b4f1ccfa84d1eff4a5c59226',
'0x7aeb351e16cfed36b9033f111c31f31ee7df767447c706b9d424ff33c1ebea04',
'0x1892846412887b7d5036629fdc4fc9224865e48ede03c086577bd0db6b8eefe0',
'0xd644517a8c291bdf1e0670bd56868f7ffa28d8c2aa6139493a7f2034dc3f101f',
'0x933c03e253d8a2e1afc5dddb72af0ded05c9d679638a6b8eb5b442af6abda6ca',
'0x4822be7ca4f233642bca21ceabd803ca9e9053e77b55d4d75ce89aae625ddd7e',
'0x78d9fa1c3d9c3113f0ca469b28ef6b9379f00f029db8557e936a55d988576ddd',
'0x879ad0ab70360c148e2b506df3ab706355c7b26a0d3525c947ffbd153cde1cf9',
'0x4a5751c853876229bea28e759860675f405d7581855902aca1cd2dbb4a5e6350',
'0xf14fa591373e19de6611feee2c6a61166bffa602bff9c742de655da83ead638a',
'0x5d0383a44c98da5ce25f7b0dccfad658fe9b7e3abe36794d67d2001bdb52ac47',
'0xad39d884cb0e164ec612edaccc4d70d782f93f6f2a4a287dea23a8eb72212ef9',
'0x88fb08867456e7c37c1b8374dae85fe573b4602d68bed910b57ec0c450f37ce3',
'0x43c3bbb9e549f14db0e23b0ce6294de229463e698eab39d99ac7089e25a515be',
'0xc82679483999b5f769f26608d0dec0791bba565e32a363bc28b25cac49b657d6'
];
// Conversion rate from wei to AVAX (assuming 1 Ether = 1 AVAX for simplicity)
// 1 Ether = 1e18 wei, so the conversion rate from wei to AVAX is 1e-18
const WEI_TO_AVAX_CONVERSION_RATE = 1e-18;
async function fetchTransactionReceipt(txHash) {
console.log(`Fetching receipt for transaction: ${txHash}`);
const response = await axios.post(INFURA_URL, {
jsonrpc: "2.0",
method: "eth_getTransactionReceipt",
params: [txHash],
id: 1
});
return response.data.result;
}
async function estimateTransactionCost(txHash) {
console.log(`Estimating cost for transaction: ${txHash}`);
const receipt = await fetchTransactionReceipt(txHash);
if (!receipt) {
console.log(`Receipt not found for transaction: ${txHash}`);
return null;
}
const gasUsed = parseInt(receipt.gasUsed, 16);
const response = await axios.post(INFURA_URL, {
jsonrpc: "2.0",
method: "eth_getTransactionByHash",
params: [txHash],
id: 1
});
const tx = response.data.result;
if (!tx) {
console.log(`Transaction details not found for transaction: ${txHash}`);
return null;
}
const gasPrice = parseInt(tx.gasPrice, 16);
const costInWei = gasUsed * gasPrice;
const costInAvax = costInWei * WEI_TO_AVAX_CONVERSION_RATE;
console.log(`Transaction cost: ${costInWei} wei (${costInAvax} AVAX)`);
return costInAvax;
}
async function main() {
try {
let totalCost = 0;
let count = 0;
for (const txHash of TRANSACTION_HASHES) {
const cost = await estimateTransactionCost(txHash);
if (cost !== null) {
totalCost += cost;
count++;
console.log(`Transaction Hash: ${txHash}, Cost: ${cost} AVAX`);
}
}
if (count > 0) {
const averageCost = totalCost / count;
const estimatedCostFor10kTransactions = averageCost * 10000;
console.log(`Average Transaction Cost: ${averageCost} AVAX`);
console.log(`Estimated Cost for 10,000 Transactions: ${estimatedCostFor10kTransactions} AVAX`);
} else {
console.log('No valid transactions found.');
}
} catch (error) {
console.error('Error fetching transactions:', error);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment