Skip to main content
logo

CosmJs

Step 1: Setting up your Development Environment

Before you start, make sure you have Node.js and npm (Node Package Manager) installed on your system. You can verify the installation by running the following commands:


_2
node -v
_2
npm -v

Next, create a new Node.js project by running:


_1
npm init -y

Install the necessary dependencies:


_1
npm install @cosmjs/cosmwasm-stargate @cosmjs/proto-signing

Step 2: Connecting to Osmosis with CosmJS

In this section, we will create a script that connects to the Osmosis network using CosmJs.


_27
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_27
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
_27
import { GasPrice } from "@cosmjs/stargate";
_27
_27
const rpc = 'https://rpc-osmosis.keplr.app'; // Osmosis RPC
_27
const mnemonic = 'your mnemonic here'; // Replace with your mnemonic
_27
const prefix = 'osmo'; // Prefix for Osmosis
_27
_27
const start = async () => {
_27
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix });
_27
_27
const [firstAccount] = await wallet.getAccounts();
_27
const walletAddress = firstAccount.address;
_27
_27
const signingClient = await SigningCosmWasmClient.connectWithSigner(rpc, wallet, {
_27
gasPrice: GasPrice.fromString("0.1uosmo"),
_27
});
_27
_27
console.log(
_27
"Connected to Osmosis, chain id:",
_27
await signingClient.getChainId(),
_27
", height:",
_27
await signingClient.getHeight()
_27
);
_27
};
_27
_27
start();

Step 3: Querying Data

Now we will create a script to query smart contract data from the Osmosis network.


_24
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_24
_24
const rpc = 'https://rpc-osmosis.keplr.app'; // Osmosis RPC
_24
const contractAddress = 'osmosis-contract-address-here'; // Replace with actual contract address
_24
_24
const start = async () => {
_24
try {
_24
const client = new CosmWasmClient(rpc);
_24
_24
// Generic Contract Query - This should be adapted based on the actual contract's query methods
_24
const contractQuery = await client.queryContractSmart(contractAddress, {
_24
"custom_query": {
_24
// ... your custom query parameters here
_24
}
_24
});
_24
_24
console.log('Contract Query Result:', contractQuery);
_24
_24
} catch (err) {
_24
console.error(err);
_24
}
_24
};
_24
_24
start();

Step 4: Sign and Broadcast Transactions

Lastly, let's create a script to sign broadcast transactions to the Osmosis network.


_36
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_36
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
_36
import { GasPrice } from "@cosmjs/stargate";
_36
_36
const rpc = 'https://rpc-osmosis.keplr.app'; // Osmosis RPC
_36
const mnemonic = 'your mnemonic here'; // Replace with your mnemonic
_36
const contractAddress = 'osmosis-contract-address-here'; // Replace with actual contract address
_36
const prefix = 'osmo'; // Prefix for Osmosis
_36
_36
const start = async () => {
_36
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix });
_36
_36
const [firstAccount] = await wallet.getAccounts();
_36
const walletAddress = firstAccount.address;
_36
_36
const signingClient = await SigningCosmWasmClient.connectWithSigner(rpc, wallet, {
_36
gasPrice: GasPrice.fromString("0.1uosmo"),
_36
});
_36
_36
const msg = {
_36
// Your transaction message here
_36
};
_36
_36
const result = await signingClient.execute(
_36
walletAddress,
_36
contractAddress,
_36
msg,
_36
"auto",
_36
"",
_36
[{ denom: "uosmo", amount: "1000000" }]
_36
);
_36
_36
console.log(JSON.stringify(result));
_36
};
_36
_36
start();

Remember to replace your mnemonic here and osmosis-contract-address-here with actual values.

Step 5: IBC Transactions

In this step, we will create a script to facilitate Inter-Blockchain Communication (IBC) transactions, which allow for the transfer of tokens between different blockchains within the Cosmos ecosystem. Create a file named ibc-transaction.js and add the following generalized code:


_55
import {
_55
CosmWasmClient,
_55
SigningCosmWasmClient,
_55
} from "@cosmjs/cosmwasm-stargate";
_55
import { coins, DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
_55
import { GasPrice } from "@cosmjs/stargate";
_55
import { SigningStargateClient } from "@cosmjs/stargate";
_55
_55
const rpc = 'YOUR_RPC_URL_HERE';
_55
const mnemonic = 'YOUR_MNEMONIC_HERE';
_55
const prefix = 'YOUR_CHAIN_PREFIX_HERE';
_55
_55
const start = async () => {
_55
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
_55
prefix: prefix
_55
});
_55
_55
const [firstAccount] = await wallet.getAccounts();
_55
const walletAddress = firstAccount.address;
_55
_55
console.log('Wallet Address:', walletAddress);
_55
_55
const signingClient = await SigningStargateClient.connectWithSigner(
_55
rpc,
_55
wallet,
_55
{ gasPrice: GasPrice.fromString("0.1YOUR_GAS_TOKEN_HERE") }
_55
);
_55
_55
// Replace with the recipient's address
_55
const recipient = 'RECIPIENT_ADDRESS_HERE';
_55
// Replace with the appropriate token denomination and amount to transfer
_55
const amount = {
_55
denom: "YOUR_TOKEN_DENOMINATION_HERE",
_55
amount: "YOUR_TOKEN_AMOUNT_HERE",
_55
};
_55
_55
// The timeout is set to one hour in the future as an example, adjust as needed
_55
const timeoutTimestamp = Math.floor(Date.now() / 1000) + 60 * 60;
_55
_55
const tx = await signingClient.sendIbcTokens(
_55
walletAddress,
_55
recipient,
_55
amount,
_55
'YOUR_PORT_HERE', // Usually 'transfer', replace if different
_55
'YOUR_CHANNEL_HERE', // Replace with the channel identifier (e.g., 'channel-0')
_55
undefined, // Timeout block height, leave undefined if using timeout timestamp
_55
timeoutTimestamp,
_55
"auto", // Fee, set to "auto" to automatically calculate the fee
_55
"" // Memo, optional
_55
);
_55
_55
console.log(JSON.stringify(tx));
_55
};
_55
_55
start();