|
| 1 | +import { ccc } from "@ckb-ccc/core"; |
| 2 | +import { ssri } from "@ckb-ccc/ssri"; |
| 3 | +import { Udt, UdtConfigLike } from "../udt/index.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * The basic metadata of a UDT token. |
| 7 | + * |
| 8 | + * @example |
| 9 | + * ```typescript |
| 10 | + * const metadataMolecule = UdtMetadata.encode({ |
| 11 | + * name: "My UDT", |
| 12 | + * symbol: "MYUDT", |
| 13 | + * decimals: 8, |
| 14 | + * icon: "https://example.com/icon.png", |
| 15 | + * }); |
| 16 | + * ``` |
| 17 | + * |
| 18 | + * @public |
| 19 | + * @category UDT |
| 20 | + */ |
| 21 | +export const UdtMetadata = ccc.mol.table({ |
| 22 | + name: ccc.mol.String, |
| 23 | + symbol: ccc.mol.String, |
| 24 | + decimals: ccc.mol.Uint8, |
| 25 | + icon: ccc.mol.String, |
| 26 | +}); |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents a UDT (User Defined Token) with separated SSRI metadata functionality. |
| 30 | + * @extends {Udt} This must be a SSRI UDT that does not fallback to xUDT. |
| 31 | + * @public |
| 32 | + */ |
| 33 | +export class UdtRegister extends Udt { |
| 34 | + constructor( |
| 35 | + code: ccc.OutPointLike, |
| 36 | + script: ccc.ScriptLike, |
| 37 | + config: UdtConfigLike & { executor: ssri.Executor }, |
| 38 | + ) { |
| 39 | + super(code, script, config); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Registers (creates) a new UDT with on-chain metadata. |
| 44 | + * This method creates a new unique UDT instance (usually with a TypeId pattern), |
| 45 | + * assigns on-chain metadata (name, symbol, decimals, icon), and returns a transaction |
| 46 | + * to instantiate the new token. Often used by the deployer/owner. |
| 47 | + * |
| 48 | + * @param signer - The signer (owner) who will initialize and own the new UDT |
| 49 | + * @param metadata - Object containing UDT metadata (name, symbol, decimals, icon) |
| 50 | + * @param tx - Optional existing transaction to build upon |
| 51 | + * @returns Promise resolving to `{ tx, tokenHash }`, where `tx` is the deployment transaction, |
| 52 | + * and `tokenHash` is the computed TypeId/Token hash of the new UDT |
| 53 | + * |
| 54 | + * @example |
| 55 | + * ```typescript |
| 56 | + * const udt = new Udt(codeOutPoint, scriptConfig); |
| 57 | + * const { tx, tokenHash } = await udt.register( |
| 58 | + * signer, |
| 59 | + * { name: "My UDT", symbol: "MYT", decimals: 8, icon: "https://..." } |
| 60 | + * ); |
| 61 | + * // Send tx.res or complete tx.res and send the transaction |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * @remarks |
| 65 | + * - Uses SSRI executor if available for advanced/SSRI-compliant registration. |
| 66 | + * - Falls back to legacy registration (TypeId pattern) if no executor is present. |
| 67 | + * - The token hash can be used as the args for the UDT type script. |
| 68 | + */ |
| 69 | + async register( |
| 70 | + signer: ccc.Signer, |
| 71 | + metadata: { |
| 72 | + name: string; |
| 73 | + symbol: string; |
| 74 | + decimals: number; |
| 75 | + icon: string; |
| 76 | + }, |
| 77 | + tx?: ccc.TransactionLike | null, |
| 78 | + ): Promise<{ |
| 79 | + tx: ssri.ExecutorResponse<ccc.Transaction>; |
| 80 | + tokenHash: ccc.Hex; |
| 81 | + }> { |
| 82 | + const owner = await signer.getRecommendedAddressObj(); |
| 83 | + const register = ccc.Transaction.from(tx ?? {}); |
| 84 | + if (register.inputs.length === 0) { |
| 85 | + await register.completeInputsAtLeastOne(signer); // For `TypeId` calcuclation |
| 86 | + } |
| 87 | + const tokenHash = ccc.hashTypeId( |
| 88 | + register.inputs[0], |
| 89 | + register.outputs.length, |
| 90 | + ); |
| 91 | + |
| 92 | + let resTx; |
| 93 | + if (this.executor) { |
| 94 | + const res = await this.executor.runScriptTry( |
| 95 | + this.code, |
| 96 | + "UDTSSRI.create", |
| 97 | + [ |
| 98 | + register.toBytes(), |
| 99 | + ccc.Script.from(owner.script).toBytes(), |
| 100 | + UdtMetadata.encode(metadata), |
| 101 | + ], |
| 102 | + ); |
| 103 | + if (res) { |
| 104 | + resTx = res.map((res) => ccc.Transaction.fromBytes(res)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Fallback logic |
| 109 | + if (!resTx) { |
| 110 | + register.addOutput( |
| 111 | + { |
| 112 | + lock: owner.script, |
| 113 | + type: { |
| 114 | + codeHash: |
| 115 | + "00000000000000000000000000000000000000000000000000545950455f4944", |
| 116 | + hashType: "type", |
| 117 | + args: tokenHash, |
| 118 | + }, |
| 119 | + }, |
| 120 | + UdtMetadata.encode(metadata), |
| 121 | + ); |
| 122 | + resTx = ssri.ExecutorResponse.new(register); |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + tx: resTx.map((tx) => this.addCellDeps(tx)), |
| 127 | + tokenHash, |
| 128 | + }; |
| 129 | + } |
| 130 | +} |
0 commit comments