blockchainton

TON blockchain: How to check a transaction is success or not?


I use tonweb lirary to send a transaction. But the response I receive is some thing like { '@type': 'ok', '@extra': '1723716788.6499352:4:0.3484654105500832' }. This is a quite useless information, I don't know if my transaction is success or failed.

I tried to call to getTransaction() method to get my latest transaction, but I dont see the information about my transaction is success or not in there.


Solution

  • Solution: Using api of tonapi.io docs here to trace all exit code in transaction and children message

    Pros: Can trace all messages in transaction just from transaction hash or message hash. You can get message hash before you send transaction to blockchain.

    Cons: Has api rate limit (1 req/s for free user)

    import { HttpClient, Api, Trace } from 'tonapi-sdk-js';
    
    const API_KEY = "API_KEY";
    // Configure the HTTP client with your host and token
    const httpClient = new HttpClient({
        baseUrl: 'https://testnet.tonapi.io',
        baseApiParams: {
            headers: {
                Authorization: `Bearer ${API_KEY}`,
                'Content-type': 'application/json'
            }
        }
    });
    
    // Initialize the API client
    const client = new Api(httpClient);
    
    (async () => {
        const trace = await client.traces.getTrace("messsage hash")
        // console.log(trace)
        console.log(isTraceSuccess(trace))
    })()
    
    function isTraceSuccess(trace: Trace): { isSuccess: boolean, errorTransaction: any } {
        if (!trace.transaction.success) {
            // console.log(trace.transaction)
            return { isSuccess: false, errorTransaction: trace.transaction };
        }
        if (trace.children) {
            for (let i = 0; i < trace.children?.length; i++) {
                const res = isTraceSuccess(trace.children[i]);
                if (!res.isSuccess) {
                    return { isSuccess: false, errorTransaction: res.errorTransaction };
                }
            }
        }
    
        return { isSuccess: true, errorTransaction: undefined };
    }
    

    ps: tonapi has api endpoint to emulate transaction before actually send to blockchain