via tonconnect

This section contains a guide for sending transactions in TON blockchain using @tonconnect

Tonconnect package uses the sendTransaction method to send a transaction to the blockchain. An example of the usage is on their DOCs.

import React from 'react';
import TonWeb from 'tonweb';
import { DEX, pTON } from '@ston-fi/sdk';
import { useTonConnectUI, useTonAddress } from '@tonconnect/ui-react';

const dex = new DEX.v1.Router({
  tonApiClient: new TonWeb.HttpProvider(),
});

export const Example = () => {
  const wallet = useTonAddress();
  const [tonConnectUI] = useTonConnectUI();

  return (
    <button
      onClick={async () => {
        const swapTxParams = await dex.buildSwapTonToJettonTxParams({
          offerAmount: TonWeb.utils.toNano('1'), // swap 1 TON
          askJettonAddress: 'EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO', // for a STON
          minAskAmount: TonWeb.utils.toNano('0.1'), // but not less than 0.1 STON
          proxyTonAddress: pTON.v1.address.toString(),
          userWalletAddress: wallet,
        });

        await tonConnectUI.sendTransaction({
          validUntil: Date.now() + 1000000,
          messages: [
            {
              address: swapTxParams.to.toString(),
              amount: swapTxParams.gasAmount.toString(),
              payload: TonWeb.utils.bytesToBase64(
                await swapTxParams.payload.toBoc(),
              ),
            },
          ],
        });
      }}
    >
      Swap 1 TON to STON
    </button>
  );
};

Last updated