Git Product home page Git Product logo

tonlib.net's Introduction

TonLib.NET

Wrapper around tonlibjson library for accessing Telegram Open Network lite servers (nodes) directly via ADNL protocol.

Does not require TonAPI, TonCenter API, TonKeeper API or any other HTTP API.

NuGet NuGet downloads Framework Framework Framework GitHub License

โš  For net6.0 uses System.Text.Json package v7.0.0 (from net7.0) - it makes [de]serialization much simpler (because of Polymorphic serialization). It only updates System.Text.Encodings.Web (v6.0 -> v7.0) as a transitive dependency, which I think is acceptable.

Features

  • Read account balance and transaction history (sample);
  • Operate with keys and mnemonics (sample);
  • Send TON to different account (sample);
  • Work with BOCs and Cells (read and parse using Slices, compose using CellBuilder) (sample), including HashmapE (tests);
  • Read and parse smartcontract data, call get-methods (sample);
  • Resolve domains (sample)
  • TonRecipes class (ready-to-use one-liners) to work with:

And more:

  • Easy-to-extend:
    • Describe and call new TonLib methods without waiting for new version (sample);
    • Add your own Recipe methods to existing repice classes without recompiling library;
  • Connects to random LiteServer or choosen by you;
  • Reconnects to different LiteServer if previous one fails (but you need to handle exceptions and implement retry logic yourself, for example with Polly);
  • No 3rd-party packages except native .NET assemblies;

Usage

Register in Startup (for console projects - create instance manually or see demo project for hosted sample):

services.AddSingleton<ITonClient, TonClient>();

And use:

// Obtain client from DI
var tonClient = app.Services.GetRequiredService<ITonClient>();

// You need to init it before first use.
// During this, TON network config file is loaded from internet.
// Subsequent calls to `InitIfNeeded` will be ignored, 
//   so no need for you to have additional variable 
//   to remember that you already called it.
await tonClient.InitIfNeeded();

// Use 'Execute' to send requests.
var lsi = await tonClient.Execute(new LiteServerGetInfo());
logger.LogInformation("Server time: {Now}", lsi.Now);

Run Demo project for more samples.

Installing dependencies and running a demo

This library is a wrapper around tonlibjson library. You need to obtain complied copy of it (and its dependencies) yourself.

Go to https://github.com/ton-blockchain/ton/releases, open latest release, scroll to "Assets" and download tonlibjson.* for your OS. Make sure this file will be available for your running program (for example, add it to your project and set "Copy to Output Directory" to "Copy if newer").

The number of additional dependencies you need depends of what you already have on your machine. On my Win machine I also needed libcrypto-1_1-x64.dll from OpenSSL v1.1. You may use Process Monitor to find what it wants if it fails to run.

3rd-party libraries and dependencies

  • Microsoft.Extensions.Logging.Abstractions v6.0.0 / v7.0.0 / v8.0.0
  • Microsoft.Extensions.Options v6.0.0 / v7.0.0 / v8.0.0
  • System.Text.Json v7.0.0 for net6.0 and net7.0, v8.0.0 for net8.0
    • System.Text.Encodings.Web v7.0.0 for net6.0 as transitive dependency

Donate

just_dmitry.ton

Useful links

tonlib.net's People

Contributors

justdmitry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tonlib.net's Issues

Extensibility, liteServer.* methods.

Hello again! Wonderful lib as always.

I'm looking to add through extensibility or as a PR, the ability to use more liteServer.* methods available here: https://raw.githubusercontent.com/ton-blockchain/ton/master/tl/generate/scheme/lite_api.tl

Specifically I want the capability to call liteServer.runSmcMethod which allows you to specify the block height you are executing at, and so read historical state.

I've seen other libs implement this such as this JS example: https://github.com/ton-core/ton-lite-client/blob/8e7089c35aaac7b7b75833c3b68a777d1d023c84/src/client.ts#L408

I first tried to add liteServer.getVersion which is on the referenced .tl and seems innocous enough, should just give a subset of what is available in liteServer.getInfo.

I've added this Request:

[TLSchema("liteServer.getVersion = liteServer.Version")]
public class GetVersion : RequestBase<Types.LiteServer.VersionDto>
{
    // Nothing
}

But I get TonLibDotNet.TonClientException: 'Empty response received'

Am I misunderstanding how to add new request to your SDK? I'm not yet using extensibility, I've just added it to TonClientLiteServerExtensions and referencing it in Samples just to check it works.

Any advice would be appreciated.

Example on how to deserialize to TEP64 dictionary.

Hello! Thank you for your excellent library.

I'm trying to pull Jetton Master data, I am using the reference specification to assist me: https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md#specification

I have the following example:

First, I pull the get_jetton_data:

Info smcRouter = await _ton.TrySmcLoad(Address);
var result = await _ton.SmcRunGetMethod(smcRouter.Id, new MethodIdName("get_jetton_data"));

var stack = result.Stack.ToArray();

var totalSupply = stack[0].ToBigInteger();
var mintable = stack[1].ToBigInteger();
var adminAddress = stack[2].GetAddress();
var metadataCell = stack[3].ToBoc().RootCells[0];

Then, I want to deserialize the metadata cell to it's components, however I'm struggling to see how to do this.

Initially I want to decompose it to first grab the Decimals, where the case is there are onchain properties for, I am using this jUSDT token to test this:

https://tonscan.com/EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA?tab=methods

This reports the following for jetton_content:

{
  "type": "onchain",
  "data": {
    "image": "https://bridge.ton.org/token/1/0xdac17f958d2ee523a2206206994597c13d831ec7.png",
    "uri": "https://bridge.ton.org/token/1/0xdac17f958d2ee523a2206206994597c13d831ec7.json",
    "decimals": "6"
  }
}

Now I want to deserialize my metadataCell into a dictionary for me to extract the above representation, but I can't see how to do this. I can call LoadDict on the cell, which it should be, I get back a Cell.
I then use the specification referenced for Token Data, that the key should be a SHA256 as a string, and the value should be serialized later (so I pull the Slice). This isn't working for me and fails to deserialize the dictionary (No dictionary found).

public static Metadata ParseFromCell(Cell cell)
{
    const int OnchainContent = 0;
    const int OffchainContent = 1;

    var slice = cell.BeginRead();
    var contentType = slice.LoadByte();

    if(contentType != OnchainContent)
        throw new Exception("Unsupported representation type");

    var dict = slice.LoadDict();
    var output = dict.BeginRead().LoadAndParseDict<string, TonLibDotNet.Cells.Slice>(
        256,
        x => x.LoadString()!,
        x => x,
        new StringComparer());

    return new Metadata();
}

Do you have any suggestions on how I can get this deserialized correctly, or at least get a reference to a Dictionary<TKey,TValue> for me to take this further?

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.