This article focuses on how to use CosmoWasmJS for connectiong to OKBC nodes and interacting with smart contracts. CosmWasmJS is an SDK that facilitates contract development for DApp developers. Combined with CosmJS, you can easily and conveniently develop OKBCWasm smart contracts.

CosmWasmJS + CosmJS is equivalent to ETH's web3.js.

ComWasmJsA JS SDK for smart contract interactions provided by CosmWasm. ComWasmJs is developed based on ComJs.
ComJsThe JS SDK that Cosmos SDK provides to interact with nodes.

Note: If you want a more detailed explanation of CosmWasmJS and wasm smart contract interactions, please refer to CosmWasmJS


Need to install Node while using CosmoWasmJS

#Choice of network

You can choose between testnet and local testnet.

Cosmwasm on mainnet is coming soon. Use testnet instead for now.


There is no need to build nodes on testnet, and the development of OKBCWasm contracts can directly access the RPC node services provided by OKBC. If you need to build your own testnet node, please refer to testnet node set up.

Below is the URL of the testnet RPC node

const rpcEndpoint = "https://okbtestrpc.okbchain.org"

#Local testnet

Download the OKBC source code and set up the OKBC local testnet through the script we provide

git clone https://github.com/okx/okbchain.git
cd okbchain/dev
sh ./wasm-test.sh 

Below is the URL of the testnet RPC node

const rpcEndpoint = "https://localhost:26657"

#Install Node.js


#Create project

Note: everything in this subsection uses local testnet

Create project directory

# use an empty directory
mkdir cosmwasmjs && cd cosmwasmjs

Install the dependencies

# install the dependencies
npm install cosmwasm
npm install @cosmjs/crypto

Create a code file

# create an emtpy file to write code
touch client.mjs

#Compile JS code

Note: everything in this subsection uses local testnet

Copy the following codes into client.mjs

import { SigningCosmWasmClient, Secp256k1HdWallet, coin, parseCoins } from "cosmwasm";
import { stringToPath } from "@cosmjs/crypto";

// This is your rpc endpoint
// If you choice mainnet ,please use "https://okbrpc.okbchain.org"
// If you choice testnet ,please use "https://okbtestrpc.okbchain.org"
const rpcEndpoint = "http://localhost:26657";

// Using mnemonic
// You must change mnemonic if you use your mnemonic
const mnemonic = "palace cube bitter light woman side pave cereal donor bronze twice work";

async function main() {
// Create a wallet
const path = stringToPath("m/44'/118'/0'/0/0");
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, {hdPaths:[path], "prefix":"ex"});
const accs = await wallet.getAccounts();

    // Create Wasm Client
    const client = await SigningCosmWasmClient.connectWithSigner(
    const balance = await client.getBalance(accs[0].address,'okb')
    console.log("SigningCosmWasmClient CONNECTION Success")
    // Query Wasm all code 
    const codes = await client.getCodes();
    console.log("Query all Wasm code:")


#Run code

Note: everything in this subsection uses local testnet

Next, run code

node client.mjs

#Contract interaction interface

The above only shows how to connect OKBC nodes through CosmWasmJS, the actions for querying account balances and all wasm contract code operations. If you want to conduct other smart contract actions, you can refer to the interfaces in the table below for uploading contract codes, deploying contracts and interacting with contracts.


CosmWasmClient : this client is only used for querying

SigningCosmWasmClient : this client is used for sending transactions

OrderSdk interface nameDescriptionOKBC supportHost ClientRemarks
1getChainIdQuery chain IDYCosmWasmClient
2getHeightQuery heightYCosmWasmClient
3getAccountQuery accountYCosmWasmClient
4getSequenceQuery account sequence numberYCosmWasmClient
5getBlockQuery blockYCosmWasmClient
6getBalanceQuery balanceYCosmWasmClient
7getTxQuery transactionYCosmWasmClient
8searchTxSearch transaction by tagYCosmWasmClient
10broadcastTxBroadcast transactionYCosmWasmClient
11getCodesQuery all contract codes of wasmYCosmWasmClient
12getCodeDetailsQuery contract code informationYCosmWasmClient
13getContractsQuery all contracts with codeIdYCosmWasmClient
14getContractQuery contractYCosmWasmClient
15getContractCodeHistoryQuery contract upgrade situationYCosmWasmClient
16queryContractRawQuery raw format contract status data with keyYCosmWasmClient
17queryContractSmartQuery contract status dataYCosmWasmClient
18connectWithSignerCreate client and connect to nodeYSigningCosmWasmClient
19offlineCreate offline clientYSigningCosmWasmClient
20uploadUpload contract codeYSigningCosmWasmClient
21instantiateDeploy contractYSigningCosmWasmClient
22updateAdminUpdate contract admin accountYSigningCosmWasmClient
23clearAdminClear contract admin accountYSigningCosmWasmClient
24migrateUpgrade contractYSigningCosmWasmClient
25executeCall contractYSigningCosmWasmClient
26sendTokensSend transactionYSigningCosmWasmClient
27delegateTokens-NSigningCosmWasmClientOKBC does not support wasm call staking
28undelegateTokens-NSigningCosmWasmClientOKBC does not support wasm call staking
29withdrawRewards-NSigningCosmWasmClientOKBC does not support wasm call staking
30signAndBroadcastSign and broadcast transactionYSigningCosmWasmClient
31signSign transactionYSigningCosmWasmClient


We provide a Webpack demo example. You can follow this example to interact with OKBC through your browser and Keplr wallet using CosmWasmJS and Webpack. And we also provide another demo example. You can create evm compatible address with oks.js.