Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6c3a943
Merge pull request #4 from ton-community/add-code-of-conduct-1
Naltox Sep 28, 2022
f01190d
Fixed bad fift asm compilation, added SourceResolver callback
krigga Nov 15, 2022
182ca89
Fixed fift code newlines
krigga Nov 18, 2022
f179813
Update README.md
krigga Nov 25, 2022
657d6d6
chore: bump version for README update
krigga Nov 29, 2022
31b0cd5
feat: API changes
krigga Dec 20, 2022
5bce5ca
feat: use func-js-bin
krigga Dec 20, 2022
9f8d342
feat: cli
krigga Dec 23, 2022
d556e65
chore: cli improvements
mrbonezy Dec 25, 2022
af7386a
chore: bump func-js-bin to v0.4.0
krigga Jan 9, 2023
e446d9c
chore: update readme
krigga Jan 13, 2023
37cf8bf
chore: bump func-js-bin to v0.4.1
krigga Feb 1, 2023
cdcb4c9
feat: added --cwd arg
krigga Feb 9, 2023
ff69de8
chore: bump version
krigga Feb 9, 2023
75932a2
fix: test
krigga Mar 13, 2023
b8af081
chore: bump func-js-bin to v0.4.2, bump version
krigga Mar 13, 2023
d751911
bump func-js-bin to v0.4.3, bump version, update dev dependencies
krigga Apr 17, 2023
ad6727c
chore: bump func-js-bin to v0.4.4, bump version
krigga May 18, 2023
0e0f98f
chore: bump func-js-bin to v0.4.4-newops, bump version
krigga Dec 10, 2023
3c4d0ff
chore: bump func-js-bin to v0.4.4-newops.1, bump version
krigga Mar 27, 2024
5ed76b6
fix: test
krigga May 28, 2024
44ee58c
chore: use func v0.4.5
krigga Nov 4, 2024
dd813df
chore: update func to 0.4.6
krigga Dec 18, 2024
4c9f0ae
chore: update func-js-bin version
krigga Feb 13, 2025
5c32201
Initial commit
Monireh6569 May 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
[![Version npm](https://img.shields.io/npm/v/@ton-community/func-js.svg?logo=npm)](https://www.npmjs.com/package/@ton-community/func-js)

Cross-platform bindings for TON FunC compiler.
Cross-platform bindings and CLI for TON FunC compiler.

## Features

- 🚀 No need to compile of download FunC binaries
- 📦 Works both in Node.js & WEB (WASM support is required)
- ⚙️ Compiles straight to BOC with code Cell
- ⚙️ Assembly is returned fot debugging purposes
- 📁 Does not depend on file-system
- ⚙️ Assembly is returned for debugging purposes
- 📁 Does not depend on filesystem

## How it works

Expand All @@ -36,6 +36,11 @@ or
npm i @ton-community/func-js
```

## CLI
Usage: `npx func-js ./stdlib.fc ./wallet.fc --boc ./output.boc`

See more output options by running `npx func-js -h`

## Usage example

```typescript
Expand All @@ -47,13 +52,13 @@ async function main() {
let version = await compilerVersion();

let result = await compileFunc({
// Entry points of your project
entryPoints: ['main.fc'],
// Targets of your project
targets: ['main.fc'],
// Sources
sources: {
"stdlib.fc": "<stdlibCode>",
"main.fc": "<contractCode>",
// Rest of the files which are included in main.fc if some
// The rest of the files which are included in main.fc if any
}
});

Expand All @@ -70,11 +75,44 @@ async function main() {
}
```

Note that all FunC source files contents used in your project should be passed to ```sources```, including:
- entry points
Instead of a source map, you can also use a source array, like so:
```typescript
let result = await compileFunc({
// Sources
sources: [
{
filename: "stdlib.fc",
content: "<stdlibCode>",
},
{
filename: "main.fc",
content: "<contractCode>",
},
// The rest of the files which are included in main.fc if any
]
});
```
Notice that passing a sources *array* makes `targets` optional (if not passed, `targets` will be set to `filename`s of `sources` in the order they were given).

You can also pass a resolver (a function of type `(path: string) => string`) into `sources` instead of a source map object or array, for example if `main.fc` and all contracts used by it (such as `stdlib.fc`) are located in the same directory as the compiling .ts/.js file, you can use the following:
```typescript
import { readFileSync } from "fs";
import { compileFunc } from "@ton-community/func-js";

let result = await compileFunc({
// Targets of your project
targets: ['main.fc'],
// Sources
sources: (path) => readFileSync(__dirname + '/' + path).toString()
});
```
And the resolver will be called for each required source file (including the targets) using the same name as in the `#include` statement. Note however that the resolver must be synchronous and must return a string; if you need the resolver to get files from the network, you can repeatedly run the compiler with the known sources, check if the compilation failed, download the required sources and rerun the compiler until compilation succeeds.

Note that all FunC source files contents used in your project should be passed to `sources` (if it is a source map or array) or be resolvable by it (if it is a resolver), including:
- targets
- stdlib.fc (if you use it)
- all files included in entry points
- all files included in targets

## License

This package is released under the [MIT License](LICENSE).
This package is released under the [MIT License](LICENSE).
36 changes: 21 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
{
"name": "@ton-community/func-js",
"version": "0.1.5",
"version": "0.9.1",
"description": "The TON FunC smart-contract compiler",
"main": "dist/index.js",
"bin": {
"func-js": "./dist/cli.js"
},
"author": "TonTech",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/the-ton-tech/ton-compiler.git"
"url": "git+https://github.com/ton-community/func-js.git"
},
"files": [
"dist/**/*"
],
"scripts": {
"copy:wasmlib": "rm -rf ./dist/wasmlib && mkdir ./dist/wasmlib && cp ./src/wasmlib/funcfiftlib.js ./dist/wasmlib/funcfiftlib.js && cp ./src/wasmlib/funcfiftlib.wasm.js ./dist/wasmlib/funcfiftlib.wasm.js",
"build": "yarn wasm:pack && tsc && yarn copy:wasmlib",
"test": "jest",
"wasm:pack": "node ./scripts/pack_wasm.js",
"release": "rm -fr dist && jest && yarn build && yarn publish --access public"
"build": "rm -rf dist && tsc",
"test": "yarn jest",
"release": "yarn test && yarn build && yarn publish --access public"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^24.0.23",
"jest": "^26.4.2",
"ts-jest": "^26.4.0",
"typescript": "^3.7.2",
"ton": "^11.18.2"
}
}
"@types/jest": "^29.5.0",
"jest": "^29.5.0",
"ton-core": "^0.49.0",
"ton-crypto": "^3.2.0",
"ts-jest": "^29.1.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@ton-community/func-js-bin": "0.4.6-wasmfix.0",
"arg": "^5.0.2"
},
"packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
}
6 changes: 0 additions & 6 deletions scripts/pack_wasm.js

