|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Copyright © Aptos Foundation |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +import { parseArgs } from "node:util"; |
| 7 | +import { writeFileSync } from "node:fs"; |
| 8 | + |
| 9 | +import { AptosConfig } from "../api/aptosConfig"; |
| 10 | +import { Network } from "../utils/apiEndpoints"; |
| 11 | +import { |
| 12 | + generateModuleAbiJSON, |
| 13 | + generateEntryFunctionAbiJSON, |
| 14 | + generateViewFunctionAbiJSON, |
| 15 | +} from "../transactions/transactionBuilder/abiJson"; |
| 16 | + |
| 17 | +const USAGE = ` |
| 18 | +Usage: generate-abi-json [options] |
| 19 | +
|
| 20 | +Fetches on-chain Move module ABIs and outputs JSON-serializable entry/view |
| 21 | +function ABI definitions that can be embedded in application source code. |
| 22 | +
|
| 23 | +Options: |
| 24 | + --network <network> Network to connect to: mainnet, testnet, devnet, local |
| 25 | + (default: mainnet) |
| 26 | + --node-url <url> Custom fullnode URL (overrides --network) |
| 27 | + --address <address> Account address of the module (required) |
| 28 | + --module <name> Module name (required) |
| 29 | + --function <name> Specific function name (omit to output all functions) |
| 30 | + --output <file> Write JSON to a file instead of stdout |
| 31 | + --pretty Pretty-print JSON with 2-space indentation |
| 32 | + --help Show this help message |
| 33 | +
|
| 34 | +Examples: |
| 35 | + # All entry/view ABIs for the coin module on mainnet |
| 36 | + generate-abi-json --address 0x1 --module coin --pretty |
| 37 | +
|
| 38 | + # Single entry function, written to a file |
| 39 | + generate-abi-json --address 0x1 --module coin --function transfer --output coin_transfer.json |
| 40 | +
|
| 41 | + # Use a custom node URL (include /v1 path) |
| 42 | + generate-abi-json --node-url http://localhost:8080/v1 --address 0x1 --module coin |
| 43 | +`.trim(); |
| 44 | + |
| 45 | +const NETWORK_MAP: Record<string, Network> = { |
| 46 | + mainnet: Network.MAINNET, |
| 47 | + testnet: Network.TESTNET, |
| 48 | + devnet: Network.DEVNET, |
| 49 | + local: Network.LOCAL, |
| 50 | +}; |
| 51 | + |
| 52 | +async function main() { |
| 53 | + const { values } = parseArgs({ |
| 54 | + options: { |
| 55 | + network: { type: "string", default: "mainnet" }, |
| 56 | + "node-url": { type: "string" }, |
| 57 | + address: { type: "string" }, |
| 58 | + module: { type: "string" }, |
| 59 | + function: { type: "string" }, |
| 60 | + output: { type: "string" }, |
| 61 | + pretty: { type: "boolean", default: false }, |
| 62 | + help: { type: "boolean", default: false }, |
| 63 | + }, |
| 64 | + strict: true, |
| 65 | + }); |
| 66 | + |
| 67 | + if (values.help) { |
| 68 | + console.log(USAGE); |
| 69 | + process.exit(0); |
| 70 | + } |
| 71 | + |
| 72 | + if (!values.address) { |
| 73 | + console.error("Error: --address is required\n"); |
| 74 | + console.error(USAGE); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + if (!values.module) { |
| 78 | + console.error("Error: --module is required\n"); |
| 79 | + console.error(USAGE); |
| 80 | + process.exit(1); |
| 81 | + } |
| 82 | + |
| 83 | + const networkKey = values.network!.toLowerCase(); |
| 84 | + const network = NETWORK_MAP[networkKey]; |
| 85 | + if (!network && !values["node-url"]) { |
| 86 | + console.error(`Error: unknown network '${values.network}'. Use one of: mainnet, testnet, devnet, local\n`); |
| 87 | + console.error("Or provide --node-url for a custom endpoint."); |
| 88 | + process.exit(1); |
| 89 | + } |
| 90 | + |
| 91 | + const aptosConfig = values["node-url"] |
| 92 | + ? new AptosConfig({ fullnode: values["node-url"], network: Network.CUSTOM }) |
| 93 | + : new AptosConfig({ network }); |
| 94 | + |
| 95 | + try { |
| 96 | + let result: unknown; |
| 97 | + |
| 98 | + if (values.function) { |
| 99 | + // Try entry function first, then view function |
| 100 | + try { |
| 101 | + result = await generateEntryFunctionAbiJSON({ |
| 102 | + aptosConfig, |
| 103 | + accountAddress: values.address, |
| 104 | + moduleName: values.module, |
| 105 | + functionName: values.function, |
| 106 | + }); |
| 107 | + } catch { |
| 108 | + result = await generateViewFunctionAbiJSON({ |
| 109 | + aptosConfig, |
| 110 | + accountAddress: values.address, |
| 111 | + moduleName: values.module, |
| 112 | + functionName: values.function, |
| 113 | + }); |
| 114 | + } |
| 115 | + } else { |
| 116 | + result = await generateModuleAbiJSON({ |
| 117 | + aptosConfig, |
| 118 | + accountAddress: values.address, |
| 119 | + moduleName: values.module, |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + const jsonStr = values.pretty ? JSON.stringify(result, null, 2) : JSON.stringify(result); |
| 124 | + |
| 125 | + if (values.output) { |
| 126 | + writeFileSync(values.output, `${jsonStr}\n`, "utf-8"); |
| 127 | + console.error(`Wrote ABI JSON to ${values.output}`); |
| 128 | + } else { |
| 129 | + console.log(jsonStr); |
| 130 | + } |
| 131 | + } catch (err: unknown) { |
| 132 | + const message = err instanceof Error ? err.message : String(err); |
| 133 | + console.error(`Error: ${message}`); |
| 134 | + process.exit(1); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +main(); |
0 commit comments