ignite / cli

Ignite is a CLI tool and hub designed for constructing Proof of Stake Blockchains rooted in Cosmos-SDK
https://ignite.com
Other
1.25k stars 549 forks source link

Ignite TypeScript client code generation #2765

Closed fadeev closed 2 years ago

fadeev commented 2 years ago

From the very beginning, the frontend component has been an important part of Ignite CLI.

Currently, Ignite's frontend is composed of two parts:

The relationship between blockchain-related functionality and the frontend is complementary rather than a dependency. It would be a mistake to say that Ignite CLI somehow depends on Vue, for example.

Code Generation

Code generation can be performed in an Ignite scaffolded chain or any other Cosmos SDK blockchain. It might require tweaking if your chain doesn't follow the patterns common to other SDK chains especially when it comes to structuring in proto. Eventually it should work with any chain and it would be cool to have automated tests that clone chains and gen code for them.

Code generation is performed with ignite generate vuex. Even though it says "vuex" it generates both a TS client as well as a Vue client (there is an issue to create a command that only generates a TS client: https://github.com/ignite/cli/issues/2758). By default, it's generated in vue/src/store/generated.

Let's see what it generates using a simple example that you can recreate using these commands:

ignite scaffold chain blog
cd blog
ignite scaffold list post title

In the generated directory we have directory namespacing based on proto package names:

blog/
  blog.blog/ <-- custom module
cosmos/
  cosmos-sdk/ <-- standard Cosmos SDK modules
    cosmos.auth.v1beta1/
    cosmos.bank.v1beta1/
    cosmos.gov.v1beta1/
   // ...
  ibc-go/ <-- ibc-go modules
index.ts

The reason it generated the blog.blog directory is because that's what we have as a package name in proto: package blog.blog.

Clients for each module are generated using the same template, so it doesn't matter whether it's blog/blog.blog or cosmos/cosmos-sdk/cosmos.bank.v1beta1, let's review the one for the custom blog module (I'm ignoring some files like package.json for brevity):

blog/blog.blog
  module/
    types/
    index.ts
    rest.ts
  index.ts

To understand how client generation works here are the main components:

Generating the TS client requires ts-proto, which is bundled with Ignite CLI alongside a Node.js runtime.

Advantages of client generation with Ignite:

Vue client

It's a bit outdated, it's using Vuex. There was an initiative to remove Vuex and replace it with a more lightweight client for Vue, but we are yet to reach consensus on how it should be implemented: https://github.com/ignite/cli/pull/2143 But we all agree that Vuex has to go.

This is a short overview of the code generation functionality of Ignite. This issue can be closed once this becomes no longer relevant. I just wanted something I can link to on Twitter.

ilgooz commented 2 years ago

cc @clockworkgr

fadeev commented 2 years ago

With the introduction of the updated version of codegen this has become outdated.

See the docs.