You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.1 KiB
JavaScript
156 lines
4.1 KiB
JavaScript
|
1 month ago
|
import fs from "node:fs/promises";
|
||
|
|
import ganache from "ganache";
|
||
|
|
import { ethers } from "ethers";
|
||
|
|
import solc from "solc";
|
||
|
|
|
||
|
|
const action = process.argv[2];
|
||
|
|
const inputPath = process.argv[3];
|
||
|
|
|
||
|
|
if (!action || !inputPath) {
|
||
|
|
process.stderr.write("Usage: node runner.mjs <compile|deploy> <input.json>\n");
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const payload = JSON.parse(await fs.readFile(inputPath, "utf8"));
|
||
|
|
|
||
|
|
try {
|
||
|
|
if (action === "compile") {
|
||
|
|
process.stdout.write(JSON.stringify(compileContract(payload.sourceCode)));
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (action === "deploy") {
|
||
|
|
const result = await deployContract(payload);
|
||
|
|
process.stdout.write(JSON.stringify(result));
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error(`Unsupported action: ${action}`);
|
||
|
|
} catch (error) {
|
||
|
|
process.stderr.write(`${error?.message || String(error)}\n`);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
function compileContract(sourceCode) {
|
||
|
|
const input = {
|
||
|
|
language: "Solidity",
|
||
|
|
sources: {
|
||
|
|
"InsuranceContract.sol": {
|
||
|
|
content: sourceCode
|
||
|
|
}
|
||
|
|
},
|
||
|
|
settings: {
|
||
|
|
optimizer: {
|
||
|
|
enabled: false,
|
||
|
|
runs: 200
|
||
|
|
},
|
||
|
|
outputSelection: {
|
||
|
|
"*": {
|
||
|
|
"*": ["abi", "evm.bytecode.object"]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const output = JSON.parse(solc.compile(JSON.stringify(input)));
|
||
|
|
const diagnostics = output.errors || [];
|
||
|
|
const errors = diagnostics.filter((item) => item.severity === "error");
|
||
|
|
if (errors.length > 0) {
|
||
|
|
throw new Error(errors.map((item) => item.formattedMessage || item.message).join("\n"));
|
||
|
|
}
|
||
|
|
|
||
|
|
const warnings = diagnostics
|
||
|
|
.filter((item) => item.severity === "warning")
|
||
|
|
.map((item) => item.formattedMessage || item.message);
|
||
|
|
|
||
|
|
const contracts = output.contracts?.["InsuranceContract.sol"] || {};
|
||
|
|
const [contractName, contractData] = Object.entries(contracts)[0] || [];
|
||
|
|
if (!contractName || !contractData) {
|
||
|
|
throw new Error("No contract artifact produced by solc");
|
||
|
|
}
|
||
|
|
|
||
|
|
const bytecodeObject = contractData.evm?.bytecode?.object;
|
||
|
|
if (!bytecodeObject) {
|
||
|
|
throw new Error(`Contract ${contractName} has empty bytecode`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
contractName,
|
||
|
|
sourceCode,
|
||
|
|
contractAbi: JSON.stringify(contractData.abi || []),
|
||
|
|
contractBytecode: bytecodeObject.startsWith("0x") ? bytecodeObject : `0x${bytecodeObject}`,
|
||
|
|
warnings
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
async function deployContract(payload) {
|
||
|
|
const compiled = compileContract(payload.sourceCode);
|
||
|
|
await fs.mkdir(payload.dbPath, { recursive: true });
|
||
|
|
const { server, rpcUrl } = await startGanache(payload);
|
||
|
|
try {
|
||
|
|
const provider = new ethers.JsonRpcProvider(rpcUrl);
|
||
|
|
const signer = await provider.getSigner(0);
|
||
|
|
const deployerAddress = await signer.getAddress();
|
||
|
|
const factory = new ethers.ContractFactory(
|
||
|
|
JSON.parse(compiled.contractAbi),
|
||
|
|
compiled.contractBytecode,
|
||
|
|
signer
|
||
|
|
);
|
||
|
|
const contract = await factory.deploy();
|
||
|
|
const deploymentTx = contract.deploymentTransaction();
|
||
|
|
const receipt = await deploymentTx.wait();
|
||
|
|
const contractAddress = await contract.getAddress();
|
||
|
|
const network = await provider.getNetwork();
|
||
|
|
|
||
|
|
return {
|
||
|
|
...compiled,
|
||
|
|
contractAddress,
|
||
|
|
deployTxHash: receipt?.hash || deploymentTx?.hash || "",
|
||
|
|
chainId: String(network.chainId),
|
||
|
|
rpcUrl,
|
||
|
|
deployerAddress,
|
||
|
|
blockNumber: receipt?.blockNumber ?? null
|
||
|
|
};
|
||
|
|
} finally {
|
||
|
|
await server.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function startGanache(payload) {
|
||
|
|
const server = ganache.server({
|
||
|
|
chain: {
|
||
|
|
chainId: Number(payload.chainId || 1337)
|
||
|
|
},
|
||
|
|
database: {
|
||
|
|
dbPath: payload.dbPath
|
||
|
|
},
|
||
|
|
logging: {
|
||
|
|
quiet: true
|
||
|
|
},
|
||
|
|
wallet: {
|
||
|
|
deterministic: true,
|
||
|
|
totalAccounts: 10
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
await server.listen(payload.rpcPort, payload.rpcHost);
|
||
|
|
} catch (error) {
|
||
|
|
if (error?.code !== "EADDRINUSE") {
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
await server.listen(0, payload.rpcHost);
|
||
|
|
}
|
||
|
|
|
||
|
|
const addressInfo = server.address();
|
||
|
|
const actualPort =
|
||
|
|
addressInfo && typeof addressInfo === "object" && "port" in addressInfo
|
||
|
|
? addressInfo.port
|
||
|
|
: payload.rpcPort;
|
||
|
|
|
||
|
|
return {
|
||
|
|
server,
|
||
|
|
rpcUrl: `http://${payload.rpcHost}:${actualPort}`
|
||
|
|
};
|
||
|
|
}
|