Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make additional checker tools #279

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ Feel free to use public RPCs but if you want extra security and speed, feel free
- **Network**: Configure network name
- **Script**: Configure script name and path

#### Tasks

To run hardhat task put in CLI

npx hardhat (hh) contracts --target main
hh compare --source main --target test

There are 4 tasks currently copyBatch, signatures, contracts and compare

## Releasing

To release a new rc version, tag the commit with the `-rcX` suffix, where `X` is the release candidate number.
Expand Down
93 changes: 93 additions & 0 deletions tasks/compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { task } from 'hardhat/config';
import fs from 'fs';
import path from 'path';

interface NetworkDeployment {
chainId: number;
contracts: {
[key: string]: {
bytecode: string;
address: string;
};
};
}

task('compare', 'Compare bytecodes between two deployments')
.addParam('source', 'Source network (main, test, local, pretestnet, tenderly)')
.addParam('target', 'Target network to compare against')
.setAction(async (taskArgs: { source: string; target: string }) => {
const sourceType = taskArgs.source.toLowerCase();
const targetType = taskArgs.target.toLowerCase();

const validNetworks = ['main', 'test', 'local', 'pretestnet', 'tenderly'];
if (!validNetworks.includes(sourceType) || !validNetworks.includes(targetType)) {
throw new Error('Network parameters must be one of: ' + validNetworks.join(', '));
}

const fileNames = {
main: 'mainnet_deployed.json',
test: 'testnet_deployed.json',
local: 'localhost_deployed.json',
pretestnet: 'pretestnet_deployed.json',
tenderly: 'tenderly_deployed.json',
};

const sourceFile = fileNames[sourceType as keyof typeof fileNames];
const targetFile = fileNames[targetType as keyof typeof fileNames];

try {
// Read deployment files
const sourceData: NetworkDeployment = JSON.parse(fs.readFileSync(path.join(__dirname, '..', sourceFile), 'utf8'));
const targetData: NetworkDeployment = JSON.parse(fs.readFileSync(path.join(__dirname, '..', targetFile), 'utf8'));

console.log(`\nComparing bytecodes between ${sourceType} and ${targetType}`);
console.log('=============================================');

// Compare bytecodes for each contract
Object.keys(sourceData.contracts).forEach((contractName) => {
if (targetData.contracts[contractName]) {
const sourceBytecode = sourceData.contracts[contractName].bytecode;
const targetBytecode = targetData.contracts[contractName].bytecode;

console.log(`\nContract: ${contractName}`);

if (sourceBytecode === targetBytecode) {
console.log('Bytecodes are identical');
} else {
// Find the first differing character position
let firstDiff = -1;
for (let i = 0; i < Math.max(sourceBytecode.length, targetBytecode.length); i++) {
if (sourceBytecode[i] !== targetBytecode[i]) {
firstDiff = i;
break;
}
}

console.log(`Bytecodes differ at position: ${firstDiff}`);
if (firstDiff !== -1) {
// Show a snippet around the difference
const start = Math.max(0, firstDiff - 10);
const end = firstDiff + 10;

console.log('\nSource bytecode snippet:');
console.log(sourceBytecode.slice(start, end));
console.log(' ^');
console.log('Target bytecode snippet:');
console.log(targetBytecode.slice(start, end));
console.log(' ^');
}

console.log(`\nSource length: ${sourceBytecode.length}`);
console.log(`Target length: ${targetBytecode.length}`);
}
} else {
console.log(`\nContract ${contractName} not found in ${targetType} deployment`);
}
});
} catch (error) {
if (error instanceof Error) {
console.error('Error comparing bytecodes:');
console.error(error.message);
}
}
});
47 changes: 47 additions & 0 deletions tasks/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { task } from 'hardhat/config';
import fs from 'fs';
import path from 'path';

task('contracts', 'Display contract deployment information')
.addParam('target', 'Network type (main, test, local, pretestnet, tenderly)')
.setAction(async (taskArgs: { target: string }) => {
const networkType = taskArgs.target.toLowerCase();

const validNetworks = ['main', 'test', 'local', 'pretestnet', 'tenderly'];
if (!validNetworks.includes(networkType)) {
throw new Error('Network parameter must be one of: ' + validNetworks.join(', '));
}

const fileNames = {
main: 'mainnet_deployed.json',
test: 'testnet_deployed.json',
local: 'localhost_deployed.json',
pretestnet: 'pretestnet_deployed.json',
tenderly: 'tenderly_deployed.json',
};

const fileName = fileNames[networkType as keyof typeof fileNames];

// Read JSON file
const filePath = path.join(__dirname, '..', fileName);

try {
const fileContent = fs.readFileSync(filePath, 'utf8');
const deploymentData = JSON.parse(fileContent);

// Extract and display contract information
console.log(`\nDeployed Contracts (${networkType})`);
console.log('========================');

Object.entries(deploymentData.contracts).forEach(([name, data]: [string, any]) => {
console.log(`\n${name}:`);
console.log(`Address: ${data.address}`);
console.log(`Explorer: ${data.url}`);
});
} catch (error) {
if (error instanceof Error) {
console.error(`Error: Could not read or parse ${fileName}`);
console.error(`Details: ${error.message}`);
}
}
});
2 changes: 2 additions & 0 deletions tasks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './signatures';
export * from './copybatch';
export * from './contracts';
export * from './compare';
Loading