Skip to content

NibiJS - Functions

NibiJS - Functions

Devnet

Devnet(chainNumber): CustomChain

Devnet: "Chain" configuration for a Nibiru "devnet". These networks are more ephemeral than "Testnet" and used internally by the core Nibiru dev team to live-test new features before official public release.

Parameters

NameType
chainNumbernumber

Returns

CustomChain

Defined in

chain/chain.ts:132


IncentivizedTestnet

IncentivizedTestnet(chainNumber?): CustomChain

Parameters

NameTypeDefault value
chainNumbernumber1

Returns

CustomChain

Deprecated

Incentivized testnet is no longer active. This variable exists for backwards compatibility, but "Testnet" should be used instead.

See

Testnet - Permanent Nibiru public test network.

Defined in

chain/chain.ts:114


Testnet

Testnet(chainNumber?): CustomChain

Testnet: "Chain" configuration for a Nibiru testnet. These are public networks that are upgraded in advance of Nibiru's mainnet network as a beta-testing environments.

For an updated list of active networks, see: TODO: Add networks link

By default, the "Testnet" function returns the permanent testnet if no arguments are passed.

Parameters

NameTypeDefault value
chainNumbernumber1

Returns

CustomChain

Defined in

chain/chain.ts:114


assert

assert(condition, message?): string | boolean

Asserts that a given condition is true. If the condition evaluates to false, an "AssertionError" is thrown with an optional custom message.

Parameters

NameTypeDescription
conditionbooleanThe condition to test.
message?stringOptional. A custom error message to display if the assertion fails.

Returns

string | boolean

  • Returns true if the assertion is successful.

Defined in

chain/types.ts:14


assertExpectedError

assertExpectedError(err, okErrors): void

Asserts that the given error matches one of the expected error messages.

This function parses the error object to extract its message and checks if it includes any of the specified acceptable error messages. It sets a test expectation that the error message is contained within the list of acceptable errors.

Parameters

NameTypeDescription
errunknownThe error object to be tested.
okErrorsstring[]An array of acceptable error message strings.

Returns

void

Defined in

testutil.ts:100


assertHasEventType

assertHasEventType(eventType, events): void

Asserts that an array of ABCIEvent objects contains an event with a specified type. This runs a test expectation that the specified eventType is contained in events.

Parameters

NameTypeDescription
eventTypestringThe event type to look for in the array of events.
eventsABCIEvent[]An array of ABCIEvent objects to be tested.

Returns

void

Defined in

testutil.ts:82


assertHasMsgType

assertHasMsgType(msgType, events): void

Asserts that a list of ABCIEvents contains a specific type of TxMsg. This TxMsg type is read from a "message" event's "action" attribute.

Parameters

NameTypeDescription
msgTypestringTxMsg type to look for within the event attributes.
eventsABCIEvent[]set of events over which we're searching.

Returns

void

Defined in

testutil.ts:63


assertValidBlock

assertValidBlock(block, chain): void

assertValidBlock: Performs runtime type validation on a CometBFT "Block".

Parameters

NameType
blockBlock
chainChain

Returns

void

Defined in

testutil.ts:49


assertValidBlockFromJsonRpc

assertValidBlockFromJsonRpc(blockJson): void

Validates that block queried via the JSON RPC client has the expected fields.

Parameters

NameType
blockJsonany

Returns

void

Defined in

testutil.ts:26


bytesToHex

bytesToHex(bz): string

Converts an 8-bit byte array (Uint8Array) into a hexadecimal (hex) string.

Each 8-bit byte ranges from 0 to 255 and is represented by a 2-tuple of hex digits (0-9, A-F). Values from 0-15 (0x0 to 0xF) are indeed single-digit in hex, but in the context of hex strings, these values are padded with a leading zero to maintain a consistent two-character representation for each byte.

Thus, a byte value of 9 is represented as "09".

Parameters

NameType
bzUint8Array

Returns

string

Defined in

hash.ts:14


chainToParts

chainToParts(chain): ChainIdParts

Converts a Chain object to its constituent parts.

Parameters

NameTypeDescription
chainChaina Chain object

Returns

ChainIdParts

a ChainIdParts object

Defined in

chain/chain.ts:166


eventToMap

eventToMap(event): EventMap

eventToMap: Converts an ABCIEvent into an EventMap.

Parameters

NameType
eventABCIEvent

Returns

EventMap

Defined in

tx/event.ts:44


faucetUrlFromChain

faucetUrlFromChain(chain): string

Constructs a faucet URL from a Chain object.

Parameters

NameTypeDescription
chainChaina Chain object

Returns

string

Defined in

chain/useFaucet.ts:59


findEvent

findEvent(events, eventType): undefined | ABCIEvent

findEvent: Filter 'events' by type. This is useful for checking if events of known type are present.

Parameters

NameType
eventsABCIEvent[]
eventTypestring

Returns

undefined | ABCIEvent

Defined in

tx/event.ts:54


fromSdkDec

fromSdkDec(sdkDec): number

fromSdkDec: Converts a string representation of the "sdk.Dec", a shorthand name for the "cosmossdk.io/math".LegacyDec type in Golang. An Sdk Dec is a decimal/float implemented by "big.Int" with 18 decimals of precision an abstraction for 18 decimals of precision big.Int.

Sdk Dec is a custom protobuf type encoded as a string. NOTE: The string for the raw protobuf value is not the human-readable one that can include decimal points and negative signs. It's actually a string holding the underlying "big.Int" value from which the concrete Dec type is created.

This is why we implement the functions fromSdkDec and toSdkDec. When 'TxMessages' include SdkDec types, they need the protobuf string form, not the human-readbale Dec.

Parameters

NameType
sdkDecstring

Returns

number

See

  • TxMessage // from nibijs/src/tx
  • toSdkDec

Defined in

chain/parse.ts:113


fromSdkInt

fromSdkInt(intStr): number

Parameters

NameType
intStrstring

Returns

number

Defined in

chain/parse.ts:166


getRegistry

getRegistry(): Registry

Returns

Registry

Defined in

tx/signer.ts:19


hexToBytes

hexToBytes(hex): Result<Uint8Array>

Converts a hexadecimal-encoded string into a Uint8Array.

The hexadecimal string must have an even length, as each byte is represented by two hex digits. Each of hex digit 2-tuples (ranging from 00 to FF) is converted to a single byte ranging from 0 to 255.

Parameters

NameTypeDescription
hexstringThe hexadecimal string to be decoded. If the string has an odd length or contains non-hexadecimal characters, the function returns an error wrapped in a Result object rather than throwing.

Returns

Result<Uint8Array>

The decoded Uint8Array if successful or an error result if the input is invalid.

Example

ts
// Successful decoding
const result = hexToBytes("7A919F2CC9A51B139444F7D8E84A46EE")
if (result.isOk()) {
  console.log(result.ok) // Uint8Array of bytes
} else {
  console.error(result.err) // Error
}

Example

ts
// Error handling for invalid hex string
const result = hexToBytes("7G919F")
if (result.isOk()) {
  console.log(result.ok)
} else {
  console.error(result.err.message)
  // "HexError: non-hex characters detected in hex: 7G919F"
}

Defined in

hash.ts:51


isMsgAddMarginEncodeObject

isMsgAddMarginEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:41


isMsgClosePositionEncodeObject

isMsgClosePositionEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:77


isMsgCreatePoolEncodeObject

isMsgCreatePoolEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/spot.ts:31


isMsgDonateToEcosystemFundEncodeObject

isMsgDonateToEcosystemFundEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:86


isMsgExitPoolEncodeObject

isMsgExitPoolEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/spot.ts:47


isMsgJoinPoolEncodeObject

isMsgJoinPoolEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/spot.ts:39


isMsgMultiLiquidateEncodeObject

isMsgMultiLiquidateEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:59


isMsgOpenPositionEncodeObject

isMsgOpenPositionEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:68


isMsgPartialCloseEncodeObject

isMsgPartialCloseEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:97


isMsgRemoveMarginEncodeObject

isMsgRemoveMarginEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/perp.ts:50


isMsgSwapAssetsEncodeObject

isMsgSwapAssetsEncodeObject(encodeObject): boolean

Parameters

NameType
encodeObjectEncodeObject

Returns

boolean

Defined in

msg/spot.ts:55


isRestEndptLive

isRestEndptLive(chain): Promise<boolean>

isRestEndptLive: Makes a request using the chain's REST endpoint to see if the network and endpoint are active.

Parameters

NameType
chainChain

Returns

Promise<boolean>

Defined in

chain/chain.ts:156


newCoinMapFromCoins

newCoinMapFromCoins(coins): CoinMap

Parameters

NameType
coinsreadonly Coin[]

Returns

CoinMap

Defined in

chain/types.ts:32


newRandomWallet

newRandomWallet(length?, prefix?): Promise<DirectSecp256k1HdWallet>

Generates a new wallet with a BIP39 mnemonic of length 24.

Parameters

