|
| 1 | +import fs from "fs"; |
| 2 | +import type { |
| 3 | + Network, |
| 4 | + WormholeConfigOverrides, |
| 5 | +} from "@wormhole-foundation/sdk-connect"; |
| 6 | +import { colors } from "../../colors.js"; |
| 7 | +import { loadConfig } from "../../deployments"; |
| 8 | +import { runXrpl } from "../../xrpl/helpers"; |
| 9 | +import { XrplAddress, XrplZeroAddress } from "@wormhole-foundation/sdk-xrpl"; |
| 10 | +import { options } from "../shared"; |
| 11 | + |
| 12 | +const XRP_DECIMALS = 6; |
| 13 | + |
| 14 | +export function createXrplSetTokenCommand( |
| 15 | + _overrides: WormholeConfigOverrides<Network> |
| 16 | +) { |
| 17 | + return { |
| 18 | + command: "set-token", |
| 19 | + describe: |
| 20 | + "Record the XRPL NTT token (address + decimals) in the deployment file", |
| 21 | + builder: (yargs: any) => |
| 22 | + yargs |
| 23 | + .option("token", { |
| 24 | + describe: |
| 25 | + 'Token: "native" for native XRP, an IOU "CODE.rIssuer", or a ' + |
| 26 | + "48-char hex MPT issuance id (IOU/MPT validated via the Wormhole " + |
| 27 | + "SDK XRPL address conventions)", |
| 28 | + type: "string", |
| 29 | + demandOption: true, |
| 30 | + }) |
| 31 | + .option("decimals", { |
| 32 | + describe: "Token decimals on XRPL (e.g. 6 for XRP); 0-255", |
| 33 | + type: "number", |
| 34 | + demandOption: true, |
| 35 | + }) |
| 36 | + .option("path", options.deploymentPath) |
| 37 | + .example( |
| 38 | + "$0 xrpl set-token --token native --decimals 6", |
| 39 | + "Record native XRP" |
| 40 | + ) |
| 41 | + .example( |
| 42 | + "$0 xrpl set-token --token FOO.rBa2jdUu8S2ZzaCJv8y1Lx9Pdrns51hJj --decimals 9", |
| 43 | + "Record an IOU token" |
| 44 | + ) |
| 45 | + .example( |
| 46 | + "$0 xrpl set-token --token 00EE5E8C9F... --decimals 9", |
| 47 | + "Record an MPT token (48-char hex issuance id)" |
| 48 | + ), |
| 49 | + handler: (argv: any) => |
| 50 | + runXrpl(async () => { |
| 51 | + const path = argv.path; |
| 52 | + const config = loadConfig(path); |
| 53 | + |
| 54 | + // Native XRP is recorded as the literal "native" (the zero/black-hole |
| 55 | + // account is also accepted and normalized to it). IOU/MPT identifiers |
| 56 | + // are validated + normalized via the SDK's XRPL address conventions. |
| 57 | + const raw = String(argv.token); |
| 58 | + const token = |
| 59 | + raw.toLowerCase() === "native" || raw === XrplZeroAddress |
| 60 | + ? "native" |
| 61 | + : new XrplAddress(raw).toString(); |
| 62 | + |
| 63 | + const decimals = argv.decimals; |
| 64 | + if (!Number.isInteger(decimals) || decimals < 0 || decimals > 255) { |
| 65 | + throw new Error( |
| 66 | + `--decimals must be an integer between 0 and 255, got ${decimals}` |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + if (token === "native" && decimals != XRP_DECIMALS) { |
| 71 | + throw new Error( |
| 72 | + `--decimals must be 6 if the token is XRP, got ${decimals}` |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + config.xrpl = { ...config.xrpl, token, decimals }; |
| 77 | + fs.writeFileSync(path, JSON.stringify(config, null, 2)); |
| 78 | + |
| 79 | + console.log( |
| 80 | + colors.green( |
| 81 | + `✅ Recorded XRPL token: ${colors.yellow(token)} (${decimals} decimals)` |
| 82 | + ) |
| 83 | + ); |
| 84 | + console.log(` ${path} → xrpl.token, xrpl.decimals`); |
| 85 | + }), |
| 86 | + }; |
| 87 | +} |
0 commit comments