Skip to content

Instantly share code, notes, and snippets.

@cordt-sei
Created July 26, 2024 15:59
Show Gist options
  • Save cordt-sei/76eaeec94f1c866a60cacd0654f208b2 to your computer and use it in GitHub Desktop.
Save cordt-sei/76eaeec94f1c866a60cacd0654f208b2 to your computer and use it in GitHub Desktop.

How to Compile Protobuf Files and Integrate Sei-js

This guide will walk you through compiling Protobuf (.proto) files into TypeScript code for use in Cosmos SDK-based projects, using either Telescope or Sei-js.

Using Telescope

Telescope is a TypeScript transpiler designed for Cosmos Protobuf files, converting them into TypeScript with typed interfaces and helper functions.

Steps:

  1. Install Telescope and Dependencies: Install Telescope globally along with create-cosmos-app:

    npm install -g @cosmology/telescope create-cosmos-app
  2. Create a New Project: Use the create-cosmos-app command to set up a new project:

    cca --boilerplate telescope
  3. Add Protobuf Files: Place your .proto files in the ./proto directory.

  4. Generate TypeScript Code: Run the code generation command to transpile the Protobuf files:

    yarn codegen
  5. Build the Project: Build the project after generating TypeScript files:

    yarn build

Telescope offers various options, such as Amino encoding and custom RPC clients. Modify the configuration as needed to suit your project requirements【17†source】【18†source】【19†source】.

Using Sei-js

Sei-js is a JavaScript SDK that includes precompiled Protobufs and simplifies interaction with the Sei blockchain.

Steps:

  1. Clone the Sei-js Repository: Clone the repository to your local machine:

    git clone https://github.com/Crownfi/sei-js.git
  2. Install Dependencies: Navigate to the cloned directory and install the necessary packages:

    cd sei-js
    yarn install
  3. Using Sei-js: Sei-js provides precompiled Protobufs and utility functions, making it easier to interact with the Sei blockchain. You can directly use these functions in your project to handle transactions, queries, and more.

Example Usage

Here's an example of how you might use Sei-js to interact with the Sei blockchain:

const { MsgSend } = require('sei-js/proto/seiprotocol/seichain/evm/tx');
const { SigningStargateClient } = require('@cosmjs/stargate');
const { DirectSecp256k1HdWallet } = require('@cosmjs/proto-signing');

async function sendTransaction() {
  const wallet = await DirectSecp256k1HdWallet.fromMnemonic("your mnemonic");
  const client = await SigningStargateClient.connectWithSigner(
    "https://rpc.sei-chain.com",
    wallet
  );

  const msg = MsgSend.create({
    fromAddress: "sei1...",
    toAddress: "sei1...",
    amount: [{ denom: "usei", amount: "1000" }]
  });

  const fee = {
    amount: [{ denom: "usei", amount: "500" }],
    gas: "200000",
  };

  const result = await client.signAndBroadcast("sei1...", [msg], fee);
  console.log(result);
}

sendTransaction();

Choosing Between Telescope and Sei-js

  • Telescope: Offers detailed customization and is suitable for projects requiring specific control over generated code.
  • Sei-js: Provides a quicker setup with precompiled resources, ideal for projects focusing on the Sei blockchain.

Both tools streamline the process of integrating Protobuf files into your TypeScript projects, each catering to different project needs and complexity levels.

Feel free to choose the tool that best fits your project's requirements and development workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment