Generates Typescript definitions from Sway ABI Json files
npm install @fuel-ts/abi-typegen@fuel-ts/abi-typegenGenerate TypeScript bindings for Sway smart contracts.
See the full ABI-spec here.
- Documentation
- Installation
- Help
- Generating Types
- Programmatic API
- Full SDK Installation
- Type's Conversion Table
- Contributing
- Changelog
- License
``sh`
pnpm add @fuel-ts/abi-typegenor
npm add @fuel-ts/abi-typegen
`console
$ fuels-typegen -h
Generate Typescript from Sway ABI JSON files
Usage: fuels-typegen [options]
Options:
-V, --version output the version number
-i, --inputs
-o, --output
-c, --contract Generate types for Contracts [default]
-s, --script Generate types for Scripts
-p, --predicate Generate types for Predicates
-S, --silent Omit output messages
-h, --help display help for command
`
When using the package in a standalone fashion, its bin is prefixed with fuels-.
`sh`
npx fuels-typegen -i ./out/debug/*-abi.json -o ./src/contracts
`ts
import { ProgramTypeEnum, runTypegen } from "@fuel-ts/abi-typegen";
const cwd = process.cwd();
const input = './abis/**-abi.json'
const output = './types'
const filepaths = [ './abis/a-abi.json', './abis/b-abi.json' ]
const programType = ProgramTypeEnum.CONTRACT;
// using input global
await runTypegen({ cwd, input, output, programType });
// using filepaths' array
await runTypegen({ cwd, filepaths, output, programType });
}
`
Alternatively, we recommend you install the complete SDK using the umbrella package:
`sh`
pnpm add fuelsor
npm add fuels
Note that in this example we will interact with the fuels bin directly.
We just need to call it with the typegen command, and the rest feels the same.
`sh`
npx fuels typegen -i ./out/debug/*-abi.json -o ./src/contracts
The table below describes how Sway types are converted from/to Typescript.
| Sway | Example | TS:input | TS:output |
| ------------------: | :----------------------------- | :---------------------------------: | :---------------------------------: |
| u8 | 255 | BigNumberish | number |65535
| u16 | | BigNumberish | number |4294967295
| u32 | | BigNumberish | number |0xFFFFFFFFFFFFFFFF
| u64 | | BigNumberish | BN |anything
| str | | string | string |true
| bool | | boolean | boolean |0x000...
| b256 | | string | string |fuel1a7r...
| b512 | | string | string |MyType
| tuples | (, MyType) | [MyType, MyType] | [MyType, MyType] |MyEnum
| enums | enum { y: (), n: () } | MyEnum = Enum<{ y: [], n: [] }> | MyEnum = Enum<{ y: [], n: [] }> |MyStruct
| structs | { a: u8, b: u16 } | MyStruct | MyStruct |MyType
| vectors | Vec<> | MyType[] | MyType[] |MyType
| options | Option<> | Option | Option |123
| raw untyped ptr | | BigNumberish | BN |
> For more info on Sway types, click here
In order to contribute to @fuel-ts/abi-typegen, please see the main fuels-ts monorepo.
The @fuel-ts/abi-typegen changelog can be found at CHANGELOG.
The primary license for @fuel-ts/abi-typegen is Apache 2.0`, see LICENSE.