How can I wait for transaction to confirm using tonweb?
How can I wait for transaction to confirm using tonweb?
You can save transaction hash before sending it and then query Toncenter API method [getTransactionByInMessageHash](https://toncenter.com/api/index/#/default/get_transaction_by_in_message_hash_getTransactionByInMessageHash_get) to check if transaction with such hash was confirmed or not
Example:
```js
// Sleep function:
const sleep = ms => new Promise(r => setTimeout(r, ms))
// `msg` is a Cell containing your external message
// Convert message Cell to BOC String
const boc = await msg.toBoc(false)
// Calculate it's hash
const hash = tonweb.utils.bytesToBase64(await msg.hash())
// Send message and run a loop until transaction with that hash confirms
await tonweb.sendBoc(boc)
var txs = []
while (txs.length == 0) {
await sleep(1200) // some delay between API calls
const resp = await fetch('https://toncenter.com/api/index/getTransactionByInMessageHash?&include_msg_body=false&msg_hash=' + encodeURIComponent(hash))
txs = await resp.json()
}
console.log('Done!')
```
You can save transaction hash before sending it and then query Toncenter API method [getTransactionByInMessageHash](https://toncenter.com/api/index/#/default/get_transaction_by_in_message_hash_getTransactionByInMessageHash_get) to check if transaction with such hash was confirmed or not
Example:
```js
// Sleep function:
const sleep = ms => new Promise(r => setTimeout(r, ms))
...
// `msg` is a Cell containing your external message
// Convert message Cell to BOC String
const boc = await msg.toBoc(false)
// Calculate it's hash
const hash = tonweb.utils.bytesToBase64(await msg.hash())
// Send message and run a loop until transaction with that hash confirms
await tonweb.sendBoc(boc)
var txs = []
while (txs.length == 0) {
await sleep(1200) // some delay between API calls
const resp = await fetch('https://toncenter.com/api/index/getTransactionByInMessageHash?&include_msg_body=false&msg_hash=' + encodeURIComponent(hash))
txs = await resp.json()
}
console.log('Done!')
```
How can I wait for transaction to confirm using tonweb?
How can I wait for transaction to confirm using tonweb?
You can save transaction hash before sending it and then query Toncenter API method [getTransactionByInMessageHash](https://toncenter.com/api/index/#/default/get_transaction_by_in_message_hash_getTransactionByInMessageHash_get) to check if transaction with such hash was confirmed or not
Example:
```js
// Sleep function:
const sleep = ms => new Promise(r => setTimeout(r, ms))
...
// `msg` is a Cell containing your external message
// Convert message Cell to BOC String
const boc = await msg.toBoc(false)
// Calculate it's hash
const hash = tonweb.utils.bytesToBase64(await msg.hash())
// Send message and run a loop until transaction with that hash confirms
await tonweb.sendBoc(boc)
var txs = []
while (txs.length == 0) {
await sleep(1200) // some delay between API calls
const resp = await fetch('https://toncenter.com/api/index/getTransactionByInMessageHash?&include_msg_body=false&msg_hash=' + encodeURIComponent(hash))
txs = await resp.json()
}
console.log('Done!')
```
You can save transaction hash before sending it and then query Toncenter API method [getTransactionByInMessageHash](https://toncenter.com/api/index/#/default/get_transaction_by_in_message_hash_getTransactionByInMessageHash_get) to check if transaction with such hash was confirmed or not
Example:
```js
// Sleep function:
const sleep = ms => new Promise(r => setTimeout(r, ms))
...
// `msg` is a Cell containing your external message
// Convert Cell to BOC String
const boc = await msg.toBoc(false)
// Calculate it's hash
const hash = tonweb.utils.bytesToBase64(await msg.hash())
// Send message and run a loop until transaction with that hash confirms
await tonweb.sendBoc(boc)
var txs = []
while (txs.length == 0) {
await sleep(1200) // some delay between API calls
const resp = await fetch('https://toncenter.com/api/index/getTransactionByInMessageHash?&include_msg_body=false&msg_hash=' + encodeURIComponent(hash))
txs = await resp.json()
}
console.log('Done!')
```
How can I wait for transaction to confirm using tonweb?
How can I wait for transaction to confirm using tonweb?
You can save transaction hash before sending it and then query Toncenter API method [getTransactionByInMessageHash](https://toncenter.com/api/index/#/default/get_transaction_by_in_message_hash_getTransactionByInMessageHash_get) to check if transaction with such hash was confirmed or not
Example:
```js
// Sleep function:
const sleep = ms => new Promise(r => setTimeout(r, ms))
...
// `msg` is a Cell containing your external message
// Convert Cell to BOC String
const boc = await msg.toBoc(false)
// Calculate it's hash
const hash = tonweb.utils.bytesToBase64(await msg.hash())
// Send message and run a loop until transaction with that hash confirms
await tonweb.sendBoc(boc)
var txs = []
while (txs.length == 0) {
await sleep(1200) // some delay between API calls
const resp = await fetch('https://toncenter.com/api/index/getTransactionByInMessageHash?&include_msg_body=false&msg_hash=' + encodeURIComponent(hash))
txs = await resp.json()
}
console.log('Done!')
```
A simple approach would be saving the user's last transaction and run a loop until the user's address will have new transactions.
You can do it like that with tonweb:
```js
// Get user's wallet address from TON wallet browser extension
const address = (await ton.send('ton_requestAccounts'))[0]
// Get user's last transaction hash using tonweb
const lastTx = (await tonweb.getTransactions(address, 1))[0]
const lastTxHash = lastTx.transaction_id.hash
// Send your transaction
await ton.send('ton_sendTransaction', [{
to: 'some address',
value: '1000'
}]
)
// Run a loop until user's last tx hash changes
var txHash = lastTxHash
while (txHash == lastTxHash) {
await sleep(1500) // some delay between API calls
let tx = (await tonweb.getTransactions(address, 1))[0]
txHash = tx.transaction_id.hash
}
console.log('Done!')
```
How can I wait for transaction to confirm using tonweb?
How can I wait for transaction to confirm using tonweb?
A simple approach would be saving the user's last transaction and run a loop until the user's address will have new transactions.
You can do it like that with tonweb:
```js
// Get user's wallet address from TON wallet browser extension
const address = (await ton.send('ton_requestAccounts'))[0]
// Get user's last transaction hash using tonweb
const lastTx = (await tonweb.getTransactions(address, 1))[0]
const lastTxHash = lastTx.transaction_id.hash
// Send your transaction
await ton.send('ton_sendTransaction', [{
to: 'some address',
value: '1000'
}]
)
// Run a loop until user's last tx hash changes
var txHash = lastTxHash
while (txHash == lastTxHash) {
await sleep(1500) // some delay between API calls
let tx = (await tonweb.getTransactions(address, 1))[0]
txHash = tx.transaction_id.hash
}
console.log('Done!')
```
Simple approach would be saving last user's transaction and run a loop until user's address will have new transacations.
You can do it like that with tonweb:
```
// Get user's wallet address from TON wallet browser extension
const address = (await ton.send('ton_requestAccounts'))[0]
// Get user's last transaction hash using tonweb
const lastTx = (await tonweb.getTransactions(address, 1))[0]
const lastTxHash = lastTx.transaction_id.hash
// Send your transaction
await ton.send('ton_sendTransaction', [{
to: 'some address',
value: '1000'
}]
)
// Run a loop until user's last tx hash changes
var txHash = lastTxHash
while (txHash == lastTxHash) {
await sleep(1500) // some delay between API calls
let tx = (await tonweb.getTransactions(address, 1))[0]
txHash = tx.transaction_id.hash
}
console.log('Done!')
```
How can I wait for transaction to confirm using tonweb?
Simple approach would be saving last user's transaction and run a loop until user's address will have new transacations.
You can do it like that with tonweb:
```
// Get user's wallet address from TON wallet browser extension
const address = (await ton.send('ton_requestAccounts'))[0]
// Get user's last transaction hash using tonweb
const lastTx = (await tonweb.getTransactions(address, 1))[0]
const lastTxHash = lastTx.transaction_id.hash
// Send your transaction
await ton.send('ton_sendTransaction', [{
to: 'some address',
value: '1000'
}]
)
// Run a loop until user's last tx hash changes
var txHash = lastTxHash
while (txHash == lastTxHash) {
await sleep(1500) // some delay between API calls
let tx = (await tonweb.getTransactions(address, 1))[0]
txHash = tx.transaction_id.hash
}
console.log('Done!')
```