Options
All
  • Public
  • Public/Protected
  • All
Menu

Package ckb-indexer

CKB indexer is based on ckb-indexer with more features. It is designed for:

  • Web client usage.
  • CKB's RPC query.

Usage

Indexer

const { Indexer } = require("@ckb-lumos/ckb-indexer");
const nodeUri = "https://testnet.ckb.dev/rpc";
const indexUri = "https://testnet.ckb.dev/indexer";
const indexer = new Indexer(indexUri, nodeUri);

CellCollector

To query existing cells, you can create a CellCollector:

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x0000000000000000000000000000000000000000000000000000000000000000",
    hash_type: "data",
    args: "0x62e907b15cbf27d5425399ebf6f0fb50ebb88f18",
  },
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

Specify lock or type script as constraints for advance search:

cellCollector = new CellCollector(indexer, {
  lock: {
    args: "0x92aad3bbab20f225cff28ec1d856c6ab63284c7a",
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
  },
  type: {
    args: "0x",
    code_hash:
      "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
    hash_type: "type",
  },
});

Query cells in certain block_numbers range (fromBlock and toBlock are included):

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
  fromBlock: "0x225510", // "0x" + 2250000n.toString(16)
  toBlock: "0x225ce0", // "0x" + 2252000n.toString(16)
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

Skip a certain number of query results, e.g. the below code snippet means it would skip the first 100 cells and return from the 101st one

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
  skip: 100,
});

for await (const tx of cellCollector.collect()) {
  console.log(tx);
}

Order by block number is supported by setting order field explicitly:

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
  fromBlock: "0x253b40", // "0x" + 2440000n.toString(16)
  toBlock: "0x253f28", // "0x" + 2441000n.toString(16)
  order: "desc", // default option is "asc"
  skip: 300,
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

Prefix search is supported on args. The default argsLen is -1, which means you pass the full slice of original args, and you can specify it when the args field is the prefix of original args.

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df3", // truncate the last byte of orignal args: 0xa528f2b9a51118b193178db4cf2f3db92e7df323
  },
  argsLen: 20, // default option is -1
  fromBlock: "0x253b40", // "0x" + 2440000n.toString(16)
  toBlock: "0x253f28", // "0x" + 2441000n.toString(16)
  order: "desc", // default option is "asc"
  skip: 300,
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

You can also set it as any when the argsLen has multiple possibilities. For example, lock script's args is 20 in normal scenario and 28 in multisig scenario, or any other length in customized scenarios.

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7d", // truncate the last two bytes of original args: 0xa528f2b9a51118b193178db4cf2f3db92e7df323
  },
  argsLen: "any",
  fromBlock: "0x253b40", // "0x" + 2440000n.toString(16)
  toBlock: "0x253f28", // "0x" + 2441000n.toString(16)
  order: "desc", // default option is "asc"
  skip: 300,
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

Fine grained query for cells can be achieved by using ScriptWrapper, with customized options like argsLen:

cellCollector = new CellCollector(indexer, {
  lock: {
    script: {
      code_hash:
        "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
      hash_type: "type",
      args: "0xe60f7f88c94ef365d540afc1574c46bb017765", // trucate the last byte of original args: 0xe60f7f88c94ef365d540afc1574c46bb017765a2
    },
    argsLen: 20,
  },
  type: {
    script: {
      code_hash:
        "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
      hash_type: "type",
      args: "0x",
    },
    // when the `argsLen` is not setted here, it will use the outside `argsLen` config, which in this case is -1 by default
  },
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

outputDataLenRange for filtering cell by data length, and outputCapacityRange for filtering cell by capacity:

cellCollector = new CellCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7d", // truncate the last two bytes of original args: 0xa528f2b9a51118b193178db4cf2f3db92e7df323
  },
  outputDataLenRange: [0x0, 0x160],
  outputCapacityRange: [0x10000, 0x100000],
});

for await (const cell of cellCollector.collect()) {
  console.log(cell);
}

To return block_hash in the result, add the following query options:

const otherQueryOptions: OtherQueryOptions = {
    withBlockHash: true,
    ckbRpcUrl: nodeUri,
  };
  const cellCollector = new CellCollector(
    indexer,
   { lock: type }
   otherQueryOptions
  );

