Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending GeneratedType with MessageFns in Ignite v28 Blog Tutorial #1617

Open
Takuro1000 opened this issue Jan 4, 2025 · 0 comments
Open

Comments

@Takuro1000
Copy link

Issue: Add MessageFns to generateType

In the blog network tutorial created with Ignite v28, I successfully made a post from the frontend.
The steps are outlined below, but would it be acceptable to add the MessageFns generated by buf to the generateType of the @cosmjs/proto-signing module?


Environment

  • Ignite v28.6.0
  • Cosmos SDK v0.50.9

Steps Taken

  1. Created the v28 blog tutorial chain
    Blog Tutorial
  2. Generated proto files using buf generate with buf.gen.ts.yaml.
  3. Imported proto/blog/blog.tx.ts into the frontend.
  4. Created frontend/registry.ts.
  5. Added MessageFns to the GeneratedType definition in proto-signing/build/registry.d.ts.
  6. Mapped msgType to the Registry in frontend/registry.ts.

node_modules/@cosmjs/proto-signing/build/registry.d.ts

//.. Omitted above
export type GeneratedType = TelescopeGeneratedType | TsProtoGeneratedType | PbjsGeneratedType | MessageFns;
//Omitted below ..

frontend/registry.ts

import type { GeneratedType } from "@cosmjs/proto-signing";
import {
  MsgUpdatePost,
  MsgCreatePost,
  MsgDeletePost,
} from "./proto/blog/blog/tx";

//NOTE: Since GeneratedType has been edited, please review @cosmjs/proto-signing/build/registry.d.ts.
const msgTypes = [] as Array<[string, GeneratedType]>;
msgTypes.push(
  ["/blog.blog.MsgUpdatePost", MsgUpdatePost],
  ["/blog.blog.MsgCreatePost", MsgCreatePost],
  ["/blog.blog.MsgDeletePost", MsgDeletePost],
);

export { msgTypes };

frontend/main.ts

import { SigningStargateClient } from "@cosmjs/stargate";
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import type { StdFee } from "@cosmjs/stargate";
import type { EncodeObject } from "@cosmjs/proto-signing";
import { MsgCreatePost } from "$lib/proto/blog/blog/tx";
import { msgTypes } from "$lib/registry";

const rpcUrl = "http://0.0.0.0:26657";
const aliceMnemonic = "mnemonic phrase";
const defaultFee: StdFee = {
  amount: [],
  gas: "200000",
};
const memo = "test memo";
const registry = new Registry(msgTypes);

type msgCreatePostParams = {
  value: MsgCreatePost;
};

async function semdCreatePostTx() {
  const aliceWallet = await DirectSecp256k1HdWallet.fromMnemonic(aliceMnemonic);
  const { address } = (await aliceWallet.getAccounts())[0];
  const value: MsgCreatePost = {
    title: "test title",
    body: "test body",
    creator: address,
  };

  const client = await SigningStargateClient.connectWithSigner(
    rpcUrl,
    aliceWallet,
    { registry },
  );
  let msg = msgCreatePost({ value: MsgCreatePost.fromPartial(value) });
  return await client.signAndBroadcast(address, [msg], defaultFee, memo);
}

function msgCreatePost({ value }: msgCreatePostParams): EncodeObject {
  try {
    return {
      typeUrl: "/blog.blog.MsgCreatePost",
      value: MsgCreatePost.fromPartial(value),
    };
  } catch (e: any) {
    throw new Error(
      "TxClient:MsgCreatePost: Could not create message: " + e.message,
    );
  }
}

Notes

The above code has been refactored for the purpose of this issue.
Since the frontend is created with Svelte, if the repository prior to refactoring is needed, I will attach it separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant