96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
/**
|
|
* DON'T MODIFY OR DELETE THIS SCRIPT (unless you know what you're doing)
|
|
*
|
|
* This script generates the file containing the contracts Abi definitions.
|
|
* These definitions are used to derive the types needed in the custom scaffold-eth hooks, for example.
|
|
* This script should run as the last deploy script.
|
|
*/
|
|
|
|
import * as fs from "fs";
|
|
import prettier from "prettier";
|
|
|
|
const generatedContractComment = `
|
|
/**
|
|
* This file is autogenerated by generateAbis.ts
|
|
* You should not edit it manually or your changes might be overwritten.
|
|
*/
|
|
`;
|
|
|
|
const DEPLOYMENTS_DIR = "./ignition/deployments";
|
|
|
|
function getChainDirectories(path: string) {
|
|
return fs
|
|
.readdirSync(path, { withFileTypes: true })
|
|
.filter(
|
|
(dirent) => dirent.isDirectory() && dirent.name.startsWith("chain-"),
|
|
)
|
|
.map((dirent) => dirent.name);
|
|
}
|
|
|
|
function getContractDataFromDeployments() {
|
|
if (!fs.existsSync(DEPLOYMENTS_DIR)) {
|
|
throw Error(
|
|
"At least one deployment should exist to generate contract ABIs.",
|
|
);
|
|
}
|
|
const output: Record<string, any> = {};
|
|
for (const chainDir of getChainDirectories(DEPLOYMENTS_DIR)) {
|
|
const chainId = chainDir.split("-")[1];
|
|
if (!chainId) continue;
|
|
const chainPath = `${DEPLOYMENTS_DIR}/${chainDir}`;
|
|
const artifactsPath = `${chainPath}/artifacts`;
|
|
const addressesPath = `${chainPath}/deployed_addresses.json`;
|
|
if (!fs.existsSync(addressesPath) || !fs.existsSync(artifactsPath))
|
|
continue;
|
|
const addresses = JSON.parse(fs.readFileSync(addressesPath, "utf-8"));
|
|
const contracts: Record<string, any> = {};
|
|
for (const artifactId of Object.keys(addresses)) {
|
|
const artifactFile = `${artifactsPath}/${artifactId}.json`;
|
|
if (!fs.existsSync(artifactFile)) continue;
|
|
const artifact = JSON.parse(fs.readFileSync(artifactFile, "utf-8"));
|
|
contracts[artifact.contractName] = {
|
|
address: addresses[artifactId],
|
|
abi: artifact.abi,
|
|
};
|
|
}
|
|
output[chainId] = contracts;
|
|
}
|
|
return output;
|
|
}
|
|
|
|
/**
|
|
* Generates the TypeScript contract definition file based on the json output of the contract deployment scripts
|
|
* This script should be run last.
|
|
*/
|
|
const generateTsAbis = async () => {
|
|
const TARGET_DIR = "./src/contracts/";
|
|
const allContractsData = getContractDataFromDeployments();
|
|
|
|
const fileContent = Object.entries(allContractsData).reduce(
|
|
(content, [chainId, chainConfig]) => {
|
|
return `${content}${parseInt(chainId).toFixed(0)}:${JSON.stringify(chainConfig, null, 2)},`;
|
|
},
|
|
"",
|
|
);
|
|
|
|
if (!fs.existsSync(TARGET_DIR)) {
|
|
fs.mkdirSync(TARGET_DIR);
|
|
}
|
|
fs.writeFileSync(
|
|
`${TARGET_DIR}deployedContracts.ts`,
|
|
await prettier.format(
|
|
`${generatedContractComment} import { GenericContractsDeclaration } from "@oko-wallet-sandbox-evm/utils/scaffold-eth/contract"; \n\n
|
|
const deployedContracts = {${fileContent}} as const; \n\n export default deployedContracts satisfies GenericContractsDeclaration`,
|
|
{
|
|
parser: "typescript",
|
|
},
|
|
),
|
|
);
|
|
|
|
console.log(
|
|
`📝 Updated TypeScript contract definition file on ${TARGET_DIR}deployedContracts.ts`,
|
|
);
|
|
};
|
|
|
|
generateTsAbis().catch(console.error);
|