TransactionCollector

Similar usage for quering transactions:

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x0000000000000000000000000000000000000000000000000000000000000000",
    hash_type: "data",
    args: "0x62e907b15cbf27d5425399ebf6f0fb50ebb88f18",
  },
  CKBRpcUrl,
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

Query cells in certain block_numbers range (fromBlock and toBlock are included):

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
  fromBlock: "0x0", // "0x" + 0n.toString(16)
  toBlock: "0x7d0" , // "0x" + 2000n.toString(16)
});

for await (const tx of txCollector.collect()) {
  console.log(tx);

Skip a certain number of query results, e.g. the below code snippet means it would skip the first 100 cells and return from the 101st one.

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
  skip: 100,
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

Order by block number is supported:

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
  fromBlock: "0x4e20", // "0x" + 20000n.toString(16)
  toBlock: "0x5208", // "0x" + 21000n.toString(16)
  order: "desc", // default option is "asc"
  skip: 10,
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

Prefix search is supported on args. The default argsLen is -1, which means you pass the full slice of original args, and you can specify it when the args field is the prefix of original args.

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df3", // truncate the last byte of orignal args: 0xa528f2b9a51118b193178db4cf2f3db92e7df323
  },
  argsLen: 20, // default option is -1
  fromBlock: "0x253b40", // "0x" + 2440000n.toString(16)
  toBlock: "0x253f28", // "0x" + 2441000n.toString(16)
  order: "desc", // default option is "asc"
  skip: 300,
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

You can also set it as any when the argsLen of the field args might have multiple possibilities, for example, lock script's args could be 20 in normal scenario and 28 in multisig scenario, or any other length in customized scenarios. However, there's some performance lost when use any rather than explicitly specified length due to the low-level implementation.

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7d", // truncate the last two bytes of original args: 0xa528f2b9a51118b193178db4cf2f3db92e7df323
  },
  argsLen: "any",
  fromBlock: "0x253b40", // "0x" + 2440000n.toString(16)
  toBlock: "0x253f28", // "0x" + 2441000n.toString(16)
  order: "desc", // default option is "asc"
  skip: 300,
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

Fine grained query for transactions can be achieved by using ScriptWrapper, with customized options like ioType, argsLen:

txCollector = new TransactionCollector(indexer, {
  lock: {
    script: {
      code_hash:
        "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
      hash_type: "type",
      args: "0xe60f7f88c94ef365d540afc1574c46bb017765", // trucate the last byte of original args: 0xe60f7f88c94ef365d540afc1574c46bb017765a2
    },
    ioType: "both",
    argsLen: 20, // when the `argsLen` is not setted here, it will use the outside `argsLen` config
  },
  type: {
    script: {
      code_hash:
        "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
      hash_type: "type",
      args: "0x",
    },
    ioType: "input",
  },
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

The ioType field is among input | output | both.

outputDataLenRange is support to filter cell by data length, outputCapacityRange is support to filter cell by capacity。you can use as below.

txCollector = new TransactionCollector(indexer, {
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7d", // truncate the last two bytes of original args: 0xa528f2b9a51118b193178db4cf2f3db92e7df323
  },
  outputDataLenRange: [0x0, 0x160],
  outputCapacityRange: [0x10000, 0x100000],
});

for await (const tx of txCollector.collect()) {
  console.log(tx);
}

EventEmitter

Besides polling pattern, event-driven pattern is also supported. After subsribing for certain lock|type script, it will emit a changed event when a block containing the subsribed script is indexed or rollbacked.

The principle of the design is unreliable notification queue, so developers are supposed to pull from the data sources via CellCollector|TransactionCollector, to find out what might happened: cell consumed, new cell generated, new transaction generated, or a chain fork happened, etc; and take the next step accordingly.

eventEmitter = indexer.subscribe({
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7df323",
  },
});

eventEmitter.on("changed", () => {
  console.log(
    "States changed with the script, please pull the data sources from the indexer to find out what happend"
  );
});

Other query options like fromBlock|argsLen|data are also supported.

