Skip to content

Commit

Permalink
Merge pull request #1125 from ugur-eren/feat-rpc-batch
Browse files Browse the repository at this point in the history
feat: rpc batch requests
  • Loading branch information
tabaktoni authored Jul 26, 2024
2 parents 7f5e42d + b30dc33 commit c10cfe3
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 10 deletions.
25 changes: 20 additions & 5 deletions __tests__/config/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import fs from 'node:fs';
import path from 'node:path';

import { Account, Provider, ProviderInterface, RpcProvider, json } from '../../src';
import { CompiledSierra, CompiledSierraCasm, LegacyCompiledContract } from '../../src/types';
import {
CompiledSierra,
CompiledSierraCasm,
LegacyCompiledContract,
RpcProviderOptions,
} from '../../src/types';
import { ETransactionVersion } from '../../src/types/api';
import { toHex } from '../../src/utils/num';
import { wait } from '../../src/utils/provider';
Expand Down Expand Up @@ -72,12 +77,22 @@ export const compiledSidMulticallCasm = readContractSierraCasm('starknetId/multi
export const compiledNonZero = readContractSierra('cairo/cairo263/zeroable.sierra');
export const compiledNonZeroCasm = readContractSierraCasm('cairo/cairo263/zeroable');

export function getTestProvider(isProvider?: true): ProviderInterface;
export function getTestProvider(isProvider?: false): RpcProvider;
export function getTestProvider(isProvider: boolean = true): ProviderInterface | RpcProvider {
export function getTestProvider(
isProvider?: true,
setProviderOptions?: RpcProviderOptions
): ProviderInterface;
export function getTestProvider(
isProvider?: false,
setProviderOptions?: RpcProviderOptions
): RpcProvider;
export function getTestProvider(
isProvider: boolean = true,
setProviderOptions?: RpcProviderOptions
): ProviderInterface | RpcProvider {
const isDevnet = process.env.IS_DEVNET === 'true';

const providerOptions = {
const providerOptions: RpcProviderOptions = {
...setProviderOptions,
nodeUrl: process.env.TEST_RPC_URL,
// accelerate the tests when running locally
...(isDevnet && { transactionRetryIntervalFallback: 1000 }),
Expand Down
49 changes: 49 additions & 0 deletions __tests__/utils/batch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { BatchClient } from '../../src/utils/batch';
import { createBlockForDevnet, getTestProvider } from '../config/fixtures';
import { initializeMatcher } from '../config/schema';

describe('Batch Client', () => {
const provider = getTestProvider(false);

const batchClient = new BatchClient({
nodeUrl: provider.channel.nodeUrl,
headers: provider.channel.headers,
interval: 0,
});

initializeMatcher(expect);

test('should batch two requests', async () => {
await createBlockForDevnet();

const fetchSpy = jest.spyOn(batchClient as any, 'sendBatch');

const [blockNumber, blockWithReceipts] = await Promise.all([
batchClient.fetch('starknet_blockNumber'),
batchClient.fetch('starknet_getBlockWithReceipts', { block_id: 'latest' }),
]);

expect(typeof blockNumber.result).toBe('number');
expect(blockWithReceipts.result).toMatchSchemaRef('BlockWithTxReceipts');

expect(fetchSpy).toHaveBeenCalledTimes(1);
fetchSpy.mockRestore();
});

test('batch request using Provider', async () => {
const myBatchProvider = getTestProvider(false, {
batch: 0,
});

// eslint-disable-next-line @typescript-eslint/dot-notation
const sendBatchSpy = jest.spyOn(myBatchProvider.channel['batchClient'] as any, 'sendBatch');

await Promise.all([
myBatchProvider.getBlock(),
myBatchProvider.getBlockLatestAccepted(),
myBatchProvider.getBlockTransactionCount('latest'),
]);

expect(sendBatchSpy).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit c10cfe3

Please sign in to comment.