Skip to content

Commit 7296850

Browse files
committed
fix: fix waitfortransaction for rotating nodes services with lifeCycleRetries default 3
1 parent 27d81b4 commit 7296850

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

src/channel/rpc_0_8_1.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -396,21 +396,22 @@ export class RpcChannel {
396396

397397
public async waitForTransaction(txHash: BigNumberish, options?: waitForTransactionOptions) {
398398
const transactionHash = toHex(txHash);
399-
let { retries } = this;
399+
let retries = options?.retries ?? this.retries;
400+
let lifeCycleRetries = options?.lifeCycleRetries ?? 3;
400401
let onchain = false;
401402
let isErrorState = false;
402403
const retryInterval = options?.retryInterval ?? this.transactionRetryIntervalDefault;
403-
const errorStates: any = options?.errorStates ?? [
404-
RPC.ETransactionStatus.REJECTED,
405-
// TODO: commented out to preserve the long-standing behavior of "reverted" not being treated as an error by default
406-
// should decide which behavior to keep in the future
407-
// RPC.ETransactionExecutionStatus.REVERTED,
408-
];
404+
const errorStates: any = options?.errorStates ?? [RPC.ETransactionStatus.REJECTED];
409405
const successStates: any = options?.successStates ?? [
410406
// RPC.ETransactionExecutionStatus.SUCCEEDED, Starknet 0.14.0 this one can have incomplete events
411407
RPC.ETransactionStatus.ACCEPTED_ON_L2,
412408
RPC.ETransactionStatus.ACCEPTED_ON_L1,
413409
];
410+
const LifeCycleErrorMessages: Record<string, string> = {
411+
[RPCSPEC09.ETransactionStatus.RECEIVED]: SYSTEM_MESSAGES.txEvictedFromMempool,
412+
[RPCSPEC09.ETransactionStatus.PRE_CONFIRMED]: SYSTEM_MESSAGES.consensusFailed,
413+
[RPCSPEC09.ETransactionStatus.CANDIDATE]: SYSTEM_MESSAGES.txFailsBlockBuildingValidation,
414+
};
414415

415416
const txLife: string[] = [];
416417
let txStatus: RPC.TransactionStatus;
@@ -451,16 +452,11 @@ export class RpcChannel {
451452

452453
if (error instanceof RpcError && error.isType('TXN_HASH_NOT_FOUND')) {
453454
logger.info('txLife: ', txLife);
454-
const errorMessages: Record<string, string> = {
455-
[RPCSPEC09.ETransactionStatus.RECEIVED]: SYSTEM_MESSAGES.txEvictedFromMempool,
456-
[RPCSPEC09.ETransactionStatus.PRE_CONFIRMED]: SYSTEM_MESSAGES.consensusFailed,
457-
[RPCSPEC09.ETransactionStatus.CANDIDATE]:
458-
SYSTEM_MESSAGES.txFailsBlockBuildingValidation,
459-
};
460-
const errorMessage = errorMessages[txLife.at(-1) as string];
461-
if (errorMessage) {
455+
const errorMessage = LifeCycleErrorMessages[txLife.at(-1) as string];
456+
if (errorMessage && lifeCycleRetries <= 0) {
462457
throw new Error(errorMessage);
463458
}
459+
lifeCycleRetries -= 1;
464460
}
465461

466462
if (retries <= 0) {

src/channel/rpc_0_9_0.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ export class RpcChannel {
403403

404404
public async waitForTransaction(txHash: BigNumberish, options?: waitForTransactionOptions) {
405405
const transactionHash = toHex(txHash);
406-
let { retries } = this;
406+
let retries = options?.retries ?? this.retries;
407+
let lifeCycleRetries = options?.lifeCycleRetries ?? 3;
407408
let onchain = false;
408409
let isErrorState = false;
409410
const retryInterval = options?.retryInterval ?? this.transactionRetryIntervalDefault;
@@ -413,6 +414,11 @@ export class RpcChannel {
413414
RPC.ETransactionFinalityStatus.ACCEPTED_ON_L2,
414415
RPC.ETransactionFinalityStatus.ACCEPTED_ON_L1,
415416
];
417+
const errorMessages: Record<string, string> = {
418+
[RPC.ETransactionStatus.RECEIVED]: SYSTEM_MESSAGES.txEvictedFromMempool,
419+
[RPC.ETransactionStatus.PRE_CONFIRMED]: SYSTEM_MESSAGES.consensusFailed,
420+
[RPC.ETransactionStatus.CANDIDATE]: SYSTEM_MESSAGES.txFailsBlockBuildingValidation,
421+
};
416422

417423
const txLife: string[] = [];
418424
let txStatus: RPC.TransactionStatus;
@@ -453,15 +459,11 @@ export class RpcChannel {
453459

454460
if (error instanceof RpcError && error.isType('TXN_HASH_NOT_FOUND')) {
455461
logger.info('txLife: ', txLife);
456-
const errorMessages: Record<string, string> = {
457-
[RPC.ETransactionStatus.RECEIVED]: SYSTEM_MESSAGES.txEvictedFromMempool,
458-
[RPC.ETransactionStatus.PRE_CONFIRMED]: SYSTEM_MESSAGES.consensusFailed,
459-
[RPC.ETransactionStatus.CANDIDATE]: SYSTEM_MESSAGES.txFailsBlockBuildingValidation,
460-
};
461462
const errorMessage = errorMessages[txLife.at(-1) as string];
462-
if (errorMessage) {
463+
if (errorMessage && lifeCycleRetries <= 0) {
463464
throw new Error(errorMessage);
464465
}
466+
lifeCycleRetries -= 1;
465467
}
466468

467469
if (retries <= 0) {

src/provider/types/configuration.type.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { NetworkName, StarknetChainId, SupportedRpcVersion } from '../../global/constants';
2-
import { BlockIdentifier } from '../../types/lib';
2+
import { BlockIdentifier, waitForTransactionOptions } from '../../types/lib';
33
import { ResourceBoundsOverhead } from './spec.type';
44

55
export interface ProviderOptions extends RpcProviderOptions {}
66

77
export type RpcProviderOptions = {
88
nodeUrl?: string | NetworkName;
9-
retries?: number;
9+
/**
10+
* Define the number of retries for waitForTransaction
11+
*/
12+
retries?: waitForTransactionOptions['retries'];
13+
/**
14+
* Define the time interval between retries in milliseconds
15+
*/
1016
transactionRetryIntervalFallback?: number;
17+
/**
18+
* Define the headers
19+
*/
1120
headers?: object;
1221
blockIdentifier?: BlockIdentifier;
1322
chainId?: StarknetChainId;

src/types/lib/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,26 @@ export type ParsedStruct = {
313313
};
314314

315315
export type waitForTransactionOptions = {
316+
/**
317+
* Define the number of retries before throwing an error for the transaction life cycle when the transaction is not found after it had a valid status.
318+
* This is useful for nodes that are not fully synced yet when connecting to service that rotate nodes.
319+
*/
320+
lifeCycleRetries?: number;
321+
/**
322+
* Define the number of retries before throwing an error
323+
*/
324+
retries?: number;
325+
/**
326+
* Define the time interval between retries in milliseconds
327+
*/
316328
retryInterval?: number;
329+
/**
330+
* Define which states are considered as successful
331+
*/
317332
successStates?: Array<TransactionFinalityStatus | TransactionExecutionStatus>;
333+
/**
334+
* Define which states are considered as errors
335+
*/
318336
errorStates?: Array<TransactionFinalityStatus | TransactionExecutionStatus>;
319337
};
320338

0 commit comments

Comments
 (0)