Skip to main content
CosmJs Examples

CosmJs Examples

✨ Javascript example for cosmJs cosmos sdk

🛠 Install

yarn add @cosmjs/stargate @cosmjs/proto-signing

🚀 Usage

Simple connection RPC

import { StargateClient } from "@cosmjs/stargate"

try {
  const client = await StargateClient.connect('https://rpc.cosmos.directory/cosmoshub')
  const getBlock = await client.getBlock()
  console.log(getBlock.header)
  client.disconnect()
} catch (e) {
  console.log("Error! Try again")
}

Simple get signer

import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"
import { SigningStargateClient, defaultRegistryTypes } from "@cosmjs/stargate"

const mnemonic = await DirectSecp256k1HdWallet.generate(12)
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic.secret.data)
const accounts = await wallet.getAccounts()

console.log(accounts)

Simple connectWithSigner

import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"
import { SigningStargateClient, GasPrice } from "@cosmjs/stargate"

const mnemonic = await DirectSecp256k1HdWallet.generate(12)
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic.secret.data)

try {
  const client = await SigningStargateClient.connectWithSigner(
    'https://rpc.cosmos.directory/cosmoshub',
    wallet,
    {
      gasPrice: GasPrice.fromString('0.0025uatom'),
    }
  )
  console.log(client)
  client.disconnect()
} catch (e) {
  console.log("Error! Try again")
}