# 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

Name Type
chainNumber number

# Returns

CustomChain

# Defined in

chain/chain.ts:132 (opens new window)


# IncentivizedTestnet

IncentivizedTestnet(chainNumber?): CustomChain

# Parameters

Name Type Default value
chainNumber number 1

# 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 (opens new window)


# 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

Name Type Default value
chainNumber number 1

# Returns

CustomChain

# Defined in

chain/chain.ts:114 (opens new window)


# 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

Name Type Description
condition boolean The condition to test.
message? string Optional. 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 (opens new window)


# 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

Name Type Description
err unknown The error object to be tested.
okErrors string[] An array of acceptable error message strings.

# Returns

void

# Defined in

testutil.ts:100 (opens new window)


# 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

Name Type Description
eventType string The event type to look for in the array of events.
events ABCIEvent[] An array of ABCIEvent objects to be tested.

# Returns

void

# Defined in

testutil.ts:82 (opens new window)


# 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

Name Type Description
msgType string TxMsg type to look for within the event attributes.
events ABCIEvent[] set of events over which we're searching.

# Returns

void

# Defined in

testutil.ts:63 (opens new window)


# assertValidBlock

assertValidBlock(block, chain): void

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

# Parameters

Name Type
block Block
chain Chain

# Returns

void

# Defined in

testutil.ts:49 (opens new window)


# assertValidBlockFromJsonRpc

assertValidBlockFromJsonRpc(blockJson): void

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

# Parameters

Name Type
blockJson any

# Returns

void

# Defined in

testutil.ts:26 (opens new window)


# 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

Name Type
bz Uint8Array

# Returns

string

# Defined in

hash.ts:14 (opens new window)


# chainToParts

chainToParts(chain): ChainIdParts

Converts a Chain object to its constituent parts.

# Parameters

Name Type Description
chain Chain a Chain object

# Returns

ChainIdParts

a ChainIdParts object

# Defined in

chain/chain.ts:166 (opens new window)


# eventToMap

eventToMap(event): EventMap

eventToMap: Converts an ABCIEvent into an EventMap.

# Parameters

Name Type
event ABCIEvent

# Returns

EventMap

# Defined in

tx/event.ts:44 (opens new window)


# faucetUrlFromChain

faucetUrlFromChain(chain): string

Constructs a faucet URL from a Chain object.

# Parameters

Name Type Description
chain Chain a Chain object

# Returns

string

# Defined in

chain/useFaucet.ts:59 (opens new window)


# findEvent

findEvent(events, eventType): undefined | ABCIEvent

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

# Parameters

Name Type
events ABCIEvent[]
eventType string

# Returns

undefined | ABCIEvent

# Defined in

tx/event.ts:54 (opens new window)


# 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

Name Type
sdkDec string

# Returns

number

See

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

# Defined in

chain/parse.ts:113 (opens new window)


# fromSdkInt

fromSdkInt(intStr): number

# Parameters

Name Type
intStr string

# Returns

number

# Defined in

chain/parse.ts:166 (opens new window)


# getRegistry

getRegistry(): Registry

# Returns

Registry

# Defined in

tx/signer.ts:19 (opens new window)


# 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

Name Type Description
hex string The 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

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

Example

Copy // 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 (opens new window)


# isMsgAddMarginEncodeObject

isMsgAddMarginEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:41 (opens new window)


# isMsgClosePositionEncodeObject

isMsgClosePositionEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:77 (opens new window)


# isMsgCreatePoolEncodeObject

isMsgCreatePoolEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/spot.ts:31 (opens new window)


# isMsgDonateToEcosystemFundEncodeObject

isMsgDonateToEcosystemFundEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:86 (opens new window)


# isMsgExitPoolEncodeObject

isMsgExitPoolEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/spot.ts:47 (opens new window)


# isMsgJoinPoolEncodeObject

isMsgJoinPoolEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/spot.ts:39 (opens new window)


# isMsgMultiLiquidateEncodeObject

isMsgMultiLiquidateEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:59 (opens new window)


# isMsgOpenPositionEncodeObject

isMsgOpenPositionEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:68 (opens new window)


# isMsgPartialCloseEncodeObject

isMsgPartialCloseEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:97 (opens new window)


# isMsgRemoveMarginEncodeObject

isMsgRemoveMarginEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/perp.ts:50 (opens new window)


# isMsgSwapAssetsEncodeObject

isMsgSwapAssetsEncodeObject(encodeObject): boolean

# Parameters

Name Type
encodeObject EncodeObject

# Returns

boolean

# Defined in

msg/spot.ts:55 (opens new window)


# 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

Name Type
chain Chain

# Returns

Promise<boolean>

# Defined in

chain/chain.ts:156 (opens new window)


# newCoinMapFromCoins

newCoinMapFromCoins(coins): CoinMap

# Parameters

Name Type
coins readonly Coin[]

# Returns

CoinMap

# Defined in

chain/types.ts:32 (opens new window)


# newRandomWallet

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

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

# Parameters

Name Type Default value Description
length? 12 | 15 | 18 | 21 | 24 undefined (optional) The number of words in the mnemonic (12, 15, 18, 21 or 24).
prefix BECH32_PREFIX BECH32_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 (opens new window)


# newSignerFromMnemonic

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

Creates a wallet from the given BIP39 mnemonic.

# Parameters

Name Type Default value Description
mnemonic string undefined
prefix BECH32_PREFIX BECH32_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 (opens new window)


# 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

Name Type
err any

# Returns

Error

# Defined in

result.ts:75 (opens new window)


# parseEventLogs

parseEventLogs(txResp): EventMap[]

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

# Parameters

Name Type
txResp DeliverTxResponse

# Returns

EventMap[]

Example

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

# Defined in

tx/event.ts:67 (opens new window)


# queryChainIdWithRest

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

# Parameters

Name Type
chain Chain

# Returns

Promise<Result<string>>

# Defined in

chain/chain.ts:139 (opens new window)


# setupEpochsExtension

setupEpochsExtension(base): EpochsExtension

# Parameters

Name Type
base QueryClient

# Returns

EpochsExtension

# Defined in

query/epochs.ts:19 (opens new window)


# setupInflationExtension

setupInflationExtension(base): InflationExtension

# Parameters

Name Type
base QueryClient

# Returns

InflationExtension

# Defined in

query/inflation.ts:29 (opens new window)


# setupOracleExtension

setupOracleExtension(base): OracleExtension

# Parameters

Name Type
base QueryClient

# Returns

OracleExtension

# Defined in

query/oracle.ts:91 (opens new window)


# setupPerpExtension

setupPerpExtension(base): PerpExtension

# Parameters

Name Type
base QueryClient

# Returns

PerpExtension

# Defined in

query/perp.ts:45 (opens new window)


# setupSpotExtension

setupSpotExtension(base): SpotExtension

# Parameters

Name Type
base QueryClient

# Returns

SpotExtension

# Defined in

query/spot.ts:102 (opens new window)


# setupSudoExtension

setupSudoExtension(base): SudoExtension

# Parameters

Name Type
base QueryClient

# Returns

SudoExtension

# Defined in

query/sudo.ts:14 (opens new window)


# 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

Name Type
dec string

# 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 (opens new window)


# toSdkInt

toSdkInt(i): string

# Parameters

Name Type
i number

# Returns

string

# Defined in

chain/parse.ts:164 (opens new window)


# transformPool

transformPool(p?): undefined | Pool

# Parameters

Name Type
p? Pool

# Returns

undefined | Pool

# Defined in

query/spot.ts:49 (opens new window)


# transformPoolParams

transformPoolParams(pp?): undefined | PoolParams

# Parameters

Name Type
pp? PoolParams

# Returns

undefined | PoolParams

# Defined in

query/spot.ts:41 (opens new window)


# useFaucet

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

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

# Parameters

Name Type
«destructured» Object
› address string
› amts? Object
› amts.nibi number
› amts.nusd number
› amts.usdt number
› chain Chain
› grecaptcha string

# Returns

Promise<Response | undefined>

# Defined in

chain/useFaucet.ts:7 (opens new window)