diff --git a/index.html b/index.html index ebacefd..37e40fc 100644 --- a/index.html +++ b/index.html @@ -25,11 +25,27 @@ $("#init-button").removeClass("loading"); }; window.doTest = async () => { - const tx = { "version": 0n, "cellDeps": [{ "outPoint": { "txHash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37", "index": 0n }, "depType": "depGroup" }], "headerDeps": [], "inputs": [{ "previousOutput": { "txHash": "0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f", "index": 7n }, "since": 0n }], "outputs": [{ "capacity": 20000000000000000n, "lock": { "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type", "args": "0xff5094c2c5f476fc38510018609a3fd921dd28ad" } }, { "capacity": 819957834400000000n, "lock": { "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type", "args": "0x64257f00b6b63e987609fa9be2d0c86d351020fb" } }], "outputsData": ["0x", "0x"], "witnesses": ["0x5500000010000000550000005500000041000000af34b54bebf8c5971da6a880f2df5a186c3f8d0b5c9a1fe1a90c95b8a4fb89ef3bab1ccec13797dcb3fee80400f953227dd7741227e08032e3598e16ccdaa49c00"] }; - console.log(await window.client.sendTransaction(tx)); - console.log(await window.client.estimateCycles(tx)); - + await window.client.setScripts([{ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23", }, scriptType: "lock", blockNumber: "0xb8d328" }], 0) + console.log(await window.client.getCellsCapacity({ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" }, scriptType: "lock" })) + console.log(await window.client.getCells({ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" }, scriptType: "lock" })) + console.log(await window.client.getTransactions({ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" }, scriptType: "lock" })) }; + window.doTest2 = async () => { + const cells = await window.client.getCells({ + script: { + codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + hashType: "type", + args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" + }, scriptType: "lock" + }, "asc", 1); + return await window.client.getCells({ + script: { + codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + hashType: "type", + args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" + }, scriptType: "lock" + }, "asc", 1, cells.lastCursor); + } diff --git a/light-client-js/src/index.ts b/light-client-js/src/index.ts index d332565..2ea52b4 100644 --- a/light-client-js/src/index.ts +++ b/light-client-js/src/index.ts @@ -3,6 +3,7 @@ import { FetchResponse, LocalNode, localNodeTo, RemoteNode, remoteNodeTo, Script 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"; +import { bytesFrom } from "@ckb-ccc/core"; const DEFAULT_BUFFER_SIZE = 50 * (1 << 20); /** @@ -174,13 +175,13 @@ class LightClient { searchKey: ClientIndexerSearchKeyLike, order?: "asc" | "desc", limit?: NumLike, - afterCursor?: string + afterCursor?: Hex ): Promise { return JsonRpcTransformers.findCellsResponseTo(await this.invokeLightClientCommand("get_cells", [ JsonRpcTransformers.indexerSearchKeyFrom(searchKey), cccOrderToLightClientWasmOrder(order ?? "asc"), Number(numFrom(numToHex(limit ?? 10))), - afterCursor + afterCursor ? bytesFrom(afterCursor) : afterCursor ])); } /** @@ -195,7 +196,7 @@ class LightClient { searchKey: ClientIndexerSearchKeyTransactionLike, order?: "asc" | "desc", limit?: NumLike, - afterCursor?: string + afterCursor?: Hex ): Promise | GetTransactionsResponse> { return lightClientGetTransactionsResultTo(await this.invokeLightClientCommand( "get_transactions", @@ -203,7 +204,7 @@ class LightClient { JsonRpcTransformers.indexerSearchKeyTransactionFrom(searchKey), cccOrderToLightClientWasmOrder(order ?? "asc"), Number(numFrom(numToHex(limit ?? 10))), - afterCursor + afterCursor ? bytesFrom(afterCursor) : afterCursor ] )); } diff --git a/light-client-lib/src/service.rs b/light-client-lib/src/service.rs index 38328c6..4f9fbd4 100644 --- a/light-client-lib/src/service.rs +++ b/light-client-lib/src/service.rs @@ -192,14 +192,14 @@ pub enum ScriptType { } #[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen)] -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "snake_case")] pub enum Order { Desc, Asc, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] pub struct Cell { pub output: CellOutput, pub output_data: Option, diff --git a/light-client-wasm/src/lib.rs b/light-client-wasm/src/lib.rs index f98837b..7f8d213 100644 --- a/light-client-wasm/src/lib.rs +++ b/light-client-wasm/src/lib.rs @@ -519,6 +519,10 @@ pub fn get_cells( if !status(0b1) { return Err(JsValue::from_str("light client not on start state")); } + debug!( + "Calling get_cells with {:?}, {:?}, {:?}, {:?}", + search_key, order, limit, after_cursor + ); let search_key: SearchKey = serde_wasm_bindgen::from_value(search_key)?; let (prefix, from_key, direction, skip) = build_query_options( @@ -560,6 +564,7 @@ pub fn get_cells( let mut cells = Vec::new(); let mut last_key = Vec::new(); for (key, value) in kvs.into_iter().map(|kv| (kv.key, kv.value)) { + debug!("get cells iterator at {:?} {:?}", key, value); let tx_hash = packed::Byte32::from_slice(&value).expect("stored tx hash"); let output_index = u32::from_be_bytes( key[key.len() - 4..] @@ -602,6 +607,7 @@ pub fn get_cells( .as_slice() .starts_with(prefix) { + debug!("skipped at {}", line!()); continue; } } @@ -611,6 +617,7 @@ pub fn get_cells( .as_slice() .starts_with(prefix) { + debug!("skipped at {}", line!()); continue; } } @@ -622,6 +629,7 @@ pub fn get_cells( ScriptType::Lock => { let script_len = extract_raw_data(&output.lock()).len(); if script_len < r0 || script_len > r1 { + debug!("skipped at {}", line!()); continue; } } @@ -632,6 +640,7 @@ pub fn get_cells( .map(|script| extract_raw_data(&script).len()) .unwrap_or_default(); if script_len < r0 || script_len > r1 { + debug!("skipped at {}", line!()); continue; } } @@ -640,6 +649,7 @@ pub fn get_cells( if let Some([r0, r1]) = filter_output_data_len_range { if output_data.len() < r0 || output_data.len() >= r1 { + debug!("skipped at {}", line!()); continue; } } @@ -647,19 +657,20 @@ pub fn get_cells( if let Some([r0, r1]) = filter_output_capacity_range { let capacity: core::Capacity = output.capacity().unpack(); if capacity < r0 || capacity >= r1 { + debug!("skipped at {}", line!()); continue; } } if let Some([r0, r1]) = filter_block_range { if block_number < r0 || block_number >= r1 { + debug!("skipped at {}", line!()); continue; } } last_key = key.to_vec(); - - cells.push(Cell { + let cell_to_push = Cell { output: output.into(), output_data: if with_data { Some(output_data.into()) @@ -669,9 +680,11 @@ pub fn get_cells( out_point: packed::OutPoint::new(tx_hash, output_index).into(), block_number: block_number.into(), tx_index: tx_index.into(), - }); + }; + debug!("pushed cell {:#?}", cell_to_push); + cells.push(cell_to_push); } - + debug!("get_cells last_key={:?}", last_key); Ok((Pagination { objects: cells, last_cursor: JsonBytes::from_vec(last_key),