Skip to content

Commit

Permalink
feat: add dynamic wait for transaction based on envelope timeout (#71)
Browse files Browse the repository at this point in the history
* feat: add transaction hash to error STP007

* feat: add timeout to wait tx

* packaging: bump version to 0.5.5
  • Loading branch information
fazzatti authored Jan 19, 2024
1 parent b99f9b2 commit 9078110
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stellar-plus",
"version": "0.5.4",
"version": "0.5.5",
"description": "beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/stellar-plus/core/contract-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ export class ContractEngine extends SorobanTransactionProcessor {
response: await this.processSorobanTransaction(
assembledTransaction,
updatedTxInvocation.signers,
updatedTxInvocation.feeBump
updatedTxInvocation.feeBump,
updatedTxInvocation.header.timeout
),
transactionResources,
}
Expand Down
8 changes: 6 additions & 2 deletions src/stellar-plus/core/soroban-transaction-processor/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,18 @@ const verifyOpErrorCode = (sorobanGetTransactionData: GetTransactionFailedErrorI

const transactionSubmittedNotFound = (
response: SorobanRpc.Api.GetTransactionResponse,
waitTimeout: number
waitTimeout: number,
transactionHash: string
): StellarPlusError => {
return new StellarPlusError({
code: SorobanTransactionProcessorErrorCodes.STP007,
message: 'Transaction not found!',
source: 'SorobanTransactionProcessor',
details: `The transaction submitted was not found within the waiting period of ${waitTimeout} ms. Althought the transaction was sent for processing, the subsequent attempts to verify the transaction status didn't succeed to locate it. Review the error message for further information about the failure.`,
meta: { sorobanGetTransactionData: extractGetTransactionData(response) },
meta: {
transactionHash,
sorobanGetTransactionData: extractGetTransactionData(response),
},
})
}

Expand Down
12 changes: 7 additions & 5 deletions src/stellar-plus/core/soroban-transaction-processor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export class SorobanTransactionProcessor extends TransactionProcessor {
protected async processSorobanTransaction(
envelope: Transaction,
signers: AccountHandler[],
feeBump?: FeeBumpHeader
feeBump?: FeeBumpHeader,
secondsToWait?: number
): Promise<SorobanRpcNamespace.Api.GetSuccessfulTransactionResponse> {
const signedInnerTransaction = await this.signEnvelope(envelope, signers)

Expand All @@ -162,7 +163,7 @@ export class SorobanTransactionProcessor extends TransactionProcessor {
: (TransactionBuilder.fromXDR(signedInnerTransaction, this.network.networkPassphrase) as Transaction)

const rpcResponse = await this.submitTransaction(finalEnvelope)
const processedTransaction = await this.postProcessSorobanSubmission(rpcResponse)
const processedTransaction = await this.postProcessSorobanSubmission(rpcResponse, secondsToWait)

return processedTransaction
}
Expand All @@ -176,15 +177,16 @@ export class SorobanTransactionProcessor extends TransactionProcessor {
* @returns {Promise<SorobanRpcNamespace.GetSuccessfulTransactionResponse>} The response from the Soroban server.
*/
protected async postProcessSorobanSubmission(
response: SorobanRpcNamespace.Api.SendTransactionResponse
response: SorobanRpcNamespace.Api.SendTransactionResponse,
secondsToWait?: number
): Promise<SorobanRpcNamespace.Api.GetSuccessfulTransactionResponse> {
if (response.status === 'ERROR') {
throw STPError.failedToSubmitTransactionWithResponse(response)
}

if (response.status === 'PENDING' || response.status === 'TRY_AGAIN_LATER') {
// console.log('Waiting for Transaction!: ')
return await this.waitForTransaction(response.hash, 15) // Arbitrary 15 seconds timeout
return await this.waitForTransaction(response.hash, secondsToWait ? secondsToWait : 15) // Arbitrary 15 seconds timeout default
}

throw STPError.failedToVerifyTransactionSubmission(response)
Expand Down Expand Up @@ -232,7 +234,7 @@ export class SorobanTransactionProcessor extends TransactionProcessor {
throw STPError.transactionSubmittedFailed(updatedTransaction)
}

throw STPError.transactionSubmittedNotFound(updatedTransaction, timeout)
throw STPError.transactionSubmittedNotFound(updatedTransaction, timeout, transactionHash)
}

protected postProcessTransaction(
Expand Down
1 change: 1 addition & 0 deletions src/stellar-plus/error/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type Meta = {
data?: object
message?: string
transactionXDR?: string
transactionHash?: string
axiosError?: AxiosErrorInfo
transactionData?: TransactionData
sorobanSimulationData?: SimulationErrorInfo
Expand Down

0 comments on commit 9078110

Please sign in to comment.