Skip to content

Commit

Permalink
feat: enable base fetch override
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Apr 8, 2024
1 parent d396275 commit 167065f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 29 deletions.
27 changes: 20 additions & 7 deletions __tests__/rpcChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { RPC07 } from '../src';
import { RPC06, RPC07 } from '../src';
import { createBlockForDevnet, getTestProvider } from './config/fixtures';
import { initializeMatcher } from './config/schema';

describe('RPC 0.7.0', () => {
const rpcProvider = getTestProvider(false);
const channel = rpcProvider.channel as RPC07.RpcChannel;
describe('RpcChannel', () => {
const { nodeUrl } = getTestProvider(false).channel;
const channel07 = new RPC07.RpcChannel({ nodeUrl });
initializeMatcher(expect);

beforeAll(async () => {
await createBlockForDevnet();
});

test('getBlockWithReceipts', async () => {
const response = await channel.getBlockWithReceipts('latest');
expect(response).toMatchSchemaRef('BlockWithTxReceipts');
test('baseFetch override', async () => {
const baseFetch = jest.fn();
const fetchChannel06 = new RPC06.RpcChannel({ nodeUrl, baseFetch });
const fetchChannel07 = new RPC07.RpcChannel({ nodeUrl, baseFetch });
(fetchChannel06.fetch as any)();
expect(baseFetch).toHaveBeenCalledTimes(1);
baseFetch.mockClear();
(fetchChannel07.fetch as any)();
expect(baseFetch).toHaveBeenCalledTimes(1);
});

describe('RPC 0.7.0', () => {
test('getBlockWithReceipts', async () => {
const response = await channel07.getBlockWithReceipts('latest');
expect(response).toMatchSchemaRef('BlockWithTxReceipts');
});
});
});
8 changes: 8 additions & 0 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ describeIfRpc('RPCProvider', () => {
await createBlockForDevnet();
});

test('baseFetch override', async () => {
const { nodeUrl } = rpcProvider.channel;
const baseFetch = jest.fn();
const fetchProvider = new RpcProvider({ nodeUrl, baseFetch });
(fetchProvider.fetch as any)();
expect(baseFetch.mock.calls.length).toBe(1);
});

test('getChainId', async () => {
const fetchSpy = jest.spyOn(rpcProvider.channel as any, 'fetchEndpoint');
(rpcProvider as any).chainId = undefined as unknown as StarknetChainId;
Expand Down
32 changes: 22 additions & 10 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,46 @@ export class RpcChannel {

public headers: object;

readonly retries: number;

public requestId: number;

readonly blockIdentifier: BlockIdentifier;

readonly retries: number;

readonly waitMode: boolean; // behave like web2 rpc and return when tx is processed

private chainId?: StarknetChainId;

private specVersion?: string;

readonly waitMode: Boolean; // behave like web2 rpc and return when tx is processed
private baseFetch: NonNullable<RpcProviderOptions['baseFetch']>;

constructor(optionsOrProvider?: RpcProviderOptions) {
const { nodeUrl, retries, headers, blockIdentifier, chainId, specVersion, waitMode } =
optionsOrProvider || {};
const {
baseFetch,
blockIdentifier,
chainId,
headers,
nodeUrl,
retries,
specVersion,
waitMode,
} = optionsOrProvider || {};
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
} else if (nodeUrl) {
this.nodeUrl = nodeUrl;
} else {
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default);
}
this.retries = retries || defaultOptions.retries;
this.headers = { ...defaultOptions.headers, ...headers };
this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier;
this.baseFetch = baseFetch ?? fetch;
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;
this.chainId = chainId;
this.headers = { ...defaultOptions.headers, ...headers };
this.retries = retries ?? defaultOptions.retries;
this.specVersion = specVersion;
this.waitMode = waitMode || false;
this.waitMode = waitMode ?? false;

this.requestId = 0;
}

Expand All @@ -82,7 +94,7 @@ export class RpcChannel {
method,
...(params && { params }),
};
return fetch(this.nodeUrl, {
return this.baseFetch(this.nodeUrl, {
method: 'POST',
body: stringify(rpcRequestBody),
headers: this.headers as Record<string, string>,
Expand Down
32 changes: 22 additions & 10 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,46 @@ export class RpcChannel {

public headers: object;

readonly retries: number;

public requestId: number;

readonly blockIdentifier: BlockIdentifier;

readonly retries: number;

readonly waitMode: boolean; // behave like web2 rpc and return when tx is processed

private chainId?: StarknetChainId;

private specVersion?: string;

readonly waitMode: Boolean; // behave like web2 rpc and return when tx is processed
private baseFetch: NonNullable<RpcProviderOptions['baseFetch']>;

constructor(optionsOrProvider?: RpcProviderOptions) {
const { nodeUrl, retries, headers, blockIdentifier, chainId, specVersion, waitMode } =
optionsOrProvider || {};
const {
baseFetch,
blockIdentifier,
chainId,
headers,
nodeUrl,
retries,
specVersion,
waitMode,
} = optionsOrProvider || {};
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
} else if (nodeUrl) {
this.nodeUrl = nodeUrl;
} else {
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default);
}
this.retries = retries || defaultOptions.retries;
this.headers = { ...defaultOptions.headers, ...headers };
this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier;
this.baseFetch = baseFetch ?? fetch;
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;
this.chainId = chainId;
this.headers = { ...defaultOptions.headers, ...headers };
this.retries = retries ?? defaultOptions.retries;
this.specVersion = specVersion;
this.waitMode = waitMode || false;
this.waitMode = waitMode ?? false;

this.requestId = 0;
}

Expand All @@ -82,7 +94,7 @@ export class RpcChannel {
method,
...(params && { params }),
};
return fetch(this.nodeUrl, {
return this.baseFetch(this.nodeUrl, {
method: 'POST',
body: stringify(rpcRequestBody),
headers: this.headers as Record<string, string>,
Expand Down
1 change: 1 addition & 0 deletions src/types/provider/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type RpcProviderOptions = {
specVersion?: string;
default?: boolean;
waitMode?: boolean;
baseFetch?: WindowOrWorkerGlobalScope['fetch'];
feeMarginPercentage?: {
l1BoundMaxAmount: number;
l1BoundMaxPricePerUnit: number;
Expand Down
2 changes: 0 additions & 2 deletions src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { toBigInt } from '../num';
import { isString } from '../shortString';
import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark';
import { ResponseParser } from '.';
import { isString } from '../shortString';


export class RPCResponseParser
implements
Expand Down

0 comments on commit 167065f

Please sign in to comment.