Skip to content

Commit

Permalink
0.3.0 Events and Block fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
ExoMonk committed Aug 22, 2022
1 parent a67af4a commit ca369d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
53 changes: 43 additions & 10 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,41 @@ export class RpcProvider implements ProviderInterface {
}

public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
const method =
typeof blockIdentifier === 'string' && isHex(blockIdentifier)
? 'starknet_getBlockByHash'
: 'starknet_getBlockByNumber';
const method = 'starknet_getBlockWithTxHashes';
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
);
}
}

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 @@ -270,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
16 changes: 6 additions & 10 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 @@ -143,12 +144,12 @@ export namespace RPC {
};

export type Methods = {
starknet_getBlockByHash: {
starknet_getBlockWithTxHashes: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetBlockResponse;
};
starknet_getBlockByNumber: {
starknet_getBlockWithTxs: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetBlockResponse;
Expand Down Expand Up @@ -178,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 ca369d1

Please sign in to comment.