This file was deleted.

103 changes: 103 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env node
import arg from 'arg';
import { compileFunc, compilerVersion } from '.';
import { readFileSync, writeFileSync } from 'fs';
import path from 'path';

const main = async () => {
const args = arg({
'--version': Boolean,
'--help': Boolean,
'--require-version': String,
'--artifact': String,
'--boc': String,
'--boc-base64': String,
'--fift': String,
'--cwd': String,

'-v': '--version',
'-h': '--help',
'-C': '--cwd',
});

if (args['--help']) {
console.log(`Usage: func-js [OPTIONS] targets
Options:
-h, --help - print this and exit
-v, --version - print func version and exit
--cwd <path>, -C <path> - run func-js as if it was launched from the given path (useful in npm scripts)
--require-version <version> - set the required func version, exit if it is different
--artifact <path> - path where JSON artifact, containing BOC and FIFT output, will be written
--boc <path> - path where compiled code will be written as binary bag of cells
--boc-base64 <path> - path where compiled code will be written as bag of cells using base64 encoding
--fift <path> - path where compiled fift code will be written
`);
process.exit(0);
}

const v = await compilerVersion();

if (args['--require-version'] !== undefined && v.funcVersion !== args['--require-version']) {
console.error(`Failed to run func-js: the required func version is ${args['--require-version']}, but the func version is ${v.funcVersion}`);
process.exit(1);
}

if (args['--version']) {
console.log(`func v${v.funcVersion}`);
process.exit(0);
}

if (args._.length === 0) {
console.error('No targets were specified. Run with -h to see help.');
process.exit(1);
}

console.log(`Compiling using func v${v.funcVersion}`);

const basePath = args['--cwd'];
const pathResolver = basePath === undefined
? (p: string) => p
: (p: string) => path.join(basePath, p);

const cr = await compileFunc({
targets: args._,
sources: (reqPath: string) => readFileSync(pathResolver(reqPath)).toString(),
});

if (cr.status === 'error') {
console.error(cr.message);
process.exit(1);
}

if (args['--artifact'] !== undefined) {
writeFileSync(args['--artifact'], JSON.stringify({
artifactVersion: 1,
version: v.funcVersion,
sources: cr.snapshot,
codeBoc: cr.codeBoc,
fiftCode: cr.fiftCode,
}));
}

if (args['--boc-base64'] !== undefined) {
writeFileSync(args['--boc-base64'], cr.codeBoc);
}

if (args['--boc'] !== undefined) {
writeFileSync(args['--boc'], Buffer.from(cr.codeBoc, 'base64'));
}

if (args['--fift'] !== undefined) {
writeFileSync(args['--fift'], cr.fiftCode);
}

console.log('Compiled successfully!');

if (!args['--artifact'] && !args['--boc'] && !args['--boc-base64'] && !args['--fift']) {
console.warn('Warning: No output options were specified. Run with -h to see help.');
} else {
console.log('Written output files.');
}
};

main();
Loading