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.
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…
Copy
Ask AI
// Connect to a remote nodeconst 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)
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
Copy
Ask AI
// set up a contract interfaceconst contract = new quais.Contract(contractAddress, contractABI, provider)// call a contract functionconst result = await contract.setGreeting('Hello, world!')// read a contract state variableconst greeting = await contract.staticCallResult.greet()
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
Copy
Ask AI
// get zone name from an addressconst shard = quais.getZoneFromAddress('0xa844d9a88331e9688d3065f92c11e25ab1e50aa6')// parse units to BigNumberconst quai = quais.parseQuai('1')// base58 encode a stringconst decoded = quais.decodeBase58('MergedMining')