4.3.2 ā€¢ Published 6 days ago

@nibiruchain/nibijs v4.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

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 add

Usage

The entrypoint for nibijs is the Sdk object, which is meant to mimic the root of a command line interface. It can be used for both queries and transactions.

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 block
const blockHeight = 200000
const block = await querier.getBlock(blockHeight)
console.log("block: %o", block)

// Query PERP markets
const perpMarkets = await querier.nibiruExtensions.perp.markets()
console.log("perpMarkets: %o", perpMarkets)

// Query SPOT pools
const spotPools = await querier.nibiruExtensions.spot.pools()
console.log("spotPools: %o", spotPools)

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")
const txResp = await txClient.sendTokens(
  fromAddr,
  exampleAddress,
  tokens,
  5000 // gas fee 5000 unibi
)
console.log(txResp)

// Execution could take several seconds
const delay = (ms) => new Promise((res) => setTimeout(res, ms))
await delay(10000)

// Check balance after send tokens
balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)

Example: Transaction with arbitrary messages

import {
  NibiruTxClient,
  newSignerFromMnemonic,
  Msg,
  Testnet,
  NibiruQuerier,
} from "@nibiruchain/nibijs"
import { coin } from "@cosmjs/proto-signing"

const mnemonic = "Your mnemonic here"
export const CHAIN = Testnet(2)
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()
const pair = "ubtc:unusd"

// Construct tx msgs
const msgs = [
  Msg.perp.openPosition({
    sender: fromAddr,
    pair: pair,
    quoteAssetAmount: 10,
    leverage: 1,
    goLong: true,
    baseAssetAmountLimit: 0,
  }),
  Msg.perp.addMargin({
    sender: fromAddr,
    pair: pair,
    margin: coin("20", "unusd"),
  }),
  Msg.perp.removeMargin({
    sender: fromAddr,
    pair: pair,
    margin: coin("5", "unusd"),
  }),
  // final margin value of 10 (open) + 20 (add) - 5 (remove) = 25
]

// Broadcast tx
const txResp = await txClient.signAndBroadcast(fromAddr, msgs, "auto")
console.log(txResp)

// Check your open PERP positions
const delay = (ms) => new Promise((res) => setTimeout(res, ms))
await delay(5000)

const perpPositions = await querier.nibiruExtensions.perp.positions({
  trader: fromAddr,
})
console.log("perpPositions: %o", perpPositions)

Codebase structure

Directories of @nibiruchain/nibijsPurpose/Utility
commonhome to several commonly needed types, constants and configurations such as Network.
msgImplements functions for creating messages (Msgs). These are objects that trigger state-transitions and get wrapped into transactions.
queryFor querying state via the consensus engine of a full-node and the application blockchain interface (ABCI).
txFor signing and to submitting transactions given a set of Msg objects.
walletA 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

  1. Install and use nvm.

    wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bash
    nvm use
  2. Install yarn.

    npm install -g yarn
  3. Then, install package dependencies. At the root of the repository, run

    yarn
  4. Lastly, compile the code in each package.

    yarn build

See HACKING.md for the full development guide. It includes instructions on:

  1. Running tests
  2. Generating code for the @nibiruchain/protojs package
  3. 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.

4.3.2

6 days ago

4.3.1

9 days ago

4.3.0

9 days ago

4.2.0

30 days ago

4.1.0

2 months ago

4.0.0

2 months ago

3.3.4

2 months ago

3.3.3

2 months ago

3.3.1

3 months ago

3.3.0

3 months ago

3.3.2

3 months ago

3.2.0

3 months ago

3.1.1

3 months ago

3.1.0

3 months ago

3.0.14

3 months ago

3.0.13

3 months ago

3.0.12

4 months ago

3.0.10

4 months ago

3.0.11

4 months ago

3.0.9

4 months ago

3.0.4

4 months ago

3.0.3

4 months ago

3.0.2

4 months ago

3.0.1

4 months ago

3.0.8

4 months ago

3.0.7

4 months ago

3.0.6

4 months ago

3.0.5

4 months ago

3.0.0

4 months ago

2.0.1

4 months ago

1.0.2

4 months ago

2.0.0

4 months ago

1.0.1

4 months ago

0.21.45

5 months ago

0.21.44

5 months ago

0.21.43

5 months ago

0.19.22

10 months ago

0.19.23

10 months ago

0.19.24

10 months ago

0.19.25

10 months ago

0.19.26

10 months ago

0.21.18

9 months ago

0.21.19

9 months ago

0.21.16

9 months ago

0.21.17

9 months ago

0.21.14

9 months ago

0.21.15

9 months ago

0.21.12

9 months ago

0.21.13

9 months ago

0.21.10

10 months ago

0.21.11

9 months ago

0.19.21

10 months ago

0.21.41

6 months ago

0.21.8

10 months ago

0.21.42

6 months ago

0.21.7

10 months ago

0.21.6

10 months ago

0.21.40

6 months ago

0.21.4

10 months ago

0.21.3

10 months ago

0.21.2

10 months ago

0.21.1

11 months ago

0.21.9

10 months ago

0.21.38

7 months ago

0.21.39

7 months ago

0.21.36

7 months ago

0.21.37

7 months ago

0.21.34

7 months ago

0.21.35

7 months ago

0.21.32

8 months ago

0.21.33

8 months ago

0.21.30

8 months ago

0.21.31

8 months ago

0.21.29

8 months ago

0.21.27

8 months ago

0.21.28

8 months ago

0.21.25

8 months ago

0.21.26

8 months ago

0.21.23

8 months ago

0.21.24

8 months ago

0.21.21

9 months ago

0.21.22

8 months ago

0.21.20

9 months ago

0.19.9

1 year ago

0.19.20

11 months ago

0.19.11

12 months ago

0.19.12

12 months ago

0.19.13

12 months ago

0.19.14

11 months ago

0.19.15

11 months ago

0.19.16

11 months ago

0.19.17

11 months ago

0.19.18

11 months ago

0.19.19

11 months ago

0.19.2

1 year ago

0.19.3

1 year ago

0.19.5

1 year ago

0.19.6

1 year ago

0.19.7

1 year ago

0.8.6

1 year ago

0.19.0

1 year ago

0.19.1

1 year ago

0.8.5

1 year ago

0.8.4

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.7.6

1 year ago

0.7.5

2 years ago

0.8.0-beta.1

1 year ago

0.7.0-alpha.2

2 years ago

0.7.1

2 years ago

0.7.4

2 years ago

0.7.3

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.3

2 years ago