diff --git a/__tests__/config/fixtures.ts b/__tests__/config/fixtures.ts index bb8132383..d02a6008b 100644 --- a/__tests__/config/fixtures.ts +++ b/__tests__/config/fixtures.ts @@ -2,12 +2,7 @@ import fs from 'node:fs'; import path from 'node:path'; import { Account, Provider, ProviderInterface, RpcProvider, json } from '../../src'; -import { - CompiledSierra, - CompiledSierraCasm, - LegacyCompiledContract, - waitForTransactionOptions, -} from '../../src/types'; +import { CompiledSierra, CompiledSierraCasm, LegacyCompiledContract } from '../../src/types'; import { ETransactionVersion } from '../../src/types/api'; import { toHex } from '../../src/utils/num'; @@ -73,22 +68,18 @@ export const compiledTestRejectSierra = readContractSierra('cairo/testReject/tes export const compiledTestRejectCasm = readContractSierraCasm('cairo/testReject/test_reject'); export const compiledSidMulticall = readContractSierra('starknetId/multicall/multicall.sierra'); export const compiledSidMulticallCasm = readContractSierraCasm('starknetId/multicall/multicall'); + export function getTestProvider(isProvider?: true): ProviderInterface; export function getTestProvider(isProvider?: false): RpcProvider; export function getTestProvider(isProvider: boolean = true): ProviderInterface | RpcProvider { - const provider = isProvider - ? new Provider({ nodeUrl: process.env.TEST_RPC_URL }) - : new RpcProvider({ nodeUrl: process.env.TEST_RPC_URL }); + const isDevnet = process.env.IS_DEVNET === 'true'; - if (process.env.IS_DEVNET === 'true') { + const providerOptions = { + nodeUrl: process.env.TEST_RPC_URL, // accelerate the tests when running locally - const originalWaitForTransaction = provider.waitForTransaction.bind(provider); - provider.waitForTransaction = (txHash: string, options: waitForTransactionOptions = {}) => { - return originalWaitForTransaction(txHash, { retryInterval: 1000, ...options }); - }; - } - - return provider; + ...(isDevnet && { transactionRetryIntervalFallback: 1000 }), + }; + return isProvider ? new Provider(providerOptions) : new RpcProvider(providerOptions); } export const TEST_TX_VERSION = process.env.TX_VERSION === 'v3' ? ETransactionVersion.V3 : undefined; diff --git a/src/channel/rpc_0_7.ts b/src/channel/rpc_0_7.ts index 53e51aed2..923e2bcfc 100644 --- a/src/channel/rpc_0_7.ts +++ b/src/channel/rpc_0_7.ts @@ -50,11 +50,21 @@ export class RpcChannel { private specVersion?: string; + private transactionRetryIntervalFallback?: number; + readonly waitMode: Boolean; // behave like web2 rpc and return when tx is processed constructor(optionsOrProvider?: RpcProviderOptions) { - const { nodeUrl, retries, headers, blockIdentifier, chainId, specVersion, waitMode } = - optionsOrProvider || {}; + const { + nodeUrl, + retries, + headers, + blockIdentifier, + chainId, + specVersion, + waitMode, + transactionRetryIntervalFallback, + } = optionsOrProvider || {}; if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) { this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default); } else if (nodeUrl) { @@ -69,6 +79,11 @@ export class RpcChannel { this.specVersion = specVersion; this.waitMode = waitMode || false; this.requestId = 0; + this.transactionRetryIntervalFallback = transactionRetryIntervalFallback; + } + + private get transactionRetryIntervalDefault() { + return this.transactionRetryIntervalFallback ?? 5000; } public setChainId(chainId: StarknetChainId) { @@ -250,7 +265,7 @@ export class RpcChannel { let { retries } = this; let onchain = false; let isErrorState = false; - const retryInterval = options?.retryInterval ?? 5000; + const retryInterval = options?.retryInterval ?? this.transactionRetryIntervalDefault; const errorStates: any = options?.errorStates ?? [ RPC.ETransactionStatus.REJECTED, // TODO: commented out to preserve the long-standing behavior of "reverted" not being treated as an error by default diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index 68b2f5396..26f3953d7 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -198,6 +198,7 @@ export class RpcProvider implements ProviderInterface { txHash, options )) as GetTxReceiptResponseWithoutHelper; + return new ReceiptTx(receiptWoHelper) as GetTransactionReceiptResponse; } diff --git a/src/types/provider/configuration.ts b/src/types/provider/configuration.ts index 71eaf534f..db1825f42 100644 --- a/src/types/provider/configuration.ts +++ b/src/types/provider/configuration.ts @@ -6,6 +6,7 @@ export interface ProviderOptions extends RpcProviderOptions {} export type RpcProviderOptions = { nodeUrl?: string | NetworkName; retries?: number; + transactionRetryIntervalFallback?: number; headers?: object; blockIdentifier?: BlockIdentifier; chainId?: StarknetChainId; diff --git a/src/utils/url.ts b/src/utils/url.ts index c43389688..b1318cf85 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -67,7 +67,7 @@ export function isUrl(s?: string): boolean { * const defaultPath = "/"; * const urlOrPath = "/docs"; * const result = buildUrl(baseUrl, defaultPath, urlOrPath); - * + * * result = "https://starknetjs.com/docs" */ export function buildUrl(baseUrl: string, defaultPath: string, urlOrPath?: string) {