Build
SDK
Typescript SDK
Quick Start

Rooch TypeScript SDK quick start

The Rooch TypeScript SDK is a modular toolkit for interacting with Rooch, encapsulating RPC methods. It simplifies the process of sending queries to RPC nodes, building and signing transactions, and interacting with the Rooch network.

This article mainly guides you to quickly get started with Rooch's TypeScript developer tools (TS SDK).

Create a new project

Note: This tutorial is demonstrated in a Linux environment!

First, create a directory and then initialize the project using JS/TS package management tools — sdk-quick-start:

# Create project directory
mkdir sdk-quick-start
 
# Switch to the project root directory
cd sdk-quick-start
 
# Initialize the project
npm init -y

Add dependencies

Here, you only need to add the rooch-sdk dependency:

npm i @roochnetwork/rooch-sdk

Check the package.json file, and the dependencies should already include the list of dependencies we need:

"dependencies": {
    "@roochnetwork/rooch-sdk": "latest"
}

Update configuration

Add the following line in package.json to enable module features:

"type": "module",

Call RPC methods in the code

import { RoochClient, getRoochNodeUrl } from '@roochnetwork/rooch-sdk'
 
async function main() {
  try {
    // Create a client to connect to the testnet
    const client = new RoochClient({
      url: getRoochNodeUrl('testnet')
    })
    console.log('Connected to Rooch testnet')
 
    // Query chain information
    const chainInfo = await client.getChainId()
    console.log('Chain Info:', chainInfo)
 
  } catch (error) {
    console.error('Error:', error)
  }
}
 
main()

Here, we mainly use RoochClient to create a client instance and use it to get some common Rooch RPC operation methods.

getRoochNodeUrl('testnet') connects to the testnet and uses a client-provided RPC method chainId() to view the current chain ID:

$ npm run start
 
> [email protected] start
> node index.js
 
Connected to Rooch testnet
Chain Info: 2

Call the method to query account balance through the SDK

Slightly modify the above program, this time connect to the mainnet and query the balance of an account on the mainnet:

import pkg from '@roochnetwork/rooch-sdk';
const { RoochClient, getRoochNodeUrl } = pkg;
 
async function main() {
  try {
    // Create a client to connect to the mainnet
    const client = new RoochClient({
      url: getRoochNodeUrl('mainnet')
    })
    console.log('Connected to Rooch mainnet')
 
    // Query account balance
    const balances = await client.getBalances({
      owner: "bc1ph25pk4xm36y3mt6uwclm0fgj8dgdnf5u7xf987qvyv58x987cldswp09hc",
      cursor: null,
      limit: 10
    })
    console.log(balances)
 
  } catch (error) {
    console.error('Error:', error)
  }
}
 
main()

Output:

[joe@mx my-rooch-app]$ npm start
 
> [email protected] start
> node index.js
 
Connected to Rooch mainnet
{
  data: [
    {
      coin_type: '0x3::gas_coin::RGas',
      name: 'Rooch Gas Coin',
      symbol: 'RGAS',
      icon_url: '<?xml version="1.0" encoding="UTF-8"?>\n' +
        '<svg id="uuid-f3c10da3-9417-410e-a3ab-8ce1ab7d75a2" data-name="layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">\n' +
        '  <defs>\n' +
        '    <style>\n' +
        '      .uuid-f8ed1a91-d770-44e4-b9a1-8898aed79e23 {\n' +
        '        fill: #006840;\n' +
        '      }\n' +
        '\n' +
        '      .uuid-06e2debf-0aef-494d-b577-6d49611e1cf8 {\n' +
        '        fill: #b2ff04;\n' +
        '      }\n' +
        '    </style>\n' +
        '  </defs>\n' +
        '  <circle class="uuid-06e2debf-0aef-494d-b577-6d49611e1cf8" cx="250" cy="250" r="250"/>\n' +
        '  <path class="uuid-f8ed1a91-d770-44e4-b9a1-8898aed79e23" d="M346.39,403.15c-26.17-23.18-51.58-45.69-77.73-68.86-.13,2.58-.28,4.18-.29,5.77-.19,30.24-.36,60.48-.54,90.73q-.04,6.48-6.74,6.45c-8.78-.05-17.57-.27-26.35-.07-3.46,.08-4.36-.83-4.32-4.3,.33-31.62,.46-63.23,.64-94.85,0-1.05,.01-2.09,.02-4.12-26.46,22.99-52.29,45.44-78.06,67.84-1.6-.72-.96-1.95-.96-2.88,.04-14.97,.2-29.94,.17-44.9,0-2.36,.75-3.86,2.52-5.42,28.99-25.57,57.91-51.22,86.85-76.84,.67-.59,1.45-1.07,2.4-1.76-1.89-1.66-3.64-1.09-5.18-1.1-27.12-.2-54.24-.43-81.36-.41-3.91,0-4.88-1.05-4.74-4.83,.34-9.46,.42-18.94,.16-28.4-.11-3.83,1.29-4.39,4.68-4.35,26.96,.28,53.93,.38,80.9,.54,1.68,0,3.37,.02,5.83,.03-2.08-3.59-5.06-5.5-7.53-7.74-27-24.56-54.06-49.05-81.17-73.49-1.49-1.35-2.15-2.64-2.13-4.66,.17-16.03,.23-32.07,.33-49.24,26.24,23.34,51.86,46.12,78.27,69.61,.01-2.46,.02-4.06,.03-5.65,.18-31.01,.43-62.01,.45-93.02,0-3.71,1.1-4.55,4.65-4.44,9.39,.29,18.8,.42,28.19,.14,4.06-.12,4.69,1.28,4.65,4.93-.33,30.7-.44,61.4-.62,92.1,0,1.63-.02,3.27-.04,6.19,26.52-23.13,52.16-45.5,77.98-68.01,1.04,1.6,.64,2.99,.64,4.27-.05,13.9-.29,27.8-.13,41.7,.05,3.86-1.14,6.41-4.05,8.97-28.31,24.95-56.48,50.05-84.69,75.1-.78,.69-1.52,1.41-2.45,2.27,1.66,1.71,3.6,1.03,5.27,1.04,26.96,.21,53.93,.41,80.9,.44,3.42,0,4.77,.6,4.62,4.41-.37,9.61-.37,19.24-.19,28.86,.06,3.38-.77,4.35-4.31,4.31-27.12-.32-54.24-.4-81.36-.56-1.64,0-3.28-.02-6.14-.04,8.77,7.94,16.64,15.09,24.54,22.22,21.63,19.52,43.26,39.04,64.91,58.53,1.08,.97,1.81,1.89,1.8,3.46-.14,16.32-.22,32.64-.32,50.05Z"/>\n' +
        '</svg>',
      decimals: 8,
      supply: '50000000200000000',
      balance: '9758826626',
      fixedBalance: 97.58826626
    }
  ],
  next_cursor: { tx_order: '57954988', state_index: '2' },
  has_next_page: false
}

Note the balance field in the returned result!

Summary

So far, you have learned how to use the SDK!

With the SDK, we can conveniently use RPC methods in the code to handle corresponding operations without manually entering RPC methods in the command line.

Next, we will start the Rooch TypeScript SDK development journey!