Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

chore: generate simpler lookups from ss58 registry #256

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions _tasks/build_known.ts

This file was deleted.

75 changes: 75 additions & 0 deletions _tasks/build_known_lookups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { constantCase, snakeCase, upperCase } from "../deps/case.ts";
import ss58Registry from "../deps/ss58_registry.ts";
import * as path from "../deps/std/path.ts";

type LookupValue = {
displayName: string;
prefix: number;
symbols: string[];
decimals: number[];
stdAccount: string;
website: string;
};
type Lookups = { [K in keyof LookupValue]: Record<string, LookupValue[K]> };
const lookups: Lookups = {
displayName: {},
prefix: {},
symbols: {},
decimals: {},
stdAccount: {},
website: {},
};
for (const e of ss58Registry) {
if (!(e.network.startsWith("reserved"))) {
const key = snakeCase(e.network);
lookups.displayName[key] = upperCase(key);
lookups.prefix[key] = e.prefix;
if (e.symbols.length) {
lookups.symbols[key] = e.symbols;
}
if (e.decimals.length) {
lookups.decimals[key] = e.decimals;
}
if (e.standardAccount) {
lookups.stdAccount[key] = e.standardAccount;
}
if (e.website) {
lookups.website[key] = e.website;
}
}
}
let generated = "";
concatLookup("displayName", "string", quote);
concatLookup("prefix", "number", (value) => {
return `${value}`;
});
concatLookup("symbols", "string[]", (value) => {
return `["${value.join(`", "`)}"]`;
});
concatLookup("decimals", "number[]", (value) => {
return `[${value.join(", ")}]`;
});
concatLookup("stdAccount", `"*25519" | "Sr25519" | "Ed25519" | "secp256k1"`, quote);
concatLookup("website", "string", quote);

const final = `// @generated file from build script, do not edit\n${generated}`;
const dest = path.join(Deno.cwd(), `known/lookups.ts`);
console.log(`Writing "${dest}".`);
await Deno.writeTextFile(dest, final);

function concatLookup<LookupKey extends keyof Lookups>(
lookupKey: LookupKey,
constraint: string,
printValue: (value: Lookups[LookupKey][keyof Lookups[LookupKey]]) => string,
) {
generated += `\nexport const ${constantCase(lookupKey)}: Record<string, ${constraint}> = {`;
const lookup = lookups[lookupKey];
for (const networkKey in lookup) {
generated += `\n ${networkKey}: ${printValue(lookup[networkKey])},`;
}
generated += "\n};\n";
}

function quote(value: string): string {
return `"${value}"`;
}
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"run:browser": "deno task run _tasks/run_browser.ts",
"debug": "deno task run --inspect-brk",
"download:frame_metadata": "deno task run _tasks/download_frame_metadata.ts",
"build:known": "deno task run _tasks/build_known.ts",
"build:known_lookups": "deno task run _tasks/build_known_lookups.ts",
"udd": "deno task star && deno task run https://deno.land/x/[email protected]/main.ts _star.ts",
"dnt": "deno task run _tasks/dnt.ts",
"star": "deno task run _tasks/star.ts && deno cache --no-check=remote _star.ts",
Expand Down
Loading