Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: public responseParser #1154

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FeeEstimate,
RPC,
RPC06,
RPCResponseParser,
ReceiptTx,
RpcProvider,
TransactionExecutionStatus,
Expand All @@ -25,14 +26,14 @@ import {
compiledL1L2,
compiledOpenZeppelinAccount,
createBlockForDevnet,
describeIfRpc,
describeIfNotDevnet,
describeIfDevnet,
describeIfNotDevnet,
describeIfRpc,
describeIfTestnet,
devnetETHtokenAddress,
getTestAccount,
getTestProvider,
describeIfTestnet,
waitNextBlock,
devnetETHtokenAddress,
} from './config/fixtures';
import { initializeMatcher } from './config/schema';

Expand All @@ -50,6 +51,19 @@ describeIfRpc('RPCProvider', () => {
await createBlockForDevnet();
});

test('instantiate from rpcProvider', () => {
const newInsRPCProvider = new RpcProvider();

let FinalInsRPCProvider = new RpcProvider(newInsRPCProvider);
expect(FinalInsRPCProvider.channel).toBe(newInsRPCProvider.channel);
expect(FinalInsRPCProvider.responseParser).toBe(newInsRPCProvider.responseParser);

delete (newInsRPCProvider as any).responseParser;
FinalInsRPCProvider = new RpcProvider(newInsRPCProvider);
expect(FinalInsRPCProvider.channel).toBe(newInsRPCProvider.channel);
expect(FinalInsRPCProvider.responseParser).toBeInstanceOf(RPCResponseParser);
});

test('getChainId', async () => {
const fetchSpy = jest.spyOn(rpcProvider.channel as any, 'fetchEndpoint');
(rpcProvider as any).chainId = undefined as unknown as StarknetChainId;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export * as starknetId from './utils/starknetId';
export * as provider from './utils/provider';
export * as selector from './utils/selector';
export * as events from './utils/events/index';
export * from './utils/responseParser';
export * from './utils/cairoDataTypes/uint256';
export * from './utils/cairoDataTypes/uint512';
export * from './utils/address';
Expand Down
20 changes: 12 additions & 8 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SPEC } from 'starknet-types-07';
import { bytesToHex } from '@noble/curves/abstract/utils';
import { keccak_256 } from '@noble/hashes/sha3';
import type { SPEC } from 'starknet-types-07';

import { RPC06, RPC07, RpcChannel } from '../channel';
import {
AccountInvocations,
Expand Down Expand Up @@ -29,27 +30,30 @@ import {
getSimulateTransactionOptions,
waitForTransactionOptions,
} from '../types';
import { getAbiContractVersion } from '../utils/calldata/cairo';
import { isSierra } from '../utils/contract';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { GetTransactionReceiptResponse, ReceiptTx } from '../utils/transactionReceipt';
import type { TransactionWithHash } from '../types/provider/spec';
import assert from '../utils/assert';
import { hexToBytes, toHex } from '../utils/num';
import { getAbiContractVersion } from '../utils/calldata/cairo';
import { isSierra } from '../utils/contract';
import { addHexPrefix, removeHexPrefix } from '../utils/encode';
import { hexToBytes, toHex } from '../utils/num';
import { wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { GetTransactionReceiptResponse, ReceiptTx } from '../utils/transactionReceipt';
import { LibraryError } from './errors';
import { ProviderInterface } from './interface';

export class RpcProvider implements ProviderInterface {
private responseParser: RPCResponseParser;
public responseParser: RPCResponseParser;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest making this private again or adding an export for the RPCResponseParser class somewhere so it is exposed by the library and can be picked up by the automated documentation generation.

Copy link
Collaborator Author

@tabaktoni tabaktoni Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c2887ed

Exported both interface and Imp


public channel: RPC07.RpcChannel | RPC06.RpcChannel;

constructor(optionsOrProvider?: RpcProviderOptions | ProviderInterface | RpcProvider) {
if (optionsOrProvider && 'channel' in optionsOrProvider) {
this.channel = optionsOrProvider.channel;
this.responseParser = (optionsOrProvider as any).responseParser;
this.responseParser =
'responseParser' in optionsOrProvider
? optionsOrProvider.responseParser
: new RPCResponseParser();
} else {
this.channel = new RpcChannel({ ...optionsOrProvider, waitMode: false });
this.responseParser = new RPCResponseParser(optionsOrProvider?.feeMarginPercentage);
Expand Down
35 changes: 2 additions & 33 deletions src/utils/responseParser/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,2 @@
import {
BlockWithTxHashes,
FeeEstimate,
CallContractResponse,
DeclareContractResponse,
DeployContractResponse,
EstimateFeeResponse,
GetBlockResponse,
GetTransactionResponse,
InvokeFunctionResponse,
SimulateTransactionResponse,
} from '../../types';
import type { GetTransactionReceiptResponse } from '../transactionReceipt';

export abstract class ResponseParser {
abstract parseGetBlockResponse(res: BlockWithTxHashes): GetBlockResponse;

abstract parseGetTransactionResponse(res: any): GetTransactionResponse;

abstract parseGetTransactionReceiptResponse(res: any): GetTransactionReceiptResponse;

abstract parseFeeEstimateResponse(res: FeeEstimate[]): EstimateFeeResponse;

abstract parseCallContractResponse(res: any): CallContractResponse;

abstract parseInvokeFunctionResponse(res: any): InvokeFunctionResponse;

abstract parseDeployContractResponse(res: any): DeployContractResponse;

abstract parseDeclareContractResponse(res: any): DeclareContractResponse;

abstract parseSimulateTransactionResponse(res: any): SimulateTransactionResponse;
}
export * from './interface';
export * from './rpc';
33 changes: 33 additions & 0 deletions src/utils/responseParser/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
BlockWithTxHashes,
FeeEstimate,
CallContractResponse,
DeclareContractResponse,
DeployContractResponse,
EstimateFeeResponse,
GetBlockResponse,
GetTransactionResponse,
InvokeFunctionResponse,
SimulateTransactionResponse,
} from '../../types';
import type { GetTransactionReceiptResponse } from '../transactionReceipt';

export abstract class ResponseParser {
abstract parseGetBlockResponse(res: BlockWithTxHashes): GetBlockResponse;

abstract parseGetTransactionResponse(res: any): GetTransactionResponse;

abstract parseGetTransactionReceiptResponse(res: any): GetTransactionReceiptResponse;

abstract parseFeeEstimateResponse(res: FeeEstimate[]): EstimateFeeResponse;

abstract parseCallContractResponse(res: any): CallContractResponse;

abstract parseInvokeFunctionResponse(res: any): InvokeFunctionResponse;

abstract parseDeployContractResponse(res: any): DeployContractResponse;

abstract parseDeclareContractResponse(res: any): DeclareContractResponse;

abstract parseSimulateTransactionResponse(res: any): SimulateTransactionResponse;
}
4 changes: 2 additions & 2 deletions src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Map RPC Response to common interface response
* Intersection (sequencer response ∩ (∪ rpc responses))
*/
import {
import type {
BlockWithTxHashes,
ContractClassPayload,
ContractClassResponse,
Expand All @@ -19,7 +19,7 @@ import {
import { toBigInt } from '../num';
import { isString } from '../shortString';
import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark';
import { ResponseParser } from '.';
import { ResponseParser } from './interface';

export class RPCResponseParser
implements
Expand Down