UNPKG

@eliseev_s/tolk-tlb-transpiler

Version:

Transpile Tolk structs to TLB definitions and generate TypeScript wrappers for TON blockchain smart contracts

122 lines 4.75 kB
"use strict"; // ============================================================================ // Utility Handlers - Convenient Functions for Tolk-to-TLB Transpilation // ============================================================================ Object.defineProperty(exports, "__esModule", { value: true }); exports.formatTlbDefinitions = formatTlbDefinitions; exports.getTlbStats = getTlbStats; exports.transpileTolkToTlbDefinitions = transpileTolkToTlbDefinitions; exports.transpileTolkToTlb = transpileTolkToTlb; exports.generateContractWrapper = generateContractWrapper; const parser_1 = require("./parser"); const wrapper_generator_1 = require("./wrapper-generator"); /** * Format TLB definitions into a readable string format * * @param definitions - Array of TLB definitions * @param options - Formatting options * @returns Formatted TLB string */ function formatTlbDefinitions(definitions, headerComment = 'TLB Schema Generated') { const output = []; if (headerComment) { output.push('// ============================================================================'); output.push(`// ${headerComment}`); output.push('// ============================================================================'); output.push(''); } for (const definition of definitions) { output.push(`// ${definition.name}`); for (const constructor of definition.statement) { output.push(constructor); } output.push(''); } return output.join('\n'); } /** * Get statistics about TLB definitions * * @param definitions - Array of TLB definitions * @returns Statistics object */ function getTlbStats(definitions) { let totalConstructors = 0; const definitionNames = []; for (const definition of definitions) { definitionNames.push(definition.name); totalConstructors += definition.statement.length; } return { totalDefinitions: definitions.length, totalConstructors, definitionNames, }; } /** * Transpile Tolk code and return TLB definitions * * @param tolkCode - The Tolk source code * @returns Promise resolving to TLB definitions */ async function transpileTolkToTlbDefinitions(tolkCode) { const processor = await parser_1.TolkASTProcessor.initialize(); return processor.transpile(tolkCode); } /** * Transpile Tolk code and return formatted TLB string * * @param tolkCode - The Tolk source code * @param formatOptions - Optional formatting options * @returns Promise resolving to formatted TLB string */ async function transpileTolkToTlb(tolkCode, formatOptions) { const definitions = await transpileTolkToTlbDefinitions(tolkCode); return formatTlbDefinitions(definitions, formatOptions); } /** * Generate TypeScript wrapper with TLB types for smart contract interaction * * @param tolkStructuresCode - Tolk code containing struct definitions * @param tolkGetMethodsCode - Tolk code containing get method definitions * @param contractName - Name of the contract (e.g., "Minter") * @returns Object with TLB definitions, TypeScript code, and wrapper code */ async function generateContractWrapper(tolkStructuresCode, tolkGetMethodsCode, contractName) { // Initialize processor const processor = await parser_1.TolkASTProcessor.initialize(); // Process structures to get TLB definitions const tlbDefinitions = processor.transpile(tolkStructuresCode); // Get structures for send method generation const structuresTree = processor.parse(tolkStructuresCode); processor.processAST(structuresTree); const structures = processor.getStructs(); const typeAliases = processor.getTypeAliases(); // Parse get methods from get methods file const getMethodsTree = processor.parse(tolkGetMethodsCode); processor.processAST(getMethodsTree); // This will populate getMethods const getMethods = processor.getGetMethods(); // Create wrapper generator instance using static factory method const wrapperGenerator = await wrapper_generator_1.WrapperGenerator.create({ contractName, storageType: `${contractName}Storage`, internalMessageType: `${contractName}InternalMessage`, getMethods, structures, typeAliases, tlbDefinitions, }); // Get all generated components const tlbString = wrapperGenerator.getTlbString(); const typescriptCode = wrapperGenerator.getTypescriptCode(); const wrapperCode = wrapperGenerator.getWrapperCode(); const fullCode = wrapperGenerator.render(); return { tlbDefinitions, tlbString, typescriptCode, wrapperCode, fullCode, }; } //# sourceMappingURL=handlers.js.map