Skip to content

Commit

Permalink
fix bug of get cells
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong committed Dec 13, 2024
1 parent d699a39 commit a3ce904
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
11 changes: 6 additions & 5 deletions light-client-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientFindCellsResponse, ClientFindTransactionsGroupedResponse, ClientFindTransactionsResponse, ClientIndexerSearchKeyLike, ClientIndexerSearchKeyTransactionLike, ClientTransactionResponse } from "@ckb-ccc/core";
import { FetchResponse, LocalNode, localNodeTo, RemoteNode, remoteNodeTo, ScriptStatus, scriptStatusFrom, scriptStatusTo, LightClientSetScriptsCommand, transformFetchResponse, cccOrderToLightClientWasmOrder, GetTransactionsResponse, TxWithCell, TxWithCells, lightClientGetTransactionsResultTo, LightClientLocalNode, LightClientRemoteNode, LightClientScriptStatus, NetworkSetting } from "./types";
import { ClientIndexerSearchKeyLike, ClientIndexerSearchKeyTransactionLike, ClientTransactionResponse } from "@ckb-ccc/core";
import { FetchResponse, LocalNode, localNodeTo, RemoteNode, remoteNodeTo, ScriptStatus, scriptStatusFrom, scriptStatusTo, LightClientSetScriptsCommand, transformFetchResponse, cccOrderToLightClientWasmOrder, GetTransactionsResponse, TxWithCell, TxWithCells, lightClientGetTransactionsResultTo, LightClientLocalNode, LightClientRemoteNode, LightClientScriptStatus, NetworkSetting, GetCellsResponse, getCellsResponseFrom } from "./types";
import { ClientBlock, ClientBlockHeader, Hex, hexFrom, HexLike, Num, numFrom, NumLike, numToHex, TransactionLike } from "@ckb-ccc/core/barrel";
import { JsonRpcBlockHeader, JsonRpcTransformers } from "@ckb-ccc/core/advancedBarrel";
import { Mutex } from "async-mutex";
Expand Down Expand Up @@ -177,13 +177,14 @@ class LightClient {
order?: "asc" | "desc",
limit?: NumLike,
afterCursor?: Hex
): Promise<ClientFindCellsResponse> {
return JsonRpcTransformers.findCellsResponseTo(await this.invokeLightClientCommand("get_cells", [
): Promise<GetCellsResponse> {
const resp = await this.invokeLightClientCommand("get_cells", [
JsonRpcTransformers.indexerSearchKeyFrom(searchKey),
cccOrderToLightClientWasmOrder(order ?? "asc"),
Number(numFrom(numToHex(limit ?? 10))),
afterCursor ? bytesFrom(afterCursor) : afterCursor
]));
]);
return getCellsResponseFrom(resp);
}
/**
* See https://github.com/nervosnetwork/ckb-indexer#get_transactions
Expand Down
47 changes: 45 additions & 2 deletions light-client-js/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "@jest/globals";
import { cccOrderToLightClientWasmOrder, FetchResponse, GetTransactionsResponse, lightClientGetTransactionsResultTo, LightClientLocalNode, LightClientOrder, LightClientRemoteNode, LightClientScriptStatus, LightClientTxWithCell, LightClientTxWithCells, LocalNode, localNodeTo, RemoteNode, remoteNodeTo, scriptStatusFrom, scriptStatusTo, transformFetchResponse, TxWithCell, TxWithCells } from "./types";
import { Transaction } from "@ckb-ccc/core";
import { cccOrderToLightClientWasmOrder, FetchResponse, GetCellsResponse, getCellsResponseFrom, GetTransactionsResponse, lightClientGetTransactionsResultTo, LightClientLocalNode, LightClientOrder, LightClientRemoteNode, LightClientScriptStatus, LightClientTxWithCell, LightClientTxWithCells, LocalNode, localNodeTo, RemoteNode, remoteNodeTo, scriptStatusFrom, scriptStatusTo, transformFetchResponse, TxWithCell, TxWithCells } from "./types";
import { CellOutput, OutPoint, Transaction } from "@ckb-ccc/core";
test("test transform fetch response", () => {
const a: FetchResponse<number> = { status: "fetched", data: 1 };
const b: FetchResponse<number> = { status: "added", timestamp: 0n };
Expand Down Expand Up @@ -203,3 +203,46 @@ test("test lightClientGetTransactionsResultTo", () => {
]
} as GetTransactionsResponse<TxWithCells>);
});

test("test getCellsResponseFrom", () => {
const lhsValue = getCellsResponseFrom({
last_cursor: "0x12345678",
objects: [{
block_number: "0x2345",
tx_index: "0x9999",
out_point: {
index: "0x11112222",
tx_hash: "0x23456789"
},
output: {
capacity: "0x111",
lock: {
args: "0x2345",
code_hash: "0x2222",
hash_type: "data"
}
}
}]
});
const rhsValue: GetCellsResponse = {
lastCursor: "0x12345678",
cells: [{
blockNumber: 9029n,
txIndex: 39321n,
outPoint: OutPoint.from({
index: 0x11112222n,
txHash: "0x23456789"
}),
outputData: "0x",
cellOutput: CellOutput.from({
capacity: 0x111n,
lock: {
args: "0x2345",
codeHash: "0x2222",
hashType: "data"
}
})
}]
}
expect(lhsValue).toStrictEqual(rhsValue);
});
47 changes: 46 additions & 1 deletion light-client-js/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { numFrom } from "@ckb-ccc/core";
import { Hex } from "@ckb-ccc/core";
import { hexFrom } from "@ckb-ccc/core";
import { Cell } from "@ckb-ccc/core";
import { CellOutputLike } from "@ckb-ccc/core";
import { BytesLike } from "@ckb-ccc/core";
import { OutPointLike } from "@ckb-ccc/core";
import { numToHex } from "@ckb-ccc/core";
import { ClientBlockHeader } from "@ckb-ccc/core";
import { ScriptLike } from "@ckb-ccc/core";
import { JsonRpcBlockHeader, JsonRpcScript, JsonRpcTransaction, JsonRpcTransformers } from "@ckb-ccc/core/advancedBarrel";
import { JsonRpcBlockHeader, JsonRpcCellOutput, JsonRpcOutPoint, JsonRpcScript, JsonRpcTransaction, JsonRpcTransformers } from "@ckb-ccc/core/advancedBarrel";
import { Num, Transaction } from "@ckb-ccc/core/barrel";

interface WorkerInitializeOptions {
Expand Down Expand Up @@ -260,9 +264,48 @@ export function lightClientGetTransactionsResultTo(input: LightClientPagination<
}))
}) as GetTransactionsResponse<TxWithCells>
}
}

interface LightClientGetCellsResponse {
last_cursor: string;
objects: {
out_point: JsonRpcOutPoint;
output: JsonRpcCellOutput;
output_data?: Hex;
block_number: Hex;
tx_index: Hex;
}[];
}


interface CellWithBlockNumAndTxIndex {
outPoint: OutPointLike;
cellOutput: CellOutputLike;
outputData: BytesLike;
blockNumber: Num;
txIndex: Num;
};

interface GetCellsResponse {
lastCursor: string;
cells: CellWithBlockNumAndTxIndex[];
}



export function getCellsResponseFrom(input: LightClientGetCellsResponse): GetCellsResponse {
return {
lastCursor: input.last_cursor,
cells: input.objects.map(item => ({
outPoint: JsonRpcTransformers.outPointTo(item.out_point),
cellOutput: JsonRpcTransformers.cellOutputTo(item.output),
outputData: item.output_data ?? "0x",
blockNumber: numFrom(item.block_number),
txIndex: numFrom(item.tx_index)
}))
}
}

export type {
LightClientFunctionCall,
WorkerInitializeOptions,
Expand All @@ -281,4 +324,6 @@ export type {
TxWithCell,
TxWithCells,
GetTransactionsResponse,
CellWithBlockNumAndTxIndex,
GetCellsResponse
}

0 comments on commit a3ce904

Please sign in to comment.