Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "remove all" #28

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c66e974
Revert "remove all"
PolyProgrammist Jan 11, 2024
cc4dd29
add bit type
PolyProgrammist Jan 12, 2024
124cf03
add grams builtin
PolyProgrammist Jan 12, 2024
8d78029
address
PolyProgrammist Jan 12, 2024
1546ee5
varinteger
PolyProgrammist Jan 12, 2024
25df103
add comments before structs
PolyProgrammist Jan 12, 2024
6169930
Handle HashmapE as Dictionary
PolyProgrammist Jan 12, 2024
4bf7e50
HashmapAugE
PolyProgrammist Jan 15, 2024
d636070
start cli & npm package
PolyProgrammist Jan 16, 2024
1c76154
finalize binary
PolyProgrammist Jan 16, 2024
aff9e4b
Add exports for NPM package
PolyProgrammist Jan 16, 2024
9e6eb1d
Create README.md
PolyProgrammist Jan 16, 2024
623301e
Add license
PolyProgrammist Jan 16, 2024
131673a
Fix CI
PolyProgrammist Jan 16, 2024
93ce069
Add npm run build to CI
PolyProgrammist Jan 16, 2024
86d76d5
Move npm run build after + to #
PolyProgrammist Jan 16, 2024
b826168
chore: rename package and update package.json
thekiba Feb 11, 2024
b9cdba8
Switch to @ton-community/tlb-parser and add one more Block cell to tests
PolyProgrammist Feb 11, 2024
764b626
beginParse for exotic cells
PolyProgrammist Feb 11, 2024
f448595
Change import 'ton' for 'ton-core'
PolyProgrammist Feb 11, 2024
12dd1dd
chore: update @ton-community/tlb-parser@^0.1.2
thekiba Feb 11, 2024
96d5848
refactor: optimize imports
thekiba Feb 11, 2024
7e9495a
chore: replace ton-core with @ton/core and rebuild tests
thekiba Feb 11, 2024
4eb9e63
chore: chore update @ton-community/tlb-parser@^0.1.3
thekiba Feb 11, 2024
c784072
docs: update
thekiba Feb 11, 2024
90fc50a
docs: update
thekiba Feb 11, 2024
3df7c61
feat: log after execution
thekiba Feb 11, 2024
731a7b5
chore: release v1.0.0
thekiba Feb 11, 2024
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
16 changes: 16 additions & 0 deletions .github/workflows/jest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Typescript Generator Tests
run-name: ${{ github.actor }} is testing out tlbgenerator for typescript
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install
- run: npm install -g ts-node
- run: npm run build
- run: ts-node generate_tests.ts
- run: npm run test
69 changes: 69 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next


## Custom
## ======

.idea
dist/
build/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Vadim Volodin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# TLB Generator
This package allows you to generate `Typescript` code for serializing and deserializing structures according to the `TLB` scheme provided.

