When building on The Open Network (TON), developers often ask how to retrieve the outcome after submitting a transaction. Many also wonder about the meaning of the "boc" field returned by TonClient and how to extract actionable insights from it. This guide addresses these questions and clarifies TON's transaction handling process.
Understanding Transactions on TON
A transaction on TON represents a fundamental unit of operation. It consists of several key components:
- An initial incoming message that triggers a smart contract
- Contract actions resulting from the message, such as state changes
- Optional outgoing messages sent to other participants
Essentially, transferring TON tokens involves sending a message to a wallet contract. The contract then executes the transfer based on the message content.
Preparing Transaction Messages
Before sending a transaction via TonConnect, you must define a valid message structure. The JavaScript SDK requires specific parameters:
export declare interface SendTransactionRequest {
validUntil: number;
network?: CHAIN;
from?: string;
messages: {
address: string;
amount: string;
stateInit?: string;
payload?: string;
}[];
}
For a basic transfer, construct a simple transaction object:
const transaction = {
validUntil: Math.floor(Date.now() / 1000) + 600,
messages: [
{
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
amount: "20000000",
},
],
};
To include a comment or custom payload, you must encode it as a Cell and convert it to Base64:
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"),
},
],
};
Retrieving Transaction Results
After sending a transaction, you receive a response containing a "boc" (Bag of Cells) field:
const [tonConnectUi] = useTonConnectUI();
const result = await tonConnectUi.sendTransaction(transaction);
This boc represents the initial message that triggered the transaction. To confirm transaction completion and obtain results, you must monitor the network using TonClient.
The following function demonstrates how to wait for transaction confirmation by comparing message hashes:
const waitForTransaction = async (
options: WaitForTransactionOptions,
client: TonClient
): Promise<Transaction | null> => {
const { hash, refetchInterval = 1000, refetchLimit, address } = options;
return new Promise((resolve) => {
let refetches = 0;
const walletAddress = Address.parse(address);
const interval = setInterval(async () => {
refetches += 1;
console.log("waiting transaction...");
const state = await client.getContractState(walletAddress);
if (!state || !state.lastTransaction) {
clearInterval(interval);
resolve(null);
return;
}
const lastLt = state.lastTransaction.lt;
const lastHash = state.lastTransaction.hash;
const lastTx = await client.getTransaction(walletAddress, lastLt, lastHash);
if (lastTx && lastTx.inMessage) {
const msgCell = beginCell()
.store(storeMessage(lastTx.inMessage))
.endCell();
const inMsgHash = msgCell.hash().toString("base64");
if (inMsgHash === hash) {
clearInterval(interval);
resolve(lastTx);
}
}
if (refetchLimit && refetches >= refetchLimit) {
clearInterval(interval);
resolve(null);
}
}, refetchInterval);
});
};
This implementation periodically checks the contract state, retrieves the latest transaction, and compares the incoming message hash with the original transaction hash. Alternative approaches include monitoring transaction lists and filtering for relevant messages.
👉 Explore transaction monitoring tools
Frequently Asked Questions
What does "boc" stand for in TON transactions?
BOC stands for "Bag of Cells," which is the fundamental data structure used in TON blockchain. It represents serialized binary data that contains information about messages, transactions, or blocks. When you receive a boc after sending a transaction, it contains the serialized message that was sent to the network.
How long does it typically take for a TON transaction to confirm?
TON transactions usually confirm within 5-10 seconds under normal network conditions. However, the actual confirmation time can vary based on network congestion, transaction complexity, and the current load on validators. The asynchronous nature of TON means developers should implement proper waiting mechanisms.
Can I retrieve transaction results without constantly polling the network?
While polling is a common approach, TON also supports subscription-based methods through websockets or other real-time interfaces. Some services provide webhook capabilities that can notify your application when transactions complete, reducing the need for continuous polling.
What information can I extract from a confirmed transaction?
A confirmed transaction contains valuable information including transaction hash, logical time (lt), storage fees, computation fees, action phases, bounce phases, the incoming message that triggered it, and any outgoing messages it generated. This data helps developers understand the complete execution context.
How does TON's transaction model differ from Ethereum's?
Unlike Ethereum's synchronous transaction execution, TON uses an asynchronous message-passing architecture. Transactions don't have immediate results—instead, they generate messages that trigger subsequent transactions. This design enables parallel processing and higher throughput but requires different development approaches.
What should I do if my transaction doesn't confirm?
First, check that you've provided sufficient gas fees and that the transaction parameters are correct. If the transaction remains unconfirmed for an extended period, you may need to resend it with a higher fee or investigate potential network issues. Monitoring tools can help diagnose transaction problems.
Conclusion
TON's message-based architecture requires developers to adopt different approaches compared to traditional blockchain platforms. By understanding how transactions work and implementing proper monitoring techniques, you can effectively track transaction outcomes and build robust applications on TON.
The key takeaways include properly constructing transaction messages, understanding the significance of the boc field, and implementing efficient polling mechanisms to detect transaction completion. As TON continues to evolve, these fundamental concepts will remain essential for developers building decentralized applications on this innovative platform.