#48 | Simplifying Development on Nibiru
· 4 min read
Developing on the Nibiru is made easy with NibiJS. This TypeScript library simplifies decentralized application (dApp) development, providing a user-friendly interface that abstracts away blockchain complexities. With NibiJS, developers can focus on innovation and building cutting-edge dApps.
Installation
To get started with nibijs, you can install it using npm or yarn. First, ensure you
have nvm installed for managing node versions. Then, install the nibijs package:
This installation process can be used in both node applications and frontend applications, depending on your specific use case.
Interacting with the Nibiru Blockchain
With nibijS, developers have access to the NibiruTxClient class, which extends
SigningStargateClient and serves as a signing client as well as a SigningCosmWasmClient.
This class allows developers to interact with smart contracts on the Nibiru blockchain,
including deploying, instantiating, querying, and executing contracts. Here is a snippet
of the NibiruTxClient class:
With this, developers can rely on NibiruTxClient to interact with contracts (instantiate, deploy and execute) and query from the Nibiru Blockchain itself.
Smart Contracts Management
To interact with a smart contract, developers can use the SigningCosmWasmClient within the NibiruTxClient class. They can upload the contract's bytecode, set the sender address, specify the fee, and deploy the contract to the blockchain. Smart contracts instantiation, querying and execution is also made easy:
Deploying a smart contract
// ---------------- Deploying a Smart Contract ----------------
import {NibiruTxClient, Chain, newSignerFromMnemonic, Mainnet,} from "@nibiruchain/nibijs"
const deployContract = async () => {/** * A "Chain" object exposes all endpoints for Nibiru node such as the * gRPC server, Tendermint RPC endpoint, and REST server. * * The most important one for nibijs is "Chain.endptTM", the Tendermint RPC. **/ const chain: Chain = Mainnet() // Permanent testnet
// ---------------- NibiruTxClient ----------------
// const signer = await newRandomWallet() // Signer: randomly generated const signer = await newSignerFromMnemonic("mnemonic here...") // Signer: in-practice
const txClient = await NibiruTxClient.connectWithSigner(chain.endptTm, signer)
const senderAddress = "" // Read the wasm file as a Uint8Array const wasmCode: Uint8Array = fs.readFileSync("path_to_your_wasm_file.wasm") const fee = "auto" const deployContract = await txClient.wasmClient.upload(senderAddress, wasmCode, fee)
return { deployContract }}
Note: For Fee, it is set to auto, but developers can input whatever fee they want. The minimum is 0.025unibi. unibi represents Micro nibi Tokens, which is 1e6 of native Nibiru nibi tokens.
Instantiate a Smart Contract
// ---------------- Instantiate a Smart Contract ----------------
import { NibiruTxClient, Chain, newSignerFromMnemonic, Mainnet, } from "@nibiruchain/nibijs"
const instantiateContract = async () => { /** * A "Chain" object exposes all endpoints for Nibiru node such as the * gRPC server, Tendermint RPC endpoint, and REST server. * * The most important one for nibijs is "Chain.endptTM", the Tendermint RPC. **/ const chain: Chain = Mainnet() // Permanent testnet
// ---------------- NibiruTxClient ----------------
// const signer = await newRandomWallet() // Signer: randomly generated const signer = await newSignerFromMnemonic("mnemonic here...") // Signer: in-practice
const txClient = await NibiruTxClient.connectWithSigner(chain.endptTm, signer)
const senderAddress = "" // Contract CodeId, generated after deploying a contract const codeId = 12345
// msg can of type any, it depends on the smart contract instantiation message requirements const msg = "" const label = "Contract label" const fee = "auto" const instantiateContract = await txClient.wasmClient.instantiate( senderAddress, codeId, msg, label, fee )
return { instantiateContract } }
Querying a Smart Contract
// ---------------- Querying a Smart Contract ----------------
import { NibiruTxClient, Chain, newSignerFromMnemonic, Mainnet, } from "@nibiruchain/nibijs"
const queryContract = async () => { /** * A "Chain" object exposes all endpoints for Nibiru node such as the * gRPC server, Tendermint RPC endpoint, and REST server. * * The most important one for nibijs is "Chain.endptTM", the Tendermint RPC. **/ const chain: Chain = Mainnet() // Permanent testnet
// ---------------- NibiruTxClient ----------------
// const signer = await newRandomWallet() // Signer: randomly generated const signer = await newSignerFromMnemonic("mnemonic here...") // Signer: in-practice
const txClient = await NibiruTxClient.connectWithSigner(chain.endptTm, signer)
const senderAddress = "" // Contract CodeId, can be found after deploying a contract const codeId = 12345 const queryContract = await txClient.wasmClient.queryContractSmart( senderAddress, codeId )
return { queryContract } }
Executing a Smart Contract
// ---------------- Executing a Smart Contract ----------------
import { NibiruTxClient, Chain, newSignerFromMnemonic, Mainnet, } from "@nibiruchain/nibijs"
const executeContract = async () => { /** * A "Chain" object exposes all endpoints for Nibiru node such as the * gRPC server, Tendermint RPC endpoint, and REST server. * * The most important one for nibijs is "Chain.endptTM", the Tendermint RPC. **/ const chain: Chain = Mainnet() // Permanent testnet
// ---------------- NibiruTxClient ----------------
// const signer = await newRandomWallet() // Signer: randomly generated const signer = await newSignerFromMnemonic("mnemonic here...") // Signer: in-practice
const txClient = await NibiruTxClient.connectWithSigner(chain.endptTm, signer)
const senderAddress = "" // Contract Address, can be found after deploying a contract const contractAddress = "" // msg can of type any, it depends on the smart contract execution message requirements const msg = "" const fee = "auto" const executeContract = await txClient.wasmClient.execute( senderAddress, contractAddress, msg, fee )
return { executeContract } }
Querying Blockchain Data
nibijs also provides a NibiruQuerier class for querying blockchain data.
Developers can use this class to query token balances, block details, oracle
prices, staking delegations, and more. The NibiruQuerier class simplifies the
process of accessing blockchain data, making it easier for developers to build
dApps on the Nibiru blockchain. With the NibiruQuerier, we can query pretty
much everything from the chain. Below are some basic examples, but there are many.
Note that all these queries can also be accessed via NibiruTxClient.
import { Chain, NibiruQuerier, Testnet } from "@nibiruchain/nibijs"
const queryExample = async () => { /** * A "Chain" object exposes all endpoints for Nibiru node such as the * gRPC server, Tendermint RPC endpoint, and REST server. * * The most important one for nibijs is "Chain.endptTM", the Tendermint RPC. **/ const chain: Chain = Testnet()
// ---------------- NibiruQuerier ---------------- const querier = await NibiruQuerier.connect(chain.endptTm)
const address = "" // your nibi address
// Return all token balances for the given address const allBalances = await querier.getAllBalances(address)
// Return a single token balance for the given address const balance = await querier.getBalance(address, "unibi")
// Return latest block height const latestHeight = await querier.getHeight()
// Return block details by height such as the block has, parent block hash, block height, timestamp, etc. const block = await querier.tm.block(latestHeight)
// Return latest block. Passing in the height as an argument is equivalent to querier.tm.block(height) const latestBlock = await querier.getBlock()
// ---------------- NibiruQuerier w/ Nibiru Extensions ----------------
// Return Oracle Price exchange rate between unibi & uusdt const usdtOraclPrice = await querier.nibiruExtensions.oracle.exchangeRate("unibi:uusdt")
// Return all IbC Channels const allIBCChannels = await querier.nibiruExtensions.ibc.channel.allChannels()
// Return all staking delegation for a given address const delegations = await querier.nibiruExtensions.staking.delegatorDelegations( address )
// Return all unbonding staking delegation for a given address const unbonding = await querier.nibiruExtensions.staking.delegatorUnbondingDelegations( address )
return { // ---------------- NibiruQuerier ---------------- allBalances, balance, block, latestBlock, // ---------------- NibiruQuerier w/ Nibiru Extensions ---------------- usdtOraclPrice, allIBCChannels, delegations, unbonding, } }
About Nibiru
Nibiru is a breakthrough L1 blockchain and smart contract ecosystem sporting superior throughput and unparalleled security. Nibiru aims to be the most developer-friendly and user-friendly smart contract ecosystem, leading the charge toward mainstream Web3 adoption by innovating at each layer of the stack: dApp development, infra, consensus, a comprehensive dev toolkit, value accrual.