NameTypeDefault valueDescription
length?12 | 15 | 18 | 21 | 24undefined(optional) The number of words in the mnemonic (12, 15, 18, 21 or 24).
prefixBECH32_PREFIXBECH32_PREFIX.ADDR(optional) Bech32 address prefix. Defaults to "nibi".

Returns

Promise<DirectSecp256k1HdWallet>

A wallet for protobuf based signing using SIGN_MODE_DIRECT.

Export

Defined in

tx/signer.ts:45


newSignerFromMnemonic

newSignerFromMnemonic(mnemonic, prefix?): Promise<DirectSecp256k1HdWallet>

Creates a wallet from the given BIP39 mnemonic.

Parameters

NameTypeDefault valueDescription
mnemonicstringundefined
prefixBECH32_PREFIXBECH32_PREFIX.ADDR(optional) Bech32 address prefix. Defaults to "nibi".

Returns

Promise<DirectSecp256k1HdWallet>

A wallet for protobuf based signing using SIGN_MODE_DIRECT

Export

Defined in

tx/signer.ts:29


parseError

parseError(err): Error

parseError: Guarantees runtime strong error typing since this isn't guaranteed in JS by default. The error that comes out of a try-catch may not have type "Error" since it's perfectly valid to throw strings or undefined.

Parameters

NameType
errany

Returns

Error

Defined in

result.ts:75


parseEventLogs

parseEventLogs(txResp): EventMap[]

parseEventLogs: Returns a mutable and typed version of the events payload from a tx response.

Parameters

NameType
txRespDeliverTxResponse

Returns

EventMap[]

Example

ts
let txResp: DeliverTxResponse // assume this is given
const eventLogs = parseEventLogs(txResp)

Defined in

tx/event.ts:67


queryChainIdWithRest

queryChainIdWithRest(chain): Promise<Result<string>>

Parameters

NameType
chainChain

Returns

Promise<Result<string>>

Defined in

chain/chain.ts:139


setupEpochsExtension

setupEpochsExtension(base): EpochsExtension

Parameters

NameType
baseQueryClient

Returns

EpochsExtension

Defined in

query/epochs.ts:19


setupInflationExtension

setupInflationExtension(base): InflationExtension

Parameters

NameType
baseQueryClient

Returns

InflationExtension

Defined in

query/inflation.ts:29


setupOracleExtension

setupOracleExtension(base): OracleExtension

Parameters

NameType
baseQueryClient

Returns

OracleExtension

Defined in

query/oracle.ts:91


setupPerpExtension

setupPerpExtension(base): PerpExtension

Parameters

NameType
baseQueryClient

Returns

PerpExtension

Defined in

query/perp.ts:45


setupSpotExtension

setupSpotExtension(base): SpotExtension

Parameters

NameType
baseQueryClient

Returns

SpotExtension

Defined in

query/spot.ts:102


setupSudoExtension

setupSudoExtension(base): SudoExtension

Parameters

NameType
baseQueryClient

Returns

SudoExtension

Defined in

query/sudo.ts:14


toSdkDec

toSdkDec(dec): string

toSdkDec converts the input float string to an sdk.Dec. The maximum number of decimal places for an sdk.Dec is 18. NOTE: An error is console loggedd if more decimal digits are provided than the precision, 18.

ref: Reimplementation of cosmos-sdk/types/decimal.go

Valid inputs must come in the form: (-) integer digits (.) fractional digits Examples of acceptable input include: -123.456 456.7890 345 -456789

CONTRACT - This function does not mutate the input str.

Parameters

NameType
decstring

Returns

string

  • Protobuf string for an sdk.Dec, which is represented by its underlying "big.Int".

See

fromSdkDec - The inverse of this function that converts an sdk.Dec protobuf string into a number.

Export

Defined in

chain/parse.ts:30


toSdkInt

toSdkInt(i): string

Parameters

NameType
inumber

Returns

string

Defined in

chain/parse.ts:164


transformPool

transformPool(p?): undefined | Pool

Parameters

NameType
p?Pool

Returns

undefined | Pool

Defined in

query/spot.ts:49


transformPoolParams

transformPoolParams(pp?): undefined | PoolParams

Parameters

NameType
pp?PoolParams

Returns

undefined | PoolParams

Defined in

query/spot.ts:41


useFaucet

useFaucet(«destructured»): Promise<Response | undefined>

Sends 11 NIBI, 100 NUSD, and 100 USDT to the given address from the testnet faucet.

Parameters

NameType
«destructured»Object
› addressstring
› amts?Object
› amts.nibinumber
› amts.nusdnumber
› amts.usdtnumber
› chainChain
› grecaptchastring

Returns

Promise<Response | undefined>

Defined in

chain/useFaucet.ts:7