Skip to content

Commit

Permalink
analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
mariapiamo committed May 21, 2024
1 parent ba4f45f commit f8d4f3b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions scripts/analyzers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Code analyzer script
- [analyzeContracts.js script](https://github.com/valory-xyz/autonolas-tokenomucs/blob/main/scripts/analyzers/analyzeContracts.js) provides a report of solidity code lines for some contracts. This script can be run as follows:

```
node scripts/analyzers/analyzeContracts.js
```

87 changes: 87 additions & 0 deletions scripts/analyzers/analyzeContracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { execSync } = require("child_process");
const fs = require("fs");

// Function to get all Solidity files in a directory
function getSolidityFiles(directory) {
try {
return fs.readdirSync(directory).filter(file => file.endsWith(".sol"));
} catch (error) {
console.error(`Error reading directory ${directory}:`, error);
return [];
}
}

// Output file path
const outputFilePath = "scripts/analyzers/code_line_report.txt";

// Function to run cloc for a contract and extract code lines
function getCodeLines(contractPath) {
try {
const output = execSync(`cloc --csv --quiet --include-lang=solidity ${contractPath}`).toString();
const lines = output.split("\n");
if (lines.length >= 3) {
const codeLine = lines[2].split(",")[4]; // Assuming the code line count is in the 5th column
return parseInt(codeLine);
}
} catch (error) {
console.error(`Error running cloc for ${contractPath}:`, error);
return 0; // Return 0 in case of error
}
return 0; // Return 0 if lines.length < 3
}

// Function to write report to file
function writeReportToFile(report) {
try {
fs.writeFileSync(outputFilePath, report, "utf-8");
console.log(`Report has been written to ${outputFilePath}`);
} catch (error) {
console.error("Error writing report to file:", error);
}
}

// Main function to generate report
function generateReport() {
let report = " ".padStart(6) + "| Contract".padEnd(123) + "| CodeLine |\n";
report += "-".repeat(142) + "\n";

let totalCodeLines = 0;
let contractNumber = 1;

// Dynamically included contracts in contracts/staking/ directory
const BridgeDir = "contracts/multisigs/bridge_verifier/";
const solidityFilesInBridgeDir = getSolidityFiles(BridgeDir);
solidityFilesInBridgeDir.forEach(contractName => {
const contractPath = BridgeDir + contractName;
const codeLines = getCodeLines(contractPath);
report += `${contractNumber.toString().padStart(5)} | ${contractName.padEnd(120)} | ${codeLines.toString().padStart(10)} |\n`;
totalCodeLines += codeLines;
contractNumber++;
});

// Manually specified contracts in contracts/ directory
const contractsInContractsDir = [
"multisigs/GuardCM.sol",
"multisigs/VerifyData.sol",
"VoteWeighting.sol"

];

contractsInContractsDir.forEach(contractName => {
const contractPath = "contracts/" + contractName;
const codeLines = getCodeLines(contractPath);
report += `${contractNumber.toString().padStart(5)} | ${contractPath.padEnd(120)} | ${codeLines.toString().padStart(10)} |\n`;
totalCodeLines += codeLines;
contractNumber++;
});

//Add separator
report += "-".repeat(142) + "\n";
// Add a row for all tokenomics contracts
report += `${(contractNumber - 1).toString().padStart(5)} | All Governance contracts`.padEnd(128) + ` | ${totalCodeLines.toString().padStart(10)} |\n`;

writeReportToFile(report);
}

// Generate report
generateReport();
13 changes: 13 additions & 0 deletions scripts/analyzers/code_line_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
| Contract | CodeLine |
----------------------------------------------------------------------------------------------------------------------------------------------
1 | ProcessBridgedDataArbitrum.sol | 30 |
2 | ProcessBridgedDataGnosis.sol | 42 |
3 | ProcessBridgedDataOptimism.sol | 42 |
4 | ProcessBridgedDataPolygon.sol | 32 |
5 | ProcessBridgedDataWormhole.sol | 41 |
6 | VerifyBridgedData.sol | 36 |
7 | contracts/multisigs/GuardCM.sol | 253 |
8 | contracts/multisigs/VerifyData.sol | 14 |
9 | contracts/VoteWeighting.sol | 422 |
----------------------------------------------------------------------------------------------------------------------------------------------
9 | All Governance contracts | 912 |

0 comments on commit f8d4f3b

Please sign in to comment.