Skip to content

Commit

Permalink
feat(rpc): support get_header with verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Sep 25, 2023
1 parent 02d881d commit 1e8389b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
14 changes: 8 additions & 6 deletions packages/rpc/src/Base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export interface GetTransaction {
(hash: CKBComponents.Hash, verbosity: "0x2", onlyCommitted?: boolean): Promise<CKBComponents.TransactionWithStatus>;
}

// prettier-ignore
export interface GetHeader<Q> {
(query: Q, verbosity?: "0x1"): Promise<CKBComponents.BlockHeader>;
(query: Q, verbosity: "0x0"): Promise<string>;
}

export interface Base {
/* Chain */

Expand Down Expand Up @@ -91,19 +97,15 @@ export interface Base {
* @description Returns the information about a block header by hash.
* @params {Promise<string>} block hash
*/
getHeader: (
blockHash: CKBComponents.Hash
) => Promise<CKBComponents.BlockHeader>;
getHeader: GetHeader<CKBComponents.Hash>;

/**
* @method getHeaderByNumber
* @memberof DefaultRPC
* @description Returns the information about a block header by block number
* @params {Promise<string>} block number
*/
getHeaderByNumber: (
blockNumber: CKBComponents.BlockNumber | bigint
) => Promise<CKBComponents.BlockHeader>;
getHeaderByNumber: GetHeader<CKBComponents.BlockNumber | bigint>;

/**
* @method getLiveCell
Expand Down
11 changes: 9 additions & 2 deletions packages/rpc/src/resultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const toNullable =
const toNumber = (number: RPC.BlockNumber): CKBComponents.BlockNumber =>
number.toString();
const toHash = (hash: RPC.Hash256): CKBComponents.Hash256 => hash;
const toHeader = (header: RPC.Header): CKBComponents.BlockHeader => {

function toHeader(header: RPC.Header): CKBComponents.BlockHeader;
function toHeader(header: string): string;
function toHeader(
header: string | RPC.Header
): string | CKBComponents.BlockHeader {
if (typeof header === "string") return header;
if (!header) return header;
const {
compact_target: compactTarget,
Expand All @@ -42,7 +48,8 @@ const toHeader = (header: RPC.Header): CKBComponents.BlockHeader => {
extraHash,
...rest,
};
};
}

const toScript = (script: RPC.Script): CKBComponents.Script => {
if (!script) return script;
const { code_hash: codeHash, hash_type: hashType, ...rest } = script;
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export namespace CKBComponents {
export type Transaction = Required<api.Transaction>;
export type TransactionWithStatus<Tx = Transaction> =
api.TransactionWithStatus<Tx>;
export type BlockHeader = api.Header;
export type BlockHeader<T = api.Header> = T;
export type Block = api.Block;
export type UncleBlock = api.UncleBlock;
export type LiveCell = api.LiveCell;
Expand Down

0 comments on commit 1e8389b

Please sign in to comment.