From ca369d16acda541323d059baad17666909e5e13d Mon Sep 17 00:00:00 2001 From: 0xExoMonk <0xexomonk@gmail.com> Date: Mon, 22 Aug 2022 16:18:20 +0200 Subject: [PATCH] 0.3.0 Events and Block fetching --- src/provider/rpc.ts | 53 +++++++++++++++++++++++++++++++++++--------- src/types/api/rpc.ts | 16 +++++-------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index fea185158..70d6f3e1b 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -90,14 +90,41 @@ export class RpcProvider implements ProviderInterface { } public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise { - 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 { + 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( @@ -270,10 +297,16 @@ export class RpcProvider implements ProviderInterface { public async getTransactionCount( blockIdentifier: BlockIdentifier ): Promise { - 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]); } /** diff --git a/src/types/api/rpc.ts b/src/types/api/rpc.ts index 2264f0a24..d326a2ebf 100644 --- a/src/types/api/rpc.ts +++ b/src/types/api/rpc.ts @@ -1,5 +1,6 @@ import { StarknetChainId } from '../../constants'; import { Status } from '../lib'; +import { BlockIdentifier } from '../../provider/utils'; export namespace RPC { export type Response = { @@ -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; @@ -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; @@ -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;