Skip to main content

CosmWasm

CosmWasm is a smart contracting platform built for the Cosmos ecosystem. Simply put, it's the Cosmos (Cosm) way of using WebAssembly (Wasm), hence the name.

More information can be found here: https://docs.cosmwasm.com/

The following guides show you how to deploy and consume your wasm contract using the client JS library.

Prerequisites

You will need to have your contract code compiled into bytecode. To do that, please go to https://docs.cosmwasm.com/ and follow the instructions.

Also, make sure you have either of the following packages installed:

npm i @shareledgerjs/client @shareledgerjs/types

or

yarn add @shareledgerjs/client @shareledgerjs/types

Once you have your bytecode in .wasm ready:

Store the contract code

import {AccessConfig, AccessType} from "@shareledgerjs/types/cosmwasm/wasm/v1/types";
import {Secp256k1HdWallet, calculateFee, ShareledgerSigningClient} from "@shareledgerjs/client";

const mnemonic = "your 12 (15, 18, 21 or 24) -words passphrase";
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic);
const [firstAccount] = await wallet.getAccounts();

const rpcEndpoint = "https://rpc-testnet.shareri.ng";
const client = await ShareledgerSigningClient.connectWithSigner(endpoint, wallet);

const wasm = fs.readFileSync("path/to/code.wasm", null)
const msg = client.wasm.storeCode(firstAccount.address, wasm, AccessConfig.fromPartial({
permission: AccessType.ACCESS_TYPE_EVERYBODY
}));
const gasLimit = 250000;
const fee = calculateFee(gasLimit, "500nshr");
const result = client.signAndBroadcast(sender, [msg], fee);
assertIsBroadcastTxSuccess(result);

Instantiate your smart contract

// see above for initializing client and wallet

const sender = firstAccount.address;
const contractAdmin = firstAccount.address;
const codeId = longify(12); // code_id returned after store bytecode successfully
const contractLabel = "Contract label";
const contractMsg = {}; // some message to instantiate the contract
const funds = [{ amount: "1000000000", denom: "nshr" }]; // optional funds for the contract

const msg = client.wasm.instantiateContract(sender, contractAdmin, codeId, contractLabel, contractMsg, funds);

const gasLimit = 250000;
const fee = calculateFee(gasLimit, "500nshr");

const result = client.signAndBroadcast(sender, [msg], fee);
assertIsBroadcastTxSuccess(result);

Execute your smart contract

Assuming that you know your contract schema (which was generated when you compiled the contract code), this is how you can execute your contract.

// see above for initializing client and wallet

const sender = firstAccount.address;
const contractAddress = "shareledger1...."; // contract address returned after you successfully instantiated the contract

// this message is to execute `mint` function on cw20-base contract. See more at https://github.com/CosmWasm/cw-plus/tree/main/contracts/cw20-base
const message = {
mint: {
to: "shareledger address",
token_id: "tokenId",
value: "amount",
msg: {}
}
};
const funds = [{ amount: "1000000000", denom: "nshr" }]; // optional funds for the contract
const res = client.wasm.executeContract(sender, contractAddress, message, funds);

const gasLimit = 250000;
const fee = calculateFee(gasLimit, "500nshr");

const result = client.signAndBroadcast(sender, [msg], fee);
assertIsBroadcastTxSuccess(result);

Query your smart contract

Similarly, based on your contract schema you can pass a message to query the contract state:

// this message is to query `balance` on cw20-base contract. See more at https://github.com/CosmWasm/cw-plus/tree/main/contracts/cw20-base
const res = await client.wasm.smartContractState(contractAddress, {
balance: {
owner: "shareledger address",
token_id: "tokenId"
}
});
console.log(res)