@ckb-ccc/udt 0.1.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.
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.udt = void 0;
30
+ __exportStar(require("./barrel.js"), exports);
31
+ exports.udt = __importStar(require("./barrel.js"));
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,108 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { ssri } from "@ckb-ccc/ssri";
3
+ /**
4
+ * Represents a User Defined Token (UDT) script compliant with the SSRI protocol.
5
+ *
6
+ * This class provides a comprehensive implementation for interacting with User Defined Tokens,
7
+ * supporting various token operations such as querying metadata, checking balances, and performing transfers.
8
+ * It supports both SSRI-compliant UDTs and legacy sUDT/xUDT standard tokens.
9
+ *
10
+ * @public
11
+ * @category Blockchain
12
+ * @category Token
13
+ */
14
+ export declare class Udt extends ssri.Trait {
15
+ readonly script: ccc.Script;
16
+ /**
17
+ * Constructs a new UDT (User Defined Token) script instance.
18
+ * By default it is a SSRI-compliant UDT. By providing `xudtType`, it is compatible with the legacy xUDT.
19
+ *
20
+ * @param executor - The SSRI executor instance.
21
+ * @param code - The script code cell of the UDT.
22
+ * @param script - The type script of the UDT.
23
+ * @example
24
+ * ```typescript
25
+ * const udt = new Udt(executor, code, script);
26
+ * ```
27
+ */
28
+ constructor(code: ccc.OutPointLike, script: ccc.ScriptLike, config?: {
29
+ executor?: ssri.Executor | null;
30
+ } | null);
31
+ /**
32
+ * Retrieves the human-readable name of the User Defined Token.
33
+ *
34
+ * @returns A promise resolving to the token's name.
35
+ */
36
+ name(context?: ssri.ContextScript): Promise<ssri.ExecutorResponse<string | undefined>>;
37
+ /**
38
+ * Retrieves the symbol of the UDT.
39
+ * @returns The symbol of the UDT.
40
+ */
41
+ symbol(context?: ssri.ContextScript): Promise<ssri.ExecutorResponse<string | undefined>>;
42
+ /**
43
+ * Retrieves the decimals of the UDT.
44
+ * @returns The decimals of the UDT.
45
+ */
46
+ decimals(context?: ssri.ContextScript): Promise<ssri.ExecutorResponse<ccc.Num | undefined>>;
47
+ /**
48
+ * Retrieves the icon of the UDT
49
+ * @returns The icon of the UDT.
50
+ */
51
+ icon(context?: ssri.ContextScript): Promise<ssri.ExecutorResponse<string | undefined>>;
52
+ /**
53
+ * Transfers UDT to specified addresses.
54
+ * @param tx - Transfer on the basis of an existing transaction to achieve combined actions. If not provided, a new transaction will be created.
55
+ * @param transfers - The array of transfers.
56
+ * @param transfers.to - The receiver of token.
57
+ * @param transfers.amount - The amount of token to the receiver.
58
+ * @returns The transaction result.
59
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction object.
60
+ * @example
61
+ * ```typescript
62
+ * const { script: change } = await signer.getRecommendedAddressObj();
63
+ * const { script: to } = await ccc.Address.fromString(receiver, signer.client);
64
+ *
65
+ * const udt = new Udt(
66
+ * {
67
+ * txHash: "0x4e2e832e0b1e7b5994681b621b00c1e65f577ee4b440ef95fa07db9bb3d50269",
68
+ * index: 0,
69
+ * },
70
+ * {
71
+ * codeHash: "0xcc9dc33ef234e14bc788c43a4848556a5fb16401a04662fc55db9bb201987037",
72
+ * hashType: "type",
73
+ * args: "0x71fd1985b2971a9903e4d8ed0d59e6710166985217ca0681437883837b86162f"
74
+ * },
75
+ * );
76
+ *
77
+ * const { res: tx } = await udtTrait.transfer(
78
+ * signer,
79
+ * [{ to, amount: 100 }],
80
+ * );
81
+ *
82
+ * const completedTx = udt.completeUdtBy(tx, signer);
83
+ * await completedTx.completeInputsByCapacity(signer);
84
+ * await completedTx.completeFeeBy(signer);
85
+ * const transferTxHash = await signer.sendTransaction(completedTx);
86
+ * ```
87
+ */
88
+ transfer(signer: ccc.Signer, transfers: {
89
+ to: ccc.ScriptLike;
90
+ amount: ccc.NumLike;
91
+ }[], tx?: ccc.TransactionLike | null): Promise<ssri.ExecutorResponse<ccc.Transaction>>;
92
+ /**
93
+ * Mints new tokens to specified addresses. See the example in `transfer` as they are similar.
94
+ * @param tx - Optional existing transaction to build upon
95
+ * @param mints - Array of mints
96
+ * @param mints.to - receiver of token
97
+ * @param mints.amount - amount to the receiver
98
+ * @returns The transaction containing the mint operation
99
+ * @tag Mutation - This method represents a mutation of the onchain state
100
+ */
101
+ mint(signer: ccc.Signer, mints: {
102
+ to: ccc.ScriptLike;
103
+ amount: ccc.NumLike;
104
+ }[], tx?: ccc.TransactionLike | null): Promise<ssri.ExecutorResponse<ccc.Transaction>>;
105
+ completeChangeToLock(txLike: ccc.TransactionLike, signer: ccc.Signer, change: ccc.ScriptLike): Promise<ccc.Transaction>;
106
+ completeBy(tx: ccc.TransactionLike, from: ccc.Signer): Promise<ccc.Transaction>;
107
+ }
108
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/udt/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAErC;;;;;;;;;;GAUG;AACH,qBAAa,GAAI,SAAQ,IAAI,CAAC,KAAK;IACjC,SAAgB,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;IAEnC;;;;;;;;;;;OAWG;gBAED,IAAI,EAAE,GAAG,CAAC,YAAY,EACtB,MAAM,EAAE,GAAG,CAAC,UAAU,EACtB,MAAM,CAAC,EAAE;QACP,QAAQ,CAAC,EAAE,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;KACjC,GAAG,IAAI;IAMV;;;;OAIG;IACG,IAAI,CACR,OAAO,CAAC,EAAE,IAAI,CAAC,aAAa,GAC3B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAcrD;;;OAGG;IACG,MAAM,CACV,OAAO,CAAC,EAAE,IAAI,CAAC,aAAa,GAC3B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAmBrD;;;OAGG;IACG,QAAQ,CACZ,OAAO,CAAC,EAAE,IAAI,CAAC,aAAa,GAC3B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,GAAG,SAAS,CAAC,CAAC;IAmBtD;;;OAGG;IACG,IAAI,CACR,OAAO,CAAC,EAAE,IAAI,CAAC,aAAa,GAC3B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAcrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACG,QAAQ,CACZ,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,SAAS,EAAE;QACT,EAAE,EAAE,GAAG,CAAC,UAAU,CAAC;QACnB,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC;KACrB,EAAE,EACH,EAAE,CAAC,EAAE,GAAG,CAAC,eAAe,GAAG,IAAI,GAC9B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IA2ClD;;;;;;;;OAQG;IACG,IAAI,CACR,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,KAAK,EAAE;QACL,EAAE,EAAE,GAAG,CAAC,UAAU,CAAC;QACnB,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC;KACrB,EAAE,EACH,EAAE,CAAC,EAAE,GAAG,CAAC,eAAe,GAAG,IAAI,GAC9B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IA2C5C,oBAAoB,CACxB,MAAM,EAAE,GAAG,CAAC,eAAe,EAC3B,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,MAAM,EAAE,GAAG,CAAC,UAAU;IAqBlB,UAAU,CAAC,EAAE,EAAE,GAAG,CAAC,eAAe,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM;CAK3D"}
@@ -0,0 +1,226 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Udt = void 0;
4
+ const core_1 = require("@ckb-ccc/core");
5
+ const ssri_1 = require("@ckb-ccc/ssri");
6
+ /**
7
+ * Represents a User Defined Token (UDT) script compliant with the SSRI protocol.
8
+ *
9
+ * This class provides a comprehensive implementation for interacting with User Defined Tokens,
10
+ * supporting various token operations such as querying metadata, checking balances, and performing transfers.
11
+ * It supports both SSRI-compliant UDTs and legacy sUDT/xUDT standard tokens.
12
+ *
13
+ * @public
14
+ * @category Blockchain
15
+ * @category Token
16
+ */
17
+ class Udt extends ssri_1.ssri.Trait {
18
+ /**
19
+ * Constructs a new UDT (User Defined Token) script instance.
20
+ * By default it is a SSRI-compliant UDT. By providing `xudtType`, it is compatible with the legacy xUDT.
21
+ *
22
+ * @param executor - The SSRI executor instance.
23
+ * @param code - The script code cell of the UDT.
24
+ * @param script - The type script of the UDT.
25
+ * @example
26
+ * ```typescript
27
+ * const udt = new Udt(executor, code, script);
28
+ * ```
29
+ */
30
+ constructor(code, script, config) {
31
+ super(code, config?.executor);
32
+ this.script = core_1.ccc.Script.from(script);
33
+ }
34
+ /**
35
+ * Retrieves the human-readable name of the User Defined Token.
36
+ *
37
+ * @returns A promise resolving to the token's name.
38
+ */
39
+ async name(context) {
40
+ if (this.executor) {
41
+ const res = await this.executor.runScriptTry(this.code, "UDT.name", [], {
42
+ script: this.script,
43
+ ...context,
44
+ });
45
+ if (res) {
46
+ return res.map((res) => core_1.ccc.bytesTo(res, "utf8"));
47
+ }
48
+ }
49
+ return ssri_1.ssri.ExecutorResponse.new(undefined);
50
+ }
51
+ /**
52
+ * Retrieves the symbol of the UDT.
53
+ * @returns The symbol of the UDT.
54
+ */
55
+ async symbol(context) {
56
+ if (this.executor) {
57
+ const res = await this.executor.runScriptTry(this.code, "UDT.symbol", [], {
58
+ script: this.script,
59
+ ...context,
60
+ });
61
+ if (res) {
62
+ return res.map((res) => core_1.ccc.bytesTo(res, "utf8"));
63
+ }
64
+ }
65
+ return ssri_1.ssri.ExecutorResponse.new(undefined);
66
+ }
67
+ /**
68
+ * Retrieves the decimals of the UDT.
69
+ * @returns The decimals of the UDT.
70
+ */
71
+ async decimals(context) {
72
+ if (this.executor) {
73
+ const res = await this.executor.runScriptTry(this.code, "UDT.decimals", [], {
74
+ script: this.script,
75
+ ...context,
76
+ });
77
+ if (res) {
78
+ return res.map((res) => core_1.ccc.numFromBytes(res));
79
+ }
80
+ }
81
+ return ssri_1.ssri.ExecutorResponse.new(undefined);
82
+ }
83
+ /**
84
+ * Retrieves the icon of the UDT
85
+ * @returns The icon of the UDT.
86
+ */
87
+ async icon(context) {
88
+ if (this.executor) {
89
+ const res = await this.executor.runScriptTry(this.code, "UDT.icon", [], {
90
+ script: this.script,
91
+ ...context,
92
+ });
93
+ if (res) {
94
+ return res.map((res) => core_1.ccc.bytesTo(res, "utf8"));
95
+ }
96
+ }
97
+ return ssri_1.ssri.ExecutorResponse.new(undefined);
98
+ }
99
+ /**
100
+ * Transfers UDT to specified addresses.
101
+ * @param tx - Transfer on the basis of an existing transaction to achieve combined actions. If not provided, a new transaction will be created.
102
+ * @param transfers - The array of transfers.
103
+ * @param transfers.to - The receiver of token.
104
+ * @param transfers.amount - The amount of token to the receiver.
105
+ * @returns The transaction result.
106
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction object.
107
+ * @example
108
+ * ```typescript
109
+ * const { script: change } = await signer.getRecommendedAddressObj();
110
+ * const { script: to } = await ccc.Address.fromString(receiver, signer.client);
111
+ *
112
+ * const udt = new Udt(
113
+ * {
114
+ * txHash: "0x4e2e832e0b1e7b5994681b621b00c1e65f577ee4b440ef95fa07db9bb3d50269",
115
+ * index: 0,
116
+ * },
117
+ * {
118
+ * codeHash: "0xcc9dc33ef234e14bc788c43a4848556a5fb16401a04662fc55db9bb201987037",
119
+ * hashType: "type",
120
+ * args: "0x71fd1985b2971a9903e4d8ed0d59e6710166985217ca0681437883837b86162f"
121
+ * },
122
+ * );
123
+ *
124
+ * const { res: tx } = await udtTrait.transfer(
125
+ * signer,
126
+ * [{ to, amount: 100 }],
127
+ * );
128
+ *
129
+ * const completedTx = udt.completeUdtBy(tx, signer);
130
+ * await completedTx.completeInputsByCapacity(signer);
131
+ * await completedTx.completeFeeBy(signer);
132
+ * const transferTxHash = await signer.sendTransaction(completedTx);
133
+ * ```
134
+ */
135
+ async transfer(signer, transfers, tx) {
136
+ let resTx;
137
+ if (this.executor) {
138
+ const txReq = core_1.ccc.Transaction.from(tx ?? {});
139
+ await txReq.completeInputsAtLeastOne(signer);
140
+ const res = await this.executor.runScriptTry(this.code, "UDT.transfer", [
141
+ txReq.toBytes(),
142
+ core_1.ccc.ScriptVec.encode(transfers.map(({ to }) => to)),
143
+ core_1.ccc.mol.Uint128Vec.encode(transfers.map(({ amount }) => amount)),
144
+ ], {
145
+ script: this.script,
146
+ });
147
+ if (res) {
148
+ resTx = res.map((res) => core_1.ccc.Transaction.fromBytes(res));
149
+ }
150
+ }
151
+ if (!resTx) {
152
+ const transfer = core_1.ccc.Transaction.from(tx ?? {});
153
+ for (const { to, amount } of transfers) {
154
+ transfer.addOutput({
155
+ lock: to,
156
+ type: this.script,
157
+ }, core_1.ccc.numLeToBytes(amount, 16));
158
+ }
159
+ resTx = ssri_1.ssri.ExecutorResponse.new(transfer);
160
+ }
161
+ resTx.res.addCellDeps({
162
+ outPoint: this.code,
163
+ depType: "code",
164
+ });
165
+ return resTx;
166
+ }
167
+ /**
168
+ * Mints new tokens to specified addresses. See the example in `transfer` as they are similar.
169
+ * @param tx - Optional existing transaction to build upon
170
+ * @param mints - Array of mints
171
+ * @param mints.to - receiver of token
172
+ * @param mints.amount - amount to the receiver
173
+ * @returns The transaction containing the mint operation
174
+ * @tag Mutation - This method represents a mutation of the onchain state
175
+ */
176
+ async mint(signer, mints, tx) {
177
+ let resTx;
178
+ if (this.executor) {
179
+ const txReq = core_1.ccc.Transaction.from(tx ?? {});
180
+ await txReq.completeInputsAtLeastOne(signer);
181
+ const res = await this.executor.runScriptTry(this.code, "UDT.mint", [
182
+ txReq.toBytes(),
183
+ core_1.ccc.ScriptVec.encode(mints.map(({ to }) => to)),
184
+ core_1.ccc.mol.Uint128Vec.encode(mints.map(({ amount }) => amount)),
185
+ ], {
186
+ script: this.script,
187
+ });
188
+ if (res) {
189
+ resTx = res.map((res) => core_1.ccc.Transaction.fromBytes(res));
190
+ }
191
+ }
192
+ if (!resTx) {
193
+ const mint = core_1.ccc.Transaction.from(tx ?? {});
194
+ for (const { to, amount } of mints) {
195
+ mint.addOutput({
196
+ lock: to,
197
+ type: this.script,
198
+ }, core_1.ccc.numLeToBytes(amount));
199
+ }
200
+ resTx = ssri_1.ssri.ExecutorResponse.new(mint);
201
+ }
202
+ resTx.res.addCellDeps({
203
+ outPoint: this.code,
204
+ depType: "code",
205
+ });
206
+ return resTx;
207
+ }
208
+ async completeChangeToLock(txLike, signer, change) {
209
+ const tx = core_1.ccc.Transaction.from(txLike);
210
+ await tx.completeInputsByUdt(signer, this.script);
211
+ const balanceDiff = (await tx.getInputsUdtBalance(signer.client, this.script)) -
212
+ tx.getOutputsUdtBalance(this.script);
213
+ if (balanceDiff > core_1.ccc.Zero) {
214
+ tx.addOutput({
215
+ lock: change,
216
+ type: this.script,
217
+ }, core_1.ccc.numLeToBytes(balanceDiff, 16));
218
+ }
219
+ return tx;
220
+ }
221
+ async completeBy(tx, from) {
222
+ const { script } = await from.getRecommendedAddressObj();
223
+ return this.completeChangeToLock(tx, from, script);
224
+ }
225
+ }
226
+ exports.Udt = Udt;
@@ -0,0 +1,41 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { ssri } from "@ckb-ccc/ssri";
3
+ import { Udt } from "../udt/index.js";
4
+ /**
5
+ * Represents a UDT (User Defined Token) with pausable functionality.
6
+ * @extends {Udt} This must be a SSRI UDT that does not fallback to xUDT.
7
+ * @public
8
+ */
9
+ export declare class UdtPausable extends Udt {
10
+ constructor(code: ccc.OutPointLike, script: ccc.ScriptLike, config: {
11
+ executor: ssri.Executor;
12
+ });
13
+ /**
14
+ * Pauses the UDT for the specified lock hashes. Pausing/Unpause without lock hashes should take effect on the global level. Note that this method is only available if the pausable UDT uses external pause list.
15
+ * @param {ccc.HexLike[]} lockHashes - The array of lock hashes to be paused.
16
+ * @param {ccc.TransactionLike} tx - The transaction to be used.
17
+ * @returns The transaction result.
18
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction to be sent.
19
+ */
20
+ pause(signer: ccc.Signer, locks: ccc.ScriptLike[], tx?: ccc.TransactionLike | null, extraLockHashes?: ccc.HexLike[] | null): Promise<ssri.ExecutorResponse<ccc.Transaction>>;
21
+ /**
22
+ * Unpauses the UDT for the specified lock hashes. Note that this method is only available if the pausable UDT uses external pause list.
23
+ * @param tx - The transaction to be used.
24
+ * @param lockHashes - The array of lock hashes to be unpaused.
25
+ * @returns The transaction result.
26
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction to be sent.
27
+ */
28
+ unpause(signer: ccc.Signer, locks: ccc.ScriptLike[], tx?: ccc.TransactionLike | null, extraLockHashes?: ccc.HexLike[] | null): Promise<ssri.ExecutorResponse<ccc.Transaction>>;
29
+ /**
30
+ * Checks if the UDT is paused for the specified lock hashes within a transaction. If not using external pause list, it can also be run on Code environment level.
31
+ * @param lockHashes - The lock hashes to check.
32
+ * @returns True if any of the lock hashes are paused, false otherwise.
33
+ */
34
+ isPaused(locks: ccc.ScriptLike[], extraLockHashes?: ccc.HexLike[] | null): Promise<ssri.ExecutorResponse<boolean[]>>;
35
+ /**
36
+ * Enumerates all paused lock hashes in UDTPausableData.
37
+ * @returns The array of lock hashes.
38
+ */
39
+ enumeratePaused(offset?: ccc.Num, limit?: ccc.Num): Promise<ssri.ExecutorResponse<ccc.Hex[]>>;
40
+ }
41
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/udtPausable/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,GAAG;gBAEhC,IAAI,EAAE,GAAG,CAAC,YAAY,EACtB,MAAM,EAAE,GAAG,CAAC,UAAU,EACtB,MAAM,EAAE;QACN,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC;KACzB;IAKH;;;;;;OAMG;IACG,KAAK,CACT,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,KAAK,EAAE,GAAG,CAAC,UAAU,EAAE,EACvB,EAAE,CAAC,EAAE,GAAG,CAAC,eAAe,GAAG,IAAI,EAC/B,eAAe,CAAC,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,IAAI,GACrC,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAyBlD;;;;;;OAMG;IACG,OAAO,CACX,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,KAAK,EAAE,GAAG,CAAC,UAAU,EAAE,EACvB,EAAE,CAAC,EAAE,GAAG,CAAC,eAAe,GAAG,IAAI,EAC/B,eAAe,CAAC,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,IAAI,GACrC,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAyBlD;;;;OAIG;IACG,QAAQ,CACZ,KAAK,EAAE,GAAG,CAAC,UAAU,EAAE,EACvB,eAAe,CAAC,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,IAAI,GACrC,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC;IAgB5C;;;OAGG;IACG,eAAe,CACnB,MAAM,CAAC,EAAE,GAAG,CAAC,GAAG,EAChB,KAAK,CAAC,EAAE,GAAG,CAAC,GAAG,GACd,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;CAU7C"}
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UdtPausable = void 0;
4
+ const core_1 = require("@ckb-ccc/core");
5
+ const index_js_1 = require("../udt/index.js");
6
+ /**
7
+ * Represents a UDT (User Defined Token) with pausable functionality.
8
+ * @extends {Udt} This must be a SSRI UDT that does not fallback to xUDT.
9
+ * @public
10
+ */
11
+ class UdtPausable extends index_js_1.Udt {
12
+ constructor(code, script, config) {
13
+ super(code, script, config);
14
+ }
15
+ /**
16
+ * Pauses the UDT for the specified lock hashes. Pausing/Unpause without lock hashes should take effect on the global level. Note that this method is only available if the pausable UDT uses external pause list.
17
+ * @param {ccc.HexLike[]} lockHashes - The array of lock hashes to be paused.
18
+ * @param {ccc.TransactionLike} tx - The transaction to be used.
19
+ * @returns The transaction result.
20
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction to be sent.
21
+ */
22
+ async pause(signer, locks, tx, extraLockHashes) {
23
+ const txReq = core_1.ccc.Transaction.from(tx ?? {});
24
+ await txReq.completeInputsAtLeastOne(signer);
25
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.pause", [
26
+ txReq.toBytes(),
27
+ core_1.ccc.mol.Byte32Vec.encode(locks
28
+ .map((l) => core_1.ccc.Script.from(l).hash())
29
+ .concat(extraLockHashes?.map(core_1.ccc.hexFrom) ?? [])),
30
+ ], { script: this.script });
31
+ const resTx = res.map((res) => core_1.ccc.Transaction.fromBytes(res));
32
+ resTx.res.addCellDeps({
33
+ outPoint: this.code,
34
+ depType: "code",
35
+ });
36
+ return resTx;
37
+ }
38
+ /**
39
+ * Unpauses the UDT for the specified lock hashes. Note that this method is only available if the pausable UDT uses external pause list.
40
+ * @param tx - The transaction to be used.
41
+ * @param lockHashes - The array of lock hashes to be unpaused.
42
+ * @returns The transaction result.
43
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction to be sent.
44
+ */
45
+ async unpause(signer, locks, tx, extraLockHashes) {
46
+ const txReq = core_1.ccc.Transaction.from(tx ?? {});
47
+ await txReq.completeInputsAtLeastOne(signer);
48
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.unpause", [
49
+ txReq.toBytes(),
50
+ core_1.ccc.mol.Byte32Vec.encode(locks
51
+ .map((l) => core_1.ccc.Script.from(l).hash())
52
+ .concat(extraLockHashes?.map(core_1.ccc.hexFrom) ?? [])),
53
+ ], { script: this.script });
54
+ const resTx = res.map((res) => core_1.ccc.Transaction.fromBytes(res));
55
+ resTx.res.addCellDeps({
56
+ outPoint: this.code,
57
+ depType: "code",
58
+ });
59
+ return resTx;
60
+ }
61
+ /**
62
+ * Checks if the UDT is paused for the specified lock hashes within a transaction. If not using external pause list, it can also be run on Code environment level.
63
+ * @param lockHashes - The lock hashes to check.
64
+ * @returns True if any of the lock hashes are paused, false otherwise.
65
+ */
66
+ async isPaused(locks, extraLockHashes) {
67
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.is_paused", [
68
+ core_1.ccc.mol.Byte32Vec.encode(locks
69
+ .map((l) => core_1.ccc.Script.from(l).hash())
70
+ .concat(extraLockHashes?.map(core_1.ccc.hexFrom) ?? [])),
71
+ ], { script: this.script });
72
+ return res.map((res) => core_1.ccc.mol.BoolVec.decode(res));
73
+ }
74
+ /**
75
+ * Enumerates all paused lock hashes in UDTPausableData.
76
+ * @returns The array of lock hashes.
77
+ */
78
+ async enumeratePaused(offset, limit) {
79
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.enumerate_paused", [core_1.ccc.numToBytes(offset ?? 0, 8), core_1.ccc.numToBytes(limit ?? 0, 8)], { script: this.script });
80
+ return res.map((res) => core_1.ccc.mol.Byte32Vec.decode(res));
81
+ }
82
+ }
83
+ exports.UdtPausable = UdtPausable;
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: "ts-jest",
4
+ testEnvironment: "node",
5
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@ckb-ccc/udt",
3
+ "version": "0.1.0",
4
+ "description": "UDT",
5
+ "author": "Alive24 <xct24@live.com>",
6
+ "license": "MIT",
7
+ "private": false,
8
+ "homepage": "https://github.com/ckb-devrel/ccc",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git://github.com/ckb-devrel/ccc.git"
12
+ },
13
+ "main": "./dist.commonjs/index.js",
14
+ "module": "./dist/index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/index.js",
18
+ "default": "./dist.commonjs/index.js"
19
+ },
20
+ "./advanced": {
21
+ "import": "./dist/advanced.js",
22
+ "default": "./dist.commonjs/advanced.js"
23
+ }
24
+ },
25
+ "devDependencies": {
26
+ "@eslint/js": "^9.1.1",
27
+ "@types/jest": "^29.5.12",
28
+ "@types/node": "^22.10.2",
29
+ "copyfiles": "^2.4.1",
30
+ "eslint": "^9.1.0",
31
+ "eslint-config-prettier": "^9.1.0",
32
+ "eslint-plugin-prettier": "^5.1.3",
33
+ "jest": "^29.7.0",
34
+ "prettier": "^3.2.5",
35
+ "prettier-plugin-organize-imports": "^3.2.4",
36
+ "rimraf": "^5.0.5",
37
+ "ts-jest": "^29.1.4",
38
+ "typescript": "^5.4.5",
39
+ "typescript-eslint": "^7.7.0"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "dependencies": {
45
+ "@ckb-ccc/core": "1.3.0",
46
+ "@ckb-ccc/ssri": "0.1.0"
47
+ },
48
+ "scripts": {
49
+ "test": "jest",
50
+ "build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json && copyfiles -u 2 misc/basedirs/**/* .",
51
+ "lint": "eslint ./src",
52
+ "format": "prettier --write . && eslint --fix ./src"
53
+ }
54
+ }
package/src/barrel.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./udt/index.js";
2
+ export * from "./udtPausable/index.js";
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./barrel.js";
2
+ export * as udt from "./barrel.js";