via ton

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

Ton package uses the sendTransfer method to send a transaction to the blockchain. An example of the usage is on their README.

import TonWeb from "tonweb";
import { TonClient, WalletContractV4, internal, toNano, Cell } from "@ton/ton";
import { mnemonicToPrivateKey } from "@ton/crypto";
import { DEX, pTON } from "@ston-fi/sdk";

const MNEMONIC = Array.from({ length: 24 }, (_, i) => `your mnemonic word ${i + 1}`); // replace with your mnemonic
const TON_API_URL = "https://toncenter.com/api/v2/jsonRPC";

const client = new TonClient({
    endpoint: TON_API_URL,
});

const keyPair = await mnemonicToPrivateKey(MNEMONIC);

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

const contract = client.open(wallet);

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

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

await contract.sendTransfer({
    seqno: await contract.getSeqno(),
    secretKey: keyPair.secretKey,
    messages: [internal({
        to: swapTxParams.to.toString(),
        value: BigInt(swapTxParams.gasAmount.toString()),
        body: Cell.fromBase64(TonWeb.utils.bytesToBase64(await swapTxParams.payload.toBoc())),
    })]
});

Last updated