From da017e670577db1767e4f55bb1ef25fecc2ca65d Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 4 Dec 2024 16:16:32 +0100 Subject: [PATCH] add rme info --- README.md | 9 +++++ tasks/compare.ts | 93 ++++++++++++++++++++++++++++++++++++++++++++++ tasks/contracts.ts | 49 ++++++++++++++++-------- tasks/index.ts | 1 + 4 files changed, 136 insertions(+), 16 deletions(-) create mode 100644 tasks/compare.ts diff --git a/README.md b/README.md index e9005079..adcda8ac 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tasks/compare.ts b/tasks/compare.ts new file mode 100644 index 00000000..80399a1f --- /dev/null +++ b/tasks/compare.ts @@ -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); + } + } + }); diff --git a/tasks/contracts.ts b/tasks/contracts.ts index 804ae256..45aa0cec 100644 --- a/tasks/contracts.ts +++ b/tasks/contracts.ts @@ -3,28 +3,45 @@ import fs from 'fs'; import path from 'path'; task('contracts', 'Display contract deployment information') - .addParam('target', 'Network type (main or test)') + .addParam('target', 'Network type (main, test, local, pretestnet, tenderly)') .setAction(async (taskArgs: { target: string }) => { const networkType = taskArgs.target.toLowerCase(); - if (networkType !== 'main' && networkType !== 'test') { - throw new Error('Network parameter must be either "main" or "test"'); + const validNetworks = ['main', 'test', 'local', 'pretestnet', 'tenderly']; + if (!validNetworks.includes(networkType)) { + throw new Error('Network parameter must be one of: ' + validNetworks.join(', ')); } - const fileName = networkType === 'main' ? 'mainnet_deployed.json' : 'testnet_deployed.json'; + 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); - 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}`); - }); + + 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}`); + } + } }); diff --git a/tasks/index.ts b/tasks/index.ts index f8d7b03b..d1fb45c6 100644 --- a/tasks/index.ts +++ b/tasks/index.ts @@ -1,3 +1,4 @@ export * from './signatures'; export * from './copybatch'; export * from './contracts'; +export * from './compare';