polkadot-cli 0.8.1 → 0.9.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.
Files changed (3) hide show
  1. package/README.md +35 -0
  2. package/dist/cli.mjs +78 -17
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -102,6 +102,20 @@ dot query System.Number --output json | jq '.+1'
102
102
  dot query kusama.System.Account 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
103
103
  ```
104
104
 
105
+ #### Output formatting
106
+
107
+ Query results automatically convert on-chain types for readability:
108
+
109
+ - **BigInt** values (e.g. balances) render as decimal strings
110
+ - **Binary** fields (e.g. token `name`, `symbol`) render as text when valid UTF-8, or as `0x`-prefixed hex otherwise
111
+ - **Uint8Array** values render as `0x`-prefixed hex
112
+
113
+ ```bash
114
+ # Token metadata — symbol and name display as text, not {}
115
+ dot query assethub-paseo.Assets.Metadata 50000413
116
+ # { "deposit": "6693666000", "name": "Paseo Token", "symbol": "PAS", ... }
117
+ ```
118
+
105
119
  ### Look up constants
106
120
 
107
121
  ```bash
@@ -153,6 +167,27 @@ dot tx 0x0503008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48
153
167
  dot tx Utility.batchAll '[{"type":"Balances","value":{"type":"transfer_keep_alive","value":{"dest":"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty","value":1000000000000}}},{"type":"Balances","value":{"type":"transfer_keep_alive","value":{"dest":"5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y","value":2000000000000}}}]' --from alice
154
168
  ```
155
169
 
170
+ #### Enum shorthand
171
+
172
+ Enum arguments accept a concise `Variant(value)` syntax instead of verbose JSON:
173
+
174
+ ```bash
175
+ # Instead of: '{"type":"system","value":{"type":"Authorized"}}'
176
+ dot tx Utility.dispatch_as 'system(Authorized)' $(dot tx System.remark 0xcafe --encode) --from alice
177
+
178
+ # Nested enums work too
179
+ dot tx Utility.dispatch_as 'system(Signed(5FHneW46...))' <call> --from alice
180
+
181
+ # Void variants — empty parens or just the name
182
+ dot tx ... 'Root()' ...
183
+ dot tx ... 'Root' ...
184
+
185
+ # JSON inside parens for struct values
186
+ dot tx ... 'AccountId32({"id":"0xd435..."})' ...
187
+ ```
188
+
189
+ Variant matching is case-insensitive (`system` resolves to `system`, `authorized` to `Authorized`). All existing formats (JSON objects, hex, SS58 addresses) continue to work unchanged.
190
+
156
191
  #### Encode call data
157
192
 
158
193
  Encode a call to hex without signing or submitting. Useful for preparing calls to pass to `Sudo.sudo`, multisig proposals, or governance. Works offline from cached metadata and does not require `--from`.
package/dist/cli.mjs CHANGED
@@ -5,7 +5,7 @@ var __require = /* @__PURE__ */ createRequire(import.meta.url);
5
5
  // src/cli.ts
6
6
  import cac from "cac";
7
7
  // package.json
8
- var version = "0.8.1";
8
+ var version = "0.9.0";
9
9
 
10
10
  // src/config/accounts-store.ts
11
11
  import { access as access2, mkdir as mkdir2, readFile as readFile2, writeFile as writeFile2 } from "node:fs/promises";
@@ -212,6 +212,7 @@ async function resolveAccountSigner(name) {
212
212
  }
213
213
 
214
214
  // src/core/output.ts
215
+ import { Binary } from "polkadot-api";
215
216
  var isTTY = process.stdout.isTTY ?? false;
216
217
  var RESET = isTTY ? "\x1B[0m" : "";
217
218
  var CYAN = isTTY ? "\x1B[36m" : "";
@@ -224,6 +225,10 @@ var BOLD = isTTY ? "\x1B[1m" : "";
224
225
  function replacer(_key, value) {
225
226
  if (typeof value === "bigint")
226
227
  return value.toString();
228
+ if (value instanceof Binary) {
229
+ const text = value.asText();
230
+ return text.includes("�") ? value.asHex() : text;
231
+ }
227
232
  if (value instanceof Uint8Array)
228
233
  return `0x${Buffer.from(value).toString("hex")}`;
229
234
  return value;
@@ -1201,7 +1206,7 @@ function parseValue(arg) {
1201
1206
 
1202
1207
  // src/commands/tx.ts
1203
1208
  import { getViewBuilder } from "@polkadot-api/view-builder";
1204
- import { Binary } from "polkadot-api";
1209
+ import { Binary as Binary2 } from "polkadot-api";
1205
1210
 
1206
1211
  // src/core/explorers.ts
1207
1212
  var pjsAppsLink = (rpc, hash) => `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(rpc)}#/explorer/query/${hash}`;
