@patchworkdev/pdk
Version:
Patchwork Development Kit
106 lines (103 loc) • 4.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePonderConfig = generatePonderConfig;
exports.networkTemplate = networkTemplate;
exports.contractTemplate = contractTemplate;
const lodash_1 = __importDefault(require("lodash"));
const path_1 = __importDefault(require("path"));
const config_1 = require("../common/helpers/config");
const error_1 = require("../common/helpers/error");
const file_1 = require("../common/helpers/file");
const lockFile_1 = __importDefault(require("../common/helpers/lockFile"));
const logger_1 = require("../common/helpers/logger");
const text_1 = require("../common/helpers/text");
async function generatePonderConfig(configPath) {
// Resolve the full path of the config file
const fullConfigPath = path_1.default.isAbsolute(configPath) ? configPath : path_1.default.resolve(process.cwd(), configPath);
const configDir = path_1.default.dirname(fullConfigPath);
// Define paths relative to the config file
const abiDir = path_1.default.join(configDir, 'ponder', 'abis');
const ponderConfigPath = path_1.default.join(configDir, 'ponder', 'ponder.config.ts');
const abis = await (0, config_1.importABIFiles)(abiDir);
const projectConfig = await (0, config_1.importPatchworkConfig)(fullConfigPath);
if (!projectConfig.networks) {
logger_1.logger.error(`No networks found in the project config. Cannot build network configuration.`);
throw new error_1.PDKError(error_1.ErrorCode.PROJECT_CONFIG_MISSING_NETWORKS, `No networks found in the project config at ${fullConfigPath}`);
}
const lockFileManager = new lockFile_1.default(configPath);
const fragmentRelationships = (0, config_1.getFragmentRelationships)(projectConfig);
const entityEvents = ['Frozen', 'Locked', 'Transfer', 'Unlocked', 'Thawed'];
const imports = new Set();
// ToDo
// Need to add in the contract config for the Patchwork Protocol. Config needs to be added to the contracts array either before or after the entities
const contracts = Object.entries(projectConfig.contracts)
.map(([contractName, contractConfig]) => {
imports.add(contractName);
if (!projectConfig.networks) {
logger_1.logger.warn(`No networks found. Cannot build contract config for ${contractName}`);
return '';
}
return contractTemplate(lockFileManager, contractName, projectConfig.networks);
})
.filter(Boolean);
const networks = Object.entries(projectConfig.networks).map(([networkName, network]) => {
return networkTemplate(networkName, network);
});
const config = configTemplate(imports, networks.join(), contracts.join());
await (0, file_1.formatAndSaveFile)(ponderConfigPath, config);
logger_1.logger.info(`Ponder config generated successfully: ${ponderConfigPath}`);
}
function configTemplate(imports, networkConfig, contractConfig) {
return `
import { createConfig, mergeAbis } from '@ponder/core';
import { Address, http } from 'viem';
import { ${Array.from(imports).join(', ')} } from './abis/index';
export default createConfig({
database:{
kind:"postgres",
connectionString:"postgres://postgres:password@postgres:5432/ponder"
},
networks: {
${networkConfig}
},
contracts: {
${contractConfig}
},
});
`;
}
// ToDo
// currently we don't allow for setting values as process.env.SOMETHING
// need to work out a way to do this as some runtime config for deployments
// should not be committed or be known ahead of time
function networkTemplate(name, network) {
return ` ${name}: {
chainId: ${network.chain.id},
transport: http(process.env.${lodash_1.default.upperCase(name)}_RPC),
}`;
}
function contractTemplate(lockFileManager, name, network) {
return `${name}: {
network: {
${Object.entries(network)
.map(([networkName, network]) => contractNetworkTemplate(lockFileManager, name, networkName, network))
.filter((s) => s !== undefined)
.join(',')}
},
abi: mergeAbis([${name}]),
}`;
}
function contractNetworkTemplate(lockFileManager, name, networkName, network) {
const deployment = lockFileManager.getLatestDeploymentForContract(name, networkName);
if (!deployment) {
logger_1.logger.info(`No deployment found for ${name} on ${networkName} network`);
return undefined;
}
return `${networkName}: {
startBlock: Number(process.env.${(0, text_1.envVarCase)(name)}_BLOCK),
address: process.env.${(0, text_1.envVarCase)(name)}_ADDRESS as Address,
}`;
}