@fil-b/filfox-verifier
Version:
CLI tool for verifying smart contracts on Filfox explorer from hardhat and foundry projects
139 lines • 5.55 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FoundryProject = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const glob_1 = require("glob");
class FoundryProject {
constructor(rootPath = process.cwd()) {
this.rootPath = path.resolve(rootPath);
this.foundryTomlPath = path.join(this.rootPath, "foundry.toml");
this.srcPath = path.join(this.rootPath, "src");
}
async initialize() {
if (!fs.existsSync(this.foundryTomlPath)) {
throw new Error(`foundry.toml not found at ${this.foundryTomlPath}. Make sure you're in a Foundry project.`);
}
if (!fs.existsSync(this.srcPath)) {
throw new Error(`src directory not found at ${this.srcPath}`);
}
}
async compile(contractPath, contractName) {
const fullContractPath = path.resolve(this.rootPath, contractPath);
if (!fs.existsSync(fullContractPath)) {
throw new Error(`Contract file not found: ${fullContractPath}`);
}
const artifactPath = path.join(this.rootPath, "out", path.basename(contractPath), `${contractName}.json`);
try {
(0, child_process_1.execSync)("forge build", {
cwd: this.rootPath,
stdio: "pipe"
});
}
catch (error) {
throw new Error(`Compilation failed: ${error.message}`);
}
if (!fs.existsSync(artifactPath)) {
throw new Error(`Artifact not found after compilation: ${artifactPath}`);
}
const artifactContent = fs.readFileSync(artifactPath, "utf8");
let artifact;
try {
artifact = JSON.parse(artifactContent);
}
catch (error) {
throw new Error(`Failed to parse artifact JSON: ${error.message}`);
}
if (!artifact.metadata) {
throw new Error(`No metadata found in artifact for ${contractName}`);
}
let metadata;
if (typeof artifact.metadata === 'string') {
try {
metadata = JSON.parse(artifact.metadata);
}
catch (error) {
throw new Error(`Failed to parse metadata JSON: ${error.message}`);
}
}
else {
metadata = artifact.metadata;
}
const sourceFiles = await this.collectSourceFiles(fullContractPath, metadata);
return {
metadata,
sourceFiles
};
}
async collectSourceFiles(mainContractPath, metadata) {
const sourceFiles = {};
const sourceUnit = metadata.sources || {};
for (const sourcePath of Object.keys(sourceUnit)) {
let fullPath;
if (path.isAbsolute(sourcePath)) {
fullPath = sourcePath;
}
else {
fullPath = path.resolve(this.rootPath, sourcePath);
}
if (!fs.existsSync(fullPath)) {
const libPath = path.join(this.rootPath, "lib");
if (fs.existsSync(libPath)) {
const possiblePaths = await (0, glob_1.glob)(`${libPath}/**/${sourcePath}`, { absolute: true });
if (possiblePaths.length > 0) {
fullPath = possiblePaths[0];
}
}
}
if (fs.existsSync(fullPath)) {
const relativePath = path.relative(this.rootPath, fullPath);
const content = fs.readFileSync(fullPath, "utf8");
sourceFiles[relativePath] = content;
}
else {
console.warn(`Warning: Source file not found: ${sourcePath}`);
}
}
const mainContractRelative = path.relative(this.rootPath, mainContractPath);
if (!sourceFiles[mainContractRelative]) {
sourceFiles[mainContractRelative] = fs.readFileSync(mainContractPath, "utf8");
}
return sourceFiles;
}
}
exports.FoundryProject = FoundryProject;
//# sourceMappingURL=FoundryProject.js.map