UNPKG

@ickb/core

Version:

iCKB Core utils built on top of CCC

94 lines 3.67 kB
import { ccc } from "@ckb-ccc/core"; import { ReceiptData } from "./entities.js"; import { UdtManager, } from "@ickb/utils"; export class IckbUdtManager extends UdtManager { constructor(script, cellDeps, logicScript, daoManager) { super(script, cellDeps, "iCKB", "iCKB", 8); Object.defineProperty(this, "logicScript", { enumerable: true, configurable: true, writable: true, value: logicScript }); Object.defineProperty(this, "daoManager", { enumerable: true, configurable: true, writable: true, value: daoManager }); } static calculateScript(udt, ickbLogic) { const { codeHash, hashType } = udt; return new ccc.Script(codeHash, hashType, [ickbLogic.hash(), "00000080"].join("")); } async getInputsUdtBalance(client, tx) { return ccc.reduceAsync(tx.inputs, async (acc, input) => { await input.completeExtraInfos(client); const { previousOutput: outPoint, cellOutput, outputData } = input; if (!cellOutput || !outputData) { throw new Error("Unable to complete input"); } const { type, lock } = cellOutput; if (!type) { return acc; } const cell = new ccc.Cell(outPoint, cellOutput, outputData); const [udtValue, capacity] = acc; if (this.isUdt(cell)) { return [ udtValue + ccc.udtBalanceFrom(outputData), capacity + cellOutput.capacity, ]; } if (this.logicScript.eq(type)) { const header = await tx.getHeader(client, { type: "txHash", value: outPoint.txHash, }); const { depositQuantity, depositAmount } = ReceiptData.decode(outputData); return [ udtValue + ickbValue(depositAmount, header) * depositQuantity, capacity + cellOutput.capacity, ]; } if (this.logicScript.eq(lock) && this.daoManager.isDeposit(cell)) { const header = await tx.getHeader(client, { type: "txHash", value: outPoint.txHash, }); return [ udtValue - ickbValue(cell.capacityFree, header), capacity + cellOutput.capacity, ]; } return acc; }, [0n, 0n]); } } export function ickbValue(ckbUnoccupiedCapacity, header) { let ickbAmount = convert(true, ckbUnoccupiedCapacity, header, false); if (ICKB_DEPOSIT_CAP < ickbAmount) { ickbAmount -= (ickbAmount - ICKB_DEPOSIT_CAP) / 10n; } return ickbAmount; } export const ICKB_DEPOSIT_CAP = ccc.fixedPointFrom(100000); export function convert(isCkb2Udt, amount, rate, accountDepositCapacity = true) { if ("dao" in rate) { rate = ickbExchangeRatio(rate, accountDepositCapacity); } return isCkb2Udt ? (amount * rate.ckbScale) / rate.udtScale : (amount * rate.udtScale) / rate.ckbScale; } export function ickbExchangeRatio(header, accountDepositCapacity = true) { const AR_m = header.dao.ar; return { ckbScale: AR_0, udtScale: accountDepositCapacity ? AR_m + depositCapacityDelta : AR_m, }; } const AR_0 = 10000000000000000n; const depositUsedCapacity = ccc.fixedPointFrom(82); const depositCapacityDelta = (depositUsedCapacity * AR_0) / ICKB_DEPOSIT_CAP; //# sourceMappingURL=udt.js.map