Set Up a New Wallet

You can use any seed phrase generation tool to set up a new wallet. We're going to use Keplr to generate our seed phrase, since our next step will be to tap a testnet faucet within the browser. Make sure you have the Keplr browser extension installed, open the "add account" page, and choose "Create new account":

Create new account

On the next screen, you'll be given your new seed phrase. Copy that string; you'll need it shortly to both finish the Keplr setup, and to continue writing our test code:

Seed phrase

Then, on the next screen, confirm you've written down the seed phrase:

Confirm phrase

After finishing the setup, make sure you select the new wallet within the Keplr interface.

Signing Client

Now we can take our seed phrase back to our TypeScript code and create a signer and signing client:

import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"

// 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"

// Our personal wallet seed phrase
const seedPhrase =
  "hen punch extra relax craft bicycle iron core purity tissue talk impact kitchen inhale slush hip amateur during ranch inspire much correct century where"

const runAll = async () => {
  const signer = await DirectSecp256k1HdWallet.fromMnemonic(seedPhrase, {
    // This is the prefix used by Osmosis testnet
    prefix: "osmo"
  })

  // Get the address of the account. This should match what you see inside Keplr
  const accounts = await signer.getAccounts()
  const address = accounts[0].address
  console.log(`Wallet address is ${address}`)

  const client = await SigningCosmWasmClient.connectWithSigner(
    rpcEndpoint,
    signer
  )

  // And print some information about this account
  const account = await client.getAccount(address)
  console.log(`Account information: ${JSON.stringify(account)}`)
  const balance = await client.getBalance(address, "uosmo")
  console.log(`Balance is ${balance.amount}${balance.denom}`)
}

runAll()

Running this program initially resulted in the following output:

Wallet address is osmo1g4unz9tzpdlaluqgyt0jjq5kyzuylg2a36086t
Account information: null
Balance is 0uosmo

The wallet address does in fact match what I see in Keplr. However, the account information is null. Until we are sent some native coins, our wallet isn't usable on the chain.

Tap the faucet

To get native coins, you can use the faucet built into the Levana Perps beta site or the Osmosis Faucet. Connect your wallet on that page and then click on the wallet address in the top right of the screen:

Faucet menu

Once you click on the faucet button, you'll need to solve a captcha. Once that is completed, you should receive some OSMO coins for gas and ATOM tokens for collateral. Complete the captcha and then rerun the program above; you should see something like this:

Wallet address is osmo1g4unz9tzpdlaluqgyt0jjq5kyzuylg2a36086t
Account information: {"address":"osmo1g4unz9tzpdlaluqgyt0jjq5kyzuylg2a36086t","pubkey":null,"accountNumber":17447,"sequence":0}
Balance is 10000000uosmo

Checking CW20 collateral token balance

The getBalance method on the signing client can give us information on our native coins, but doesn't tell us anything about our CW20 balances. To do that, we need to query the CW20 contract directly. We saw this address previously in querying the market contract's status. Let's put things together and find our balance:

import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"

// 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"

// Our personal wallet seed phrase
const seedPhrase =
  "hen punch extra relax craft bicycle iron core purity tissue talk impact kitchen inhale slush hip amateur during ranch inspire much correct century where"

// ATOM_USD contract address on Osmosis testnet
const marketAddress =
  "osmo1kus6tmx9ggmvgg5tf88ukgxcz0ynakx38hyjy0sjgahvp7d3ut2qqtfhf4"

const runAll = async () => {
  const signer = await DirectSecp256k1HdWallet.fromMnemonic(seedPhrase, {
    // This is the prefix used by Osmosis testnet
    prefix: "osmo"
  })
  const accounts = await signer.getAccounts()
  const walletAddress = accounts[0].address

  const client = await SigningCosmWasmClient.connectWithSigner(
    rpcEndpoint,
    signer
  )

  const status = await client.queryContractSmart(marketAddress, {
    status: {}
  })
  const collateralAddress = status.collateral.cw20.addr

  const balance = await client.queryContractSmart(collateralAddress, {
    balance: { address: walletAddress }
  })
  console.log(balance)
}

runAll()

If you tapped the faucet via the Levana Perps beta site, then you should see something like:

{ balance: "1000000000" }

If, instead, you used the Osmosis Faucet, then you were sent gas coins but no collateral, so you will instead see:

{ balance: '0' }

The CW20 provides 6 digits of precision. You can get your token balance by dividing the value above by 10^6, or 1,000,000. This gives the 1,000 ATOM we were promised on the faucet screen.

Going forward, we'll hard-code the collateralAddress value in our program, again for brevity. But in real life code, the approach above of querying the status to find the correct collateral contract works great.

Now that we have a working wallet and some funds, we're ready to begin executing transactions against the market contract. Let's start by playing with liquidity.