Transactions with SDKs 🛠️

CosmJS

CosmJS is a JavaScript/TypeScript library for Cosmos SDK. This library is a powerful tool that supports most of the functionalities of Cosmos SDK. We will explore how to use CosmJS to create and broadcast a BankSend transaction.

Install CosmJS library

pnpm install @cosmjs/proto-signing
pnpm install @cosmjs/stargate
pnpm install cosmjs-types

Create a transaction

send_token.mjs Create a file and write the following code:

import {encodePubkey, Registry, makeAuthInfoBytes} from "@cosmjs/proto-signing"
import { StargateClient } from "@cosmjs/stargate"
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx.js";

//------------------------------------------------
// Please fill in the information needed to send a transaction.
const sender = "";
const recipient = "";
const denom = ""
const amount = "";
const endpoints = "";
//------------------------------------------------

sendToken(sender, recipient, denom, amount, endpoints);

// ---- implementation ----
async function sendToken(sender, recipient, denom, amount, rpcAddress) {
    const signingClient = await StargateClient.connect(rpcAddress);
    const registry = new Registry();
    const account = await signingClient.getAccount(sender);

    const pubkey = encodePubkey({
        type: "tendermint/PubKeySecp256k1",
        value: account.pubkey.value,
    });

    const txBodyFields = {
        typeUrl: "/cosmos.tx.v1beta1.TxBody",
        value: {
            messages: [
                {
                    typeUrl: "/cosmos.bank.v1beta1.MsgSend",
                    value: {
                        fromAddress: sender,
                        toAddress: recipient,
                        amount: [{
                            amount,
                            denom
                        }],
                    },
                },
            ],
        },
    };

    const feeAmount = [
        {
            amount: "1000000",
            denom,
        },
    ];

    const txBodyBytes = registry.encode(txBodyFields);

    const gasLimit = 200000;
    const feeGranter = undefined;
    const feePayer = undefined;
    const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence: 0 }], feeAmount, gasLimit, feeGranter, feePayer);

    const txRaw = TxRaw.fromPartial({
        bodyBytes: txBodyBytes,
        authInfoBytes: authInfoBytes,
        signatures: [],
    });
    const txRawBytes = Uint8Array.from(TxRaw.encode(txRaw).finish());

    const result = await signingClient.broadcastTx(txRawBytes,);

    console.log(result)

}

Send a transaction

Execute the following command to send the transaction.

node sendToken.mjs

Propose Block

If you sent the transaction successfully, you should see the transaction in the Mempool tab. The next step is to propose the block. You can do so by clicking on the "Propose Block" button in the Propose Block section.

The proposed block will include all the transactions in the mempool, and the next blockchain state will be computed based on the transactions.

The result of the transaction will be shown in the Transactions tab.