What is @cosmjs/tendermint-rpc?
@cosmjs/tendermint-rpc is an npm package that provides a client for interacting with Tendermint-based blockchains. It allows developers to query blockchain data, subscribe to events, and broadcast transactions using Tendermint's RPC protocol.
What are @cosmjs/tendermint-rpc's main functionalities?
Querying Blockchain Data
This feature allows you to query various pieces of information from the blockchain, such as the current status, block information, and more. The code sample demonstrates how to connect to a Tendermint node and fetch its status.
const { Tendermint34Client } = require('@cosmjs/tendermint-rpc');
async function queryBlockchain() {
const client = await Tendermint34Client.connect('http://localhost:26657');
const status = await client.status();
console.log(status);
}
queryBlockchain();
Subscribing to Events
This feature allows you to subscribe to various events happening on the blockchain, such as new blocks being created. The code sample demonstrates how to subscribe to new block events and handle them.
const { Tendermint34Client, SubscriptionEventType } = require('@cosmjs/tendermint-rpc');
async function subscribeToEvents() {
const client = await Tendermint34Client.connect('http://localhost:26657');
const subscription = client.subscribe({
query: 'tm.event = "NewBlock"'
});
subscription.addListener(SubscriptionEventType.NewBlock, (event) => {
console.log('New block event:', event);
});
}
subscribeToEvents();
Broadcasting Transactions
This feature allows you to broadcast transactions to the blockchain. The code sample demonstrates how to connect to a Tendermint node and broadcast a sample transaction.
const { Tendermint34Client } = require('@cosmjs/tendermint-rpc');
async function broadcastTransaction(tx) {
const client = await Tendermint34Client.connect('http://localhost:26657');
const result = await client.broadcastTxCommit({ tx });
console.log(result);
}
const sampleTx = new Uint8Array([0x00, 0x01, 0x02]);
broadcastTransaction(sampleTx);
Other packages similar to @cosmjs/tendermint-rpc
tendermint
The 'tendermint' npm package provides a client for interacting with Tendermint nodes. It offers similar functionalities such as querying blockchain data, subscribing to events, and broadcasting transactions. However, it may not be as actively maintained or feature-rich as @cosmjs/tendermint-rpc.
cosmos-client
The 'cosmos-client' npm package is designed for interacting with Cosmos SDK-based blockchains. It provides functionalities for querying blockchain data, managing accounts, and broadcasting transactions. While it offers similar features, it is more focused on the Cosmos SDK ecosystem compared to the more general Tendermint focus of @cosmjs/tendermint-rpc.
tendermint-rpc
The 'tendermint-rpc' npm package is another client for interacting with Tendermint nodes. It provides basic functionalities for querying blockchain data and broadcasting transactions. It is simpler and may lack some of the advanced features and optimizations found in @cosmjs/tendermint-rpc.
@cosmjs/tendermint-rpc
This package provides a type-safe wrapper around
Tendermint RPC. Notably, all binary
data is passed in and out as Uint8Array
, and this module is reponsible for the
hex/base64 encoding/decoding depending on the field and version of Tendermint.
Also handles converting numbers to and from strings.
License
This package is part of the cosmjs repository, licensed under the Apache License
2.0 (see NOTICE and
LICENSE).