@m3s/smart-contract
Version:
A modular toolkit for generating, compiling, deploying, and interacting with Ethereum-compatible smart contracts
85 lines • 5.09 kB
JavaScript
export default class CodeGenerator {
async generate(input) {
console.log(`[CodeGenerator] Generating contract for language: ${input.language}, template: ${input.template || 'default'}`);
const { language, template, options } = input;
try {
switch (language.toLowerCase()) {
case 'solidity':
const standard = template?.replace('openzeppelin_', '').toUpperCase();
// Use dynamic imports to avoid loading unused wizards
const wizard = await import('@openzeppelin/wizard');
switch (standard) {
case 'ERC20': return wizard.erc20.print(options);
case 'ERC721': return wizard.erc721.print(options);
case 'ERC1155': return wizard.erc1155.print(options);
// Add other Solidity templates if OZ Wizard supports them
default: throw new Error(`Unsupported Solidity template via OZ Wizard: ${template}`);
}
case 'cairo':
try {
const cairoModule = await import('@openzeppelin/wizard-cairo');
const { printContract: cairoPrint, ...cairoWizards } = cairoModule;
const cairoTemplate = template?.toLowerCase();
if (!cairoTemplate)
throw new Error(`Cairo template not specified.`);
if (cairoTemplate === 'custom' && typeof cairoPrint === 'function') {
return cairoPrint(options);
}
const wizardImpl = cairoWizards[cairoTemplate];
if (wizardImpl && typeof wizardImpl === 'object' && 'print' in wizardImpl && typeof wizardImpl.print === 'function') {
return wizardImpl.print(options);
}
throw new Error(`Unsupported or invalid Cairo template: ${template}`);
}
catch (e) {
if (e.code === 'ERR_MODULE_NOT_FOUND')
throw new Error("Cairo generation requires '@openzeppelin/wizard-cairo'. Please install it.");
throw e; // Re-throw other errors
}
case 'stellar': // Soroban (Rust)
try {
const { fungible: stellarFungible } = await import('@openzeppelin/wizard-stellar');
if (template?.toLowerCase() === 'fungible') {
return stellarFungible.print(options);
}
throw new Error(`Unsupported Stellar template: ${template}. Use 'fungible'.`);
}
catch (e) {
if (e.code === 'ERR_MODULE_NOT_FOUND')
throw new Error("Stellar generation requires '@openzeppelin/wizard-stellar'. Please install it.");
throw e;
}
case 'stylus': // Arbitrum Stylus (Rust)
try {
const stylusModule = await import('@openzeppelin/wizard-stylus');
const { printContract: stylusPrint, ...stylusWizards } = stylusModule;
const stylusTemplate = template?.toLowerCase();
if (!stylusTemplate)
throw new Error(`Stylus template not specified.`);
const wizardImpl = stylusWizards[stylusTemplate];
if (wizardImpl && typeof wizardImpl === 'object' && 'print' in wizardImpl && typeof wizardImpl.print === 'function') {
return wizardImpl.print(options);
}
throw new Error(`Unsupported or invalid Stylus template: ${template}. Use 'erc20', 'erc721', or 'erc1155'.`);
}
catch (e) {
if (e.code === 'ERR_MODULE_NOT_FOUND')
throw new Error("Stylus generation requires '@openzeppelin/wizard-stylus'. Please install it.");
throw e;
}
default:
throw new Error(`Unsupported contract language for generation: ${language}`);
}
}
catch (error) {
console.error(`[CodeGenerator] Failed to generate contract: ${error.message}`, error.stack);
// Avoid re-wrapping the error if it's already informative
if (error instanceof Error && error.message.startsWith('Failed to generate contract'))
throw error;
if (error instanceof Error && error.message.includes('requires'))
throw error; // Pass specific requirement errors up
throw new Error(`Failed to generate contract: ${error.message}`);
}
}
}
//# sourceMappingURL=generator.js.map