Overview

Every application built on top of Quai Network requires a connection to the network in order to interact with smart contracts, send transactions, or sign messages on behalf of a user. Using the direct client JSON-RPC API is possible, but the methods can be quite verbose for use in application interfaces.

JavaScript APIs and SDKs offer a simplified interface for web applications to interact with Quai Network via one-line methods, conversion utils, and smart contract wrappers. The available libraries can be found below.

quais.js

A complete Quai Network interaction library for JavaScript and TypeScript.

Library Features

Connect to Quai Nodes

Abstract providers make it easy to connect to, read data from, and broadcast transactions to Quai Network nodes. Providers are composable, so you can connect to remote endpoints, local nodes, or even custom infrastructure.

Providers can query any zone chain in the network for:

  • Block data
  • Transaction data and gas estimates
  • Account balances
  • Smart contract data
  • And more…
// Connect to a remote node
const provider = new quais.JsonRpcProvider('https://rpc.quai.network', undefined, { usePathing: true })

// Connect to an injected provider (e.g. Pelagus)
const provider = new quais.BrowserProvider(window.pelagus)

Smart Contract Functionality

Libraries like Quais.js provide smart contract wrappers that make it easy to call smart contract functions, return event data, and read state variables.

Smart contract wrappers serve as JavaScript interpreters for contract ABIs, allowing you to call functions and read data from smart contracts without having to interface with Solidity directly.

Smart contract wrappers also provide a number of useful features, including:

  • Transaction simulation
  • Raw transaction generation
  • Event filtering
// set up a contract interface
const contract = new quais.Contract(contractAddress, contractABI, provider)

// call a contract function
const result = await contract.setGreeting('Hello, world!')

// read a contract state variable
const greeting = await contract.staticCallResult.greet()

Utilities

Quais.js ships with a number of small but powerful utilities that make working with Quai Network simple. They provide useful shortcuts for converting units, getting shard information, and formatting data.

Other useful utilities include:

  • Unit conversion and parsing
  • Data encoding and formatting
  • Address validation
// get zone name from an address
const shard = quais.getZoneFromAddress('0xa844d9a88331e9688d3065f92c11e25ab1e50aa6')

// parse units to BigNumber
const quai = quais.parseQuai('1')

// base58 encode a string
const decoded = quais.decodeBase58('MergedMining')