Skip to content

Commit 09612a0

Browse files
committed
handle 501 exit code for TonPoolStaker
1 parent 0b6ab1b commit 09612a0

File tree

4 files changed

+94
-47
lines changed

4 files changed

+94
-47
lines changed

book/docs/classes/ton_src.TonPoolStaker.md

+30-30
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
- [buildUnstakeTx](ton_src.TonPoolStaker.md#buildunstaketx)
2020
- [getStake](ton_src.TonPoolStaker.md#getstake)
2121
- [getPoolParams](ton_src.TonPoolStaker.md#getpoolparams)
22+
- [getTxStatus](ton_src.TonPoolStaker.md#gettxstatus)
2223
- [getMinStake](ton_src.TonPoolStaker.md#getminstake)
2324
- [getPoolStatus](ton_src.TonPoolStaker.md#getpoolstatus)
2425
- [getPastElections](ton_src.TonPoolStaker.md#getpastelections)
2526
- [init](ton_src.TonPoolStaker.md#init)
2627
- [buildDeployWalletTx](ton_src.TonPoolStaker.md#builddeploywallettx)
2728
- [sign](ton_src.TonPoolStaker.md#sign)
2829
- [broadcast](ton_src.TonPoolStaker.md#broadcast)
29-
- [getTxStatus](ton_src.TonPoolStaker.md#gettxstatus)
3030

3131
# Constructors
3232

@@ -274,6 +274,35 @@ Returns a promise that resolves to the staking information for the specified poo
274274

275275
___
276276

277+
## getTxStatus
278+
279+
**getTxStatus**(`params`): `Promise`\<`TonTxStatus`\>
280+
281+
Retrieves the status of a transaction using the transaction hash.
282+
283+
This method is intended to check for transactions made recently (within limit) and not for historical transactions.
284+
285+
### Parameters
286+
287+
| Name | Type | Description |
288+
| :------ | :------ | :------ |
289+
| `params` | `Object` | Parameters for the transaction status request |
290+
| `params.address` | `string` | The account address to query |
291+
| `params.txHash` | `string` | The transaction hash to query |
292+
| `params.limit?` | `number` | (Optional) The maximum number of transactions to fetch |
293+
294+
### Returns
295+
296+
`Promise`\<`TonTxStatus`\>
297+
298+
A promise that resolves to an object containing the transaction status.
299+
300+
### Overrides
301+
302+
TonBaseStaker.getTxStatus
303+
304+
___
305+
277306
## getMinStake
278307

279308
**getMinStake**(): `Promise`\<`bigint`\>
@@ -409,32 +438,3 @@ Returns a promise that resolves to the response of the transaction that was broa
409438
### Inherited from
410439

411440
TonBaseStaker.broadcast
412-
413-
___
414-
415-
## getTxStatus
416-
417-
**getTxStatus**(`params`): `Promise`\<`TonTxStatus`\>
418-
419-
Retrieves the status of a transaction using the transaction hash.
420-
421-
This method is intended to check for transactions made recently (within limit) and not for historical transactions.
422-
423-
### Parameters
424-
425-
| Name | Type | Description |
426-
| :------ | :------ | :------ |
427-
| `params` | `Object` | Parameters for the transaction status request |
428-
| `params.address` | `string` | The account address to query |
429-
| `params.txHash` | `string` | The transaction hash to query |
430-
| `params.limit?` | `number` | (Optional) The maximum number of transactions to fetch |
431-
432-
### Returns
433-
434-
`Promise`\<`TonTxStatus`\>
435-
436-
A promise that resolves to an object containing the transaction status.
437-
438-
### Inherited from
439-
440-
TonBaseStaker.getTxStatus

packages/ton/src/TonBaseStaker.ts

+27-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
Address,
2020
TransactionDescriptionGeneric,
2121
beginCell,
22-
storeMessage
22+
storeMessage,
23+
Transaction
2324
} from '@ton/ton'
2425
import { mnemonicToSeed, deriveEd25519Path, keyPairFromSeed } from '@ton/crypto'
2526
import { pbkdf2_sha512 } from '@ton/crypto-primitives'
@@ -452,20 +453,12 @@ export class TonBaseStaker {
452453
* @returns A promise that resolves to an object containing the transaction status.
453454
*/
454455
async getTxStatus (params: { address: string; txHash: string; limit?: number }): Promise<TonTxStatus> {
455-
const client = this.getClient()
456-
const { address, txHash, limit } = params
457-
458-
const transactions = await client.getTransactions(Address.parse(address), { limit: limit ?? 10 })
459-
const transaction = transactions.find((tx) => {
460-
// Check tx hash
461-
if (tx.hash().toString('hex') === txHash) return true
462-
463-
// Check inMessage tx hash(that is the one we get from broadcast method)
464-
if (tx.inMessage && beginCell().store(storeMessage(tx.inMessage)).endCell().hash().toString('hex') === txHash)
465-
return true
456+
const transaction = await this.getTransactionByHash(params)
457+
return this.matchTransactionStatus(transaction)
458+
}
466459

467-
return false
468-
})
460+
/** @ignore */
461+
protected matchTransactionStatus (transaction: Transaction | undefined): TonTxStatus {
469462
if (transaction === undefined) {
470463
return { status: 'unknown', receipt: null }
471464
}
@@ -507,6 +500,26 @@ export class TonBaseStaker {
507500
return { status: 'success', receipt: transaction }
508501
}
509502

503+
/** @ignore */
504+
protected async getTransactionByHash (params: { address: string; txHash: string; limit?: number }): Promise<Transaction | undefined> {
505+
const client = this.getClient()
506+
const { address, txHash, limit } = params
507+
508+
const transactions = await client.getTransactions(Address.parse(address), { limit: limit ?? 10 })
509+
const transaction = transactions.find((tx) => {
510+
// Check tx hash
511+
if (tx.hash().toString('hex') === txHash) return true
512+
513+
// Check inMessage tx hash(that is the one we get from broadcast method)
514+
if (tx.inMessage && beginCell().store(storeMessage(tx.inMessage)).endCell().hash().toString('hex') === txHash)
515+
return true
516+
517+
return false
518+
})
519+
520+
return transaction
521+
}
522+
510523
/** @ignore */
511524
protected getClient (): TonClient {
512525
if (!this.client) {

packages/ton/src/TonPoolStaker.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Address, beginCell, fromNano, toNano, Slice, Builder, DictionaryValue, Dictionary, Cell } from '@ton/ton'
1+
import { Address, beginCell, fromNano, toNano, Slice, Builder, DictionaryValue, Dictionary, Cell, TransactionDescriptionGeneric } from '@ton/ton'
22
import { defaultValidUntil, getDefaultGas, getRandomQueryId, TonBaseStaker } from './TonBaseStaker'
3-
import { UnsignedTx, Election, FrozenSet, PoolStatus, GetPoolAddressForStakeResponse, Message } from './types'
3+
import { UnsignedTx, Election, FrozenSet, PoolStatus, GetPoolAddressForStakeResponse, Message, TonTxStatus } from './types'
44

55
export class TonPoolStaker extends TonBaseStaker {
66
/**
@@ -237,6 +237,40 @@ export class TonPoolStaker extends TonBaseStaker {
237237
}
238238
}
239239

240+
/**
241+
* Retrieves the status of a transaction using the transaction hash.
242+
*
243+
* This method is intended to check for transactions made recently (within limit) and not for historical transactions.
244+
*
245+
* @param params - Parameters for the transaction status request
246+
* @param params.address - The account address to query
247+
* @param params.txHash - The transaction hash to query
248+
* @param params.limit - (Optional) The maximum number of transactions to fetch
249+
*
250+
* @returns A promise that resolves to an object containing the transaction status.
251+
*/
252+
async getTxStatus (params: { address: string; txHash: string; limit?: number }): Promise<TonTxStatus> {
253+
const transaction = await this.getTransactionByHash(params)
254+
255+
if (transaction === undefined) {
256+
return { status: 'unknown', receipt: null }
257+
}
258+
259+
if (transaction.description.type === 'generic') {
260+
const description = transaction.description as TransactionDescriptionGeneric
261+
262+
if (description.computePhase.type === 'vm') {
263+
const compute = description.computePhase
264+
265+
if (compute.exitCode === 501) {
266+
return { status: 'failure', receipt: transaction, reason: 'withdraw_below_minimum_stake' }
267+
}
268+
}
269+
}
270+
271+
return this.matchTransactionStatus(transaction)
272+
}
273+
240274
private async getPoolParamsUnformatted (params: { validatorAddress: string }) {
241275
const { validatorAddress } = params
242276
const client = this.getClient()

packages/ton/src/types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ export declare interface SignedTx {
141141
export interface TonTxStatus {
142142
status: 'success' | 'failure' | 'pending' | 'unknown'
143143
receipt: Transaction | null
144-
reason?: 'out_of_storage' | 'aborted' | 'compute_phase' | 'action_phase' | 'bounce_phase'
144+
reason?: 'out_of_storage' | 'aborted' | 'compute_phase' | 'action_phase' | 'bounce_phase' | 'withdraw_below_minimum_stake'
145145
}

0 commit comments

Comments
 (0)