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.
Telescope is a TypeScript transpiler designed for Cosmos Protobuf files, converting them into TypeScript with typed interfaces and helper functions.
-
Install Telescope and Dependencies: Install Telescope globally along with
create-cosmos-app:npm install -g @cosmology/telescope create-cosmos-app
-
Create a New Project: Use the
create-cosmos-appcommand to set up a new project:cca --boilerplate telescope
-
Add Protobuf Files: Place your
.protofiles in the./protodirectory. -
Generate TypeScript Code: Run the code generation command to transpile the Protobuf files:
yarn codegen
-
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】.
Sei-js is a JavaScript SDK that includes precompiled Protobufs and simplifies interaction with the Sei blockchain.
-
Clone the Sei-js Repository: Clone the repository to your local machine:
git clone https://github.com/Crownfi/sei-js.git
-
Install Dependencies: Navigate to the cloned directory and install the necessary packages:
cd sei-js yarn install -
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.
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();- 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.