Getting started with Cosmos JavaScript

Getting started with Cosmos JavaScript

What is CosmosJS? Cosmos JavaScript Library: A JavasSript Open Source Library for Cosmos Network, IRISnet and Kava. This library supports cosmos address generation and verification.

What is CosmosJS?

CosmosJS: A JavasSript Open Source Library for Cosmos Network, IRISnet and Kava.

This library supports cosmos address generation and verification. It enables you to create an offline signature functions of different types of transaction messages. It will eventually support all the other blockchains that are based on Tendermint in the future, such as IOV and others.

Installation

In order to fully use this library, you need to run a local or remote full node and set up its rest server, which acts as an intermediary between the front-end and the full-node

NPM

npm install @cosmostation/cosmosjs

Yarn

yarn add @cosmostation/cosmosjs

Browser Distribution

CosmosJS supports browserify.

Import

NodeJS

const cosmosjs = require("@cosmostation/cosmosjs");

Browser

script src='js/cosmosjs-bundle.js'>/script>

Usage

Cosmos: Generate Cosmos address from mnemonic

const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "cosmoshub-2";
const cosmos = cosmosjs.network(lcdUrl, chainId);

const mnemonic = "..."
cosmos.setPath("m/44'/118'/0'/0/0");
const address = cosmos.getAddress(mnemonic);
const ecpairPriv = cosmos.getECPairPriv(mnemonic);

Iris

const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "irishub";
const iris = cosmosjs.network(lcdUrl, chainId);
iris.setBech32MainPrefix("iaa");

Kava

const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "kava-2";
const kava = cosmosjs.network(lcdUrl, chainId);
kava.setBech32MainPrefix("kava");

Generate ECPairPriv value that is needed for signing signatures

const ecpairPriv = cosmos.getECPairPriv(mnemonic);

Transfer ATOM to designated address.

Make sure to input proper type, account number, and sequence of the cosmos account to generate StdSignMsg. You can get those account information on blockchain

cosmos.getAccounts(address).then(data => {
    let stdSignMsg = cosmos.NewStdMsg({
        type: "cosmos-sdk/MsgSend",
        from_address: address,
        to_address: "cosmos18vhdczjut44gpsy804crfhnd5nq003nz0nf20v",
        amountDenom: "uatom",
        amount: 100000,
        feeDenom: "uatom",
        fee: 5000,
        gas: 200000,
        memo: "",
        account_number: data.value.account_number,
        sequence: data.value.sequence
    });

    ...
})

Sign transaction by using stdSignMsg and broadcast by using /txs REST API

const signedTx = cosmos.sign(stdSignMsg, ecpairPriv);
cosmos.broadcast(signedTx).then(response => console.log(response));

Cosmostation offers LCD url(https://lcd-do-not-abuse.cosmostation.io).

API Rate Limiting: 10 requests per second

Supporting Message Types (Updating...)

cosmos-sdk/MsgSend

let stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgSend",
    from_address: address,
    to_address: "cosmos18vhdczjut44gpsy804crfhnd5nq003nz0nf20v",
    amountDenom: "uatom",
    amount: 1000000,
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgDelegate

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgDelegate",
    delegator_address: address,
    validator_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
    amountDenom: "uatom",
    amount: 1000000,
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgUndelegate

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgUndelegate",
    delegator_address: address,
    validator_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
    amountDenom: "uatom",
    amount: 1000000,
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgWithdrawDelegationReward

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgWithdrawDelegationReward",
    delegator_address: address,
    validator_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgSubmitProposal

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgSubmitProposal",
    title: "Activate the Community Pool",
    description: "Enable governance to spend funds from the community pool. Full proposal: https://ipfs.io/ipfs/QmNsVCsyRmEiep8rTQLxVNdMHm2uiZkmaSHCR6S72Y1sL1",
    initialDepositDenom: "uatom",
    initialDepositAmount: 1000000,
    proposal_type: "Text",
    proposer: address,
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgDeposit

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgDeposit",
    depositor: address,
    proposal_id: 1,
    amountDenom: "uatom",
    amount: 1000000,
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgVote

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgVote",
    voter: address,
    proposal_id: 1,
    option: "Yes",    // Yes, No, NowithVeto, Abstain
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgBeginRedelegate

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgBeginRedelegate",
    delegator_address: address,
    validator_src_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
    validator_dst_address: "cosmosvaloper1ec3p6a75mqwkv33zt543n6cnxqwun37rr5xlqv",
    amountDenom: "uatom",
    amount: 1000000,
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

cosmos-sdk/MsgModifyWithdrawAddress

stdSignMsg = cosmos.NewStdMsg({
    type: "cosmos-sdk/MsgModifyWithdrawAddress",
    delegator_address: address,
    withdraw_address: "cosmos133mtfk63fuac5e2npfgcktwufnty2536wedfal",
    feeDenom: "uatom",
    fee: 5000,
    gas: 200000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/bank/Send

stdSignMsg = iris.NewStdMsg({
    type: "irishub/bank/Send",
    inputsAddress:address,
    inputsCoinsDenom:"iris-atto",
    inputsCoinsAmount: 1000000000000000000,        // 18 decimal places
    outputsAddress:"iaa12g4vfyq65yf5cds4v5pr3jmdd4v6s40fkaaxtf",
    outputsCoinsDenom:"iris-atto",
    outputsCoinsAmount: 1000000000000000000,
    feeDenom: "iris-atto",
    fee: 400000000000000000,
    gas: 50000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/stake/MsgDelegate

stdSignMsg = iris.NewStdMsg({
    type: "irishub/stake/MsgDelegate",
    delegator_addr: address,
    validator_addr: "iva18pva3yzzzaxj7l5a9uk66a0q7lflscyw966jud",
    amountDenom: "iris-atto",
    amount: 1000000000000000000,
    feeDenom: "iris-atto",
    fee: 400000000000000000,
    gas: 50000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/stake/BeginUnbonding

stdSignMsg = iris.NewStdMsg({
    type: "irishub/stake/BeginUnbonding",
    delegator_addr: address,
    validator_addr: "iva18pva3yzzzaxj7l5a9uk66a0q7lflscyw966jud",
    amountDenom: "iris-atto",
    amount: 1000000000000000000,
    feeDenom: "iris-atto",
    fee: 400000000000000000,
    gas: 50000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/distr/MsgWithdrawDelegationReward

stdSignMsg = iris.NewStdMsg({
    type: "irishub/distr/MsgWithdrawDelegationReward",
    delegator_addr: address,
    validator_addr: "iva18pva3yzzzaxj7l5a9uk66a0q7lflscyw966jud",
    feeDenom: "iris-atto",
    fee: 600000000000000000,
    gas: 100000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/distr/MsgWithdrawDelegationRewardsAll

stdSignMsg = iris.NewStdMsg({
    type: "irishub/distr/MsgWithdrawDelegationRewardsAll",
    delegator_addr: address,
    feeDenom: "iris-atto",
    fee: 600000000000000000,
    gas: 100000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/distr/MsgModifyWithdrawAddress

stdSignMsg = iris.NewStdMsg({
    type: "irishub/distr/MsgModifyWithdrawAddress",
    delegator_addr: address,
    withdraw_addr: "iaa12g4vfyq65yf5cds4v5pr3jmdd4v6s40fkaaxtf",
    feeDenom: "iris-atto",
    fee: 400000000000000000,
    gas: 50000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

irishub/stake/BeginRedelegate

stdSignMsg = iris.NewStdMsg({
    type: "irishub/stake/BeginRedelegate",
    delegator_addr: address,
    validator_src_addr: "iva18pva3yzzzaxj7l5a9uk66a0q7lflscyw966jud",
    validator_dst_addr: "iva1msqqkd3v0gmullzwm56c4frevyczzxfeczvjru",
    shares_amount: 1000000000000000000,
    feeDenom: "iris-atto",
    fee: 600000000000000000,
    gas: 65000,
    memo: "",
    account_number: data.value.account_number,
    sequence: data.value.sequence
});

Kava has the same message types as Cosmos.

Documentation

This library is simple and easy to use. We don't have any formal documentation yet other than examples. Ask for help if our examples aren't enough to guide you

Contribution

  • Contributions, suggestions, improvements, and feature requests are always welcome

When opening a PR with a minor fix, make sure to add #trivial to the title/description of said PR.

Cosmostation's Services and Community

javascript blockchain cosmos IRISnet Kava

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

5 Blockchain Applications That Have Transformed the World of Technology

The blockchain is the decentralized database of the blocks of information, which gets recorded in the chain format and linked in a secured crypto graphical manner. This technology ensures proper safety of the data due to its secure nature, and it totally changes how people carry out transactions. It also brings about a faster and secure process of validating information needed to establish reliability.

What Is True Blockchain Interoperability?

If you are in the crypto world, then the word "blockchain interoperability" may familiar to you because there are so many projects are gaining publicity and funds from investors by using this term, and many projects like Chainlink, Cosmos, Polkadot, and Wanchain are claiming to give true blockchain interoperability. So in this article, we will discuss about these projects and try to understand what kind of blockchain interoperability they are giving and what benefits we can get because of blockchain interoperability? Before start, this article I want to tell you the meaning of blockchain interoperability in brief: Blockchain interoperability enables two blockchains to communicate with each other and to do cross-chain transactions without depending on a third party {Reference}.

What Is The Difference Between Blockchain And Bitcoin?

Our Blockchain Online Training will provide you to learn about Blockchain technology aspects with realty. Our Blockchain Course also includes live sessions, live Projects

What Makes a Blockchain Secure?

Our Blockchain Online Training will provide you to learn about Blockchain technology aspects with realty. Our Blockchain Course also includes live sessions, live Projects

How to Create a Blockchain Chat App with Javascript - Blockchain for Beginner

From beginner to blockchain. In this webinar recap, you'll learn how Proof of Stake works and then will build a simple blockchain chat app in Javascript!