@nibiruchain/nibijs
v6.4.2
Published
The TypeScript SDK for the Nibiru blockchain.
Downloads
3,229
Readme
The NibiJS (@nibiruchain/nibijs) package makes it possible to interact with Nibiru from a Node.js or browser environment. nibijs provides simple abstractions for core data structures, serialization, key management, API requests, and the submission of transactions.
The nibijs source code can be found in the src directory.
Table of Contents
To learn more about Nibiru, see nibiru.fi/docs
Installation
@nibiruchain/nibijs is available on the npm registry.
npm install @nibiruchain/nibijs # or yarn addUsage
NibiruQuerier reads chain state and NibiruTxClient signs and broadcasts transactions. HeartMonitor reads the indexer. Wallets come from newRandomWallet and newSignerFromMnemonic.
Example: Creating a wallet
import { newRandomWallet } from "@nibiruchain/nibijs"
// Create a new Nibiru wallet
const wallet = await newRandomWallet()
const [{ address }] = await wallet.getAccounts()
// Save the mnemonic somewhere to re-use the account
console.log("mnemonic: ", wallet.mnemonic)
console.log("address: ", address)Example: Querying
import { NibiruQuerier, Testnet } from "@nibiruchain/nibijs"
export const CHAIN = Testnet(2)
const querier = await NibiruQuerier.connect(CHAIN.endptTm)
// Query balances
const exampleAddress = "nibi17dz4cdw5fmm2cxd4ht9xvjmpw3ycmpkpcc6js9"
const balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)
// Query the latest block. Pass a height to getBlock when the node still has that block.
const block = await querier.getBlock()
console.log("block: %o", block)Indexer queries
Heart Monitor examples for balances, transaction history, IBC transfers, staking, and oracle prices are in QUERIES.md.
Example: Sending funds
import {
NibiruTxClient,
newSignerFromMnemonic,
Testnet,
NibiruQuerier,
} from "@nibiruchain/nibijs"
import { coins } from "@cosmjs/proto-signing"
export const CHAIN = Testnet(2)
const mnemonic = "your mnemonic here..."
const signer = await newSignerFromMnemonic(mnemonic)
const querier = await NibiruQuerier.connect(CHAIN.endptTm)
const txClient = await NibiruTxClient.connectWithSigner(CHAIN.endptTm, signer)
const [{ address: fromAddr }] = await signer.getAccounts()
// Check balance before sending tokens
const exampleAddress = "nibi1mzjkw9z5ugajxchl884y0c28lk2627hpuljuw4"
let balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)
const tokens = coins(5, "unibi")
// "auto" simulates gas and multiplies it by 1.4. The client prices that gas at 0.025unibi.
// A number here is that multiplier, not a fee in unibi.
const txResp = await txClient.sendTokens(
fromAddr,
exampleAddress,
tokens,
"auto"
)
console.log(txResp)
// sendTokens resolves after the transaction is included in a block.
balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)Codebase structure
| Directories of @nibiruchain/nibijs | Purpose/Utility |
| :----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| common | home to several commonly needed types, constants and configurations such as Network. |
| msg | Implements functions for creating messages (Msgs). These are objects that trigger state-transitions and get wrapped into transactions. |
| query | For querying state via the consensus engine of a full-node and the application blockchain interface (ABCI). |
| tx | For signing and to submitting transactions given a set of Msg objects. |
| wallet | A simple wrapper around the Keplr wallet. This module will grow as support is added for other wallets (like MetaMask). |
@nibiruchain/protojs provides types generated from the protocol buffers of the Cosmos-SDK, Tendermint Core, and Nibiru Chain. For most use cases, it won't be necessary to interact with this layer.
Development Quick Start
Install and use
nvm.wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bash nvm useInstall yarn.
npm install -g yarnThen, install package dependencies. At the root of the repository, run
yarnInitialize git submodules
git submodule init git submodule updateLastly, compile the code in each package.
yarn build
See HACKING.md for the full development guide. It includes instructions on:
- Running tests
- Generating code for the @nibiruchain/protojs package
- Generating documentation in HTML or Markdown from the comments of @nibiruchain/nibijs
🔓 License
This software is licensed under the MIT license. See LICENSE for full disclosure.
© 2023 Nibi, Inc.
