# Javascript & Smart Contracts

This guide describes interacting with Osmosis Smart contracts via JavaScript runtimes such as Node.js and the browser.

# Prerequisites

  • npm and node.js
  • localosmosis - (Not needed when connecting to mainnet)
  • Keplr browser not used on the guide yet

Start a new project

npm init -y
1

Install cosmwasm

npm i cosmwasm
1

Open the package.json file in a code editor and add

"type": "module",.

  {
        // ...
        "type": "module",
        // ...
    }
1
2
3
4
5
6
7

Create a new index.ts file

touch index.ts
1

The class CosmWasmClient is exported from the CosmJS package @cosmjs/cosmwasm-stargate. Learn more in the offical docs (opens new window).

import { CosmWasmClient } from "cosmwasm";

// This is your rpc endpoint
const rpcEndpoint = "http://localhost:26657/";

async function main() {
  const client = await CosmWasmClient.connect(rpcEndpoint);
  console.log(client);
}

main();
1
2
3
4
5
6
7
8
9
10
11

:: tip You can also connect to the mainnet by replacing rpcEndpoint to https://rpc.osmosis.zone/ Learn more here. (opens new window) ::

# Run it

npm index.js
1

You should see something like:

# Other methods

As documented on the official docs (opens new window).

async function moreExamples() {
  const client = await CosmWasmClient.connect(rpcEndpoint);
  const chainId = await client.getChainId()
  const getHeight = await client.getHeight()
  const getAccount = await client.getAccount("osmo1phaxpevm5wecex2jyaqty2a4v02qj7qmlmzk5a")
  const getSequence = await client.getSequence("osmo1phaxpevm5wecex2jyaqty2a4v02qj7qmlmzk5a")
  const getBlock = await client.getBlock(1)
  const getBalance = await client.getBalance("osmo1phaxpevm5wecex2jyaqty2a4v02qj7qmlmzk5a","uosmo")

  console.log(chainId);
  console.log(client);
  console.log(getHeight);
  console.log(getAccount);
  console.log(getSequence);
  console.log(getBlock);
  console.log(getBalance);
}

moreExamples();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Transactions

Method Description Params
.getTx() id: string
.searchTx() query: SearchTxQuery, filter: SearchTxFilter = {}
.txsQuery() query: string

# Codes

Method Description Params
.getCodes() none
.getCodeDetails() codeId: number

# Smart Contracts

Method Description Params
.getContracts() cideId: number
.getContract() address: string
.queryContractRaw() address: string, key: Uint8Array
.queryContractSmart() address: string, queryMsg: Record<string, unknown>

# Querying Smart Contracts

WARNING

Before we query smart contracts in localOsmosis we must deploy one by following this guide (opens new window).

Once you have deployed the smart contract you can get the <CONTRACT-ID> by running:

osmosisd query wasm list-code
1

In this particular example there are 7 contracts available. The latest one is 7.

You can now get the contract address by running

osmosisd query wasm list-contract-by-code <CONTRACT-ID>

1
2

That's the contract address osmo1zlmaky7753d2fneyhduwz0rn3u9ns8rse3tudhze8rc2g54w9ysq725p3a for code contract id 7.

# Get contract

import { CosmWasmClient } from "cosmwasm";

// This is your rpc endpoint
const rpcEndpoint = "http://localhost:26657/";

async function queryContract() {
  const client = await CosmWasmClient.connect(rpcEndpoint);
 
    const getContract = await client.getContract("osmo1zlmaky7753d2fneyhduwz0rn3u9ns8rse3tudhze8rc2g54w9ysq725p3a")
    console.log(getContract);
}

queryContract();

1
2
3
4
5
6
7
8
9
10
11
12
13
14

Run the code

node index.js
1

The output should look like this:

# Get the count from the contract

The contract we are interacting with has a few simple functions. 'get_count', 'increment' and 'reset'. These two functions can be called via by using the queryContractSmart method.

TIP

Please note there is a complete guide on how to upload the example contract on localOsmosis here.

async function getCount() {
    const client = await CosmWasmClient.connect(rpcEndpoint);

    const getCount = await client.queryContractSmart("osmo1zlmaky7753d2fneyhduwz0rn3u9ns8rse3tudhze8rc2g54w9ysq725p3a",{ "get_count": {}})
    console.log(getCount);
}
getCount();

1
2
3
4
5
6
7
8

# Increase the counter

async function increaseCounter() {
    const client = await CosmWasmClient.connect(rpcEndpoint);

    const increaseCounter = await client.queryContractRaw("osmo1zlmaky7753d2fneyhduwz0rn3u9ns8rse3tudhze8rc2g54w9ysq725p3a","{\"increment\":{}}")
    console.log(increaseCounter);
}

queryContract();

1
2
3
4
5
6
7
8
9

WIP