dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
212 lines • 7.58 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeploymentTracker = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const logger_1 = require("../../logsAndMetrics/core/logger");
const types_1 = require("./types");
// Helper function to get network name
function getNetworkNameFromChainId(chainId) {
const networks = {
1: 'mainnet',
5: 'goerli',
11155111: 'sepolia',
137: 'polygon',
80001: 'mumbai',
42161: 'arbitrum',
421613: 'arbitrum-goerli',
10: 'optimism',
420: 'optimism-goerli',
56: 'bsc',
97: 'bsc-testnet',
// Add more networks as needed
};
return networks[chainId] || `chain-${chainId}`;
}
class DeploymentTracker {
constructor() {
this.eventListeners = [];
this.historyPath = path.join(process.cwd(), '.dopstick', 'deployment-history.json');
this.history = this.loadHistory();
}
static getInstance() {
if (!DeploymentTracker.instance) {
DeploymentTracker.instance = new DeploymentTracker();
}
return DeploymentTracker.instance;
}
loadHistory() {
try {
if (fs.existsSync(this.historyPath)) {
const data = fs.readFileSync(this.historyPath, 'utf8');
return JSON.parse(data);
}
}
catch (error) {
logger_1.Logger.warn('Failed to load deployment history, creating new history');
}
return {
deployments: [],
lastUpdate: Date.now()
};
}
saveHistory() {
try {
const dirPath = path.dirname(this.historyPath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
fs.writeFileSync(this.historyPath, JSON.stringify(this.history, null, 2));
}
catch (error) {
logger_1.Logger.error('Failed to save deployment history:', error);
}
}
async startDeployment(contractName, constructorArgs, chainId) {
const network = {
chainId,
name: getNetworkNameFromChainId(chainId)
};
this.currentDeployment = {
contractName,
address: '',
deploymentDate: Date.now(),
network,
constructor: {
args: constructorArgs,
types: constructorArgs.map(arg => typeof arg)
},
transaction: {
hash: '',
blockNumber: 0,
gasUsed: '0',
effectiveGasPrice: '0'
},
verified: false,
events: []
};
this.emitEvent({
type: types_1.DeploymentEventType.DEPLOYMENT_STARTED,
timestamp: Date.now(),
contractName,
status: 'pending',
network
});
}
async trackDeploymentSubmitted(transactionHash) {
if (!this.currentDeployment)
return;
this.emitEvent({
type: types_1.DeploymentEventType.DEPLOYMENT_SUBMITTED,
timestamp: Date.now(),
contractName: this.currentDeployment.contractName,
transactionHash,
status: 'pending',
network: this.currentDeployment.network
});
}
async trackDeploymentConfirmed(address, receipt) {
if (!this.currentDeployment)
return;
this.currentDeployment.address = address;
this.currentDeployment.transaction = {
hash: receipt.transactionHash,
blockNumber: receipt.blockNumber,
gasUsed: receipt.gasUsed.toString(),
effectiveGasPrice: receipt.effectiveGasPrice.toString()
};
this.emitEvent({
type: types_1.DeploymentEventType.DEPLOYMENT_CONFIRMED,
timestamp: Date.now(),
contractName: this.currentDeployment.contractName,
address,
transactionHash: receipt.transactionHash,
status: 'success',
gasUsed: receipt.gasUsed.toString(),
effectiveGasPrice: receipt.effectiveGasPrice.toString(),
blockNumber: receipt.blockNumber,
network: this.currentDeployment.network
});
// Add to history
this.history.deployments.push(this.currentDeployment);
this.history.lastUpdate = Date.now();
this.saveHistory();
}
async trackDeploymentFailed(error) {
if (!this.currentDeployment)
return;
this.emitEvent({
type: types_1.DeploymentEventType.DEPLOYMENT_FAILED,
timestamp: Date.now(),
contractName: this.currentDeployment.contractName,
status: 'failed',
error: error.message,
network: this.currentDeployment.network
});
}
async trackVerification(address, success, error) {
const deployment = this.findDeployment(address);
if (!deployment)
return;
const event = {
type: success ?
types_1.DeploymentEventType.VERIFICATION_COMPLETED :
types_1.DeploymentEventType.VERIFICATION_FAILED,
timestamp: Date.now(),
contractName: deployment.contractName,
address,
status: success ? 'success' : 'failed',
error,
network: deployment.network
};
deployment.verified = success;
deployment.events.push(event);
this.saveHistory();
this.emitEvent(event);
}
addEventListener(listener) {
this.eventListeners.push(listener);
}
removeEventListener(listener) {
this.eventListeners = this.eventListeners.filter(l => l !== listener);
}
emitEvent(event) {
var _a;
(_a = this.currentDeployment) === null || _a === void 0 ? void 0 : _a.events.push(event);
this.eventListeners.forEach(listener => listener(event));
}
getDeploymentHistory() {
return this.history.deployments;
}
findDeployment(address) {
return this.history.deployments.find(d => d.address.toLowerCase() === address.toLowerCase());
}
getDeploymentsByContract(contractName) {
return this.history.deployments.filter(d => d.contractName === contractName);
}
}
exports.DeploymentTracker = DeploymentTracker;
//# sourceMappingURL=deployment-tracker.js.map