How to Retrieve Transaction Results After Sending on TON – Using TonConnect as an Example

·

In the Telegram Dev Channel, a common question arises: How can I retrieve the result of a transaction sent on TON? Or, when using TonClient, what does the returned Boc (Bag of Cells) represent, and how can I extract more useful information? This article answers these questions to deepen your understanding of TON’s transaction workflow.


Understanding TON Transactions

Before diving into transaction results, let’s clarify what a TON transaction entails. A TON transaction consists of:

For example, a TON transfer transaction is fundamentally a message sent to a wallet contract, which executes the transfer based on the message content.


Preparing the Transaction Message

When sending a transaction via TonConnect, you must define a valid message structure. Using the JS TonConnect SDK, the transaction parameters include:

ParameterDescription
validUntilUnix timestamp until which the transaction is valid.
messages[]Array of message objects containing recipient address, amount, and payload.

Example: Basic Transfer

const transaction = {
  validUntil: Math.floor(Date.now() / 1000) + 600,
  messages: [{
    address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
    amount: "20000000",
  }],
};

Example: Transfer with Comment

To attach a comment, encode the payload as a Base64 string from a Cell:

import { beginCell } from "@ton/ton";
const body = beginCell()
  .storeUint(0, 32)
  .storeStringTail("Hello, TON!")
  .endCell();
const transaction = {
  validUntil: Math.floor(Date.now() / 1000) + 600,
  messages: [{
    address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
    amount: "20000000",
    payload: body.toBoc().toString("base64"),
  }],
};

👉 Explore more message types here


Retrieving Transaction Results

After sending a transaction via TonConnect, the response includes a boc (Bag of Cells) string:

const [tonConnectUi] = useTonConnectUI();
const result = await tonConnectUi.sendTransaction(transaction);
// Response: { boc: "..." }

This boc represents the initial message triggering the transaction. To confirm completion:

  1. Monitor Transaction Status: Use TonClient to query the latest transaction and compare its inMessage hash with your boc.
  2. Validate Completion: If the hashes match, the transaction is confirmed.

Code Example:

const waitForTransaction = async (options, client) => {
  const { hash, address } = options;
  const walletAddress = Address.parse(address);
  const state = await client.getContractState(walletAddress);
  if (!state?.lastTransaction) return null;

  const lastTx = await client.getTransaction(
    walletAddress,
    state.lastTransaction.lt,
    state.lastTransaction.hash
  );
  if (lastTx?.inMessage) {
    const msgCell = beginCell()
      .store(storeMessage(lastTx.inMessage))
      .endCell();
    const inMsgHash = msgCell.hash().toString("base64");
    if (inMsgHash === hash) return lastTx;
  }
  return null;
};

Key Takeaways

👉 View the full code demo on GitHub


FAQ

Q: What is a Boc in TON?
A: A "Bag of Cells" (Boc) is TON’s serialized data format for messages and transactions.

Q: How long does a TON transaction take?
A: Typically a few seconds, but always validate via inMessage hash matching.

Q: Can I attach comments to TON transfers?
A: Yes, by encoding the payload as a Base64 Cell.

Q: Why monitor inMessage hashes?
A: It ensures the transaction you sent is the one confirmed on-chain.


This guide equips you to handle TON transactions confidently. For further exploration, refer to TON’s official docs. Happy building! 🚀