eventEmitter = indexer.subscribe({
  lock: {
    code_hash:
      "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    hash_type: "type",
    // the args bytes length is 18, truncate the last 2 bytes.
    args: "0xa528f2b9a51118b193178db4cf2f3db92e7d",
  },
  // default value is -1
  argsLen: 20,
  // default value is "any"
  data: "0x",
  // default value is 0
  fromBlock: 0x3e8, // "0x" + 1000n.toString(16)
});

Listen to median time change when blocks changed.

const medianTimeEmitter = indexer.subscribeMedianTime();
medianTimeEmitter.on("changed", (medianTime) => {
  console.log(medianTime);
});

Migration

If you want to migrate native indexer to ckb-indexer, please check more detail in our migration docs

Index

References

Classes

Interfaces

Type aliases

Variables

Functions

References

RPC

Re-exports RPC

RPC

Re-exports RPC

Type aliases

Bytes32

Bytes32: string

Bytes32

Bytes32: string

CellOutput

CellOutput: { capacity: HexNumber; lock: Script; type?: Script }

Type declaration

CellOutput

CellOutput: { capacity: HexNumber; lock: Script; type?: Script }

Type declaration

CellOutput

CellOutput: { capacity: HexNumber; lock: Script; type?: Script }

Type declaration

CellOutput

CellOutput: { capacity: HexNumber; lock: Script; type?: Script }

Type declaration

HexNum

HexNum: string

HexNum

HexNum: string

HexadecimalRange

HexadecimalRange: [Hexadecimal, Hexadecimal]

HexadecimalRange

HexadecimalRange: [Hexadecimal, Hexadecimal]

HexadecimalRange

HexadecimalRange: [Hexadecimal, Hexadecimal]

HexadecimalRange

HexadecimalRange: [Hexadecimal, Hexadecimal]

IOType

IOType: "input" | "output" | "both"

IOType

IOType: "input" | "output" | "both"

IndexerTransaction

IndexerTransaction: { blockNumber: HexNum; ioIndex: HexNum; ioType: IOType; txHash: Bytes32; txIndex: HexNum }

Type declaration

IndexerTransaction

IndexerTransaction: { blockNumber: HexNum; ioIndex: HexNum; ioType: IOType; txHash: Bytes32; txIndex: HexNum }

Type declaration

Order

Order: "asc" | "desc"

Order

Order: "asc" | "desc"

Script

Script: { args: HexString; code_hash: HexString; hash_type: "type" | "data" | "data1" }

Type declaration

Script

Script: { args: HexString; code_hash: HexString; hash_type: "type" | "data" | "data1" }

Type declaration

ScriptType

ScriptType: "type" | "lock"

ScriptType

ScriptType: "type" | "lock"

ScriptType

ScriptType: "type" | "lock"

ScriptType

ScriptType: "type" | "lock"

Terminator

Terminator: (index: number, cell: Cell) => TerminatorResult

Type declaration

Terminator

Terminator: (index: number, cell: Cell) => TerminatorResult

Type declaration

Tip

Tip: { blockHash: HexNumber; blockNumber: HexString }

Type declaration

Tip

Tip: { block_hash: HexNumber; block_number: HexString }

Type declaration

Tip

Tip: { block_hash: HexNumber; block_number: HexString }

Type declaration

Tip

Tip: { blockHash: HexNumber; blockNumber: HexString }

Type declaration

Variables

Const CellCollector

CellCollector: typeof CKBCellCollector

CellCollector will not get cell with blockHash by default, please use OtherQueryOptions.withBlockHash and OtherQueryOptions.CKBRpcUrl to get blockHash if you need.

Const CellCollector

CellCollector: CKBCellCollector = CKBCellCollector

CellCollector will not get cell with blockHash by default, please use OtherQueryOptions.withBlockHash and OtherQueryOptions.CKBRpcUrl to get blockHash if you need.

Const Indexer

Indexer: typeof CkbIndexer

CkbIndexer.collector will not get cell with blockHash by default, please use OtherQueryOptions.withBlockHash and OtherQueryOptions.CKBRpcUrl to get blockHash if you need.

Const Indexer

Indexer: CkbIndexer = CkbIndexer

CkbIndexer.collector will not get cell with blockHash by default, please use OtherQueryOptions.withBlockHash and OtherQueryOptions.CKBRpcUrl to get blockHash if you need.

Const TransactionCollector

