Querying Blockchain Data NibiJS
includes a NibiruQuerier
class for querying blockchain data. This class allows developers to query token balances, block details, oracle prices, staking delegations, and more. The NibiruQuerier
makes accessing blockchain data straightforward, enabling developers to efficiently build dApps on the Nibiru blockchain. With NibiruQuerier
, nearly all blockchain data can be queried. Below are some basic examples, but many more queries are possible. Note that these queries can also be accessed via NibiruTxClient
.
Getting Started with NibiruQuerier First, import the necessary classes and establish a connection to the blockchain.
Copy
import { Chain, NibiruQuerier, Testnet } from "@nibiruchain/nibijs" ;
const connectToQuerier = async ( ) => {
const chain : Chain = Testnet ( 1 ) ;
const querier = await NibiruQuerier. connect ( chain. endptTm) ;
return querier;
} ;
Querying Token Balances All Token Balances Retrieve all token balances for a given address.
Copy
const queryAllBalances = async ( querier, address ) => {
const allBalances = await querier. getAllBalances ( address) ;
console. log ( "All Balances:" , allBalances) ;
return allBalances;
} ;
const querier = await connectToQuerier ( ) ;
const address = "your_nibi_address" ;
const allBalances = await queryAllBalances ( querier, address) ;
Single Token Balance Retrieve the balance of a specific token for a given address.
Copy
const queryBalance = async ( querier, address, denom ) => {
const balance = await querier. getBalance ( address, denom) ;
console. log ( ` ${ denom} Balance: ` , balance) ;
return balance;
} ;
const querier = await connectToQuerier ( ) ;
const address = "your_nibi_address" ;
const balance = await queryBalance ( querier, address, "unibi" ) ;
Latest Block Height Get the height of the latest block.
Copy
const queryLatestHeight = async ( querier ) => {
const latestHeight = await querier. getHeight ( ) ;
console. log ( "Latest Block Height:" , latestHeight) ;
return latestHeight;
} ;
const querier = await connectToQuerier ( ) ;
const latestHeight = await queryLatestHeight ( querier) ;
Block Details by Height Get details of a block by its height.
Copy
const queryBlockByHeight = async ( querier, height ) => {
const block = await querier. tm. block ( height) ;
console. log ( "Block Details:" , block) ;
return block;
} ;
const querier = await connectToQuerier ( ) ;
const latestHeight = await queryLatestHeight ( querier) ;
const block = await queryBlockByHeight ( querier, latestHeight) ;
Latest Block Details Get details of the latest block.
Copy
const queryLatestBlock = async ( querier ) => {
const latestBlock = await querier. getBlock ( ) ;
console. log ( "Latest Block Details:" , latestBlock) ;
return latestBlock;
} ;
const querier = await connectToQuerier ( ) ;
const latestBlock = await queryLatestBlock ( querier) ;
Advanced Queries with Nibiru Extensions Oracle Price Exchange Rate Get the exchange rate between UNIBI and UUSDT.
Copy
const queryOraclePrice = async ( querier, pair ) => {
const price = await querier. nibiruExtensions. oracle. exchangeRate ( pair) ;
console. log ( ` Oracle Price ( ${ pair} ): ` , price) ;
return price;
} ;
const querier = await connectToQuerier ( ) ;
const oraclePrice = await queryOraclePrice ( querier, "unibi:uusdt" ) ;
All IBC Channels Retrieve all IBC channels.
Copy
const queryAllIBCChannels = async ( querier ) => {
const channels = await querier. nibiruExtensions. ibc. channel. allChannels ( ) ;
console. log ( "All IBC Channels:" , channels) ;
return channels;
} ;
const querier = await connectToQuerier ( ) ;
const allIBCChannels = await queryAllIBCChannels ( querier) ;
Staking Delegations Get all staking delegations for a given address.
Copy
const queryDelegations = async ( querier, address ) => {
const delegations = await querier. nibiruExtensions. staking. delegatorDelegations ( address) ;
console. log ( "Delegations:" , delegations) ;
return delegations;
} ;
const querier = await connectToQuerier ( ) ;
const address = "your_nibi_address" ;
const delegations = await queryDelegations ( querier, address) ;
Unbonding Staking Delegations Get all unbonding staking delegations for a given address.
Copy
const queryUnbondingDelegations = async ( querier, address ) => {
const unbondingDelegations = await querier. nibiruExtensions. staking. delegatorUnbondingDelegations ( address) ;
console. log ( "Unbonding Delegations:" , unbondingDelegations) ;
return unbondingDelegations;
} ;
const querier = await connectToQuerier ( ) ;
const address = "your_nibi_address" ;
const unbondingDelegations = await queryUnbondingDelegations ( querier, address) ;
Related Pages