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

Handle 501 ton exit code #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions book/docs/classes/ton_src.TonPoolStaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
- [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)
- [init](ton_src.TonPoolStaker.md#init)
- [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

Expand Down Expand Up @@ -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`\>
Expand Down Expand Up @@ -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
45 changes: 31 additions & 14 deletions packages/ton/src/TonBaseStaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<TonTxStatus> {
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 }
}
Expand Down Expand Up @@ -507,6 +500,30 @@ export class TonBaseStaker {
return { status: 'success', receipt: transaction }
}

/** @ignore */
protected async getTransactionByHash (params: {
address: string
txHash: string
limit?: number
}): Promise<Transaction | undefined> {
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) {
Expand Down
57 changes: 55 additions & 2 deletions packages/ton/src/TonPoolStaker.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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<TonTxStatus> {
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()
Expand Down
8 changes: 7 additions & 1 deletion packages/ton/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}