In this page, We'll have a script demo about how to use TONX API SDK.

Follow the demo and deploy your first inscription!


Install tonfura-sdk and demo-related packages

npm install tonfura-sdk yargs dotenv

In order to build the real transaction, we need to build the BoC. We can leverage the popular ton dev packages.

npm install @ton/core @ton/crypto @ton/ton

Set up your .env file

TONFURA_KEY=
MNEMONICS=

We provide 3 examples below.

  1. Get chain information
  2. Get the Ton foundation(testnet) address's balance.
  3. Create(deploy) our own inscription. Please read the code annotation.
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const {
  internal,
  WalletContractV4,
  external,
  SendMode,
  beginCell,
  storeMessage,
} = require("@ton/ton");
const {
  createWalletTransferV4,
} = require("@ton/ton/dist/wallets/signing/createWalletTransfer");
const { mnemonicToPrivateKey } = require("@ton/crypto");

const { Network, Tonfura } = require("tonfura-sdk");

require("dotenv").config();

const argvs = yargs(hideBin(process.argv)).parse();

if (!argvs.cmd) {
  console.error("Please specify a command");
  process.exit(1);
}

const { TONFURA_KEY, MNEMONICS } = process.env;

const main = async () => {
  const tonfura = new Tonfura({
    apiKey: TONFURA_KEY,
    network: Network.Testnet,
  });

  switch (argvs.cmd) {
    case "getMasterchainInfo":
      const masterchainInfo = await tonfura.core.getMasterchainInfo();
      console.log(
        `masterchainInfo: ${JSON.stringify(masterchainInfo.data.result)}`
      );
      break;
    case "getAddressBalance":
      const TON_FOUNDATION_ADDRESS =
        "kQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqKYH";
      const addressBalance = await tonfura.core.getAddressBalance(
        TON_FOUNDATION_ADDRESS
      );
      console.log(
        `Get ton foudation address TON balance: ${
          addressBalance.data.result / 1e9
        }`
      );
      break;
    case "sendBoc":
      /*
        Inscription deployment demo
        For sending transaction, you need mnemonics to generate private key and sign the transaction.
        This demo skip the wallet deployment part. In this case you need an activated wallet(V4).
        https://answers.ton.org/question/1538715235714600960/how-can-i-activate-my-wallet
      */
      const mnemonics = MNEMONICS.split(" ");
      const keyPair = await mnemonicToPrivateKey(mnemonics);

      const workchain = 0;
      const wallet = WalletContractV4.create({
        workchain,
        publicKey: keyPair.publicKey,
      });

      console.log(`wallet address: ${wallet.address}`);

      const seqno = (
        await tonfura.core.getWalletInformation(wallet.address.toString())
      ).data.result.seqno;

      console.log(`wallet seqno: ${seqno}`);

      const internalMessage = createWalletTransferV4({
        seqno,
        secretKey: keyPair.secretKey,
        walletId: wallet.walletId,
        sendMode: SendMode.PAY_GAS_SEPARATELY,
        messages: [
          internal({
            value: "0.1", // you need to prepare 0.1 TON in your wallet
            to: wallet.address,
            // you can change this line to customize your inscription deployment
            body: 'data:application/json,{"p":"ton-20","op":"deploy","tick":"YourFirstInscription","amt":"1"}',
          }),
        ],
      });

      const externalMessage = external({
        to: wallet.address,
        init: null,
        body: internalMessage,
      });

      const boc = beginCell()
        .store(storeMessage(externalMessage))
        .endCell()
        .toBoc();

      await tonfura.transact.sendBoc(boc.toString("base64"));
      break;
  }
  process.exit(0);
};

try {
  main();
} catch (error) {
  console.error(error);
}