ton-connect / sdk

SDK for TON Connect 2.0 — a comprehensive communication protocol between wallets and apps in TON ecosystem
Apache License 2.0
326 stars 92 forks source link

[<SDK or UI or UI-REACT or PROTOCOL>]: <brief description of your question> #232

Open devue4 opened 3 months ago

devue4 commented 3 months ago

Your Question

hello I wanted to make a function that after the user accepts the transaction Based on the boc that the sendTransaction method returns, I will measure whether it has been deposited or not

I want to use this in two places First, after accepting the transaction by the user, check and tell the user whether the transaction was done correctly or not

And secondly, the server side checks whether the transaction is successful or not If it is successful, it will be added to the user's inventory

I wrote this code and it works But it has many problems and cannot be used in real programs I would be grateful if you could help me

Context

i am tying make a online shop can pay with ton

What have you tried so far?

https://medium.com/coinmonks/understanding-ton-transactions-how-to-track-transaction-results-and-utilize-tonclient-b992336eb3a3 https://docs.ton.org/develop/dapps/asset-processing/#check-contracts-transactions

Relevant Code or Commands


import { TonConnectUI } from "@tonconnect/ui";
import {v4 as uuidv4} from "uuid"
import { beginCell,toNano} from "@ton/ton";
import TonWeb from "tonweb";

const tonconnect = new TonConnectUI({
  manifestUrl: 'https://..../manifest.json',
  buttonRootId: 'connect'
})

const tonweb = new TonWeb(new TonWeb.HttpProvider('https://toncenter.com/api/v2/jsonRPC', {apiKey: '....'}))

function MakeTxInvoice(){
  const txId = uuidv4()

  const body = beginCell()
  .storeUint(0, 32)
  .storeStringTail(txId)
  .endCell();

  return{
    txid : txId,
    validUntil: Math.floor(Date.now() / 1000) + 360,
    messages: [
        {
            address: "UQCM4-pSTwwn-JIGKBb4FlilvEVAkdBUlJTykNpWBUtQ-VHm",
            amount: toNano(0.05).toString(),
        },
        {
          address: "UQCM4-pSTwwn-JIGKBb4FlilvEVAkdBUlJTykNpWBUtQ-VHm",
          amount: toNano(0.05).toString(),
          payload: body.toBoc().toString("base64")
        }
    ]
}
}

tonconnect.onStatusChange(async (state)=>{
  if(state){
    const Tx = MakeTxInvoice()
    const txId = Tx.txid
    const result = await tonconnect.sendTransaction(Tx)
    await checkTransaction(txId);
  }
})

async function checkTransaction(txId) {
  const maxAttempts = 20;
  let attempts = 0;
  while (attempts < maxAttempts) {
    const lastTx = await tonweb.getTransactions(tonconnect.account.address, 1);

    const lastTxID = lastTx[0].in_msg.message;
    if (lastTxID === txId) {
      alert('Transaction confirmed!');
      return;
    }
    attempts++;
    await new Promise(resolve => setTimeout(resolve, 5000));
  }
  alert('Transaction not confirmed within the maximum number of attempts.');
}```

### Documentation Check

- [X] Yes, I have checked the documentation.