1- # Installation
1+ # Install Nimiq Utils
22
3- Learn how to install and set up Nimiq Utils in your project with different package managers and bundlers .
3+ A comprehensive JavaScript/TypeScript utility library for the Nimiq blockchain ecosystem .
44
5- ## Package Manager Installation
5+ ::: code-group
66
7- ### pnpm (Recommended)
8-
9- ``` bash
7+ ``` bash [pnpm]
108pnpm add @nimiq/utils
119```
1210
13- ### npm
14-
15- ``` bash
11+ ``` bash [npm]
1612npm install @nimiq/utils
1713```
1814
19- ### yarn
20-
21- ``` bash
15+ ``` bash [yarn]
2216yarn add @nimiq/utils
2317```
2418
19+ ``` bash [bun]
20+ bun add @nimiq/utils
21+ ```
22+
23+ :::
24+
2525## Basic Usage
2626
2727Nimiq Utils is designed with tree-shaking in mind. Import only the modules you need to keep your bundle size optimized.
2828
2929``` typescript
3030import { AddressBook } from ' @nimiq/utils/address-book'
3131import { FormattableNumber } from ' @nimiq/utils/formattable-number'
32- // Import specific modules
32+ import { calculateStakingRewards } from ' @nimiq/utils/rewards-calculator '
3333import { ValidationUtils } from ' @nimiq/utils/validation-utils'
3434
3535// Use the utilities
@@ -38,18 +38,25 @@ const isValid = ValidationUtils.isValidAddress(address)
3838const label = AddressBook .getLabel (address )
3939const amount = new FormattableNumber (' 1234.56' ).toString ({ maxDecimals: 2 })
4040
41- console .log ({ isValid , label , amount })
41+ // Calculate staking rewards
42+ const rewards = calculateStakingRewards ({
43+ stakedSupplyRatio: 0.5 ,
44+ amount: 100000 ,
45+ days: 365
46+ })
47+
48+ console .log ({ isValid , label , amount , rewards })
4249```
4350
4451## TypeScript Support
4552
4653Nimiq Utils is written in TypeScript and includes full type definitions out of the box. No additional ` @types ` packages needed.
4754
4855``` typescript
49- import type { StakingParams , StakingResult } from ' @nimiq/utils/rewards-calculator'
56+ import type { CalculateStakingRewardsParams , CalculateStakingRewardsResult } from ' @nimiq/utils/rewards-calculator'
5057import { calculateStakingRewards } from ' @nimiq/utils/rewards-calculator'
5158
52- const params: StakingParams = {
59+ const params: CalculateStakingRewardsParams = {
5360 stakedSupplyRatio: 0.5 ,
5461 amount: 100000 ,
5562 days: 365 ,
@@ -58,16 +65,16 @@ const params: StakingParams = {
5865 fee: 0.02
5966}
6067
61- const result: StakingResult = calculateStakingRewards (params )
68+ const result: CalculateStakingRewardsResult = calculateStakingRewards (params )
6269```
6370
6471## Framework Integration
6572
66- ### Vue.js / Nuxt
73+ ::: code-group
6774
68- ``` typescript
69- import { FormattableNumber } from ' @nimiq/utils/formattable-number'
75+ ``` typescript [Vue.js / Nuxt]
7076// composables/useNimiqUtils.ts
77+ import { FormattableNumber } from ' @nimiq/utils/formattable-number'
7178import { ValidationUtils } from ' @nimiq/utils/validation-utils'
7279
7380export function useNimiqUtils () {
@@ -89,12 +96,10 @@ export function useNimiqUtils() {
8996}
9097```
9198
92- ### React / Next.js
93-
94- ``` typescript
99+ ``` typescript [React / Next.js]
100+ // hooks/useNimiqUtils.ts
95101import { FormattableNumber } from ' @nimiq/utils/formattable-number'
96102import { ValidationUtils } from ' @nimiq/utils/validation-utils'
97- // hooks/useNimiqUtils.ts
98103import { useCallback } from ' react'
99104
100105export function useNimiqUtils() {
@@ -116,9 +121,7 @@ export function useNimiqUtils() {
116121}
117122```
118123
119- ### Vanilla JavaScript
120-
121- ``` html
124+ ``` html [Vanilla JavaScript]
122125<!DOCTYPE html>
123126<html >
124127<head >
@@ -141,74 +144,25 @@ export function useNimiqUtils() {
141144</html >
142145```
143146
144- ## Bundle Size Optimization
145-
146- Nimiq Utils is designed for optimal tree-shaking. Only import what you use:
147-
148- ``` typescript
149- // ❌ Avoid - Imports entire library
150- import * as NimiqUtils from ' @nimiq/utils'
151- import { FormattableNumber } from ' @nimiq/utils/formattable-number'
152-
153- // ✅ Good - Only imports specific modules
154- import { ValidationUtils } from ' @nimiq/utils/validation-utils'
155- ```
156-
157- ## Environment Requirements
158-
159- - ** Node.js** : 16.0 or higher
160- - ** Browser** : Modern browsers with ES2020 support
161- - ** TypeScript** : 4.5 or higher (optional)
162-
163- ## CDN Usage
164-
165- For quick prototyping or legacy environments:
166-
167- ``` html
147+ ``` html [CDN Usage]
168148<script type =" module" >
169149 import { ValidationUtils } from ' https://cdn.skypack.dev/@nimiq/utils/validation-utils'
170150 // Use ValidationUtils...
171151 </script >
172152```
173153
174- ## Troubleshooting
175-
176- ### Module Resolution Issues
177-
178- If you encounter module resolution issues, ensure your bundler supports ES modules:
179-
180- ``` json
181- // tsconfig.json
182- {
183- "compilerOptions" : {
184- "moduleResolution" : " node" ,
185- "esModuleInterop" : true ,
186- "allowSyntheticDefaultImports" : true
187- }
188- }
189- ```
154+ :::
190155
191- ### Vite Configuration
192-
193- For Vite projects, no additional configuration is needed. Nimiq Utils works out of the box.
156+ ## Bundle Size Optimization
194157
195- ### Webpack Configuration
158+ Nimiq Utils is designed for optimal tree-shaking. Only import what you need:
196159
197- For older Webpack versions, you might need to ensure ES module support:
160+ ``` typescript
161+ // ❌ Avoid - Imports entire library
162+ import * as NimiqUtils from ' @nimiq/utils'
198163
199- ``` javascript
200- // webpack.config.js
201- module .exports = {
202- resolve: {
203- extensionAlias: {
204- ' .js' : [' .ts' , ' .js' ]
205- }
206- }
207- }
164+ // ✅ Good - Import from specific modules for best tree-shaking
165+ import { FormattableNumber } from ' @nimiq/utils/formattable-number'
166+ import { calculateStakingRewards } from ' @nimiq/utils/rewards-calculator'
167+ import { ValidationUtils } from ' @nimiq/utils/validation-utils'
208168```
209-
210- ## Next Steps
211-
212- - ** [ 📖 Why Nimiq Utils?] ( ./why ) ** - Learn about the benefits
213- - ** [ 🚀 Browse Modules] ( ./index#available-modules ) ** - Explore all available utilities
214- - ** [ 💡 Examples] ( https://github.com/nimiq/nimiq-utils/tree/master/examples ) ** - See real-world usage examples
0 commit comments