@ickb/sdk 1000.0.82 → 9001.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -32
- package/dist/constants.d.ts +9 -22
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +62 -76
- package/dist/constants.js.map +1 -1
- package/dist/conversion/estimate.d.ts +33 -0
- package/dist/conversion/estimate.d.ts.map +1 -0
- package/dist/conversion/estimate.js +117 -0
- package/dist/conversion/estimate.js.map +1 -0
- package/dist/conversion/maturity.d.ts +15 -0
- package/dist/conversion/maturity.d.ts.map +1 -0
- package/dist/conversion/maturity.js +113 -0
- package/dist/conversion/maturity.js.map +1 -0
- package/dist/conversion/plans.d.ts +20 -0
- package/dist/conversion/plans.d.ts.map +1 -0
- package/dist/conversion/plans.js +106 -0
- package/dist/conversion/plans.js.map +1 -0
- package/dist/conversion/projection.d.ts +21 -0
- package/dist/conversion/projection.d.ts.map +1 -0
- package/dist/conversion/projection.js +94 -0
- package/dist/conversion/projection.js.map +1 -0
- package/dist/conversion/types.d.ts +264 -0
- package/dist/conversion/types.d.ts.map +1 -0
- package/dist/conversion/types.js +2 -0
- package/dist/conversion/types.js.map +1 -0
- package/dist/conversion/withdrawal_ring.d.ts +34 -0
- package/dist/conversion/withdrawal_ring.d.ts.map +1 -0
- package/dist/conversion/withdrawal_ring.js +87 -0
- package/dist/conversion/withdrawal_ring.js.map +1 -0
- package/dist/dao.d.ts +100 -0
- package/dist/dao.d.ts.map +1 -0
- package/dist/dao.js +125 -0
- package/dist/dao.js.map +1 -0
- package/dist/index.d.ts +49 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -3
- package/dist/index.js.map +1 -1
- package/dist/logic.d.ts +81 -0
- package/dist/logic.d.ts.map +1 -0
- package/dist/logic.js +173 -0
- package/dist/logic.js.map +1 -0
- package/dist/order/cells.d.ts +168 -0
- package/dist/order/cells.d.ts.map +1 -0
- package/dist/order/cells.js +309 -0
- package/dist/order/cells.js.map +1 -0
- package/dist/order/conversion.d.ts +37 -0
- package/dist/order/conversion.d.ts.map +1 -0
- package/dist/order/conversion.js +120 -0
- package/dist/order/conversion.js.map +1 -0
- package/dist/order/fee.d.ts +9 -0
- package/dist/order/fee.d.ts.map +1 -0
- package/dist/order/fee.js +24 -0
- package/dist/order/fee.js.map +1 -0
- package/dist/order/fill.d.ts +36 -0
- package/dist/order/fill.d.ts.map +1 -0
- package/dist/order/fill.js +53 -0
- package/dist/order/fill.js.map +1 -0
- package/dist/order/info.d.ts +62 -0
- package/dist/order/info.d.ts.map +1 -0
- package/dist/order/info.js +102 -0
- package/dist/order/info.js.map +1 -0
- package/dist/order/master.d.ts +49 -0
- package/dist/order/master.d.ts.map +1 -0
- package/dist/order/master.js +27 -0
- package/dist/order/master.js.map +1 -0
- package/dist/order/matcher.d.ts +43 -0
- package/dist/order/matcher.d.ts.map +1 -0
- package/dist/order/matcher.js +124 -0
- package/dist/order/matcher.js.map +1 -0
- package/dist/order/order.d.ts +63 -0
- package/dist/order/order.d.ts.map +1 -0
- package/dist/order/order.js +234 -0
- package/dist/order/order.js.map +1 -0
- package/dist/order/order_data.d.ts +51 -0
- package/dist/order/order_data.d.ts.map +1 -0
- package/dist/order/order_data.js +67 -0
- package/dist/order/order_data.js.map +1 -0
- package/dist/order/ratio.d.ts +40 -0
- package/dist/order/ratio.d.ts.map +1 -0
- package/dist/order/ratio.js +74 -0
- package/dist/order/ratio.js.map +1 -0
- package/dist/order/relative.d.ts +43 -0
- package/dist/order/relative.d.ts.map +1 -0
- package/dist/order/relative.js +57 -0
- package/dist/order/relative.js.map +1 -0
- package/dist/owned_owner.d.ts +108 -0
- package/dist/owned_owner.d.ts.map +1 -0
- package/dist/owned_owner.js +260 -0
- package/dist/owned_owner.js.map +1 -0
- package/dist/sdk.d.ts +113 -41
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +410 -209
- package/dist/sdk.js.map +1 -1
- package/dist/send/sign_and_send_transaction.d.ts +61 -0
- package/dist/send/sign_and_send_transaction.d.ts.map +1 -0
- package/dist/send/sign_and_send_transaction.js +201 -0
- package/dist/send/sign_and_send_transaction.js.map +1 -0
- package/dist/send/wait_transaction.d.ts +42 -0
- package/dist/send/wait_transaction.d.ts.map +1 -0
- package/dist/send/wait_transaction.js +172 -0
- package/dist/send/wait_transaction.js.map +1 -0
- package/dist/udt.d.ts +108 -0
- package/dist/udt.d.ts.map +1 -0
- package/dist/udt.js +180 -0
- package/dist/udt.js.map +1 -0
- package/dist/utils/chain.d.ts +3 -0
- package/dist/utils/chain.d.ts.map +1 -0
- package/dist/utils/chain.js +2 -0
- package/dist/utils/chain.js.map +1 -0
- package/dist/utils/codec.d.ts +19 -0
- package/dist/utils/codec.d.ts.map +1 -0
- package/dist/utils/codec.js +69 -0
- package/dist/utils/codec.js.map +1 -0
- package/dist/utils/utils.d.ts +99 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +130 -0
- package/dist/utils/utils.js.map +1 -0
- package/dist/utils/verified_client.d.ts +19 -0
- package/dist/utils/verified_client.d.ts.map +1 -0
- package/dist/utils/verified_client.js +92 -0
- package/dist/utils/verified_client.js.map +1 -0
- package/package.json +19 -26
- package/src/constants.ts +65 -181
- package/src/conversion/estimate.ts +155 -0
- package/src/conversion/maturity.ts +158 -0
- package/src/conversion/plans.ts +144 -0
- package/src/conversion/projection.ts +136 -0
- package/src/conversion/types.ts +302 -0
- package/src/conversion/withdrawal_ring.ts +122 -0
- package/src/dao.ts +196 -0
- package/src/index.ts +89 -3
- package/src/logic.ts +283 -0
- package/src/order/cells.ts +383 -0
- package/src/order/conversion.ts +162 -0
- package/src/order/fee.ts +30 -0
- package/src/order/fill.ts +83 -0
- package/src/order/info.ts +181 -0
- package/src/order/master.ts +42 -0
- package/src/order/matcher.ts +191 -0
- package/src/order/order.ts +288 -0
- package/src/order/order_data.ts +131 -0
- package/src/order/ratio.ts +127 -0
- package/src/order/relative.ts +109 -0
- package/src/owned_owner.ts +364 -0
- package/src/sdk.ts +542 -440
- package/src/send/sign_and_send_transaction.ts +256 -0
- package/src/send/wait_transaction.ts +227 -0
- package/src/udt.ts +249 -0
- package/src/utils/chain.ts +2 -0
- package/src/utils/codec.ts +78 -0
- package/src/utils/utils.ts +208 -0
- package/src/utils/verified_client.ts +136 -0
- package/dist/codec.d.ts +0 -3
- package/dist/codec.d.ts.map +0 -1
- package/dist/codec.js +0 -58
- package/dist/codec.js.map +0 -1
- package/src/codec.ts +0 -138
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { ccc, mol } from "@ckb-ccc/core";
|
|
2
|
+
import { CheckedUint128LE } from "../utils/codec.ts";
|
|
3
|
+
import { Info, type InfoLike } from "./info.ts";
|
|
4
|
+
import {
|
|
5
|
+
MasterCodec,
|
|
6
|
+
masterFrom,
|
|
7
|
+
masterValidate,
|
|
8
|
+
type Master,
|
|
9
|
+
type MasterLike,
|
|
10
|
+
} from "./master.ts";
|
|
11
|
+
|
|
12
|
+
const maxUint128 = (1n << 128n) - 1n;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Wire shape for order cell data.
|
|
16
|
+
*/
|
|
17
|
+
export interface OrderDataLike {
|
|
18
|
+
/** UDT amount held by the order cell. */
|
|
19
|
+
udtValue: ccc.FixedPointLike;
|
|
20
|
+
/** Relative or absolute pointer to the master cell. */
|
|
21
|
+
master: MasterLike;
|
|
22
|
+
/** Price and minimum-match metadata. */
|
|
23
|
+
info: InfoLike;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const OrderDataCodec = mol.struct({
|
|
27
|
+
udtValue: CheckedUint128LE,
|
|
28
|
+
master: MasterCodec,
|
|
29
|
+
info: Info,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const OrderBase = ccc.Entity.Base<OrderDataLike, OrderData>();
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Serialized order cell payload.
|
|
36
|
+
*/
|
|
37
|
+
export interface OrderData {
|
|
38
|
+
/** UDT amount held by the order cell. */
|
|
39
|
+
udtValue: ccc.FixedPoint;
|
|
40
|
+
/** Master-cell pointer. */
|
|
41
|
+
master: Master;
|
|
42
|
+
/** Price and minimum-match metadata. */
|
|
43
|
+
info: Info;
|
|
44
|
+
/** Creates a copy of this order payload. */
|
|
45
|
+
clone(): OrderData;
|
|
46
|
+
/** Returns whether another value has the same order payload. */
|
|
47
|
+
eq(other: OrderDataLike): boolean;
|
|
48
|
+
/** Resolves a relative master against `current`, or returns the absolute master. */
|
|
49
|
+
getMaster(current: ccc.OutPoint): ccc.OutPoint;
|
|
50
|
+
/** Returns the CKB hash of the serialized order payload. */
|
|
51
|
+
hash(): ccc.Hex;
|
|
52
|
+
/** Returns whether the master pointer is relative to the current output. */
|
|
53
|
+
isMint(): boolean;
|
|
54
|
+
/** Serializes the order payload to bytes. */
|
|
55
|
+
toBytes(): ccc.Bytes;
|
|
56
|
+
/** Serializes the order payload to full-width hexadecimal. */
|
|
57
|
+
toHex(): ccc.Hex;
|
|
58
|
+
/** Throws if the UDT value, master pointer, or info is invalid. */
|
|
59
|
+
validate(): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// An interface, this class expression and a typed const rather than one plain class: a
|
|
63
|
+
// class extending `ccc.Entity.Base<...>()` emits an unexported `*_base` declaration, which
|
|
64
|
+
// API Extractor's ae-forgotten-export rejects (tried 2026-10-30).
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow -- Preserve the runtime constructor name.
|
|
66
|
+
const OrderDataImplementation = class OrderData extends OrderBase {
|
|
67
|
+
static {
|
|
68
|
+
ccc.codec(OrderDataCodec)(this);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** UDT amount held by the order cell. */
|
|
72
|
+
public readonly udtValue: ccc.FixedPoint;
|
|
73
|
+
/** Master-cell pointer. */
|
|
74
|
+
public readonly master: Master;
|
|
75
|
+
/** Price and minimum-match metadata. */
|
|
76
|
+
public readonly info: Info;
|
|
77
|
+
|
|
78
|
+
/** Creates normalized order data. */
|
|
79
|
+
constructor(udtValue: ccc.FixedPoint, master: Master, info: Info) {
|
|
80
|
+
super();
|
|
81
|
+
this.udtValue = udtValue;
|
|
82
|
+
this.master = master;
|
|
83
|
+
this.info = info;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Normalizes an `OrderDataLike` wire object or existing entity into `OrderData`. */
|
|
87
|
+
public static override from(data: OrderDataLike): OrderData {
|
|
88
|
+
if (data instanceof OrderData) {
|
|
89
|
+
return data;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const { udtValue, master, info } = data;
|
|
93
|
+
return new OrderData(ccc.numFrom(udtValue), masterFrom(master), Info.from(info));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Throws when the order payload is not internally valid. */
|
|
97
|
+
public validate(): void {
|
|
98
|
+
if (this.udtValue < 0n) {
|
|
99
|
+
throw new Error("udtValue invalid, negative");
|
|
100
|
+
}
|
|
101
|
+
if (this.udtValue > maxUint128) {
|
|
102
|
+
throw new Error("udtValue exceeds Uint128");
|
|
103
|
+
}
|
|
104
|
+
masterValidate(this.master);
|
|
105
|
+
this.info.validate();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Returns true when the master pointer is relative to the current output. */
|
|
109
|
+
public isMint(): boolean {
|
|
110
|
+
return this.master.type === "relative";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Resolves the master out point relative to the current order out point. */
|
|
114
|
+
public getMaster(current: ccc.OutPoint): ccc.OutPoint {
|
|
115
|
+
const { type, value } = this.master;
|
|
116
|
+
return type === "relative"
|
|
117
|
+
? new ccc.OutPoint(current.txHash, current.index + value.distance)
|
|
118
|
+
: value;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/** CCC-backed order-data constructor and codec. */
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare -- The public type and runtime constructor intentionally share a name.
|
|
124
|
+
export const OrderData: {
|
|
125
|
+
byteLength?: number;
|
|
126
|
+
new (udtValue: ccc.FixedPoint, master: Master, info: Info): OrderData;
|
|
127
|
+
decode: (encoded: ccc.BytesLike) => OrderData;
|
|
128
|
+
encode: (data: OrderDataLike) => ccc.Bytes;
|
|
129
|
+
from: (data: OrderDataLike) => OrderData;
|
|
130
|
+
fromBytes: (encoded: ccc.BytesLike) => OrderData;
|
|
131
|
+
} = OrderDataImplementation;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { ccc, mol } from "@ckb-ccc/core";
|
|
2
|
+
import { CheckedUint64LE } from "../utils/codec.ts";
|
|
3
|
+
import { compareBigInt, type ExchangeRatio } from "../utils/utils.ts";
|
|
4
|
+
|
|
5
|
+
const maxUint64 = (1n << 64n) - 1n;
|
|
6
|
+
|
|
7
|
+
const RatioCodec = mol.struct({
|
|
8
|
+
ckbScale: CheckedUint64LE,
|
|
9
|
+
udtScale: CheckedUint64LE,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const RatioBase = ccc.Entity.Base<ExchangeRatio, Ratio>();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Serialized exchange ratio used by order info.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* A ratio is either empty (`0, 0`) or populated with both scales greater than
|
|
19
|
+
* zero. Mixed empty/populated values are invalid.
|
|
20
|
+
*/
|
|
21
|
+
export interface Ratio extends ExchangeRatio {
|
|
22
|
+
/** Creates a copy of this ratio. */
|
|
23
|
+
clone(): Ratio;
|
|
24
|
+
/** Compares effective CKB-to-UDT prices, returning their numeric ordering. */
|
|
25
|
+
compare(other: Ratio): number;
|
|
26
|
+
/** Returns whether another value has the same scales. */
|
|
27
|
+
eq(other: ExchangeRatio): boolean;
|
|
28
|
+
/** Returns the CKB hash of the serialized ratio. */
|
|
29
|
+
hash(): ccc.Hex;
|
|
30
|
+
/** Returns whether both scales are zero. */
|
|
31
|
+
isEmpty(): boolean;
|
|
32
|
+
/** Returns whether both scales are positive. */
|
|
33
|
+
isPopulated(): boolean;
|
|
34
|
+
/** Serializes the ratio to bytes. */
|
|
35
|
+
toBytes(): ccc.Bytes;
|
|
36
|
+
/** Serializes the ratio to full-width hexadecimal. */
|
|
37
|
+
toHex(): ccc.Hex;
|
|
38
|
+
/** Throws unless both scales fit Uint64 and are both zero or both positive. */
|
|
39
|
+
validate(): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// An interface, this class expression and a typed const rather than one plain class: a
|
|
43
|
+
// class extending `ccc.Entity.Base<...>()` emits an unexported `*_base` declaration, which
|
|
44
|
+
// API Extractor's ae-forgotten-export rejects (tried 2026-10-30).
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow -- Preserve the runtime constructor name.
|
|
46
|
+
const RatioImplementation = class Ratio extends RatioBase {
|
|
47
|
+
static {
|
|
48
|
+
ccc.codec(RatioCodec)(this);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** CKB-side scale. */
|
|
52
|
+
public readonly ckbScale: ccc.Num;
|
|
53
|
+
/** UDT-side scale. */
|
|
54
|
+
public readonly udtScale: ccc.Num;
|
|
55
|
+
|
|
56
|
+
/** Creates a ratio from raw scales. */
|
|
57
|
+
constructor(ckbScale: ccc.Num, udtScale: ccc.Num) {
|
|
58
|
+
super();
|
|
59
|
+
this.ckbScale = ckbScale;
|
|
60
|
+
this.udtScale = udtScale;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Normalizes an exchange ratio wire object or existing entity into `Ratio`. */
|
|
64
|
+
public static override from(ratio: ExchangeRatio): Ratio {
|
|
65
|
+
if (ratio instanceof Ratio) {
|
|
66
|
+
return ratio;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const { ckbScale, udtScale } = ratio;
|
|
70
|
+
return new Ratio(ckbScale, udtScale);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Throws when the ratio is neither empty nor fully populated. */
|
|
74
|
+
public validate(): void {
|
|
75
|
+
if (
|
|
76
|
+
this.ckbScale < 0n ||
|
|
77
|
+
this.ckbScale > maxUint64 ||
|
|
78
|
+
this.udtScale < 0n ||
|
|
79
|
+
this.udtScale > maxUint64
|
|
80
|
+
) {
|
|
81
|
+
throw new Error("Ratio scale exceeds Uint64");
|
|
82
|
+
}
|
|
83
|
+
if (!this.isEmpty() && !this.isPopulated()) {
|
|
84
|
+
throw new Error("Ratio invalid: not empty, not populated");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Returns true for the sentinel empty ratio. */
|
|
89
|
+
public isEmpty(): boolean {
|
|
90
|
+
return this.ckbScale === 0n && this.udtScale === 0n;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Returns true when both scales are positive. */
|
|
94
|
+
public isPopulated(): boolean {
|
|
95
|
+
return this.ckbScale > 0n && this.udtScale > 0n;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Creates the sentinel empty ratio. */
|
|
99
|
+
public static empty(): Ratio {
|
|
100
|
+
return new Ratio(0n, 0n);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Compares two populated ratios by effective CKB-to-UDT price. */
|
|
104
|
+
public compare(other: Ratio): number {
|
|
105
|
+
if (this.udtScale === other.udtScale) {
|
|
106
|
+
return compareBigInt(this.ckbScale, other.ckbScale);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (this.ckbScale === other.ckbScale) {
|
|
110
|
+
return compareBigInt(other.udtScale, this.udtScale);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return compareBigInt(this.ckbScale * other.udtScale, other.ckbScale * this.udtScale);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** CCC-backed ratio constructor and codec. */
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare -- The public type and runtime constructor intentionally share a name.
|
|
119
|
+
export const Ratio: {
|
|
120
|
+
byteLength?: number;
|
|
121
|
+
new (ckbScale: ccc.Num, udtScale: ccc.Num): Ratio;
|
|
122
|
+
decode: (encoded: ccc.BytesLike) => Ratio;
|
|
123
|
+
empty: () => Ratio;
|
|
124
|
+
encode: (ratio: ExchangeRatio) => ccc.Bytes;
|
|
125
|
+
from: (ratio: ExchangeRatio) => Ratio;
|
|
126
|
+
fromBytes: (encoded: ccc.BytesLike) => Ratio;
|
|
127
|
+
} = RatioImplementation;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { ccc, mol } from "@ckb-ccc/core";
|
|
2
|
+
import { CheckedInt32LE } from "../utils/codec.ts";
|
|
3
|
+
|
|
4
|
+
const minInt32 = -(1n << 31n);
|
|
5
|
+
const maxInt32 = (1n << 31n) - 1n;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wire shape for a relative master pointer.
|
|
9
|
+
*/
|
|
10
|
+
export interface RelativeLike {
|
|
11
|
+
/** Must be the 32-byte zero padding used by the standard encoding. */
|
|
12
|
+
padding: ccc.BytesLike;
|
|
13
|
+
/** Signed output-index distance from the current order to its master. */
|
|
14
|
+
distance: ccc.NumLike;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const RelativeCodec = mol.struct({
|
|
18
|
+
padding: mol.Byte32,
|
|
19
|
+
distance: CheckedInt32LE,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const RelativeBase = ccc.Entity.Base<RelativeLike, Relative>();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Relative pointer from an order output to its master output.
|
|
26
|
+
*/
|
|
27
|
+
export interface Relative {
|
|
28
|
+
/** Standard zero padding. */
|
|
29
|
+
padding: ccc.Bytes;
|
|
30
|
+
/** Signed output-index distance to the master output. */
|
|
31
|
+
distance: ccc.Num;
|
|
32
|
+
/** Creates a copy of this relative pointer. */
|
|
33
|
+
clone(): Relative;
|
|
34
|
+
/** Returns whether another value has the same padding and distance. */
|
|
35
|
+
eq(other: RelativeLike): boolean;
|
|
36
|
+
/** Returns the CKB hash of the serialized relative pointer. */
|
|
37
|
+
hash(): ccc.Hex;
|
|
38
|
+
/** Serializes the relative pointer to bytes. */
|
|
39
|
+
toBytes(): ccc.Bytes;
|
|
40
|
+
/** Serializes the relative pointer to full-width hexadecimal. */
|
|
41
|
+
toHex(): ccc.Hex;
|
|
42
|
+
/** Throws unless padding is canonical and distance fits Int32. */
|
|
43
|
+
validate(): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// An interface, this class expression and a typed const rather than one plain class: a
|
|
47
|
+
// class extending `ccc.Entity.Base<...>()` emits an unexported `*_base` declaration, which
|
|
48
|
+
// API Extractor's ae-forgotten-export rejects (tried 2026-10-30).
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow -- Preserve the runtime constructor name.
|
|
50
|
+
const RelativeImplementation = class Relative extends RelativeBase {
|
|
51
|
+
static {
|
|
52
|
+
ccc.codec(RelativeCodec)(this);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Standard zero padding. */
|
|
56
|
+
public readonly padding: ccc.Bytes;
|
|
57
|
+
/** Signed output-index distance to the master output. */
|
|
58
|
+
public readonly distance: ccc.Num;
|
|
59
|
+
|
|
60
|
+
/** Creates a normalized relative pointer. */
|
|
61
|
+
constructor(padding: ccc.Bytes, distance: ccc.Num) {
|
|
62
|
+
super();
|
|
63
|
+
this.padding = padding;
|
|
64
|
+
this.distance = distance;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Normalizes a relative pointer wire object or existing entity into `Relative`. */
|
|
68
|
+
public static override from(relative: RelativeLike): Relative {
|
|
69
|
+
if (relative instanceof Relative) {
|
|
70
|
+
return relative;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const { padding, distance } = relative;
|
|
74
|
+
return new Relative(ccc.bytesFrom(padding), ccc.numFrom(distance));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Creates a relative pointer with standard zero padding. */
|
|
78
|
+
public static create(distance: ccc.Num): Relative {
|
|
79
|
+
return new Relative(Relative.padding(), distance);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Returns the standard 32-byte zero padding. */
|
|
83
|
+
public static padding(): ccc.Bytes {
|
|
84
|
+
return new Uint8Array(32);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Throws when padding is not the standard 32-byte zero value. */
|
|
88
|
+
public validate(): void {
|
|
89
|
+
if (this.padding.length !== 32 || this.padding.some((x) => x !== 0)) {
|
|
90
|
+
throw new Error("Relative master invalid, non standard padding");
|
|
91
|
+
}
|
|
92
|
+
if (this.distance < minInt32 || this.distance > maxInt32) {
|
|
93
|
+
throw new Error("Relative master distance exceeds Int32");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/** CCC-backed relative-pointer constructor and codec. */
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare -- The public type and runtime constructor intentionally share a name.
|
|
100
|
+
export const Relative: {
|
|
101
|
+
byteLength?: number;
|
|
102
|
+
new (padding: ccc.Bytes, distance: ccc.Num): Relative;
|
|
103
|
+
create: (distance: ccc.Num) => Relative;
|
|
104
|
+
decode: (encoded: ccc.BytesLike) => Relative;
|
|
105
|
+
encode: (relative: RelativeLike) => ccc.Bytes;
|
|
106
|
+
from: (relative: RelativeLike) => Relative;
|
|
107
|
+
fromBytes: (encoded: ccc.BytesLike) => Relative;
|
|
108
|
+
padding: () => ccc.Bytes;
|
|
109
|
+
} = RelativeImplementation;
|