Skip to content

Commit

Permalink
Merge pull request #1 from ExoMonk/develop
Browse files Browse the repository at this point in the history
0.3.0 Events and Block fetching
  • Loading branch information
envusr authored Aug 22, 2022
2 parents 3b12421 + 14e4fef commit 12ebdb7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
36 changes: 33 additions & 3 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,35 @@ export class RpcProvider implements ProviderInterface {
this.responseParser.parseGetBlockResponse
);
}
else if (typeof blockIdentifier === 'number') {
return this.fetchEndpoint(method, [{ block_number: blockIdentifier }]).then(
this.responseParser.parseGetBlockResponse
);
}
else {
return this.fetchEndpoint(method, [blockIdentifier]).then(
this.responseParser.parseGetBlockResponse
);
}
}

public async getBlockWithTxs(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
const method = 'starknet_getBlockWithTxs';
if (typeof blockIdentifier === 'string' && isHex(blockIdentifier)) {
return this.fetchEndpoint(method, [{ block_hash: blockIdentifier }]).then(
this.responseParser.parseGetBlockResponse
);
}
else if (typeof blockIdentifier === 'number') {
return this.fetchEndpoint(method, [{ block_number: blockIdentifier }]).then(
this.responseParser.parseGetBlockResponse
);
}
else {
return this.fetchEndpoint(method, [blockIdentifier]).then(
this.responseParser.parseGetBlockResponse
);
}
}

public async getStorageAt(
Expand Down Expand Up @@ -273,10 +297,16 @@ export class RpcProvider implements ProviderInterface {
public async getTransactionCount(
blockIdentifier: BlockIdentifier
): Promise<RPC.GetTransactionCountResponse> {
if (typeof blockIdentifier === 'number') {
return this.fetchEndpoint('starknet_getBlockTransactionCountByNumber', [blockIdentifier]);
const method = 'starknet_getBlockTransactionCount';
if (typeof blockIdentifier === 'string' && isHex(blockIdentifier)) {
return this.fetchEndpoint(method, [{ block_hash: blockIdentifier }]);
}
else if (typeof blockIdentifier === 'number') {
return this.fetchEndpoint(method, [{ block_number: blockIdentifier }]);
}
else {
return this.fetchEndpoint(method, [blockIdentifier]);
}
return this.fetchEndpoint('starknet_getBlockTransactionCountByHash', [blockIdentifier]);
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StarknetChainId } from '../../constants';
import { Status } from '../lib';
import { BlockIdentifier } from '../../provider/utils';

export namespace RPC {
export type Response = {
Expand Down Expand Up @@ -99,8 +100,8 @@ export namespace RPC {
| boolean;

export type EventFilter = {
fromBlock: string;
toBlock: string;
fromBlock: BlockIdentifier;
toBlock: BlockIdentifier;
address: string;
keys: string[];
page_size: number;
Expand Down Expand Up @@ -148,6 +149,11 @@ export namespace RPC {
REQUEST: any[];
RESPONSE: GetBlockResponse;
};
starknet_getBlockWithTxs: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetBlockResponse;
};
starknet_getStorageAt: {
QUERY: never;
REQUEST: any[];
Expand All @@ -173,12 +179,7 @@ export namespace RPC {
REQUEST: any[];
RESPONSE: GetTransactionReceiptResponse;
};
starknet_getBlockTransactionCountByHash: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetTransactionCountResponse;
};
starknet_getBlockTransactionCountByNumber: {
starknet_getBlockTransactionCount: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetTransactionCountResponse;
Expand Down

0 comments on commit 12ebdb7

Please sign in to comment.