diff --git a/book/docs/classes/ton_src.TonPoolStaker.md b/book/docs/classes/ton_src.TonPoolStaker.md index 0610a0f..87786fe 100644 --- a/book/docs/classes/ton_src.TonPoolStaker.md +++ b/book/docs/classes/ton_src.TonPoolStaker.md @@ -19,6 +19,7 @@ - [buildUnstakeTx](ton_src.TonPoolStaker.md#buildunstaketx) - [getStake](ton_src.TonPoolStaker.md#getstake) - [getPoolParams](ton_src.TonPoolStaker.md#getpoolparams) +- [getTxStatus](ton_src.TonPoolStaker.md#gettxstatus) - [getMinStake](ton_src.TonPoolStaker.md#getminstake) - [getPoolStatus](ton_src.TonPoolStaker.md#getpoolstatus) - [getPastElections](ton_src.TonPoolStaker.md#getpastelections) @@ -26,7 +27,6 @@ - [buildDeployWalletTx](ton_src.TonPoolStaker.md#builddeploywallettx) - [sign](ton_src.TonPoolStaker.md#sign) - [broadcast](ton_src.TonPoolStaker.md#broadcast) -- [getTxStatus](ton_src.TonPoolStaker.md#gettxstatus) # Constructors @@ -274,6 +274,35 @@ Returns a promise that resolves to the staking information for the specified poo ___ +## getTxStatus + +▸ **getTxStatus**(`params`): `Promise`\<`TonTxStatus`\> + +Retrieves the status of a transaction using the transaction hash. + +This method is intended to check for transactions made recently (within limit) and not for historical transactions. + +### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `params` | `Object` | Parameters for the transaction status request | +| `params.address` | `string` | The account address to query | +| `params.txHash` | `string` | The transaction hash to query | +| `params.limit?` | `number` | (Optional) The maximum number of transactions to fetch | + +### Returns + +`Promise`\<`TonTxStatus`\> + +A promise that resolves to an object containing the transaction status. + +### Overrides + +TonBaseStaker.getTxStatus + +___ + ## getMinStake ▸ **getMinStake**(): `Promise`\<`bigint`\> @@ -409,32 +438,3 @@ Returns a promise that resolves to the response of the transaction that was broa ### Inherited from TonBaseStaker.broadcast - -___ - -## getTxStatus - -▸ **getTxStatus**(`params`): `Promise`\<`TonTxStatus`\> - -Retrieves the status of a transaction using the transaction hash. - -This method is intended to check for transactions made recently (within limit) and not for historical transactions. - -### Parameters - -| Name | Type | Description | -| :------ | :------ | :------ | -| `params` | `Object` | Parameters for the transaction status request | -| `params.address` | `string` | The account address to query | -| `params.txHash` | `string` | The transaction hash to query | -| `params.limit?` | `number` | (Optional) The maximum number of transactions to fetch | - -### Returns - -`Promise`\<`TonTxStatus`\> - -A promise that resolves to an object containing the transaction status. - -### Inherited from - -TonBaseStaker.getTxStatus diff --git a/packages/ton/src/TonBaseStaker.ts b/packages/ton/src/TonBaseStaker.ts index bcc7f6c..11dd3c0 100644 --- a/packages/ton/src/TonBaseStaker.ts +++ b/packages/ton/src/TonBaseStaker.ts @@ -19,7 +19,8 @@ import { Address, TransactionDescriptionGeneric, beginCell, - storeMessage + storeMessage, + Transaction } from '@ton/ton' import { mnemonicToSeed, deriveEd25519Path, keyPairFromSeed } from '@ton/crypto' import { pbkdf2_sha512 } from '@ton/crypto-primitives' @@ -452,20 +453,12 @@ export class TonBaseStaker { * @returns A promise that resolves to an object containing the transaction status. */ async getTxStatus (params: { address: string; txHash: string; limit?: number }): Promise { - const client = this.getClient() - const { address, txHash, limit } = params - - const transactions = await client.getTransactions(Address.parse(address), { limit: limit ?? 10 }) - const transaction = transactions.find((tx) => { - // Check tx hash - if (tx.hash().toString('hex') === txHash) return true - - // Check inMessage tx hash(that is the one we get from broadcast method) - if (tx.inMessage && beginCell().store(storeMessage(tx.inMessage)).endCell().hash().toString('hex') === txHash) - return true + const transaction = await this.getTransactionByHash(params) + return this.matchTransactionStatus(transaction) + } - return false - }) + /** @ignore */ + protected matchTransactionStatus (transaction: Transaction | undefined): TonTxStatus { if (transaction === undefined) { return { status: 'unknown', receipt: null } } @@ -507,6 +500,30 @@ export class TonBaseStaker { return { status: 'success', receipt: transaction } } + /** @ignore */ + protected async getTransactionByHash (params: { + address: string + txHash: string + limit?: number + }): Promise { + const client = this.getClient() + const { address, txHash, limit } = params + + const transactions = await client.getTransactions(Address.parse(address), { limit: limit ?? 10 }) + const transaction = transactions.find((tx) => { + // Check tx hash + if (tx.hash().toString('hex') === txHash) return true + + // Check inMessage tx hash(that is the one we get from broadcast method) + if (tx.inMessage && beginCell().store(storeMessage(tx.inMessage)).endCell().hash().toString('hex') === txHash) + return true + + return false + }) + + return transaction + } + /** @ignore */ protected getClient (): TonClient { if (!this.client) { diff --git a/packages/ton/src/TonPoolStaker.ts b/packages/ton/src/TonPoolStaker.ts index 26e0697..5d5e198 100644 --- a/packages/ton/src/TonPoolStaker.ts +++ b/packages/ton/src/TonPoolStaker.ts @@ -1,6 +1,25 @@ -import { Address, beginCell, fromNano, toNano, Slice, Builder, DictionaryValue, Dictionary, Cell } from '@ton/ton' +import { + Address, + beginCell, + fromNano, + toNano, + Slice, + Builder, + DictionaryValue, + Dictionary, + Cell, + TransactionDescriptionGeneric +} from '@ton/ton' import { defaultValidUntil, getDefaultGas, getRandomQueryId, TonBaseStaker } from './TonBaseStaker' -import { UnsignedTx, Election, FrozenSet, PoolStatus, GetPoolAddressForStakeResponse, Message } from './types' +import { + UnsignedTx, + Election, + FrozenSet, + PoolStatus, + GetPoolAddressForStakeResponse, + Message, + TonTxStatus +} from './types' export class TonPoolStaker extends TonBaseStaker { /** @@ -237,6 +256,40 @@ export class TonPoolStaker extends TonBaseStaker { } } + /** + * Retrieves the status of a transaction using the transaction hash. + * + * This method is intended to check for transactions made recently (within limit) and not for historical transactions. + * + * @param params - Parameters for the transaction status request + * @param params.address - The account address to query + * @param params.txHash - The transaction hash to query + * @param params.limit - (Optional) The maximum number of transactions to fetch + * + * @returns A promise that resolves to an object containing the transaction status. + */ + async getTxStatus (params: { address: string; txHash: string; limit?: number }): Promise { + const transaction = await this.getTransactionByHash(params) + + if (transaction === undefined) { + return { status: 'unknown', receipt: null } + } + + if (transaction.description.type === 'generic') { + const description = transaction.description as TransactionDescriptionGeneric + + if (description.computePhase.type === 'vm') { + const compute = description.computePhase + + if (compute.exitCode === 501) { + return { status: 'failure', receipt: transaction, reason: 'withdraw_below_minimum_stake' } + } + } + } + + return this.matchTransactionStatus(transaction) + } + private async getPoolParamsUnformatted (params: { validatorAddress: string }) { const { validatorAddress } = params const client = this.getClient() diff --git a/packages/ton/src/types.d.ts b/packages/ton/src/types.d.ts index 68cbd65..7ad8285 100644 --- a/packages/ton/src/types.d.ts +++ b/packages/ton/src/types.d.ts @@ -141,5 +141,11 @@ export declare interface SignedTx { export interface TonTxStatus { status: 'success' | 'failure' | 'pending' | 'unknown' receipt: Transaction | null - reason?: 'out_of_storage' | 'aborted' | 'compute_phase' | 'action_phase' | 'bounce_phase' + reason?: + | 'out_of_storage' + | 'aborted' + | 'compute_phase' + | 'action_phase' + | 'bounce_phase' + | 'withdraw_below_minimum_stake' }