@patchworkdev/pdk
Version:
Patchwork Development Kit
71 lines (70 loc) • 3.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeploymentManager = void 0;
const block_number_1 = require("../services/block-number");
class DeploymentManager {
lockFileManager;
constructor(lockFileManager) {
this.lockFileManager = lockFileManager;
}
async compareWithPreviousDeployment(network, newBytecode) {
const changes = [];
let needsDeployment = false;
if (network === 'local') {
needsDeployment = true;
Object.entries(newBytecode).forEach(([contract, info]) => {
changes.push({ contract, newHash: info.bytecodeHash });
});
return { needsDeployment, changes };
}
for (const [contract, info] of Object.entries(newBytecode)) {
const lastDeployment = this.lockFileManager.getLatestDeploymentForContract(contract, network);
const oldHash = lastDeployment?.hash;
if (!lastDeployment || oldHash !== info.bytecodeHash) {
needsDeployment = true;
changes.push({ contract, oldHash, newHash: info.bytecodeHash });
}
}
return { needsDeployment, changes };
}
logBytecodeChanges(comparison, network) {
console.info('\nBytecode Changes Detected:');
console.info('═══════════════════════════════════════════════════════════');
comparison.changes.forEach((change) => {
if (network === 'local') {
console.info(`${change.contract}: Local network - will redeploy`);
}
else if (!change.oldHash) {
console.info(`${change.contract}: New contract - needs deployment`);
}
else {
console.info(`${change.contract}: Bytecode changed`);
console.info(` Previous: ${change.oldHash}`);
console.info(` Current: ${change.newHash}`);
}
});
console.info('═══════════════════════════════════════════════════════════\n');
}
async logDeployments(deployedContracts, network, rpcUrl) {
const blockNumberService = new block_number_1.BlockNumberService();
const blockNumber = await blockNumberService.getDeploymentBlockNumber(rpcUrl);
for (const [contractName, deploymentInfo] of Object.entries(deployedContracts)) {
this.lockFileManager.logDeployment(contractName, deploymentInfo.bytecodeHash, deploymentInfo.deployedAddress, network, new Date().toISOString(), Number(blockNumber));
}
}
getExistingDeployments(bytecodeInfo, network) {
console.info('No bytecode changes detected. Skipping deployment.');
return Object.fromEntries(Object.keys(bytecodeInfo).map((contract) => {
const lastDeployment = this.lockFileManager.getLatestDeploymentForContract(contract, network);
return [
contract,
{
deployedAddress: lastDeployment.address,
bytecodeHash: lastDeployment.hash,
deploymentBlock: lastDeployment.block,
},
];
}));
}
}
exports.DeploymentManager = DeploymentManager;