Basics

Mainnet or Testnet?

TON Blockchain operates on two variations - mainnet and testnet. The mainnet allows real transactions with real TON coins, while testnet is used for testing and development purposes.

Mainnet transactions involve real TON coins whilst testnet is a sandbox for testing and development. Be cautious when working with mainnet, as any mistakes can lead to the loss of real funds.

Creating a Wallet using an App

Setting up a TON Wallet can be accomplished with the following steps:

  1. Visit https://ton.org/wallets.
  2. Choose a wallet app from the list, such as Tonkeeper or TON Wallet.
  3. Follow the instructions provided by the chosen wallet app to create a new wallet.
  4. Securely store your recovery phrase and private keys, as they are essential for accessing your wallet and funds.

Backing Up the Recovery Phrase

A 24-word recovery phrase is key to accessing your wallet. Losing this phrase will result in loss of access to your wallet and funds. Make sure to store it securely, such as by writing it down on paper and keeping it in a safe place.

Viewing the Wallet in an Explorer

Your wallet address can be retrieved by tapping on the top-left of the Tonkeeper app or the “Receive” button. Once you have your address, you can view your wallet’s details and transactions using a TON blockchain explorer, such as TON.sh or TONScan.

Funding and Activating the Wallet Contract

Funding the wallet can be achieved by transferring TON coins to your address. After the wallet is funded, the wallet contract can be deployed. This process is usually automated by the wallet app, but it’s essential to understand that the wallet contract needs to be deployed before you can start using it.

Wallet Contracts and Their Versions

The version of your smart contract code can be seen in the “Contract Type” field in the explorer. It’s important to know which version of the wallet contract you are using, as different versions may have different features and compatibility.

Setting up Local Machine for Programmatic Access

Before writing code to interact with your wallet, certain developer tools need to be installed on your local machine, such as:

  • TON SDK: A set of libraries and tools for developing TON applications.
  • Node.js: A JavaScript runtime environment.
  • A code editor, such as Visual Studio Code or WebStorm.

Fetching the Wallet Address Programmatically

After setting up your local machine, you can retrieve your wallet’s address in the code and match it with the explorer’s address. This can be done using the TON SDK, which provides functions for interacting with the TON blockchain.

Here’s an example of how to fetch your wallet address using the TON SDK in JavaScript:

const { TonClient } = require('@tonclient/core');
const { libNode } = require('@tonclient/lib-node');

TonClient.useBinaryLibrary(libNode);

async function getWalletAddress() {
  const client = new TonClient({
    network: { endpoints: ['main.ton.dev'] } // Replace with your preferred network endpoint
  });

  const walletAddress = '...'; // Replace with your wallet address

  try {
    const walletInfo = await client.net.query_collection({
      collection: 'accounts',
      filter: { id: { eq: walletAddress } },
      result: 'id'
    });

    console.log('Wallet address:', walletInfo.result[0].id);
  } catch (error) {
    console.error('Error fetching wallet address:', error);
  } finally {
    client.close();
  }
}

getWalletAddress();

Reading Wallet State from the Chain

You can read live state data from your wallet contract by directly connecting to the live blockchain network. This allows you to retrieve information such as your wallet’s balance, transaction history, and other relevant data.

Here’s an example of how to read your wallet’s balance using the TON SDK in JavaScript:

const { TonClient } = require('@tonclient/core');
const { libNode } = require('@tonclient/lib-node');

TonClient.useBinaryLibrary(libNode);

async function getWalletBalance() {
  const client = new TonClient({
    network: { endpoints: ['main.ton.dev'] } // Replace with your preferred network endpoint
  });

  const walletAddress = '...'; // Replace with your wallet address

  try {
    const walletBalance = await client.net.query_collection({
      collection: 'accounts',
      filter: { id: { eq: walletAddress } },
      result: 'balance'
    });

    console.log('Wallet balance:', walletBalance.result[0].balance);
  } catch (error) {
    console.error('Error fetching wallet balance:', error);
  } finally {
    client.close();
  }
}

getWalletBalance();

Sending a Transfer Transaction to the Chain

You can also send TON from your wallet to another wallet on the chain. However, this action will require your wallet’s private key.

Be extremely cautious when handling private keys, as they grant full access to your wallet and funds. Never share your private keys with anyone, and make sure to store them securely.

Here’s an example of how to send a transfer transaction using the TON SDK in JavaScript:

const { TonClient } = require('@tonclient/core');
const { libNode } = require('@tonclient/lib-node');

TonClient.useBinaryLibrary(libNode);

async function sendTransferTransaction() {
  const client = new TonClient({
    network: { endpoints: ['main.ton.dev'] } // Replace with your preferred network endpoint
  });

  const walletAddress = '...'; // Replace with your wallet address
  const privateKey = '...'; // Replace with your wallet's private key
  const recipientAddress = '...'; // Replace with the recipient's wallet address
  const amount = '1000000000'; // Amount to send in nanoTON (1 TON = 1000000000 nanoTON)

  try {
    const walletState = await client.net.query_collection({
      collection: 'accounts',
      filter: { id: { eq: walletAddress } },
      result: 'balance'
    });

    const seqno = walletState.result[0].seqno || 0;

    const transferMessage = {
      $$type: 'Transfer',
      amount: amount,
      destination: recipientAddress
    };

    const messageBody = await client.abi.encode_message_body({
      abi: abiContract(TransferAbi),
      call_set: {
        function_name: 'transfer',
        input: transferMessage
      },
      is_internal: true,
      signer: { type: 'Keys', keys: privateKey }
    });

    const transactionInfo = await client.processing.process_message({
      message_encode_params: {
        address: walletAddress,
        abi: abiContract(WalletAbi),
        call_set: {
          function_name: 'sendTransaction',
          input: {
            dest: recipientAddress,
            value: amount,
            bounce: false,
            flags: 3,
            payload: messageBody.body
          }
        },
        signer: { type: 'Keys', keys: privateKey }
      },
      send_events: false
    });

    console.log('Transaction sent successfully:', transactionInfo);
  } catch (error) {
    console.error('Error sending transaction:', error);
  } finally {
    client.close();
  }
}

sendTransferTransaction();

By leveraging the TON blockchain, you can perform efficient and low-cost token transactions on the ONTON Finance platform. Enjoy tight spreads and a smooth trading experience, while getting ONTON tokens as a discount on transaction fees.