@ickb/core
Version:
iCKB Core utils built on top of CCC
119 lines • 4.3 kB
JavaScript
import { ccc } from "@ckb-ccc/core";
import { defaultFindCellsLimit, unique, } from "@ickb/utils";
import { daoCellFrom, DaoManager } from "@ickb/dao";
import { OwnerData } from "./entities.js";
import { OwnerCell, WithdrawalGroup } from "./cells.js";
export class OwnedOwnerManager {
constructor(script, cellDeps, daoManager, udtHandler) {
Object.defineProperty(this, "script", {
enumerable: true,
configurable: true,
writable: true,
value: script
});
Object.defineProperty(this, "cellDeps", {
enumerable: true,
configurable: true,
writable: true,
value: cellDeps
});
Object.defineProperty(this, "daoManager", {
enumerable: true,
configurable: true,
writable: true,
value: daoManager
});
Object.defineProperty(this, "udtHandler", {
enumerable: true,
configurable: true,
writable: true,
value: udtHandler
});
}
isOwner(cell) {
return (Boolean(cell.cellOutput.type?.eq(this.script)) &&
cell.outputData.length >= 10);
}
isOwned(cell) {
return (this.daoManager.isWithdrawalRequest(cell) &&
cell.cellOutput.lock.eq(this.script));
}
requestWithdrawal(tx, deposits, lock, options) {
const isReadyOnly = options?.isReadyOnly ?? false;
if (isReadyOnly) {
deposits = deposits.filter((d) => d.isReady);
}
if (deposits.length === 0) {
return;
}
options = { ...options, isReadyOnly: false };
this.daoManager.requestWithdrawal(tx, deposits, this.script, options);
tx.addCellDeps(this.cellDeps);
tx.addUdtHandlers(this.udtHandler);
const outputData = OwnerData.encode({ ownedDistance: -deposits.length });
for (const _ of deposits) {
tx.addOutput({
lock: lock,
type: this.script,
}, outputData);
}
if (tx.outputs.length > 64) {
throw Error("More than 64 output cells in a NervosDAO transaction");
}
}
withdraw(tx, withdrawalGroups, options) {
const isReadyOnly = options?.isReadyOnly ?? false;
if (isReadyOnly) {
withdrawalGroups = withdrawalGroups.filter((g) => g.owned.isReady);
}
if (withdrawalGroups.length === 0) {
return;
}
options = { ...options, isReadyOnly: false };
tx.addCellDeps(this.cellDeps);
tx.addUdtHandlers(this.udtHandler);
const requests = withdrawalGroups.map((g) => g.owned);
this.daoManager.withdraw(tx, requests);
for (const { owner } of withdrawalGroups) {
tx.addInput(owner.cell);
}
if (tx.outputs.length > 64) {
throw Error("More than 64 output cells in a NervosDAO transaction");
}
}
async *findWithdrawalGroups(client, locks, options) {
const tip = options?.tip ?? (await client.getTipHeader());
const limit = options?.limit ?? defaultFindCellsLimit;
for (const lock of unique(locks)) {
const findCellsArgs = [
{
script: lock,
scriptType: "lock",
filter: {
script: this.script,
},
scriptSearchMode: "exact",
withData: true,
},
"asc",
limit,
];
for await (const cell of options?.onChain
? client.findCellsOnChain(...findCellsArgs)
: client.findCells(...findCellsArgs)) {
if (!this.isOwner(cell) || !cell.cellOutput.lock.eq(lock)) {
continue;
}
const owner = new OwnerCell(cell);
const owned = await daoCellFrom({
outpoint: owner.getOwned(),
isDeposit: false,
client,
tip,
});
yield new WithdrawalGroup(owned, owner);
}
}
}
}
//# sourceMappingURL=owned_owner.js.map