CosmWasmJS#

This article focuses on how to use CosmoWasmJS for connectiong to OKTC 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 OKTC Wasm smart contracts.

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

ProjectDescription
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

Preconditions#

Need to install Node while using CosmoWasmJS

Choice of network#

You can choose between mainnet or local testnet.

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

Mainnet#

There is no need to build nodes on the main network, and the development of OKTC wasm contracts can directly access the RPC node services provided by OKTC. If you need to build your own mainnet node, please refer to mainnet node set up.

Below is the URL of the mainnet RPC node

const rpcEndpoint = "https://exchaintmrpc.okex.org"

Install Node.js#

https://nodejs.org/en/download/

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://exchaintmrpc.okex.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(
        rpcEndpoint,
        wallet,
    );
    const balance = await client.getBalance(accs[0].address,'okt')
    console.log("------------------------------------------------------------------------------------")
    console.log("SigningCosmWasmClient CONNECTION Success")
    console.log("account:",accs[0].address);
    console.log("balance:",balance);
    
    // Query Wasm all code 
    const codes = await client.getCodes();
    console.log("------------------------------------------------------------------------------------")
    console.log("Query all Wasm code:")
    console.log(codes);
}

main();

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 OKTC 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.

Notes

CosmWasmClient : this client is only used for querying

SigningCosmWasmClient : this client is used for sending transactions

OrderSdk interface nameDescriptionOKTC supportHost ClientRemarks
1getChainIdQuery chain IDYCosmWasmClient
2getHeightQuery heightYCosmWasmClient
3getAccountQuery accountYCosmWasmClient
4getSequenceQuery account sequence numberYCosmWasmClient
5getBlockQuery blockYCosmWasmClient
6getBalanceQuery balanceYCosmWasmClient
7getTxQuery transactionYCosmWasmClient
8searchTxSearch transaction by tagYCosmWasmClient
9disconnectDisconnectYCosmWasmClient
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-NSigningCosmWasmClientOKTC does not support wasm call staking
28undelegateTokens-NSigningCosmWasmClientOKTC does not support wasm call staking
29withdrawRewards-NSigningCosmWasmClientOKTC does not support wasm call staking
30signAndBroadcastSign and broadcast transactionYSigningCosmWasmClient
31signSign transactionYSigningCosmWasmClient

Example#

We provide a Webpack demo example. You can follow this example to interact with OKTC 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.