dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
128 lines • 5.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeploymentCacheManager = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const logger_1 = require("./logsAndMetrics/core/logger");
const provider_1 = require("./provider");
class DeploymentCacheManager {
constructor(config) {
var _a, _b;
this.config = config;
// Use the cache path from config, or default to '.dopstick-cache'
const cachePath = ((_a = config.paths) === null || _a === void 0 ? void 0 : _a.cache) || '.dopstick-cache';
// Get network for network-specific caching
const network = process.env.NETWORK || 'testnet';
// Create a network-specific cache file path
this.cacheFile = path_1.default.join(process.cwd(), cachePath, `diamond-deployment-${network}.json`);
// Convert DopStick config to cache config
this.cacheConfig = {
network: process.env.NETWORK,
rpcUrl: process.env.RPC_URL,
paths: {
cache: cachePath // Use the same cache path
}
};
logger_1.Logger.debug('Cache configuration:', {
cachePath,
network,
cacheFile: this.cacheFile,
configPath: (_b = config.paths) === null || _b === void 0 ? void 0 : _b.cache
});
}
async save(cache) {
var _a;
try {
// Ensure the cache is enabled
if (((_a = this.config.cache) === null || _a === void 0 ? void 0 : _a.enabled) === false) {
logger_1.Logger.debug('Cache is disabled, skipping save operation');
return;
}
// Ensure cache directory exists
const cacheDir = path_1.default.dirname(this.cacheFile);
await promises_1.default.mkdir(cacheDir, { recursive: true });
// Save cache with pretty formatting
await promises_1.default.writeFile(this.cacheFile, JSON.stringify(cache, null, 2), 'utf8');
logger_1.Logger.debug(`Deployment cache saved to ${this.cacheFile}`);
}
catch (error) {
logger_1.Logger.error(`Failed to save deployment cache: ${error}`);
throw error;
}
}
async load() {
var _a;
try {
// Check if cache is enabled
if (((_a = this.config.cache) === null || _a === void 0 ? void 0 : _a.enabled) === false) {
logger_1.Logger.debug('Cache is disabled, skipping load operation');
return null;
}
const exists = await this.exists();
if (!exists) {
logger_1.Logger.debug('No existing cache file found');
return null;
}
const cacheData = await promises_1.default.readFile(this.cacheFile, 'utf8');
logger_1.Logger.debug(`Cache loaded from ${this.cacheFile}`);
return JSON.parse(cacheData);
}
catch (error) {
logger_1.Logger.error(`Failed to load deployment cache: ${error}`);
throw error;
}
}
async exists() {
var _a;
try {
// Check if cache is enabled first
if (((_a = this.config.cache) === null || _a === void 0 ? void 0 : _a.enabled) === false) {
return false;
}
await promises_1.default.access(this.cacheFile);
return true;
}
catch (_b) {
return false;
}
}
async clear() {
try {
if (await this.exists()) {
await promises_1.default.unlink(this.cacheFile);
logger_1.Logger.info(`Deployment cache cleared: ${this.cacheFile}`);
}
}
catch (error) {
logger_1.Logger.error(`Failed to clear deployment cache: ${error}`);
throw error;
}
}
async isValid(cache) {
const MAX_CACHE_AGE = 24 * 60 * 60 * 1000; // 24 hours
const now = Date.now();
if (now - cache.timestamp > MAX_CACHE_AGE) {
logger_1.Logger.warning('Cache is older than 24 hours');
return false;
}
const provider = await (0, provider_1.getProviderForCacheConfig)(this.cacheConfig);
const network = await provider.getNetwork();
if (cache.chainId !== network.chainId) {
logger_1.Logger.warning('Cache is from different network');
return false;
}
return true;
}
async updateConstructorArgs(args) {
const cache = await this.load();
if (cache) {
cache.constructorArgs = args;
await this.save(cache);
}
}
}
exports.DeploymentCacheManager = DeploymentCacheManager;
//# sourceMappingURL=cacheManager.js.map