@@ -1273,8 +1278,8 @@ function normalizeValue(lookup, entry, value) {
1273
1278
  }
1274
1279
  if (innerResolved.type === "primitive" && innerResolved.value === "u8" && typeof value === "string") {
1275
1280
  if (/^0x[0-9a-fA-F]*$/.test(value))
1276
- return Binary.fromHex(value);
1277
- return Binary.fromText(value);
1281
+ return Binary2.fromHex(value);
1282
+ return Binary2.fromText(value);
1278
1283
  }
1279
1284
  if (Array.isArray(value)) {
1280
1285
  const innerEntry = resolved.value;
@@ -1329,7 +1334,20 @@ function normalizeValue(lookup, entry, value) {
1329
1334
  return value;
1330
1335
  }
1331
1336
  }
1337
+ function parseEnumShorthand(arg) {
1338
+ if (arg.startsWith("{") || arg.startsWith("[") || arg.startsWith("0x"))
1339
+ return null;
1340
+ const firstParen = arg.indexOf("(");
1341
+ if (firstParen === -1 || !arg.endsWith(")"))
1342
+ return null;
1343
+ const variant = arg.slice(0, firstParen);
1344
+ if (!/^[a-zA-Z_]\w*$/.test(variant))
1345
+ return null;
1346
+ return { variant, inner: arg.slice(firstParen + 1, -1) };
1347
+ }
1332
1348
  function parseTypedArg(meta, entry, arg) {
1349
+ if (entry.type === "lookupEntry")
1350
+ return parseTypedArg(meta, entry.value, arg);
1333
1351
  switch (entry.type) {
1334
1352
  case "primitive":
1335
1353
  return parsePrimitive(entry.value, arg);
@@ -1347,7 +1365,7 @@ function parseTypedArg(meta, entry, arg) {
1347
1365
  case "enum": {
1348
1366
  if (/^0x[0-9a-fA-F]+$/.test(arg) && meta.lookup.call != null && entry.id === meta.lookup.call) {
1349
1367
  const callCodec = meta.builder.buildDefinition(meta.lookup.call);
1350
- return callCodec.dec(Binary.fromHex(arg).asBytes());
1368
+ return callCodec.dec(Binary2.fromHex(arg).asBytes());
1351
1369
  }
1352
1370
  if (arg.startsWith("{")) {
1353
1371
  try {
@@ -1355,6 +1373,19 @@ function parseTypedArg(meta, entry, arg) {
1355
1373
  } catch {}
1356
1374
  }
1357
1375
  const variants = Object.keys(entry.value);
1376
+ const shorthand = parseEnumShorthand(arg);
1377
+ if (shorthand) {
1378
+ const matched2 = variants.find((v) => v.toLowerCase() === shorthand.variant.toLowerCase());
1379
+ if (matched2) {
1380
+ const variantDef = entry.value[matched2];
1381
+ const resolvedDef = variantDef.type === "lookupEntry" ? variantDef.value : variantDef;
1382
+ if (resolvedDef.type === "void" || shorthand.inner === "") {
1383
+ return { type: matched2 };
1384
+ }
1385
+ const innerValue = parseTypedArg(meta, variantDef, shorthand.inner);
1386
+ return normalizeValue(meta.lookup, entry, { type: matched2, value: innerValue });
1387
+ }
1388
+ }
1358
1389
  if (variants.includes("Id")) {
1359
1390
  const idVariant = entry.value.Id;
1360
1391
  const innerType = idVariant.type === "lookupEntry" ? idVariant.value : idVariant;
@@ -1376,8 +1407,8 @@ function parseTypedArg(meta, entry, arg) {
1376
1407
  const inner = entry.value;
1377
1408
  if (inner.type === "primitive" && inner.value === "u8") {
1378
1409
  if (/^0x[0-9a-fA-F]*$/.test(arg))
1379
- return Binary.fromHex(arg);
1380
- return Binary.fromText(arg);
1410
+ return Binary2.fromHex(arg);
1411
+ return Binary2.fromText(arg);
1381
1412
  }
1382
1413
  if (arg.startsWith("[")) {
1383
1414
  try {
@@ -1385,7 +1416,7 @@ function parseTypedArg(meta, entry, arg) {
1385
1416
  } catch {}
1386
1417
  }
1387
1418
  if (/^0x[0-9a-fA-F]*$/.test(arg))
1388
- return Binary.fromHex(arg);
1419
+ return Binary2.fromHex(arg);
1389
1420
  return parseValue(arg);
1390
1421
  }
1391
1422
  case "struct":
@@ -1551,7 +1582,7 @@ function parseStorageKeys(meta, palletName, storageItem, args) {
1551
1582
 
1552
1583
  // src/commands/tx.ts
1553
1584
  import { getViewBuilder as getViewBuilder2 } from "@polkadot-api/view-builder";
1554
- import { Binary as Binary2 } from "polkadot-api";
1585
+ import { Binary as Binary3 } from "polkadot-api";
1555
1586
  function registerTxCommand(cli) {
1556
1587
  cli.command("tx [target] [...args]", "Submit an extrinsic (e.g. Balances.transferKeepAlive <dest> <amount>)").option("--from <name>", "Account to sign with (required)").option("--dry-run", "Estimate fees without submitting").option("--encode", "Encode call to hex without signing or submitting").option("--ext <json>", `Custom signed extension values as JSON, e.g. '{"ExtName":{"value":...}}'`).action(async (target, args, opts) => {
1557
1588
  if (!target) {
@@ -1616,7 +1647,7 @@ function registerTxCommand(cli) {
1616
1647
  throw new Error(`Extra arguments are not allowed when submitting a raw call hex.
1617
1648
  ` + "Usage: dot tx 0x<call_hex> --from <account>");
1618
1649
  }
1619
- const callBinary = Binary2.fromHex(target);
1650
+ const callBinary = Binary3.fromHex(target);
1620
1651
  tx = await unsafeApi.txFromCallData(callBinary);
1621
1652
  callHex = target;
1622
1653
  } else {
@@ -1637,7 +1668,7 @@ function registerTxCommand(cli) {
1637
1668
  const { codec, location } = meta.builder.buildCall(palletInfo.name, callInfo.name);
1638
1669
  const encodedArgs = codec.enc(callData);
1639
1670
  const fullCall = new Uint8Array([location[0], location[1], ...encodedArgs]);
1640
- console.log(Binary2.fromBytes(fullCall).asHex());
1671
+ console.log(Binary3.fromBytes(fullCall).asHex());
1641
1672
  return;
1642
1673
  }
1643
1674
  tx = unsafeApi.tx[palletInfo.name][callInfo.name](callData);
@@ -1820,6 +1851,10 @@ function formatEventValue(v) {
1820
1851
  return v.toString();
1821
1852
  if (v === null || v === undefined)
1822
1853
  return "null";
1854
+ if (v instanceof Binary3) {
1855
+ const text = v.asText();
1856
+ return text.includes("�") ? v.asHex() : text;
1857
+ }
1823
1858
  return JSON.stringify(v, (_k, val) => typeof val === "bigint" ? val.toString() : val);
1824
1859
  }
1825
1860
  function parseCallArgs(meta, palletName, callName, args) {
@@ -1927,8 +1962,8 @@ function normalizeValue2(lookup, entry, value) {
1927
1962
  }
1928
1963
  if (innerResolved.type === "primitive" && innerResolved.value === "u8" && typeof value === "string") {
1929
1964
  if (/^0x[0-9a-fA-F]*$/.test(value))
1930
- return Binary2.fromHex(value);
1931
- return Binary2.fromText(value);
1965
+ return Binary3.fromHex(value);
1966
+ return Binary3.fromText(value);
1932
1967
  }
1933
1968
  if (Array.isArray(value)) {
1934
1969
  const innerEntry = resolved.value;
@@ -1983,7 +2018,20 @@ function normalizeValue2(lookup, entry, value) {
1983
2018
  return value;
1984
2019
  }
1985
2020
  }
2021
+ function parseEnumShorthand2(arg) {
2022
+ if (arg.startsWith("{") || arg.startsWith("[") || arg.startsWith("0x"))
2023
+ return null;
2024
+ const firstParen = arg.indexOf("(");
2025
+ if (firstParen === -1 || !arg.endsWith(")"))
2026
+ return null;
2027
+ const variant = arg.slice(0, firstParen);
2028
+ if (!/^[a-zA-Z_]\w*$/.test(variant))
2029
+ return null;
2030
+ return { variant, inner: arg.slice(firstParen + 1, -1) };
2031
+ }
1986
2032
  function parseTypedArg2(meta, entry, arg) {
2033
+ if (entry.type === "lookupEntry")
2034
+ return parseTypedArg2(meta, entry.value, arg);
1987
2035
  switch (entry.type) {
1988
2036
  case "primitive":
1989
2037
  return parsePrimitive2(entry.value, arg);
@@ -2001,7 +2049,7 @@ function parseTypedArg2(meta, entry, arg) {
2001
2049
  case "enum": {
2002
2050
  if (/^0x[0-9a-fA-F]+$/.test(arg) && meta.lookup.call != null && entry.id === meta.lookup.call) {
2003
2051
  const callCodec = meta.builder.buildDefinition(meta.lookup.call);
2004
- return callCodec.dec(Binary2.fromHex(arg).asBytes());
2052
+ return callCodec.dec(Binary3.fromHex(arg).asBytes());
2005
2053
  }
2006
2054
  if (arg.startsWith("{")) {
2007
2055
  try {
@@ -2009,6 +2057,19 @@ function parseTypedArg2(meta, entry, arg) {
2009
2057
  } catch {}
2010
2058
  }
2011
2059
  const variants = Object.keys(entry.value);
2060
+ const shorthand = parseEnumShorthand2(arg);
2061
+ if (shorthand) {
2062
+ const matched2 = variants.find((v) => v.toLowerCase() === shorthand.variant.toLowerCase());
2063
+ if (matched2) {
2064
+ const variantDef = entry.value[matched2];
2065
+ const resolvedDef = variantDef.type === "lookupEntry" ? variantDef.value : variantDef;
2066
+ if (resolvedDef.type === "void" || shorthand.inner === "") {
2067
+ return { type: matched2 };
2068
+ }
2069
+ const innerValue = parseTypedArg2(meta, variantDef, shorthand.inner);
2070
+ return normalizeValue2(meta.lookup, entry, { type: matched2, value: innerValue });
2071
+ }
2072
+ }
2012
2073
  if (variants.includes("Id")) {
2013
2074
  const idVariant = entry.value.Id;
2014
2075
  const innerType = idVariant.type === "lookupEntry" ? idVariant.value : idVariant;
@@ -2030,8 +2091,8 @@ function parseTypedArg2(meta, entry, arg) {
2030
2091
  const inner = entry.value;
2031
2092
  if (inner.type === "primitive" && inner.value === "u8") {
2032
2093
  if (/^0x[0-9a-fA-F]*$/.test(arg))
2033
- return Binary2.fromHex(arg);
2034
- return Binary2.fromText(arg);
2094
+ return Binary3.fromHex(arg);
2095
+ return Binary3.fromText(arg);
2035
2096
  }
2036
2097
  if (arg.startsWith("[")) {
2037
2098
  try {
@@ -2039,7 +2100,7 @@ function parseTypedArg2(meta, entry, arg) {
2039
2100
  } catch {}
2040
2101
  }
2041
2102
  if (/^0x[0-9a-fA-F]*$/.test(arg))
2042
- return Binary2.fromHex(arg);
2103
+ return Binary3.fromHex(arg);
2043
2104
  return parseValue(arg);
2044
2105
  }
2045
2106
  case "struct":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polkadot-cli",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "CLI tool for querying Polkadot-ecosystem on-chain state",
5
5
  "type": "module",
6
6
  "bin": {