在 TON 上编译和构建智能合约
以下是构建智能合约的库和库列表。
简而言之:
- 在大多数情况下,使用Blueprint SDK就足够了。
 - 如果您需要更低级别的方法,可以使用ton-compiler或func-js。
 
Blueprint
概览
TON区块链的开发环境,用于编写、测试和部署智能合约。在Blueprint git库中了解更多信息。
安装
在终端运行以下命令以创建一个新项目,并按照屏幕上的指示操作:
npm create ton@latest
特点
- 构建、测试和部署智能合约的简化工作流程
 - 使用您最喜欢的钱包(例如Tonkeeper)轻松部署到主网/测试网
 - 在一个独立的区块链中快速测试多个智能合约,该区块链在进程中运行
 
技术栈
- 使用https://github.com/ton-community/func-js编译FunC(无CLI)
 - 使用https://github.com/ton-community/sandbox测试智能合约
 - 使用TON Connect 2、Tonhub wallet或
ton://深链接部署智能合约 
要求
- Node.js的最新版本,如v18,使用
node -v验证版本 - 支持TypeScript和FunC的IDE,如Visual Studio Code,配备FunC插件
 
如何使用?
- 观看DoraHacks演示,了解使用blueprint的演示。
 - 在Blueprint库中阅读详细的说明。
 
ton-compiler
概览
打包的FunC编译器,用于TON智能合约:
- GitHub:ton-community/ton-compiler
 - NPM:ton-compiler
 
安装
- npm
 - Yarn
 - pnpm
 
npm install ton-compiler
yarn add ton-compiler
pnpm add ton-compiler
特点
- 多个FunC编译器版本
 - 无需安装和编译TON
 - 程序化和CLI接口
 - 适用于cell测试
 
如何使用
这个包在项目中添加了ton-compiler二进制文件。
FunC编译是一个多阶段过程。其中之一是将Func编译为Fift代码,然后将其编译为二进制表示。Fift编译器已经内置了Asm.fif。
FunC标准库已被捆绑,但可以在运行时禁用。
控制台使用
# 编译为二进制形式(用于合约创建)
ton-compiler --input ./wallet.fc --output ./wallet.cell
# 编译为fift(用于调试)
ton-compiler --input ./wallet.fc --output-fift ./wallet.fif
# 同时编译为二进制形式和fift
ton-compiler --input ./wallet.fc --output ./wallet.cell --output-fift ./wallet.fif
# 禁用标准库
ton-compiler --no-stdlib --input ./wallet.fc --output ./wallet.cell --output-fift ./wallet.fif
# 选择版本
ton-compiler --version "legacy" --input ./wallet.fc --output ./wallet.cell --output-fift ./wallet.fif
程序化使用
import { compileContract } from "ton-compiler";
let result = await compileContract({ code: '
source code', stdlib: true, version: 'latest' });
if (result.ok) {
  console.log(result.fift); // 编译的Fift汇编器
  console.log(result.cell); // 编译的cell Buffer
} else {
  console.warn(result.logs); // 输出日志
}
func-js
概览
Cross-platform绑定TON FunC编译器。
它比ton-compiler更低级,所以只有在ton-compiler不适用时才使用它。
- GitHub:ton-community/func-js
 - NPM:@ton-community/func-js
 
安装
- npm
 - Yarn
 - pnpm
 
npm install @ton-community/func-js
yarn add @ton-community/func-js
pnpm add @ton-community/func-js
特点
- 无需编译或下载FunC二进制文件
 - 在Node.js和WEB中都可工作(需要WASM支持)
 - 直接编译为带有代码cell的BOC
 - 返回汇编版本用于调试目的
 - 不依赖文件系统
 
如何使用
在内部,这个包使用了FunC编译器和Fift解释器组合成单个编译为WASM的库。
简单架构:
(您的代码) -> WASM(FunC -> Fift -> BOC)
内部库的源代码可以在这里找到。
使用示例
import {compileFunc, compilerVersion} from '@ton-community/func-js';
import {Cell} from 'ton';
async function main() {
    // 您可以获取编译器版本
    let version = await compilerVersion();
    
    let result = await compileFunc({
        // 项目的入口点
        entryPoints: ['main.fc'],
        // 源代码
        sources: {
            "stdlib.fc": "<stdlibCode>",
            "main.fc": "<contractCode>",
            // 其他包含在main.fc中的文件
        }
    });
    if (result.status === 'error') {
        console.error(result.message)
        return;
    }
    // result.codeBoc包含编码的BOC,带有代码cell
    let codeCell = Cell.fromBoc(Buffer.from(result.codeBoc, "base64"))[0];
    
    // result.fiftCode包含您代码的汇编版本(用于调试目的)
    console.log(result.fiftCode)
}
请注意,项目中使用的所有FunC源文件内容都应传递给sources,包括:
- 入口点
 - stdlib.fc(如果您使用它)
 - 所有包含在入口点中的文件
 
经TON社区验证
- ton-community/ton-compiler — 用于TON智能合约的现成FunC编译器。
 - ton-community/func-js — TON FunC编译器的跨平台绑定。
 
第三方贡献者
- grozzzny/ton-compiler-groz — TON FunC智能合约编译器。
 - Termina1/tonc — TONC(TON编译器)。使用WASM,非常适合Linux。
 
其他
- disintar/toncli — 最受欢迎的方法之一。您甚至可以在Docker中使用它。
 - tonthemoon/ton — (封闭测试)一行TON二进制安装程序。
 - delab-team/tlbcrc — 包和CLI,根据TL-B方案生成操作码。