# @midnightntwrk/wallet-sdk-capabilities

> Internal wallet capabilities for transaction balancing on the Midnight network.

Latest version **3.3.1** (published 2026-06-19) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @midnightntwrk/wallet-sdk-capabilities
pnpm add @midnightntwrk/wallet-sdk-capabilities
yarn add @midnightntwrk/wallet-sdk-capabilities
bun add @midnightntwrk/wallet-sdk-capabilities
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.3.1 |
| Published | 2026-06-19 |
| First published | 2026-06-19 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 119.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Midnight Foundation |
| Maintainers | ntwrk-bot |

## Links

- npm: https://www.npmjs.com/package/@midnightntwrk/wallet-sdk-capabilities
- Repository: https://github.com/midnightntwrk/midnight-wallet
- Homepage: https://github.com/midnightntwrk/midnight-wallet#readme
- Issues: https://github.com/midnightntwrk/midnight-wallet/issues
- npm.io page: https://npm.io/package/@midnightntwrk/wallet-sdk-capabilities

## Dependencies (9)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.2
- [effect](https://npm.io/package/effect.md) ^3.19.19
- [@midnight-ntwrk/zkir-v2](https://npm.io/package/@midnight-ntwrk/zkir-v2.md) ^2.1.0
- [@midnight-ntwrk/ledger-v8](https://npm.io/package/@midnight-ntwrk/ledger-v8.md) ^8.1.0
- [@midnightntwrk/wallet-sdk-utilities](https://npm.io/package/@midnightntwrk/wallet-sdk-utilities.md) ^1.2.0
- [@midnightntwrk/wallet-sdk-node-client](https://npm.io/package/@midnightntwrk/wallet-sdk-node-client.md) ^1.1.2
- [@midnightntwrk/wallet-sdk-abstractions](https://npm.io/package/@midnightntwrk/wallet-sdk-abstractions.md) ^2.1.0
- [@midnightntwrk/wallet-sdk-prover-client](https://npm.io/package/@midnightntwrk/wallet-sdk-prover-client.md) ^1.2.2
- [@midnightntwrk/wallet-sdk-indexer-client](https://npm.io/package/@midnightntwrk/wallet-sdk-indexer-client.md) ^1.2.2

## Recent versions

- 3.3.1 (latest) — 2026-06-19
- 4.0.0-canary.20261013150531-6289ea3 (canary) — 2026-10-13
- 4.0.0-canary.20261009161442-8f95733 (canary-v1) — 2026-10-09
- 4.0.0-rc.1 (rc) — 2026-10-07
- 4.0.0-canary.20261007114939-7ee351c (canary-v2) — 2026-10-07
- 4.0.0-beta.4 (beta) — 2026-09-24
- 4.0.0-canary.20261013132526-8a87477 — 2026-10-13
- 4.0.0-canary.20261012130426-60e35b6 — 2026-10-12
- 4.0.0-canary.20261006161905-56542fa — 2026-10-06
- 4.0.0-canary.20261006082857-3c6134f — 2026-10-06
- 4.0.0-canary.20261005170924-1c8b4e7 — 2026-10-05
- 3.4.0-canary.20260930095107-aae483d — 2026-09-30
- 3.4.0-canary.20260929170316-1583a02 — 2026-09-29
- 4.0.0-canary.20260928141213-d0f38b3 — 2026-09-28
- 4.0.0-rc.0 — 2026-09-24
- … 83 more at https://npm.io/package/@midnightntwrk/wallet-sdk-capabilities/versions

## README

# @midnightntwrk/wallet-sdk-capabilities

Internal wallet capabilities for transaction balancing on the Midnight network.

## Installation

```bash
npm install @midnightntwrk/wallet-sdk-capabilities
```

## Overview

This package provides core transaction balancing capabilities used internally by wallet implementations. It handles the
complex logic of selecting inputs and creating outputs to balance transactions while accounting for fee overhead costs.

Key features:

- Transaction balancing algorithms
- Coin selection strategies
- Imbalance tracking and resolution
- Fee-aware counter-offer generation

## Usage

### Basic Transaction Balancing

```typescript
import { getBalanceRecipe, Imbalances, chooseCoin } from '@midnightntwrk/wallet-sdk-capabilities';

const recipe = getBalanceRecipe({
  coins: availableCoins,
  initialImbalances: Imbalances.fromEntry('NIGHT', -1000n), // Need 1000 NIGHT
  transactionCostModel: {
    inputFeeOverhead: 1000n,
    outputFeeOverhead: 500n,
  },
  feeTokenType: 'DUST',
  createOutput: (coin) => ({ type: coin.type, value: coin.value }),
  isCoinEqual: (a, b) => a.id === b.id,
  coinSelection: chooseCoin, // Optional: defaults to smallest-first selection
});

console.log('Inputs to add:', recipe.inputs);
console.log('Outputs to create:', recipe.outputs);
```

### Working with Imbalances

```typescript
import { Imbalances } from '@midnightntwrk/wallet-sdk-capabilities';

// Create imbalances from entries
const imbalances = Imbalances.fromEntries([
  ['NIGHT', -500n], // Need 500 NIGHT (negative = deficit)
  ['TOKEN_A', 200n], // Have 200 TOKEN_A excess (positive = surplus)
]);

// Merge imbalances
const merged = Imbalances.merge(imbalances1, imbalances2);

// Get value for a token type
const nightImbalance = Imbalances.getValue(imbalances, 'NIGHT');
```

### Custom Coin Selection

```typescript
import { getBalanceRecipe, CoinSelection } from '@midnightntwrk/wallet-sdk-capabilities';

// Custom strategy: prefer larger coins
const largestFirst: CoinSelection<MyCoin> = (coins, tokenType, amountNeeded, costModel) => {
  return coins
    .filter((coin) => coin.type === tokenType)
    .sort((a, b) => Number(b.value - a.value))
    .at(0);
};

const recipe = getBalanceRecipe({
  // ... other options
  coinSelection: largestFirst,
});
```

### Error Handling

```typescript
import { getBalanceRecipe, InsufficientFundsError } from '@midnightntwrk/wallet-sdk-capabilities';

try {
  const recipe = getBalanceRecipe({
    /* ... */
  });
} catch (error) {
  if (error instanceof InsufficientFundsError) {
    console.log(`Cannot balance: insufficient ${error.tokenType}`);
  }
}
```

## Exports

### Balancer

- `getBalanceRecipe` - Main function to calculate inputs/outputs needed to balance a transaction
- `createCounterOffer` - Creates counter offers with cost model awareness
- `chooseCoin` - Default coin selection strategy (smallest coin first)
- `InsufficientFundsError` - Error thrown when balancing fails due to insufficient funds
- `BalanceRecipe` - Type for balance result with inputs and outputs
- `CoinSelection` - Type for coin selection function

### Imbalances

- `Imbalances` - Utilities for creating and manipulating token imbalance maps
- `Imbalance` - Type representing a single token imbalance `[TokenType, TokenValue]`
- `TokenType` - String type alias for token identifiers
- `TokenValue` - Bigint type alias for token amounts
- `CoinRecipe` - Interface for basic coin structure with `type` and `value`

### CounterOffer

- `CounterOffer` - Class for building balanced transactions with fee awareness
- `TransactionCostModel` - Interface defining `inputFeeOverhead` and `outputFeeOverhead`

## License

Apache-2.0

---
_Source: https://npm.io/package/@midnightntwrk/wallet-sdk-capabilities · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