[Here](https://docs.ton.org/develop/data-formats/tl-b-language) you can find documentation for creating TLB schemes, and more advanced article is [here](https://docs.ton.org/develop/data-formats/tl-b-types).

This package uses [TLB-Parser](https://github.com/ton-community/tlb-parser) to get AST of the scheme.

## Installation

```bash
npm install @ton-community/tlb-codegen
```

## Usage

### CLI

Create a file with TLB scheme according to the [documentation](https://docs.ton.org/develop/data-formats/tl-b-language). This is an example of such a file (call it `example.tlb`):
```
t$_ x:# y:(uint 5) = A;
```

Then do:
```bash
npx tlb example.tlb
```

It will create a file called `example.ts` with the following code:
```typescript
export interface A {
readonly kind: 'A';
readonly x: number;
readonly y: number;
}
export function loadA(slice: Slice): A {
let x: number = slice.loadUint(32);
let y: number = slice.loadUint(5);
return {
kind: 'A',
x: x,
y: y,
}

}
export function storeA(a: A): (builder: Builder) => void {
return ((builder: Builder) => {
builder.storeUint(a.x, 32);
builder.storeUint(a.y, 5);
})

}
```

You also can set an output file with `-o` option: `npx tlb -o other_file.ts example.tlb`.

One of the examples where you can see various abilities of the tool is [block.tlb](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb). The generation result for it is [here](https://github.com/PolyProgrammist/tlbgenerator/blob/master/test/generated_files/generated_block.ts).

### Node JS
Also you can use the tool from inside JS or TS code.

```typescript
import { generateCode } from "@ton-community/tlb-codegen"
generateCode('example.tlb', 'example.ts', "typescript")
```


## Integration with @ton/core

It is integrated with [@ton/core](https://github.com/ton-org/ton-core/) in a way it uses several built-in types from there.

Built-in types supported are:
- `Bool` -> `boolean` (loaded with `loadBoolean`, stored with `storeBit`)
- `MsgAddressInt` -> `Address` (loaded with `loadAddress`, stored with `storeAddress`)
- `MsgAddressExt` -> `ExternalAddress | null` (loaded with `loadMaybeExternalAddress`, stored with `storeAddress`)
- `MsgAddress` -> `Address | ExternalAddress | null` (loaded with `loadAddressAny`, stored with `storeAddress`)
- `VarUInteger` -> `bigint` (loaded with `loadVarUintBig`, stored with `storeVarUint`)
- `VarInteger` -> `bigint` (loaded with `loadVarIntBig`, stored with `storeVarInt`)
- `Bit` -> `boolean` (loaded with `loadBit`, stored with `storeBit`)
- `Grams` -> `bigint` (loaded with `loadCoins`, stored with `storeCoins`)
- `HashmapE n Value` -> `Dictionary<bigint, Value>` (or `Dictionary<number, Value>` if n <= 64) (loaded with `Dictionary.load`, stored with `storeDict`)
- `HashmapAugE n Value Extra` -> `Dictionary<bigint, {value: Value, extra: Extra}>` (or `number` instead of `bigint` if `n <= 64`) (loaded with `Dictionary.load`, stored with `storeDict`)

> Please note that the tricky thing here with `HashmapAugE` is that in `TLB` scheme extra is [stored](https://github.com/ton-blockchain/ton/blob/062b7b4a92dd67e32d963cf3f04b8bc97d8b7ed5/crypto/block/block.tlb#L49) not only with values, but in intermediate nodes as well. However `Dictionary` in [@ton/core](https://github.com/ton-org/ton-core) doesn't store the intermediate nodes. That is why `HashmapAugE` can be correctly loaded by the generated code, but storing is incorrect.

> Please note that `BinTree` is not supported yet. In the future it will be supported as built-in type `BinTree` from [@ton/core](https://github.com/ton-org/ton-core).

## License

This package is released under the [MIT License](LICENSE).
11 changes: 11 additions & 0 deletions generate_tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from 'path'
import util from 'util'
import { generateCode } from './src/main'

function genCodeForTest(name: string) {
const fixturesDir = path.resolve(__dirname, 'test')
generateCode(path.resolve(fixturesDir, 'tlb', name + '.tlb'), 'test/generated_files/generated_' + name + '.ts', 'typescript')
}

genCodeForTest('block')
genCodeForTest('test')
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { generateCode } from "./src/main";
export { generateCode, generateCodeByAST, generateCodeWithGenerator } from "./src/main";
export { CodeGenerator } from "./src/generators/generator";
export { TypescriptGenerator } from "./src/generators/typescript/generator";
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: [
"**/?(*.)+(spec|test).[j]s?(x)"
]
};
52 changes: 52 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node
import meow from 'meow';
import { generateCode } from './src/main';

const cli = meow(`
Usage
$ tlb <tlbpath>

Options
--output, -o Output file, default = tlbpath with .ts extension e.g. ./path/to/file.tlb -> ./path/to/file.ts
--language, -l Programming language result. Supported languages: ["typescript"]. Default: "typescript"

Examples
$ tlb ./path/to/file.tlb
> Generated code is saved to ./path/to/file.ts

$ tlb -o ./path/to/output.ts ./path/to/file.tlb
> Generated code is saved to ./path/to/output.ts

$ tlb -l typescript ./path/to/file.tlb
> Generated code is saved to ./path/to/file.ts
`, {
flags: {
output: {
type: 'string',
alias: 'o'
},
language: {
type: 'string',
alias: 'l'
}
}
});


let input = cli.input.at(0)
if (input) {
if (input.match(/\.tlb$/) == null) {
console.error('Input file must have .tlb extension')
process.exit(1)
}

let output = input.replace('.tlb', '.ts')
if (cli.flags.output) {
output = cli.flags.output;
}
let language = 'typescript'
if (cli.flags.language) {
language = cli.flags.language
}
generateCode(input, output, language);
}
Loading