@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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @ckb-ccc/udt
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#130](https://github.com/ckb-devrel/ccc/pull/130) [`8c97c85`](https://github.com/ckb-devrel/ccc/commit/8c97c851db4a2d940c7e59116ca7620cfd0afae1) Thanks [@Hanssen0](https://github.com/Hanssen0)! - feat: SSRI & UDT SDK
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`4dbf4fd`](https://github.com/ckb-devrel/ccc/commit/4dbf4fd8021cf14d05282706a7667ea7d108fb09), [`4c76f9e`](https://github.com/ckb-devrel/ccc/commit/4c76f9e2a93a226fcfc4c32a5378bb531bfff08f), [`c2c4c26`](https://github.com/ckb-devrel/ccc/commit/c2c4c264e04461948e4b913b2f22054e6032ddc8), [`b6a73fa`](https://github.com/ckb-devrel/ccc/commit/b6a73fa9628ebdff51cb8f246309654cd53e36f2), [`8c97c85`](https://github.com/ckb-devrel/ccc/commit/8c97c851db4a2d940c7e59116ca7620cfd0afae1)]:
12
+ - @ckb-ccc/core@1.3.0
13
+ - @ckb-ccc/ssri@0.1.0
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ <p align="center">
2
+ <a href="https://app.ckbccc.com/">
3
+ <img alt="Logo" src="https://raw.githubusercontent.com/ckb-devrel/ccc/master/assets/logoAndText.svg" style="height: 8rem; max-width: 90%; padding: 0.5rem 0;" />
4
+ </a>
5
+ </p>
6
+
7
+ <h1 align="center" style="font-size: 48px;">
8
+ CCC's Support for User Defined Token (UDT)
9
+ </h1>
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/@ckb-ccc/udt"><img
13
+ alt="NPM Version" src="https://img.shields.io/npm/v/%40ckb-ccc%2Fudt"
14
+ /></a>
15
+ <img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/ckb-devrel/ccc" />
16
+ <img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/ckb-devrel/ccc/master" />
17
+ <img alt="GitHub branch check runs" src="https://img.shields.io/github/check-runs/ckb-devrel/ccc/master" />
18
+ <a href="https://live.ckbccc.com/"><img
19
+ alt="Playground" src="https://img.shields.io/website?url=https%3A%2F%2Flive.ckbccc.com%2F&label=Playground"
20
+ /></a>
21
+ <a href="https://app.ckbccc.com/"><img
22
+ alt="App" src="https://img.shields.io/website?url=https%3A%2F%2Fapp.ckbccc.com%2F&label=App"
23
+ /></a>
24
+ <a href="https://docs.ckbccc.com/"><img
25
+ alt="Docs" src="https://img.shields.io/website?url=https%3A%2F%2Fdocs.ckbccc.com%2F&label=Docs"
26
+ /></a>
27
+ </p>
28
+
29
+ <p align="center">
30
+ CCC - CKBers' Codebase is a one-stop solution for your CKB JS/TS ecosystem development.
31
+ <br />
32
+ Empower yourself with CCC to discover the unlimited potential of CKB.
33
+ <br />
34
+ Interoperate with wallets from different chain ecosystems.
35
+ <br />
36
+ Fully enabling CKB's Turing completeness and cryptographic freedom power.
37
+ </p>
38
+
39
+ ## Quick Start
40
+
41
+ - At the moment, `UDT` and `UDTPausable` from `@ckb-ccc/udt` are fully supported through SSRI. In the future, there will be built in TypeScript generation directly based on the Rust source code on compilation.
42
+ - To instantiate a `UDT` script compliant with SSRI, you can provide the SSRI server url and also specify the OutPoint of the script code.
43
+ - You can also instantiate a `UDTPausable` script or other scripts that extends from `UDT`.
44
+
45
+ ```ts
46
+ import { Server } from "@ckb-ccc/ssri";
47
+ import { Udt, UdtPausable } from "@ckb-ccc/udt";
48
+
49
+ const { signer } = useApp();
50
+ const server = new Server("https://localhost:9090");
51
+
52
+ const udt = new Udt(
53
+ server,
54
+ {
55
+ txHash: "0x...",
56
+ index: 0,
57
+ },
58
+ {
59
+ codeHash: "0x...",
60
+ hashType: "type",
61
+ args: "0x...",
62
+ },
63
+ );
64
+
65
+ const udtPausable = new UdtPausable(
66
+ server,
67
+ {
68
+ txHash: "0x...",
69
+ index: 0,
70
+ },
71
+ {
72
+ codeHash: "0x...",
73
+ hashType: "type",
74
+ args: "0x...",
75
+ },
76
+ );
77
+ ```
78
+
79
+ You can directly call the methods in the script:
80
+
81
+ ```ts
82
+ const { res: udtSymbol } = await udt.symbol();
83
+ const { res: pauseList } = await udtPausable.enumeratePaused();
84
+ ```
85
+
86
+ Some of the methods can return a `ccc.Transaction`. For example, you can call `transfer` with the following params:
87
+
88
+ ```ts
89
+ const { script: to } = await signer.getRecommendedAddressObj();
90
+
91
+ const { res: transferTx } = await udt.transfer(signer, [{ to, amount: 100 }]);
92
+ const completedTx = await udt.completeUdtBy(transferTx, signer);
93
+
94
+ await completedTx.completeInputsByCapacity(signer);
95
+ await completedTx.completeFeeBy(signer);
96
+ const transferTxHash = await signer.sendTransaction(completedTx);
97
+ ```
98
+
99
+ <h3 align="center">
100
+ Read more about CCC on <a href="https://docs.ckbccc.com">our website</a> or <a href="https://github.com/ckb-devrel/ccc">GitHub Repo</a>.
101
+ </h3>
@@ -0,0 +1,3 @@
1
+ export * from "./udt/index.js";
2
+ export * from "./udtPausable/index.js";
3
+ //# sourceMappingURL=barrel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"barrel.d.ts","sourceRoot":"","sources":["../src/barrel.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC"}
package/dist/barrel.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./udt/index.js";
2
+ export * from "./udtPausable/index.js";
@@ -0,0 +1,3 @@
1
+ export * from "./barrel.js";
2
+ export * as udt from "./barrel.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./barrel.js";
2
+ export * as udt from "./barrel.js";
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
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,222 @@
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 class Udt extends ssri.Trait {
15
+ /**
16
+ * Constructs a new UDT (User Defined Token) script instance.
17
+ * By default it is a SSRI-compliant UDT. By providing `xudtType`, it is compatible with the legacy xUDT.
18
+ *
19
+ * @param executor - The SSRI executor instance.
20
+ * @param code - The script code cell of the UDT.
21
+ * @param script - The type script of the UDT.
22
+ * @example
23
+ * ```typescript
24
+ * const udt = new Udt(executor, code, script);
25
+ * ```
26
+ */
27
+ constructor(code, script, config) {
28
+ super(code, config?.executor);
29
+ this.script = ccc.Script.from(script);
30
+ }
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
+ async name(context) {
37
+ if (this.executor) {
38
+ const res = await this.executor.runScriptTry(this.code, "UDT.name", [], {
39
+ script: this.script,
40
+ ...context,
41
+ });
42
+ if (res) {
43
+ return res.map((res) => ccc.bytesTo(res, "utf8"));
44
+ }
45
+ }
46
+ return ssri.ExecutorResponse.new(undefined);
47
+ }
48
+ /**
49
+ * Retrieves the symbol of the UDT.
50
+ * @returns The symbol of the UDT.
51
+ */
52
+ async symbol(context) {
53
+ if (this.executor) {
54
+ const res = await this.executor.runScriptTry(this.code, "UDT.symbol", [], {
55
+ script: this.script,
56
+ ...context,
57
+ });
58
+ if (res) {
59
+ return res.map((res) => ccc.bytesTo(res, "utf8"));
60
+ }
61
+ }
62
+ return ssri.ExecutorResponse.new(undefined);
63
+ }
64
+ /**
65
+ * Retrieves the decimals of the UDT.
66
+ * @returns The decimals of the UDT.
67
+ */
68
+ async decimals(context) {
69
+ if (this.executor) {
70
+ const res = await this.executor.runScriptTry(this.code, "UDT.decimals", [], {
71
+ script: this.script,
72
+ ...context,
73
+ });
74
+ if (res) {
75
+ return res.map((res) => ccc.numFromBytes(res));
76
+ }
77
+ }
78
+ return ssri.ExecutorResponse.new(undefined);
79
+ }
80
+ /**
81
+ * Retrieves the icon of the UDT
82
+ * @returns The icon of the UDT.
83
+ */
84
+ async icon(context) {
85
+ if (this.executor) {
86
+ const res = await this.executor.runScriptTry(this.code, "UDT.icon", [], {
87
+ script: this.script,
88
+ ...context,
89
+ });
90
+ if (res) {
91
+ return res.map((res) => ccc.bytesTo(res, "utf8"));
92
+ }
93
+ }
94
+ return ssri.ExecutorResponse.new(undefined);
95
+ }
96
+ /**
97
+ * Transfers UDT to specified addresses.
98
+ * @param tx - Transfer on the basis of an existing transaction to achieve combined actions. If not provided, a new transaction will be created.
99
+ * @param transfers - The array of transfers.
100
+ * @param transfers.to - The receiver of token.
101
+ * @param transfers.amount - The amount of token to the receiver.
102
+ * @returns The transaction result.
103
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction object.
104
+ * @example
105
+ * ```typescript
106
+ * const { script: change } = await signer.getRecommendedAddressObj();
107
+ * const { script: to } = await ccc.Address.fromString(receiver, signer.client);
108
+ *
109
+ * const udt = new Udt(
110
+ * {
111
+ * txHash: "0x4e2e832e0b1e7b5994681b621b00c1e65f577ee4b440ef95fa07db9bb3d50269",
112
+ * index: 0,
113
+ * },
114
+ * {
115
+ * codeHash: "0xcc9dc33ef234e14bc788c43a4848556a5fb16401a04662fc55db9bb201987037",
116
+ * hashType: "type",
117
+ * args: "0x71fd1985b2971a9903e4d8ed0d59e6710166985217ca0681437883837b86162f"
118
+ * },
119
+ * );
120
+ *
121
+ * const { res: tx } = await udtTrait.transfer(
122
+ * signer,
123
+ * [{ to, amount: 100 }],
124
+ * );
125
+ *
126
+ * const completedTx = udt.completeUdtBy(tx, signer);
127
+ * await completedTx.completeInputsByCapacity(signer);
128
+ * await completedTx.completeFeeBy(signer);
129
+ * const transferTxHash = await signer.sendTransaction(completedTx);
130
+ * ```
131
+ */
132
+ async transfer(signer, transfers, tx) {
133
+ let resTx;
134
+ if (this.executor) {
135
+ const txReq = ccc.Transaction.from(tx ?? {});
136
+ await txReq.completeInputsAtLeastOne(signer);
137
+ const res = await this.executor.runScriptTry(this.code, "UDT.transfer", [
138
+ txReq.toBytes(),
139
+ ccc.ScriptVec.encode(transfers.map(({ to }) => to)),
140
+ ccc.mol.Uint128Vec.encode(transfers.map(({ amount }) => amount)),
141
+ ], {
142
+ script: this.script,
143
+ });
144
+ if (res) {
145
+ resTx = res.map((res) => ccc.Transaction.fromBytes(res));
146
+ }
147
+ }
148
+ if (!resTx) {
149
+ const transfer = ccc.Transaction.from(tx ?? {});
150
+ for (const { to, amount } of transfers) {
151
+ transfer.addOutput({
152
+ lock: to,
153
+ type: this.script,
154
+ }, ccc.numLeToBytes(amount, 16));
155
+ }
156
+ resTx = ssri.ExecutorResponse.new(transfer);
157
+ }
158
+ resTx.res.addCellDeps({
159
+ outPoint: this.code,
160
+ depType: "code",
161
+ });
162
+ return resTx;
163
+ }
164
+ /**
165
+ * Mints new tokens to specified addresses. See the example in `transfer` as they are similar.
166
+ * @param tx - Optional existing transaction to build upon
167
+ * @param mints - Array of mints
168
+ * @param mints.to - receiver of token
169
+ * @param mints.amount - amount to the receiver
170
+ * @returns The transaction containing the mint operation
171
+ * @tag Mutation - This method represents a mutation of the onchain state
172
+ */
173
+ async mint(signer, mints, tx) {
174
+ let resTx;
175
+ if (this.executor) {
176
+ const txReq = ccc.Transaction.from(tx ?? {});
177
+ await txReq.completeInputsAtLeastOne(signer);
178
+ const res = await this.executor.runScriptTry(this.code, "UDT.mint", [
179
+ txReq.toBytes(),
180
+ ccc.ScriptVec.encode(mints.map(({ to }) => to)),
181
+ ccc.mol.Uint128Vec.encode(mints.map(({ amount }) => amount)),
182
+ ], {
183
+ script: this.script,
184
+ });
185
+ if (res) {
186
+ resTx = res.map((res) => ccc.Transaction.fromBytes(res));
187
+ }
188
+ }
189
+ if (!resTx) {
190
+ const mint = ccc.Transaction.from(tx ?? {});
191
+ for (const { to, amount } of mints) {
192
+ mint.addOutput({
193
+ lock: to,
194
+ type: this.script,
195
+ }, ccc.numLeToBytes(amount));
196
+ }
197
+ resTx = ssri.ExecutorResponse.new(mint);
198
+ }
199
+ resTx.res.addCellDeps({
200
+ outPoint: this.code,
201
+ depType: "code",
202
+ });
203
+ return resTx;
204
+ }
205
+ async completeChangeToLock(txLike, signer, change) {
206
+ const tx = ccc.Transaction.from(txLike);
207
+ await tx.completeInputsByUdt(signer, this.script);
208
+ const balanceDiff = (await tx.getInputsUdtBalance(signer.client, this.script)) -
209
+ tx.getOutputsUdtBalance(this.script);
210
+ if (balanceDiff > ccc.Zero) {
211
+ tx.addOutput({
212
+ lock: change,
213
+ type: this.script,
214
+ }, ccc.numLeToBytes(balanceDiff, 16));
215
+ }
216
+ return tx;
217
+ }
218
+ async completeBy(tx, from) {
219
+ const { script } = await from.getRecommendedAddressObj();
220
+ return this.completeChangeToLock(tx, from, script);
221
+ }
222
+ }
@@ -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,79 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { Udt } from "../udt/index.js";
3
+ /**
4
+ * Represents a UDT (User Defined Token) with pausable functionality.
5
+ * @extends {Udt} This must be a SSRI UDT that does not fallback to xUDT.
6
+ * @public
7
+ */
8
+ export class UdtPausable extends Udt {
9
+ constructor(code, script, config) {
10
+ super(code, script, config);
11
+ }
12
+ /**
13
+ * 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.
14
+ * @param {ccc.HexLike[]} lockHashes - The array of lock hashes to be paused.
15
+ * @param {ccc.TransactionLike} tx - The transaction to be used.
16
+ * @returns The transaction result.
17
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction to be sent.
18
+ */
19
+ async pause(signer, locks, tx, extraLockHashes) {
20
+ const txReq = ccc.Transaction.from(tx ?? {});
21
+ await txReq.completeInputsAtLeastOne(signer);
22
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.pause", [
23
+ txReq.toBytes(),
24
+ ccc.mol.Byte32Vec.encode(locks
25
+ .map((l) => ccc.Script.from(l).hash())
26
+ .concat(extraLockHashes?.map(ccc.hexFrom) ?? [])),
27
+ ], { script: this.script });
28
+ const resTx = res.map((res) => ccc.Transaction.fromBytes(res));
29
+ resTx.res.addCellDeps({
30
+ outPoint: this.code,
31
+ depType: "code",
32
+ });
33
+ return resTx;
34
+ }
35
+ /**
36
+ * Unpauses the UDT for the specified lock hashes. Note that this method is only available if the pausable UDT uses external pause list.
37
+ * @param tx - The transaction to be used.
38
+ * @param lockHashes - The array of lock hashes to be unpaused.
39
+ * @returns The transaction result.
40
+ * @tag Mutation - This method represents a mutation of the onchain state and will return a transaction to be sent.
41
+ */
42
+ async unpause(signer, locks, tx, extraLockHashes) {
43
+ const txReq = ccc.Transaction.from(tx ?? {});
44
+ await txReq.completeInputsAtLeastOne(signer);
45
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.unpause", [
46
+ txReq.toBytes(),
47
+ ccc.mol.Byte32Vec.encode(locks
48
+ .map((l) => ccc.Script.from(l).hash())
49
+ .concat(extraLockHashes?.map(ccc.hexFrom) ?? [])),
50
+ ], { script: this.script });
51
+ const resTx = res.map((res) => ccc.Transaction.fromBytes(res));
52
+ resTx.res.addCellDeps({
53
+ outPoint: this.code,
54
+ depType: "code",
55
+ });
56
+ return resTx;
57
+ }
58
+ /**
59
+ * 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.
60
+ * @param lockHashes - The lock hashes to check.
61
+ * @returns True if any of the lock hashes are paused, false otherwise.
62
+ */
63
+ async isPaused(locks, extraLockHashes) {
64
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.is_paused", [
65
+ ccc.mol.Byte32Vec.encode(locks
66
+ .map((l) => ccc.Script.from(l).hash())
67
+ .concat(extraLockHashes?.map(ccc.hexFrom) ?? [])),
68
+ ], { script: this.script });
69
+ return res.map((res) => ccc.mol.BoolVec.decode(res));
70
+ }
71
+ /**
72
+ * Enumerates all paused lock hashes in UDTPausableData.
73
+ * @returns The array of lock hashes.
74
+ */
75
+ async enumeratePaused(offset, limit) {
76
+ const res = await this.assertExecutor().runScript(this.code, "UDTPausable.enumerate_paused", [ccc.numToBytes(offset ?? 0, 8), ccc.numToBytes(limit ?? 0, 8)], { script: this.script });
77
+ return res.map((res) => ccc.mol.Byte32Vec.decode(res));
78
+ }
79
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./udt/index.js";
2
+ export * from "./udtPausable/index.js";
3
+ //# sourceMappingURL=barrel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"barrel.d.ts","sourceRoot":"","sources":["../src/barrel.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,18 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./udt/index.js"), exports);
18
+ __exportStar(require("./udtPausable/index.js"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./barrel.js";
2
+ export * as udt from "./barrel.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC"}