Code Monkey home page Code Monkey logo

tonutils-dart's Introduction

πŸ’Ž Dart/Flutter library for TON blockchain

A composable and versatile library for all things TON!

License Pub

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

πŸš€ 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 is mnemonic/mnemonic
  • Tests for addresses are located in the file test/dataformat/address/address_test.dart, and the test group name is dataformat/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

tonutils-dart's People

Contributors

novusnota avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mmmllleth mhbdev

tonutils-dart's Issues

GOAL: More examples in `example/` folder

Note: this is a collective, tracking issue towards the 1.0 release. Please, target fixes for the sub-issues mentioned below instead

Besides the improvements in docs, there has to be more examples of stand-alone usage of available features. Currently, only client/ sub-library has an example.

Sub-goals:

  • Usage examples in example/ folder
  • Standalone exemplary Dart or Flutter projects

Issues towards this goal:

  • .

GOAL: Full test coverage

Note: this is a collective, tracking issue towards the 1.0 release. Please, target fixes for the sub-issues mentioned below instead

Unit tests would help prevent implementation quirks and, potentially, allow new people to propose changes or enhancements.

Sub-goals:

  • Unit tests, 100% coverage
  • Integration tests: client/ lib, and beyond

Issues towards this goal:

  • .

JettonWallet Type caset error

run into this error using jetton getbalance

   var api = TonJsonRpc();
    var jwallet = JettonWallet(InternalAddress.parse(""));
    var openedjwallet = api.open(jwallet);
    var b = await openedjwallet.getBalance();
    print("b $b");

the errror stack

E/flutter (14171): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '_Map<String, dynamic>' is not a subtype of type 'String'
E/flutter (14171): #0      _parseCallGetMethod
jsonrpc.dart:1263
E/flutter (14171): #1      TonJsonRpc._callGetMethod
jsonrpc.dart:651
E/flutter (14171): <asynchronous suspension>
E/flutter (14171): #2      TonJsonRpc.runMethod
jsonrpc.dart:94
E/flutter (14171): <asynchronous suspension>
E/flutter (14171): #3      TonJsonRpc._createProvider.<anonymous closure>
jsonrpc.dart:454
E/flutter (14171): <asynchronous suspension>
E/flutter (14171): #4      JettonWallet.getBalance

this is the rawStack variable at the line above

