npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ton-client-web-js

v0.28.0

Published

TON Client Library for Web

Downloads

51

Readme

TON Javascript Web SDK

npm publish

Community links:

Chat on Telegram Gitter

Documentation

GraphQL API and SDK documentation

The JavaScript SDK implements the client-side libraries used by applications working with TON OS GraphQL API.
This package supports web (browser), mobile-web, and server (Node.js) clients.

Common Javascript SDK is distributed via npm package.

Attention! Because the JS library uses pre-compiled core sdk rust library, you need to install it via platform-dependable web package that will automatically install common js package + download and link pre-compiled rust core to your project:

npm install ton-client-web-js

To get started using TON Javascript SDK, see Add SDK to your Application.

Using ton-client-web-js

Requirements

You can run ton-client-web-js in a browser.

In this example, we'll use webpack to manage assets. Since it requires Node.js, you’ll need to install the latest version.

Let's start with a clean webpack project:

mkdir HelloTonClient
cd HelloTonClient
npm init -y
npm install webpack webpack-cli --save-dev

Now create the starting files in the root directory of the project:

 |- /src
   |- index.js
 |- /dist
   |- index.html

Here, the src file contains our source code for further webpack usage, and dist contains files that are directly served to the browser.

Next, create dist/index.html in your directory. It will act as the browser entry point:

<!doctype html>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <script src="main.js"></script>
  </body>
</html>

Webpack will later use src to generate main.js.

Use npm to install ton-client-web-js package:

npm install ton-client-web-js

To copy tonclient.wasm from node_modules\ton-client-web-js use copy-webpack-plugin in webpack.config.js.

Install copy-webpack-plugin

npm install copy-webpack-plugin --save-dev

Next, create webpack.config.js in your directory

const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  mode: 'development',
  plugins: [
    new CopyPlugin({
      patterns: [
        { from: './node_modules/ton-client-web-js/tonclient.wasm' }
      ],
    }),
  ],
};

Finally, add the code below to src/index.js:

import { TONClient, setWasmOptions } from 'ton-client-web-js';

function addHTML(message) {
    document.body.insertAdjacentHTML("beforeend", `<p>${message}</p>`);
}

window.addEventListener('load', () => {
    (async () => {
        // Adding an HTML update function
        setWasmOptions({
            addHTML,
        });
        let createStart = Date.now();
        // creating a TONClient wit a net.ton.dev connection
        const client = await TONClient.create({
            servers: ['net.ton.dev']
        });
        // requesting TONClient creation time stamp
        addHTML(`Client creation time: ${(Date.now() - createStart)}`);
        // displaying the current client version
        addHTML(`Client version: ${await client.config.getVersion()}`);
        addHTML(`Client connected to: ${await client.config.data.servers}`);
        const queryStart = Date.now();
        // requesting top 10 accounts sorted by balance from net.ton.dev/graphql
        const accounts = await client.queries.accounts.query({}, 'id balance', [{path:'balance', direction:'DESC'}], 10);
        addHTML(`Query time: ${(Date.now() - queryStart)}`);
        // displaying the data
        addHTML(`<table>${accounts.map(x => `<tr><td>${x.id}</td><td>${BigInt(x.balance)}</td></tr>`).join('')}</table>`);
        // displaying the data received time stamp
        addHTML(`Now is: ${new Date()}`);
    })();
});

To finish the example, let's build a dist/main.js from our source:

npx webpack

To check if it works we'll need a simple HTTP server:

npm install light-server

Now you can run your app:

npx light-server -s dist

Doing so will show you the links to the app within the browser. Open your browser and take a look at your first app! Hit CTRL-C to stop the server.


See https://docs.ton.dev/86757ecb2/p/92b041-overview for documentation


Copyright 2018-2020 TON DEV SOLUTIONS LTD.