The TON FunC Compiler is an essential tool for those who wish to compile code directly into a Binary Object Code (BOC) format without the need for external binaries. In this guide, we will walk you through the installation and usage of this remarkable tool while addressing potential troubleshooting tips to ensure a smooth experience.
Features of TON FunC Compiler
- No need to compile or download FunC binaries.
- Works seamlessly in Node.js and web environments (WASM support required).
- Directly compiles code to BOC with a code cell.
- Provides assembly code for debugging purposes.
- Does not depend on the filesystem, making it very flexible.
How It Works
The TON FunC Compiler utilizes both the FunC compiler and the Fift interpreter, combining them into a single library that is compiled into WebAssembly (WASM). You can think of this process as a chef preparing a gourmet meal using various ingredients:
Analogy: Imagine you are making a smoothie. You gather fruits (your code) and blend them together (WASM) to create a delicious drink (BOC). Each ingredient must be fresh and well-prepared; otherwise, the final product might be unpalatable (errors in compilation).
Installation
To install the TON FunC Compiler, you can simply use either of the following commands:
bash
yarn add @ton-community/func-js
bash
npm i @ton-community/func-js
Using the Command Line Interface (CLI)
The command line usage of TON FunC Compiler is straightforward. You can compile your files using the following command:
bash
npx func-js .stdlib.fc .wallet.fc --boc .output.boc
To see more output options, simply run:
bash
npx func-js -h
Example Usage in TypeScript
Here’s a practical example of how to use the TON FunC Compiler within your TypeScript code:
typescript
import compileFunc, { compilerVersion } from '@ton-community/func-js';
import Cell from 'ton';
async function main() {
// You can get the compiler version
let version = await compilerVersion();
let result = await compileFunc({
targets: ['main.fc'],
sources: {
'stdlib.fc': stdlibCode,
'main.fc': contractCode,
// Additional files if necessary
},
});
if (result.status === 'error') {
console.error(result.message);
return;
}
// Base64 encoded BOC and assembly code for debugging
let codeCell = Cell.fromBoc(Buffer.from(result.codeBoc, 'base64'))[0];
console.log(result.fiftCode);
}
Using Resolver for Source Files
If your source files are in the same directory as your compiling file, you can simplify the process using a resolver function:
typescript
import { readFileSync } from 'fs';
import compileFunc from '@ton-community/func-js';
let result = await compileFunc({
targets: ['main.fc'],
sources: (path) => readFileSync(__dirname + '/' + path).toString()
});
Troubleshooting Tips
If you encounter any issues while using the TON FunC Compiler, here are some troubleshooting suggestions:
- Ensure your source files are correctly linked in the paths.
- Check for any missing dependencies or incorrect file formats.
- If using a resolver, verify that it is synchronous and returning the correct string format.
- Consult the GitHub repository for possible updates or community support.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
The TON FunC Compiler serves as a powerful tool for developers, allowing easy compilation of FunC code into BOC without dependencies on external binaries. By following this guide, you are well on your way to utilizing this innovative compiler in your own projects.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.