Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sixty-ants-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ckb-ccc/core": major
---

feat(core)!: @codec implements a default from

70 changes: 44 additions & 26 deletions packages/core/src/ckb/epoch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { ClientBlockHeader } from "../client/clientTypes.js";
import { codec, codecPadding, codecUint, Entity } from "../codec/index.js";
import {
codec,
codecPadding,
codecUint,
DecodedType,
Entity,
} from "../codec/index.js";
import { Zero } from "../fixedPoint/index.js";
import { type Hex, type HexLike } from "../hex/index.js";
import { mol } from "../molecule/index.js";
Expand Down Expand Up @@ -36,6 +42,13 @@ export type EpochLike =
| Num
| Hex;

const EpochCodec = mol.struct({
padding: codecPadding(1),
denominator: codecUint(2),
numerator: codecUint(2),
integer: codecUint(3),
});

/**
* Epoch
*
Expand All @@ -55,28 +68,29 @@ export type EpochLike =
* @remarks
* This class is primarily a thin value-object; operations return new Epoch instances.
*/
@codec(
mol.struct({
padding: codecPadding(1),
denominator: codecUint(2),
numerator: codecUint(2),
integer: codecUint(3),
}),
)
@codec(EpochCodec)
export class Epoch extends Entity.Base<EpochLike, Epoch>() {
public readonly integer: Num;
public readonly numerator: Num;
public readonly denominator: Num;

/**
* Construct a new Epoch instance.
*
* @param integer - Whole epoch units (Num/bigint)
* @param numerator - Fractional numerator (Num).
* @param denominator - Fractional denominator (Num).
*/
public constructor(
public readonly integer: Num,
public readonly numerator: Num,
public readonly denominator: Num,
) {
public constructor({
integer,
numerator,
denominator,
}: Omit<DecodedType<typeof EpochCodec>, "padding">) {
super();

this.integer = integer;
this.numerator = numerator;
this.denominator = denominator;
}

/**
Expand Down Expand Up @@ -229,11 +243,11 @@ export class Epoch extends Entity.Base<EpochLike, Epoch>() {
static fromNum(v: NumLike): Epoch {
const num = numFrom(v);

return new Epoch(
num & numFrom("0xffffff"),
(num >> numFrom(24)) & numFrom("0xffff"),
(num >> numFrom(40)) & numFrom("0xffff"),
);
return Epoch.from({
integer: num & numFrom("0xffffff"),
numerator: (num >> numFrom(24)) & numFrom("0xffff"),
denominator: (num >> numFrom(40)) & numFrom("0xffff"),
});
}

/**
Expand All @@ -250,21 +264,25 @@ export class Epoch extends Entity.Base<EpochLike, Epoch>() {
* @param e - Value convertible to Epoch
* @returns Epoch instance
*/
static override from(e: EpochLike): Epoch {
static from(e: EpochLike): Epoch {
if (e instanceof Epoch) {
return e;
}

if (Array.isArray(e)) {
return new Epoch(numFrom(e[0]), numFrom(e[1]), numFrom(e[2]));
return new Epoch({
integer: numFrom(e[0]),
numerator: numFrom(e[1]),
denominator: numFrom(e[2]),
});
}

if (typeof e === "object") {
return new Epoch(
numFrom(e.integer),
numFrom(e.numerator),
numFrom(e.denominator),
);
return new Epoch({
integer: numFrom(e.integer),
numerator: numFrom(e.numerator),
denominator: numFrom(e.denominator),
});
}

return Epoch.fromNum(e);
Expand Down
69 changes: 23 additions & 46 deletions packages/core/src/ckb/script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Bytes, BytesLike, bytesFrom } from "../bytes/index.js";
import type { Client } from "../client/index.js";
import { KnownScript } from "../client/knownScript.js";
import { Codec, Entity, codec } from "../codec/index.js";
import {
Codec,
DecodedType,
EncodableType,
Entity,
codec,
} from "../codec/index.js";
import { Hex, HexLike, hexFrom } from "../hex/index.js";
import { mol } from "../molecule/index.js";
import {
Expand Down Expand Up @@ -98,38 +104,37 @@ export function hashTypeFromBytes(bytes: BytesLike): HashType {
return NUM_TO_HASH_TYPE[bytesFrom(bytes)[0]];
}

const ScriptCodec = mol.table({
codeHash: mol.Byte32,
hashType: HashTypeCodec,
args: mol.Bytes,
});
/**
* @public
*/
export type ScriptLike = {
codeHash: BytesLike;
hashType: HashTypeLike;
args: BytesLike;
};
export type ScriptLike = EncodableType<typeof ScriptCodec>;
/**
* @public
*/
@codec(
mol.table({
codeHash: mol.Byte32,
hashType: HashTypeCodec,
args: mol.Bytes,
}),
)
@codec(ScriptCodec)
export class Script extends Entity.Base<ScriptLike, Script>() {
public codeHash: Hex;
public hashType: HashType;
public args: Hex;

/**
* Creates an instance of Script.
*
* @param codeHash - The code hash of the script.
* @param hashType - The hash type of the script.
* @param args - The arguments for the script.
*/
constructor(
public codeHash: Hex,
public hashType: HashType,
public args: Hex,
) {
constructor({ codeHash, hashType, args }: DecodedType<typeof ScriptCodec>) {
super();

this.codeHash = codeHash;
this.hashType = hashType;
this.args = args;
}

get occupiedSize(): number {
Expand Down Expand Up @@ -174,34 +179,6 @@ export class Script extends Entity.Base<ScriptLike, Script>() {
);
}

/**
* Creates a Script instance from a ScriptLike object.
*
* @param script - A ScriptLike object or an instance of Script.
* @returns A Script instance.
*
* @example
* ```typescript
* const script = Script.from({
* codeHash: "0x1234...",
* hashType: "type",
* args: "0xabcd..."
* });
* ```
*/

static from(script: ScriptLike): Script {
if (script instanceof Script) {
return script;
}

return new Script(
hexFrom(script.codeHash),
hashTypeFrom(script.hashType),
hexFrom(script.args),
);
}

/**
* Creates a Script instance from client and known script.
*
Expand Down
Loading