Page cover

codeWASM Integration Guide

End-to-end instructions for interacting with Sai from the Cosmos (WASM) side using NibiruTxClient.


0. Getting Started

Prerequisites

  • Node.js ≥ 18

  • pnpm, npm, or yarn

New Project

mkdir sai-wasm && cd sai-wasm
pnpm init -y
pnpm add @nibiruchain/nibijs @cosmjs/cosmwasm-stargate @cosmjs/stargate cosmjs-types bignumber.js
pnpm add -D typescript ts-node @types/node dotenv
npx tsc --init --target ES2022 --module NodeNext --moduleResolution NodeNext

Environment Setup

Create a .env file with your configuration:

# Testnet-1
NIBI_RPC=https://rpc.testnet-1.nibiru.fi/

# Or Mainnet
# NIBI_RPC=https://rpc.nibiru.fi/

MNEMONIC="word1 word2 ... word12"  # Your wallet mnemonic (keep this secret!)

Bootstrap Client


1. Collateral / Payment Tokens

Decimal Handling

Default decimals for BANK amounts are 6. Always scale display amounts to BANK units by multiplying by 10^6.

Testnet-1

Token
Bank Denom

USDC

tf/nibi1pc2mmwcqhvzn9vsm0umpu40yzl6gfy6nucwn7g/usdc

stNIBI

tf/nibi1pc2mmwcqhvzn9vsm0umpu40yzl6gfy6nucwn7g/stnibi

Mainnet

Token
Bank Denom

USDC

erc20/0x0829F361A05D993d5CEb035cA6DF3446b060970b

stNIBI

tf/nibi1udqqx30cw8nwjxtl4l28ym9hhrp933zlq8dqxfjzcdhvl8y24zcqpzmh8m/ampNIBI

Converting Display to BANK Units


2. Perps – Open Trade

Message Structure

Rules

  • Market trades require open_price set to the current market price

  • Limit/Stop trades require open_price set to the trigger price

Example


3. Perps – Close Trade

Close an open position:


4. Referral – Create & Redeem Codes

Create a Referral Code

Redeem a Referral Code


5. Vault – Deposit

Deposit collateral into a vault:


6. Vault – Make Withdraw Request

Request to withdraw funds from a vault:


7. Vault – Redeem

Redeem shares from a vault:


8. Vault – Cancel Withdraw Request

Cancel a pending withdraw request:


Query Examples

Query Vault Share Denom

Query User Trades

Query Vault Info


Error Handling & Troubleshooting

Common Issues

Issue
Solution

"No account found"

Ensure your mnemonic is valid and NibiruTxClient is properly initialized

"Insufficient funds"

Verify you have enough tokens (in BANK units) in your wallet

"Invalid denom"

Double-check the bankDenom matches your network (Testnet-1 vs Mainnet)

"Contract not found"

Ensure the contract address is correct for your network

Decimal conversion errors

Always multiply display amounts by 10^6 before passing to WASM

Transaction timeout

Increase the timeout or check RPC connectivity

Best Practices

  • Always ensure the NibiruTxClient is connected before executing transactions

  • Validate that amounts are non-negative integers in BANK units

  • Use "auto" for gas estimation unless you have specific gas requirements

  • Store your mnemonic securely and never commit it to version control

  • Always query contract state before making assumptions about parameters

  • Test on Testnet-1 before moving to Mainnet

Debugging Tips

  • Check transaction details: client.getTx(transactionHash)

  • Inspect error messages: Transaction results include detailed error reasons

  • Use logs: Add console.log() statements to trace execution flow

  • Monitor gas: The transaction response includes gas used and fees paid

Last updated