Skip to content

Commit

Permalink
feat: supported minimalScriptCapacity (ckb-js#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets authored Sep 2, 2022
1 parent d5e7636 commit 3c70844
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/common-scripts/src/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ function _addDaoCellDep(
});
}

function extractDaoDataCompatible(dao: PackedDao): {
export function extractDaoDataCompatible(dao: PackedDao): {
[key: string]: BI;
} {
if (!/^(0x)?([0-9a-fA-F]){64}$/.test(dao)) {
Expand Down
25 changes: 25 additions & 0 deletions packages/helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ export interface Options {

const BECH32_LIMIT = 1023;

export function minimalScriptCapacity(
script: Script,
{ validate = true }: { validate?: boolean } = {}
): bigint {
const result = minimalScriptCapacityCompatible(script, { validate });
return BigInt(result.toString());
}

export function minimalScriptCapacityCompatible(
script: Script,
{ validate = true }: { validate?: boolean } = {}
): BI {
if (validate) {
validators.ValidateScript(script);
}

let bytes = 0;
bytes += bytify(script.codeHash).length;
bytes += bytify(script.args).length;
// hash_type field
bytes += 1;

return BI.from(bytes).mul(100000000);
}

export function minimalCellCapacity(
fullCell: Cell,
{ validate = true }: { validate?: boolean } = {}
Expand Down
24 changes: 23 additions & 1 deletion packages/helpers/tests/minimal_cell_capacity-bigint.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "ava";
import { Cell } from "@ckb-lumos/base";
import { minimalCellCapacity } from "../src";
import { minimalCellCapacity, minimalScriptCapacity } from "../src";

const normalCell: Cell = {
cellOutput: {
Expand Down Expand Up @@ -54,3 +54,25 @@ test("BigInt:cell with type and data, validate true", (t) => {

t.true(capacity === expectedCapacity);
});

test("BigInt:no args script capacity", (t) => {
const capacity = minimalScriptCapacity({
args: "0x",
codeHash:
"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
hashType: "type",
});
const expectedCapacity = BigInt(33 * 10 ** 8);
t.true(capacity === expectedCapacity);
});

test("BigInt:normal script, validate true", (t) => {
const capacity = minimalScriptCapacity({
args: "0x36c329ed630d6ce750712a477543672adab57f4c",
codeHash:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
});
const expectedCapacity = BigInt(53 * 10 ** 8);
t.true(capacity === expectedCapacity);
});
39 changes: 38 additions & 1 deletion packages/helpers/tests/minimal_cell_capacity.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import test from "ava";
import { Cell } from "@ckb-lumos/base";
import { minimalCellCapacity, minimalCellCapacityCompatible } from "../src";
import {
minimalCellCapacity,
minimalCellCapacityCompatible,
minimalScriptCapacityCompatible,
} from "../src";
import { BI } from "@ckb-lumos/bi";

const normalCell: Cell = {
Expand Down Expand Up @@ -83,3 +87,36 @@ test("cell with type and data, validate true", (t) => {
const expectedCapacity = BI.from(61 + 33 + 2).mul(BI.from(10).pow(8));
t.true(capacity.toString() === expectedCapacity.toString());
});

test("no args script capacity", (t) => {
const capacity = minimalScriptCapacityCompatible({
args: "0x",
codeHash:
"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
hashType: "type",
});
const expectedCapacity = BI.from(33).mul(BI.from(10).pow(8));
t.true(capacity.toString() === expectedCapacity.toString());
});

test("normal script, validate true", (t) => {
const capacity = minimalScriptCapacityCompatible({
args: "0x36c329ed630d6ce750712a477543672adab57f4c",
codeHash:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
});
const expectedCapacity = BI.from(53).mul(BI.from(10).pow(8));
t.true(capacity.toString() === expectedCapacity.toString());
});

test("invalid script, validate failed", (t) => {
t.throws(() => {
minimalScriptCapacityCompatible({
args: "0x36c329ed630d6ce750712a477543672adab57f4c",
codeHash:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "invalid",
} as any);
});
});

0 comments on commit 3c70844

Please sign in to comment.