From f1099feb65f84a5e6150f82d49a65d8f9abe86b4 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:31:33 +0200 Subject: [PATCH] Build msgRegistry with codecs and related msg data --- lib/msg.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/msg.ts diff --git a/lib/msg.ts b/lib/msg.ts new file mode 100644 index 0000000..2a0e8fa --- /dev/null +++ b/lib/msg.ts @@ -0,0 +1,44 @@ +import { txCosmJsTypes } from "@/types/cosmjs-types"; +import { createWasmAminoConverters } from "@cosmjs/cosmwasm-stargate"; +import { createDefaultAminoConverters } from "@cosmjs/stargate"; + +export const aminoConverters = { + ...createDefaultAminoConverters(), + ...createWasmAminoConverters(), +}; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const msgRegistry: Record = {}; + +const codecs = txCosmJsTypes + .filter( + (type) => + typeof type === "object" && + "typeUrl" in type && + "encode" in type && + "decode" in type && + "fromJSON" in type && + "toJSON" in type && + "fromPartial" in type, + ) + .filter((type) => Object.keys(aminoConverters).includes(type.typeUrl)); + +for (const codec of codecs) { + const splitTypeUrl = codec.typeUrl.split("."); + const name = splitTypeUrl[splitTypeUrl.length - 1]; + const category = splitTypeUrl[0] === "/cosmos" ? splitTypeUrl[1] : splitTypeUrl[0].slice(1); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const emptyMsg = (codec as any).fromPartial({}); + + msgRegistry[codec.typeUrl] = { + typeUrl: codec.typeUrl, + category, + name, + fields: Object.keys(emptyMsg), + emptyMsg, + codec, + }; +} + +export const getMsgRegistry = () => msgRegistry;