Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
devue4 opened this issue Aug 13, 2024 · 0 comments
Open
Assignees
Labels
question Further information is requested

Comments

@devue4
Copy link

devue4 commented Aug 13, 2024

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.
@devue4 devue4 added the question Further information is requested label Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants