Skip to content

Commit

Permalink
chore: Update gas report script and improve gas report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aboudjem committed May 31, 2024
1 parent cd764c2 commit a4de90f
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions scripts/foundry/generateGasReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ const fs = require('fs');
const readline = require('readline');
const { exec } = require('child_process');

// Define the log file, the output markdown file, and the previous report file
const LOG_FILE = 'gas.log';
const OUTPUT_FILE = 'GAS_REPORT.md';
const PREVIOUS_REPORT_FILE = 'gas_report.json';
const PREVIOUS_REPORT_FILE = 'previous_gas_report.json';

// Function to execute the `forge test` command
function runForgeTest() {
return new Promise((resolve, reject) => {
console.log('🚀 Running forge tests, this may take a few minutes...');
Expand All @@ -22,7 +20,6 @@ function runForgeTest() {
});
}

// Function to parse the log file and generate the report
async function generateReport() {
await runForgeTest();

Expand Down Expand Up @@ -82,18 +79,15 @@ async function generateReport() {
}
}

// Load the previous report if it exists
let previousResults = [];
if (fs.existsSync(PREVIOUS_REPORT_FILE)) {
const previousData = fs.readFileSync(PREVIOUS_REPORT_FILE, 'utf8');
previousResults = JSON.parse(previousData);
}

console.log('🔄 Sorting results...');
// Sort by NUMBER
results.sort((a, b) => a.NUMBER - b.NUMBER);

// Calculate the difference in gas usage
results.forEach(result => {
const previousResult = previousResults.find(prev => prev.NUMBER === result.NUMBER);
if (previousResult) {
Expand All @@ -106,7 +100,6 @@ async function generateReport() {
});

console.log('🖋️ Writing report...');
// Write the report
const outputStream = fs.createWriteStream(OUTPUT_FILE);
outputStream.write("# Gas Report\n");
outputStream.write("| **Protocol** | **Actions / Function** | **Account Type** | **Is Deployed** | **With Paymaster?** | **Receiver Access** | **Gas Used** | **Gas Difference** |\n");
Expand All @@ -117,21 +110,17 @@ async function generateReport() {
outputStream.write(`| ${result.PROTOCOL} | ${result.ACTION_FUNCTION} | ${result.ACCOUNT_TYPE} | ${result.IS_DEPLOYED} | ${result.WITH_PAYMASTER} | ${result.RECEIVER_ACCESS} | ${result.GAS_USED} | ${gasDiffDisplay} |\n`);
});

// Save the current results as the previous results for next time
fs.writeFileSync(PREVIOUS_REPORT_FILE, JSON.stringify(results, null, 2));

console.log(`📊 Gas report generated and saved to ${OUTPUT_FILE}`);
}

// Function to clean up temporary files
function cleanUp() {
fs.unlink(LOG_FILE, (err) => {
if (err) console.error(`❌ Error deleting ${LOG_FILE}: ${err}`);
else console.log(`🗑️ ${LOG_FILE} deleted successfully.`);
});
}

// Run the function to generate the report and then clean up
generateReport()
.then(cleanUp)
.catch(console.error);

0 comments on commit a4de90f

Please sign in to comment.