Tooling Setup

For this tutorial, we are going to use the CosmJs library from TypeScript with the Yarn package manager. We'll also use Osmosis testnet for Perps. In order to send transactions on Osmosis testnet you'll need some gas coins. You can get these by tapping the faucet within the Perps Beta UI or the Osmosis faucet

We're going to reuse the same project throughout this tutorial, so you'll only need to do this setup once.

  1. Install nodejs

  2. Install yarn

  3. Create a new directory for this tutorial

  4. Within that directory, create a new package.json file with the following content:

    {
        "name": "levana-perps-tutorial",
        "version": "1.0.0",
        "description": "Tutorial Project for Levana Perps",
        "main": "index.js",
        "license": "MIT",
        "dependencies": {
            "@cosmjs/cosmwasm-stargate": "0.32.2",
            "@cosmjs/stargate": "0.32.2"
        },
        "devDependencies": {
            "ts-node": "^10.9.1",
            "typescript": "^4.4.3"
        },
        "scripts": {
            "tutorial": "ts-node tutorial.ts"
        }
    }
    
  5. In the same directory, create a tutorial.ts file with the following content:

    console.log("Hello World!")
    
  6. Run yarn to install the dependencies.

  7. Run yarn tutorial to run the tutorial file.

Congratulations, you now have a basic working TypeScript file! Now let's get set up with CosmJs and Osmosis Testnet.

CosmJs, Osmosis Testnet, and Levana Perps

CosmJs code needs to run within an async function. To connect to a chain, you must provide the RPC endpoint of a blockchain node. And to interact with Levana Perps, you'll need to have the contract address of a factory contract. We'll go into more details on interacting with the factory in the next section, but for now let's start with a simple example that queries the current block height of the chain and basic metadata on the Levana Perps factory we'll be interacting with:

import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate"

// Testnet factory address
const factoryAddress =
  "osmo1ymuvx9nydujjghgxxug28w48ptzcu9ysvnynqdw78qgteafj0syq247w5u"

// Osmosis testnet RPC endpoint
// retrieved from https://github.com/cosmos/chain-registry/blob/aecb225b17d1cb5a3826887735cafeb7e62934d0/testnets/osmosistestnet/chain.json#L123
const rpcEndpoint = "https://rpc.osmotest5.osmosis.zone"

const runAll = async () => {
  const client = await CosmWasmClient.connect(rpcEndpoint)

  const height = await client.getHeight()

  console.log(`Current height: ${height}`)

  const contract = await client.getContract(factoryAddress)
  console.log(JSON.stringify(contract))
}

runAll()

If you copy this code into tutorial.ts and run yarn tutorial, you should get output that looks roughly like the following:

$ yarn tutorial
yarn run v1.22.21
$ ts-node tutorial.ts
Current height: 5794578
{
  "address":"osmo1ymuvx9nydujjghgxxug28w48ptzcu9ysvnynqdw78qgteafj0syq247w5u",
  "codeId":6056,
  "creator":"osmo12g96ahplpf78558cv5pyunus2m66guykt96lvc",
  "admin":"osmo12g96ahplpf78558cv5pyunus2m66guykt96lvc",
  "label":"Levana Perps Factory - osmobeta"
}

Now that we're able to talk to the chain, let's explore the factory.