"[[num, 0xde0b5cad2bef000], [cell, {bytes: te6cckEBAQEAJAAAQ4ASzS4DrEpLh7WTLry51yDO3F2nwpM6SbSxkfjqa9YfNrCVHfVK, object: {data: {b64: gBLNLgOsSkuHtZMuvLnXIM7cXafCkzpJtLGR+Opr1h82oA==, len: 267}, refs: [], special: false}}], [cell, {bytes: te6cckEBAQEAJAAAQ4AUqDK5iiqi2owPkkcLkzEa1z7emZemsrSBJj1p2HVpw7DAhNj1, object: {data: {b64: gBSoMrmKKqLajA+SRwuTMRrXPt6Zl6aytIEmPWnYdWnDoA==, len: 267}, refs: [], special: false}}], [cell, {bytes: te6cckECEQEAAyMAART/APSkE/S88sgLAQIBYgIDAgLMBAUAG6D2BdqJofQB9IH0gahhAgHUBgcCASAICQDDCDHAJJfBOAB0NMDAXGwlRNfA/AM4PpA+kAx+gAxcdch+gAx+gAwc6m0AALTH4IQD4p+pVIgupUxNFnwCeCCEBeNRRlSILqWMUREA/AK4DWCEFlfB7y6k1nwC+BfBIQP8vCAAET6RDBwuvLhTYAIBIAoLAIPUAQa5D2omh9AH0gfSBqGAJpj8EIC8aijKkQXUEIPe7L7wndCVj5cWLpn5j9ABgJ0CgR5CgCfQEsZ4sA54tmZPaqQB8VA9M/+gD6QCHwAe1E0PoA+kD6QNQwUTahUirHBfLiwSjC//LiwlQ0QnBUIBNUFAPIUAT6AljPFgHPFszJIsjLARL0APQAywDJIPkAcHTIywLKB8v/ydAE+kD0BDH6ACDXScIA8uLEd4AYyMsFUAjPFnD6AhfLaxPMgMAgEgDQ4AnoIQF41FGcjLHxnLP1AH+gIizxZQBs8WJfoCUAPPFslQBcwjkXKRceJQCKgToIIJycOAoBS88uLFBMmAQPsAECPIUAT6AljPFgHPFszJ7VQC9ztRND6APpA+kDUMAjTP/oAUVGgBfpA+kBTW8cFVHNtcFQgE1QUA8hQBPoCWM8WAc8WzMkiyMsBEvQA9ADLAMn5AHB0yMsCygfL/8nQUA3HBRyx8uLDCvoAUaihggiYloBmtgihggiYloCgGKEnlxBJEDg3XwTjDSXXCwGAPEADXO1E0PoA+kD6QNQwB9M/+gD6QDBRUaFSSccF8uLBJ8L/8uLCBYIJMS0AoBa88uLDghB73ZfeyMsfFcs/UAP6AiLPFgHPFslxgBjIywUkzxZw+gLLaszJgED7AEATyFAE+gJYzxYBzxbMye1UgAHBSeaAYoYIQc2LQnMjLH1Iwyz9Y+gJQB88WUAfPFslxgBDIywUkzxZQBvoCFctqFMzJcfsAECQQIwB8wwAjwgCwjiGCENUydttwgBDIywVQCM8WUAT6AhbLahLLHxLLP8ly+wCTNWwh4gPIUAT6AljPFgHPFszJ7VSV6u3X, object: {data: {b64: /wD0pBP0vPLICw==, len: 80}, refs: [{data: {b64: YA==, len: 6}, refs: [{data: {b64: zA==, len: 8}, refs: [{data: {b64: 0A==, len: 5}, refs: [{data: {b64: CDHAJJfBOAB0NMDAXGwlRNfA/AM4PpA+kAx+gAxcdch+gAx+gAwc6m0AALTH4IQD4p+pVIgupUxNFnwCeCCEBeNRRlSILqWMUREA/AK4DWCEFlfB7y6k1nwC+BfBIQP8vAA=, len: 778}, refs: [], special: false}, {data: {b64: PpEMHC68uFNA, len: 66}, refs: [], special: false}], special: false}, {data: {b64: AA==, len: 2}, refs: [{data: {b64: AA==, len: 2}, refs: [{data: {b64: UD0z/6APpAIfAB7UTQ+gD6QPpA1DBRNqFSKscF8uLBKML/8uLCVDRCcFQgE1QUA8hQBPoCWM8WAc8WzMkiyMsBEvQA9ADLAMkg+QBwdMjLAsoHy//J0AT6QPQEMfoAINdJwgDy4sR3gBjIywVQCM8WcPoCF8trE8wA==, len: 964}, refs: [{data: {b64: ghAXjUUZyMsfGcs/UAf6AiLPFlAGzxYl+gJQA88WyVAFzCORcpFx4lAIqBOgggnJw4CgFLzy4sUEyYBA+wAQI8hQBPoCWM8WAc8WzMntVA==, len: 632}, refs: [], special: false}], special: false}, {data: {b64: AA==, len: 2}, refs: [{data: {b64: O1E0PoA+kD6QNQwCNM/+gBRUaAF+kD6QFNbxwVUc21wVCATVBQDyFAE+gJYzxYBzxbMySLIywES9AD0AMsAyfkAcHTIywLKB8v/ydBQDccFHLHy4sMK+gBRqKGCCJiWgGa2CKGCCJiWgKAYoSeXEEkQODdfBOMNJdcLAQA==, len: 986}, refs: [{data: {b64: UnmgGKGCEHNi0JzIyx9SMMs/WPoCUAfPFlAHzxbJcYAQyMsFJM8WUAb6AhXLahTMyXH7ABAkECM=, len: 448}, refs: [], special: false}, {data: {b64: wwAjwgCwjiGCENUydttwgBDIywVQCM8WUAT6AhbLahLLHxLLP8ly+wCTNWwh4gPIUAT6AljPFgHPFszJ7VQ=, len: 496}, refs: [], special: false}], special: false}, {data: {b64: O1E0PoA+kD6QNQwB9M/+gD6QDBRUaFSSccF8uLBJ8L/8uLCBYIJMS0AoBa88uLDghB73ZfeyMsfFcs/UAP6AiLPFgHPFslxgBjIywUkzxZw+gLLaszJgED7AEATyFAE+gJYzxYBzxbMye1UA, len: 858}, refs: [], special: false}], special: false}], special: false}, {data: {b64: 1AEGuQ9qJofQB9IH0gahgCaY/BCAvGooypEF1BCD3uy+8J3QlY+XFi6Z+Y/QAYCdAoEeQoAn0BLGeLAOeLZmT2qg, len: 525}, refs: [], special: false}], special: false}], special: false}, {data: {b64: oPYF2omh9AH0gfSBqGA=, len: 111}, refs: [], special: false}], special: false}], special: false}}]]"

Any example on transfer jetton?

tried to figure out the way to transfer jetton, failed. need help here.
really appreciate your work, this package is great.

GOAL: A complete API reference doc, automatically generated from documentation comments

Note: this is a collective, tracking issue towards the 1.0 release. Please, target fixes for the sub-issues mentioned below instead

image

API Documentation is already being automatically generated from code by pub.dev, but it currently only covers about 34.2% of all elements. Additionally, it doesn't provide examples of usage and, consequentially, in-editor help on hover is really lacking.

Sub-goals:

  • 100% coverage of documentation comments
  • Examples of usage where it's reasonable to provide them

Issues towards this goal:

  • .

P.S.: For the reference of better in-editor and API docs, see grammY, an excellent TypeScript framework for creating Telegram bots.

Proposals for the future

Which features to add/implement next?

  • #9
  • Simple wrappers for all types of endpoints and protocols, like in ton-access (https://www.orbs.com/ton-access)
  • Speed-up of mnemonics generation β€” currently it can take up to 1.5 minutes to generate one mnemonic list of 24 words, it can be improved like 10x :)
  • Jetton API improvements: better defaults and examples of usage
  • NFT API improvements: better defaults and examples of usage
  • Tutorials for docs.ton.org on usage in Flutter or standalone Dart cli/web apps
  • Rewrite parts in C/Zig, then use WASM

Doesn't make sense to implement here, needs a low-level implementation (C/C++, Zig, Rust, ...) + high-level wrappers (for this and other libraries):

P.S.: Track progress to match https://github.com/ton-org/ton and https://github.com/ton-org/ton-core
P.P.S: For better end-user API, consider moving from sealed classes to the following Union class as seen in dart-lang/sdk:

https://github.com/dart-lang/sdk/blob/957e06e3235f8746464cd66491b28cfbbad27baf/pkg/analysis_server/tool/lsp_spec/meta_model.dart#L329

Wrong signature length

TON expects 64 byte long signatures.
This way

return signingKey.sign(Uint8List.fromList(data)).toUint8List();

produces a concatenation of signature and something else and is 96 bytes long. You need to use signingKey.sign(Uint8List.fromList(data)).signature.toUint8List();, otherwise signature check fails in the wallet smart contract for example.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.