Prerequisite You should have installed Node.js (version 18.10.0 or higher).

Step 1: Install the Onton SDK:

Step 2: Import the required libraries in your project:

import dotenv from "dotenv";
import TonWeb from "tonweb";
import { TonClient, toNano } from "@ton/ton";
import { mnemonicToKeyPair } from "tonweb-mnemonic";
import { Factory } from 'onton-sdk';
import { WalletV4ContractR2 } from "tonweb/dist/types/contract/wallet/v4/wallet-v4-contract-r2";

dotenv.config();

Step 3: Define a helper function to wait for the sequence number to change:

async function awaitSeqno(wallet: WalletV4ContractR2, seqno: number): Promise<boolean> {
  let currentSeqno = seqno;
  while (currentSeqno == seqno) {
    await new Promise((resolve) => setTimeout(resolve, 1500));
    seqno = (await wallet.methods.seqno().call()) ?? 0;
  }
  return Promise.resolve(true);
}

Step 4: Create the main function to initialize the Onton SDK and perform operations:

const main = async () => {
  const apiKey = "YOUR_API_KEY_HERE";
  const walletAddress = "YOUR_WALLET_ADDRESS_HERE";
  const token0JettonAddress = "YOUR_TOKEN0_JETTON_ADDRESS_HERE";
  const token1JettonAddress = "YOUR_TOKEN1_JETTON_ADDRESS_HERE";
  const token0Amount = toNano("YOUR_TOKEN0_AMOUNT_HERE");
  const token1Amount = toNano("YOUR_TOKEN1_AMOUNT_HERE");
  const mnemonic = "YOUR_MNEMONIC_HERE".split(" ");
  const referralAddress = "YOUR_REFERRAL_ADDRESS_HERE";
  
  const tonClient = new TonClient({
    endpoint: "https://toncenter.com/api/v2/jsonRPC",
    apiKey: apiKey
  });

  const ontonSDK = tonClient.open(new Factory());
};

Step 5: Retrieve deposit liquidity transaction messages:

  const depositLiquidityTransactionMessages = await ontonSDK.getDepositLiquidity({
    walletAddress: walletAddress,
    token0JettonAddress: token0JettonAddress,
    token0Amount: token0Amount,
    token1JettonAddress: token1JettonAddress,
    token1Amount: token1Amount
  });

Step 6: Initialize TonWeb and the wallet:

  const provider = new TonWeb.HttpProvider("https://toncenter.com/api/v2/jsonRPC", { apiKey: apiKey });
  const tonweb = new TonWeb(provider);
  const keyPair = await mnemonicToKeyPair(mnemonic);
  const wallet = new tonweb.wallet.all["v4R2"](tonweb.provider, {
    publicKey: keyPair.publicKey,
    wc: 0
  });

Step 7: Perform the first transfer:

  const response0 = await wallet.methods
    .transfer({
      secretKey: keyPair.secretKey,
      toAddress: depositLiquidityTransactionMessages[0].to.toString(),
      amount: new tonweb.utils.BN(depositLiquidityTransactionMessages[0].value),
      payload: TonWeb.boc.Cell.oneFromBoc(
        TonWeb.utils.base64ToBytes(depositLiquidityTransactionMessages[0].body!.toBoc().toString("base64"))
      ),
      sendMode: 3,
      seqno: (await wallet.methods.seqno().call()) ?? 0
    })
    .send();

Step 8: Wait for the sequence number to change:

  let seqno = (await wallet.methods.seqno().call()) ?? 0;
  await awaitSeqno(wallet, seqno);

Step 9: Perform the second transfer:

  const response1 = await wallet.methods
    .transfer({
      secretKey: keyPair.secretKey,
      toAddress: depositLiquidityTransactionMessages[1].to.toString(),
      amount: new tonweb.utils.BN(depositLiquidityTransactionMessages[1].value),
      payload: TonWeb.boc.Cell.oneFromBoc(
        TonWeb.utils.base64ToBytes(depositLiquidityTransactionMessages[1].body!.toBoc().toString("base64"))
      ),
      sendMode: 3,
      seqno: (await wallet.methods.seqno().call()) ?? 0
    })
    .send();
};

Step 10: Execute the main function:

main();

Replace the following environment variables with your actual values:

  • API_KEY
  • WALLET_ADDRESS
  • TOKEN0_JETTON_ADDRESS
  • TOKEN1_JETTON_ADDRESS
  • TOKEN0_AMOUNT
  • TOKEN1_AMOUNT
  • MNEMONIC
  • REFERRAL_ADDRESS

Example Usage

Here’s an example of how to use the Onton SDK to perform a token swap with a referral address:

async function swapTokens() {
  try {
    const swapTransactionMessage = await ontonSDK.getSwap({
      walletAddress: walletAddress,
      sendJettonAddress: "YOUR_SEND_JETTON_ADDRESS",
      receiveJettonAddress: "YOUR_RECEIVE_JETTON_ADDRESS",
      referralAddress: "YOUR_REFERRAL_ADDRESS",
      tokenAmount: toNano(30)
    });

    const response = await wallet.methods
      .transfer({
        secretKey: keyPair.secretKey,
        toAddress: swapTransactionMessage.to.toString(),
        amount: new tonweb.utils.BN(swapTransactionMessage.value),
        payload: TonWeb.boc.Cell.oneFromBoc(
          TonWeb.utils.base64ToBytes(swapTransactionMessage.body!.toBoc().toString("base64"))
        ),
        sendMode: 3,
        seqno: (await wallet.methods.seqno().call()) ?? 0
      })
      .send();

    let seqno = (await wallet.methods.seqno().call()) ?? 0;
    await awaitSeqno(wallet, seqno);

    console.log(response);
  } catch (error) {
    console.error('Swap error:', error);
  }
}

swapTokens();

Replace the following variables with your actual values:

  • YOUR_SEND_JETTON_ADDRESS
  • YOUR_RECEIVE_JETTON_ADDRESS
  • YOUR_REFERRAL_ADDRESS

Onton SDK Versions

Make sure to use the latest version of the Onton SDK to access the most recent features and improvements.

Deployment

Onton Finance is built on the TON blockchain, ensuring fast and secure transactions.

When you deploy your application using the Onton SDK, it interacts directly with the TON blockchain. There’s no need for additional deployment steps.

Troubleshooting

Here are some common issues you might encounter while working with the Onton SDK and how to resolve them.

If you encounter any other issues or have questions, please reach out to our support team or consult the Onton Finance community forums for assistance.

For the latest updates and changes to the Onton SDK, refer to the SDK changelog.