TransactionCollector: typeof CKBIndexerTransactionCollector

Const TransactionCollector

TransactionCollector: CKBIndexerTransactionCollector = CKBIndexerTransactionCollector

Const generateSearchKey

generateSearchKey: (queries: CKBIndexerQueryOptions) => SearchKey

Type declaration

Const getHexStringBytes

getHexStringBytes: (hexString: HexString) => number

Type declaration

Const requestBatch

requestBatch: (rpcUrl: string, data: unknown) => Promise<any>

Type declaration

    • (rpcUrl: string, data: unknown): Promise<any>
    • Parameters

      • rpcUrl: string
      • data: unknown

      Returns Promise<any>

Const toCellOutPut

toCellOutPut: (data: RPCType.CellOutput) => IndexerType.CellOutput

Type declaration

    • (data: RPCType.CellOutput): IndexerType.CellOutput
    • Parameters

      • data: RPCType.CellOutput

      Returns IndexerType.CellOutput

Const toOutPoint

toOutPoint: (data: OutPoint) => OutPoint

Type declaration

Const toScript

toScript: (data: Script) => RPCType.Script

Type declaration

    • (data: Script): RPCType.Script
    • Parameters

      Returns RPCType.Script

Const toScript

toScript: (data: RPCType.Script) => Script

Type declaration

    • (data: RPCType.Script): Script
    • Parameters

      • data: RPCType.Script

      Returns Script

Const toSearchFilter

toSearchFilter: (data: SearchFilter) => SearchFilter

Type declaration

Const toSearchFilter

toSearchFilter: (data: SearchFilter) => SearchFilter

Type declaration

Const toSearchKey

toSearchKey: (data: SearchKey) => SearchKey

Type declaration

Const toSearchKey

toSearchKey: (data: SearchKey) => SearchKey

Type declaration

Const toTip

toTip: (tip: RPCType.Tip) => IndexerType.Tip

Type declaration

    • (tip: RPCType.Tip): IndexerType.Tip
    • Parameters

      • tip: RPCType.Tip

      Returns IndexerType.Tip

Functions

Const DefaultTerminator

  • DefaultTerminator(): { push: true; stop: false }
  • Returns { push: true; stop: false }

    • push: true
    • stop: false

Const UnwrapScriptWrapper

defaultLogger

  • defaultLogger(level: string, message: string): void
  • Parameters

    • level: string
    • message: string

    Returns void

Const generateSearchKey

Const getHexStringBytes

  • getHexStringBytes(hexString: HexString): number
  • Parameters

    Returns number

instanceOfScriptWrapper

  • instanceOfScriptWrapper(object: unknown): object is ScriptWrapper
  • Parameters

    • object: unknown

    Returns object is ScriptWrapper

instanceOfScriptWrapper

  • instanceOfScriptWrapper(object: unknown): object is ScriptWrapper
  • Parameters

    • object: unknown

    Returns object is ScriptWrapper

Const request

  • request(ckbIndexerUrl: string, method: string, params?: any): Promise<any>
  • Parameters

    • ckbIndexerUrl: string
    • method: string
    • Optional params: any

    Returns Promise<any>

Const requestBatch

  • requestBatch(rpcUrl: string, data: unknown): Promise<any>
  • Parameters

    • rpcUrl: string
    • data: unknown

    Returns Promise<any>

Const toCellOutPut

  • toCellOutPut(data: RPCType.CellOutput): IndexerType.CellOutput
  • Parameters

    • data: RPCType.CellOutput

    Returns IndexerType.CellOutput

Const toOutPoint

  • Parameters

    Returns OutPoint

Const toScript

  • toScript(data: Script): RPCType.Script
  • Parameters

    Returns RPCType.Script

Const toScript

  • toScript(data: RPCType.Script): Script
  • Parameters

    • data: RPCType.Script

    Returns Script

Const toSearchFilter

Const toSearchFilter

Const toSearchKey

  • Parameters

    Returns SearchKey

Const toSearchKey

  • Parameters

    Returns SearchKey

Const toTip

  • toTip(tip: RPCType.Tip): IndexerType.Tip
  • Parameters

    • tip: RPCType.Tip

    Returns IndexerType.Tip

Generated using TypeDoc