@ickb/dao
Version:
NervosDAO utilities built on top of CCC
68 lines • 2.26 kB
JavaScript
import { ccc, mol } from "@ckb-ccc/core";
import { Epoch, getHeader, } from "@ickb/utils";
export async function daoCellFrom(options) {
const isDeposit = options.isDeposit;
const cell = "cell" in options
? options.cell
: await options.client.getCell(options.outpoint);
if (!cell) {
throw Error("Cell not found");
}
const tip = options.tip;
const txHash = cell.outPoint.txHash;
const oldest = "headers" in options
? options.headers[0]
: !isDeposit
? {
header: await getHeader(options.client, {
type: "number",
value: mol.Uint64LE.decode(cell.outputData),
}),
}
: {
header: await getHeader(options.client, {
type: "txHash",
value: txHash,
}),
txHash,
};
const newest = "headers" in options
? options.headers[1]
: !isDeposit
? {
header: await getHeader(options.client, {
type: "txHash",
value: txHash,
}),
txHash,
}
: { header: tip };
const interests = ccc.calcDaoProfit(cell.capacityFree, oldest.header, newest.header);
let maturity = Epoch.from(ccc.calcDaoClaimEpoch(oldest.header, newest.header));
const minLockUp = options.minLockUp ?? defaultMinLockUp;
const maxLockUp = options.maxLockUp ?? defaultMaxLockUp;
let isReady = isDeposit
? maturity.compare(minLockUp.add(tip.epoch)) > 0
: maturity.compare(tip.epoch) > 0;
if (isDeposit) {
if (!isReady) {
maturity = maturity.add([180n, 0n, 1n]);
}
isReady = maxLockUp.add(tip.epoch).compare(maturity) > 0;
}
const ckbValue = cell.cellOutput.capacity + interests;
const udtValue = 0n;
return {
cell,
isDeposit,
headers: [oldest, newest],
interests,
maturity,
isReady,
ckbValue,
udtValue,
};
}
const defaultMinLockUp = Epoch.from([0n, 1n, 24n]);
const defaultMaxLockUp = Epoch.from([18n, 0n, 1n]);
//# sourceMappingURL=cells.js.map