diff --git a/packages/rpc/src/Base/index.ts b/packages/rpc/src/Base/index.ts index 2b85c2dc1..d04bad626 100644 --- a/packages/rpc/src/Base/index.ts +++ b/packages/rpc/src/Base/index.ts @@ -21,6 +21,22 @@ export const rpcProperties: RpcPropertes = { // skip subscription }; +/** + * - `0x0`: without verbosity, returns the raw serialized block bytes + * - `0x2`: with verbosity, returns the serialized block JSON object + * - `null`: default value, the same with `0x2` + */ +export type GetBlockVerbosityOptions = "0x0" | "0x2" | null; +export type GetBlockWithCycleOptions = boolean | null; +export type VerbosityBlock = + Verbosity extends "0x0" ? string : CKBComponents.BlockView; +export type BlockResponse< + Verbosity extends GetBlockVerbosityOptions, + WithCycle extends GetBlockWithCycleOptions +> = WithCycle extends true + ? { block: VerbosityBlock; cycles: CKBComponents.UInt64 } + : VerbosityBlock; + // prettier-ignore export interface GetTransaction { (hash: CKBComponents.Hash): Promise; @@ -86,10 +102,19 @@ export interface Base { * @method getBlock * @memberof DefaultRPC * @description rpc to get block by its hash - * @param {string} hash - the block hash of the target block - * @returns {Promise} block object + * @param {string} hash + * @param {string} verbosity + * @param {boolean} withCycle + * @return {Promise} */ - getBlock: (hash: CKBComponents.Hash) => Promise; + getBlock< + V extends GetBlockVerbosityOptions = null, + C extends GetBlockWithCycleOptions = null + >( + hash: CKBComponents.Hash, + verbosity?: V, + withCycle?: C + ): Promise | null>; /** * @method getHeader @@ -191,13 +216,20 @@ export interface Base { /** * @method getBlockByNumber * @memberof DefaultRPC - * @description rpc to get block by its number - * @param {string} number - the block number of the target block - * @returns {Promise} block object - */ - getBlockByNumber: ( - number: CKBComponents.BlockNumber | bigint - ) => Promise; + * @description rpc to get block by its hash + * @param {CKBComponents.BlockNumber | bigint} number + * @param {string} verbosity + * @param {boolean} withCycle + * @return {Promise} + */ + getBlockByNumber< + V extends GetBlockVerbosityOptions = null, + C extends GetBlockWithCycleOptions = null + >( + number: CKBComponents.BlockNumber | bigint, + verbosity?: V, + withCycle?: C + ): Promise | null>; /* Experimental */ diff --git a/packages/rpc/src/resultFormatter.ts b/packages/rpc/src/resultFormatter.ts index b38147647..20efed3e7 100644 --- a/packages/rpc/src/resultFormatter.ts +++ b/packages/rpc/src/resultFormatter.ts @@ -138,7 +138,10 @@ const toTip = (tip: RPC.Tip): CKBComponents.Tip => ({ blockNumber: tip.block_number, }); -const toBlock = (block: RPC.Block): CKBComponents.Block => { +function toBlock(block: string): string; +function toBlock(block: RPC.Block): CKBComponents.Block; +function toBlock(block: string | RPC.Block): CKBComponents.Block | string { + if (typeof block === "string") return block; if (!block) return block; const { header, uncles = [], transactions = [], ...rest } = block; return { @@ -147,7 +150,8 @@ const toBlock = (block: RPC.Block): CKBComponents.Block => { transactions: transactions.map(toTransaction), ...rest, }; -}; +} + const toAlertMessage = ( alertMessage: RPC.AlertMessage ): CKBComponents.AlertMessage => {