Skip to content

Commit

Permalink
feat(core): Client.getCellLive
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Sep 1, 2024
1 parent 1900628 commit 0462a4e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-months-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/core": patch
---

feat(core): Client.getCellLive
2 changes: 1 addition & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mode": "pre",
"mode": "exit",
"tag": "alpha",
"initialVersions": {
"@ckb-ccc/ccc": "0.0.12",
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ export abstract class Client {
return cell;
}

abstract getCellLiveNoCache(
outPointLike: OutPointLike,
withData?: boolean | null,
includeTxPool?: boolean | null,
): Promise<Cell | undefined>;
async getCellLive(
outPointLike: OutPointLike,
withData?: boolean | null,
includeTxPool?: boolean | null,
): Promise<Cell | undefined> {
const cell = await this.getCellLiveNoCache(
outPointLike,
withData,
includeTxPool,
);
if (withData && cell) {
await this.cache.recordCells(cell);
}
return cell;
}

abstract findCellsPagedNoCache(
key: ClientIndexerSearchKeyLike,
order?: "asc" | "desc",
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/client/clientTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,16 @@ export type ClientBlock = {
};

export interface ErrorClientBaseLike {
message: string;
code: number;
message?: string;
code?: number;
data: string;
}
export class ErrorClientBase extends Error {
public readonly message: string;
public readonly code: number;
public readonly code?: number;
public readonly data: string;

constructor(origin: ErrorClientBaseLike) {
super(origin.message);
this.message = origin.message;
this.code = origin.code;
this.data = origin.data;
}
Expand Down
58 changes: 55 additions & 3 deletions packages/core/src/client/jsonRpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { OutPoint, TransactionLike } from "../../ckb/index.js";
import {
Cell,
OutPoint,
OutPointLike,
TransactionLike,
} from "../../ckb/index.js";
import { Hex, HexLike, hexFrom } from "../../hex/index.js";
import { Num, NumLike, numFrom, numToHex } from "../../num/index.js";
import { apply } from "../../utils/index.js";
Expand All @@ -19,7 +24,7 @@ import {
Transport,
transportFromUri,
} from "../transports/advanced.js";
import { JsonRpcTransformers } from "./advanced.js";
import { JsonRpcCellOutput, JsonRpcTransformers } from "./advanced.js";

/**
* Applies a transformation function to a value if the transformer is provided.
Expand Down Expand Up @@ -207,6 +212,48 @@ export abstract class ClientJsonRpc extends Client {
JsonRpcTransformers.transactionResponseTo,
) as (txHash: HexLike) => Promise<ClientTransactionResponse | undefined>;

/**
* Get a live cell from node.
*
* @param outPoint - The out point of the cell.
* @param withData - Include data in the response.
* @param includeTxPool - Include cells in the tx pool.
* @returns The cell
*/
getCellLiveNoCache(
outPoint: OutPointLike,
withData?: boolean | null,
includeTxPool?: boolean | null,
) {
return this.buildSender(
"get_live_cell",
[JsonRpcTransformers.outPointFrom],
({
cell,
}: {
cell?: {
output: JsonRpcCellOutput;
data?: { content: HexLike; hash: HexLike };
};
}) =>
apply(
({
output,
data,
}: {
output: JsonRpcCellOutput;
data?: { content: HexLike; hash: HexLike };
}) =>
Cell.from({
cellOutput: JsonRpcTransformers.cellOutputTo(output),
outputData: data?.content ?? "0x",
outPoint,
}),
cell,
),
)(outPoint, withData ?? true, includeTxPool) as Promise<Cell | undefined>;
}

/**
* find cells from node.
*
Expand Down Expand Up @@ -296,7 +343,12 @@ export abstract class ClientJsonRpc extends Client {
try {
return transform(await this.send(payload), outTransformer);
} catch (errAny: unknown) {
if (typeof errAny !== "object" || errAny === null) {
if (
typeof errAny !== "object" ||
errAny === null ||
!("data" in errAny) ||
typeof errAny.data !== "string"
) {
throw errAny;
}
const err = errAny as ErrorClientBaseLike;
Expand Down

0 comments on commit 0462a4e

Please sign in to comment.