π Dart/Flutter library for TON blockchain
A composable and versatile library for all things TON!
WARNING: Some underlying APIs of this library are subject to change in near future, so it's advised to pin down specific minor version rather than specifying ranges in your pubspec.yaml. At least until we hit stable 1.0.0 release.
If you love this library and want to support its development you can donate any amount of coins to this TON address βΊοΈEQDew1rvHuMmMkmxG_fQahGymzIOF2_9TpgLftMUuxpKLE_u
To donate in other cryptocurrencies, use:
π° Features
- jsonRPC client to work with TON network: lib/client
- Cell, Slice, Builder, and BOC (de)serialization: lib/dataformat
- Support for popular structures from block.tlb: lib/dataformat
- Support of TON base64 addresses: lib/dataformat
- Support of HashmapE: lib/dataformat
- Support of TON & BIP39 Mnemonics: lib/mnemonic
- Support of wallets (v3, v3r2, v4r2): lib/wallet
- Ed25519 signing of transactions and crypto primitives: lib/crypto
- Workings with Jettons: lib/jetton
- Workings with NFTs: lib/nft
- ...and much more!
π Usage
Install via dart pub
:
dart pub add tonutils
Get it all or use a few
Most common way is to import the whole library and cherry-pick the needed elements:
import 'package:tonutils/tonutils.dart' show Mnemonic;
Alternatively, consider using only the sub-libraries if you know precisely what you need:
import 'package:tonutils/mnemonic.dart'; // provides Mnemonic and WordList classes
All the individual things you can import and use are listed in the root of lib/ folder.
RPC Client (Toncenter API)
You can use one of the public endpoints:
Or host your own instance of TON HTTP API.
// Client uses testnet by default:
final testnetClient = TonJsonRpc();
// But you can specify an alternative endpoint, say, for mainnet:
final client = TonJsonRpc('https://toncenter.com/api/v2/jsonRPC');
// You can also specify an API key obtained from https://t.me/tonapibot!
// Generate a new key pair
var mnemonics = Mnemonic.generate();
var keyPair = Mnemonic.toKeyPair(mnemonics);
// Wallet contracts use workchain = 0, but this can be overriden
var wallet = WalletContractV4R2.create(publicKey: keyPair.publicKey);
// Opening a wallet contract (this specifies the TonJsonRpc as a ContractProvider)
var openedContract = client.open(wallet);
// Get the balance of the contract
var balance = await openedContract.getBalance();
print(balance);
// Create a transfer
var seqno = await openedContract.getSeqno();
var transfer = openedContract.createTransfer(
seqno: seqno,
privateKey: keyPair.privateKey,
messages: [
internal(
to: SiaString('EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N'),
value: SbiString('1.5'),
body: ScString('Hello, world!'),
)
],
);
print(transfer.toString());
As you can see from this example, this library is 90% compatible with the API of the ton-core and ton-community TypeScript libraries. The main sources of divergence are types: this library uses sealed classes and extends from them to provide union-like types, while maintaining compile-time checked type safety and soundness.
But worry not β editor hints in VS Code, Emacs, Vim, NeoVim, Helix and other editors with Language Server Provider (LSP) support won't leave you astray and provide all the answers if you ever find yourself stuck.
πΊ Videos
Playlist with examples on YouTube: link
π§ Tests
Tests are positioned to mirror the structure inside lib/src/
, and grouped by the relative path from test folder to the test file (excluding the _test.dart
suffix).
Examples:
- Tests for mnemonics are located in the file
test/mnemonic/mnemonic_test.dart
, and the test group name ismnemonic/mnemonic
- Tests for addresses are located in the file
test/dataformat/address/address_test.dart
, and the test group name isdataformat/address/address
To invoke a group of tests by their name, run:
dart test -N 'group name'
For example, to test mnemonics:
dart test -N 'mnemonic/mnemonic'
π License
Apache License 2.0
Libraries
- client
- Clients for TON Blockchain
- crypto
- Underlying crypto primitives
- dataformat
- TON-specific data formats
- jetton
- Support for workings with jettons (FTs)
- mnemonic
- TON & BIP39 Mnemonics
- nft
- Support for workings with NFTs
- tonutils
- Dart/Flutter library for TON blockchain. Import this to gain access to everything all at once!
- wallet
- Support of wallets (v3, v3r2, v4r2)