UNPKG

@ickb/utils

Version:

General utilities built on top of CCC

72 lines 2.33 kB
import { ccc } from "@ckb-ccc/core"; import { unique } from "./utils.js"; const isCapacitySymbol = Symbol("isCapacity"); export class CapacityManager { constructor(outputDataLenRange) { Object.defineProperty(this, "outputDataLenRange", { enumerable: true, configurable: true, writable: true, value: outputDataLenRange }); } static withEmptyData() { return new CapacityManager([0n, 1n]); } static withAnyData() { return new CapacityManager(undefined); } isCapacity(cell) { if (cell.cellOutput.type !== undefined) { return false; } if (!this.outputDataLenRange) { return true; } const [start, end] = this.outputDataLenRange; const dataLen = (cell.outputData.length - 2) / 2; if (start <= dataLen && dataLen < end) { return true; } return false; } addCapacities(tx, capacities) { for (const { cell } of capacities) { tx.addInput(cell); } } async *findCapacities(client, locks, options) { const limit = options?.limit ?? defaultFindCellsLimit; for (const lock of unique(locks)) { const findCellsArgs = [ { script: lock, scriptType: "lock", filter: { scriptLenRange: [0n, 1n], outputDataLenRange: this.outputDataLenRange, }, scriptSearchMode: "exact", withData: true, }, "asc", limit, ]; for await (const cell of options?.onChain ? client.findCellsOnChain(...findCellsArgs) : client.findCells(...findCellsArgs)) { if (!this.isCapacity(cell) || !cell.cellOutput.lock.eq(lock)) { continue; } yield { cell, ckbValue: cell.cellOutput.capacity, udtValue: 0n, [isCapacitySymbol]: true, }; } } } } export const defaultFindCellsLimit = 400; //# sourceMappingURL=capacity.js.map