Skip to content

Commit 223a58f

Browse files
tabaktonipenovicp
andauthored
Fix: public responseParser (#1154)
* fix: enable instance parser change * Update __tests__/rpcProvider.test.ts Co-authored-by: Petar Penović <[email protected]> * fix: export ResponseParser and RPCResponseParser * chore: noob --------- Co-authored-by: Petar Penović <[email protected]>
1 parent 2071380 commit 223a58f

File tree

6 files changed

+68
-47
lines changed

6 files changed

+68
-47
lines changed

__tests__/rpcProvider.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
FeeEstimate,
99
RPC,
1010
RPC06,
11+
RPCResponseParser,
1112
ReceiptTx,
1213
RpcProvider,
1314
TransactionExecutionStatus,
@@ -25,14 +26,14 @@ import {
2526
compiledL1L2,
2627
compiledOpenZeppelinAccount,
2728
createBlockForDevnet,
28-
describeIfRpc,
29-
describeIfNotDevnet,
3029
describeIfDevnet,
30+
describeIfNotDevnet,
31+
describeIfRpc,
32+
describeIfTestnet,
33+
devnetETHtokenAddress,
3134
getTestAccount,
3235
getTestProvider,
33-
describeIfTestnet,
3436
waitNextBlock,
35-
devnetETHtokenAddress,
3637
} from './config/fixtures';
3738
import { initializeMatcher } from './config/schema';
3839

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

54+
test('instantiate from rpcProvider', () => {
55+
const newInsRPCProvider = new RpcProvider();
56+
57+
let FinalInsRPCProvider = new RpcProvider(newInsRPCProvider);
58+
expect(FinalInsRPCProvider.channel).toBe(newInsRPCProvider.channel);
59+
expect(FinalInsRPCProvider.responseParser).toBe(newInsRPCProvider.responseParser);
60+
61+
delete (newInsRPCProvider as any).responseParser;
62+
FinalInsRPCProvider = new RpcProvider(newInsRPCProvider);
63+
expect(FinalInsRPCProvider.channel).toBe(newInsRPCProvider.channel);
64+
expect(FinalInsRPCProvider.responseParser).toBeInstanceOf(RPCResponseParser);
65+
});
66+
5367
test('getChainId', async () => {
5468
const fetchSpy = jest.spyOn(rpcProvider.channel as any, 'fetchEndpoint');
5569
(rpcProvider as any).chainId = undefined as unknown as StarknetChainId;

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export * as starknetId from './utils/starknetId';
3434
export * as provider from './utils/provider';
3535
export * as selector from './utils/hash/selector';
3636
export * as events from './utils/events/index';
37+
export * from './utils/responseParser';
3738
export * from './utils/cairoDataTypes/uint256';
3839
export * from './utils/cairoDataTypes/uint512';
3940
export * from './utils/address';

src/provider/rpc.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { SPEC } from 'starknet-types-07';
21
import { bytesToHex } from '@noble/curves/abstract/utils';
32
import { keccak_256 } from '@noble/hashes/sha3';
3+
import type { SPEC } from 'starknet-types-07';
4+
45
import { RPC06, RPC07, RpcChannel } from '../channel';
56
import {
67
AccountInvocations,
@@ -29,27 +30,30 @@ import {
2930
getSimulateTransactionOptions,
3031
waitForTransactionOptions,
3132
} from '../types';
32-
import { getAbiContractVersion } from '../utils/calldata/cairo';
33-
import { isSierra } from '../utils/contract';
34-
import { RPCResponseParser } from '../utils/responseParser/rpc';
35-
import { GetTransactionReceiptResponse, ReceiptTx } from '../utils/transactionReceipt';
3633
import type { TransactionWithHash } from '../types/provider/spec';
3734
import assert from '../utils/assert';
38-
import { hexToBytes, toHex } from '../utils/num';
35+
import { getAbiContractVersion } from '../utils/calldata/cairo';
36+
import { isSierra } from '../utils/contract';
3937
import { addHexPrefix, removeHexPrefix } from '../utils/encode';
38+
import { hexToBytes, toHex } from '../utils/num';
4039
import { wait } from '../utils/provider';
40+
import { RPCResponseParser } from '../utils/responseParser/rpc';
41+
import { GetTransactionReceiptResponse, ReceiptTx } from '../utils/transactionReceipt';
4142
import { LibraryError } from './errors';
4243
import { ProviderInterface } from './interface';
4344

4445
export class RpcProvider implements ProviderInterface {
45-
private responseParser: RPCResponseParser;
46+
public responseParser: RPCResponseParser;
4647

4748
public channel: RPC07.RpcChannel | RPC06.RpcChannel;
4849

4950
constructor(optionsOrProvider?: RpcProviderOptions | ProviderInterface | RpcProvider) {
5051
if (optionsOrProvider && 'channel' in optionsOrProvider) {
5152
this.channel = optionsOrProvider.channel;
52-
this.responseParser = (optionsOrProvider as any).responseParser;
53+
this.responseParser =
54+
'responseParser' in optionsOrProvider
55+
? optionsOrProvider.responseParser
56+
: new RPCResponseParser();
5357
} else {
5458
this.channel = new RpcChannel({ ...optionsOrProvider, waitMode: false });
5559
this.responseParser = new RPCResponseParser(optionsOrProvider?.feeMarginPercentage);

src/utils/responseParser/index.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,2 @@
1-
import {
2-
BlockWithTxHashes,
3-
FeeEstimate,
4-
CallContractResponse,
5-
DeclareContractResponse,
6-
DeployContractResponse,
7-
EstimateFeeResponse,
8-
GetBlockResponse,
9-
GetTransactionResponse,
10-
InvokeFunctionResponse,
11-
SimulateTransactionResponse,
12-
} from '../../types';
13-
import type { GetTransactionReceiptResponse } from '../transactionReceipt';
14-
15-
export abstract class ResponseParser {
16-
abstract parseGetBlockResponse(res: BlockWithTxHashes): GetBlockResponse;
17-
18-
abstract parseGetTransactionResponse(res: any): GetTransactionResponse;
19-
20-
abstract parseGetTransactionReceiptResponse(res: any): GetTransactionReceiptResponse;
21-
22-
abstract parseFeeEstimateResponse(res: FeeEstimate[]): EstimateFeeResponse;
23-
24-
abstract parseCallContractResponse(res: any): CallContractResponse;
25-
26-
abstract parseInvokeFunctionResponse(res: any): InvokeFunctionResponse;
27-
28-
abstract parseDeployContractResponse(res: any): DeployContractResponse;
29-
30-
abstract parseDeclareContractResponse(res: any): DeclareContractResponse;
31-
32-
abstract parseSimulateTransactionResponse(res: any): SimulateTransactionResponse;
33-
}
1+
export * from './interface';
2+
export * from './rpc';

src/utils/responseParser/interface.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
BlockWithTxHashes,
3+
FeeEstimate,
4+
CallContractResponse,
5+
DeclareContractResponse,
6+
DeployContractResponse,
7+
EstimateFeeResponse,
8+
GetBlockResponse,
9+
GetTransactionResponse,
10+
InvokeFunctionResponse,
11+
SimulateTransactionResponse,
12+
} from '../../types';
13+
import type { GetTransactionReceiptResponse } from '../transactionReceipt';
14+
15+
export abstract class ResponseParser {
16+
abstract parseGetBlockResponse(res: BlockWithTxHashes): GetBlockResponse;
17+
18+
abstract parseGetTransactionResponse(res: any): GetTransactionResponse;
19+
20+
abstract parseGetTransactionReceiptResponse(res: any): GetTransactionReceiptResponse;
21+
22+
abstract parseFeeEstimateResponse(res: FeeEstimate[]): EstimateFeeResponse;
23+
24+
abstract parseCallContractResponse(res: any): CallContractResponse;
25+
26+
abstract parseInvokeFunctionResponse(res: any): InvokeFunctionResponse;
27+
28+
abstract parseDeployContractResponse(res: any): DeployContractResponse;
29+
30+
abstract parseDeclareContractResponse(res: any): DeclareContractResponse;
31+
32+
abstract parseSimulateTransactionResponse(res: any): SimulateTransactionResponse;
33+
}

src/utils/responseParser/rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Map RPC Response to common interface response
33
* Intersection (sequencer response ∩ (∪ rpc responses))
44
*/
5-
import {
5+
import type {
66
BlockWithTxHashes,
77
ContractClassPayload,
88
ContractClassResponse,
@@ -19,7 +19,7 @@ import {
1919
import { toBigInt } from '../num';
2020
import { isString } from '../shortString';
2121
import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark';
22-
import { ResponseParser } from '.';
22+
import { ResponseParser } from './interface';
2323

2424
export class RPCResponseParser
2525
implements

0 commit comments

Comments
 (0)