@midnight-ntwrk/wallet-sdk-capabilities 3.0.0-beta.8 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -2
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @midnight-ntwrk/wallet-sdk-capabilities
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Internal wallet capabilities for transaction balancing on the Midnight network.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @midnight-ntwrk/wallet-sdk-capabilities
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
This package provides core transaction balancing capabilities used internally by wallet implementations. It handles the
|
|
14
|
+
complex logic of selecting inputs and creating outputs to balance transactions while accounting for fee overhead costs.
|
|
15
|
+
|
|
16
|
+
Key features:
|
|
17
|
+
|
|
18
|
+
- Transaction balancing algorithms
|
|
19
|
+
- Coin selection strategies
|
|
20
|
+
- Imbalance tracking and resolution
|
|
21
|
+
- Fee-aware counter-offer generation
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Basic Transaction Balancing
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { getBalanceRecipe, Imbalances, chooseCoin } from '@midnight-ntwrk/wallet-sdk-capabilities';
|
|
29
|
+
|
|
30
|
+
const recipe = getBalanceRecipe({
|
|
31
|
+
coins: availableCoins,
|
|
32
|
+
initialImbalances: Imbalances.fromEntry('NIGHT', -1000n), // Need 1000 NIGHT
|
|
33
|
+
transactionCostModel: {
|
|
34
|
+
inputFeeOverhead: 1000n,
|
|
35
|
+
outputFeeOverhead: 500n,
|
|
36
|
+
},
|
|
37
|
+
feeTokenType: 'DUST',
|
|
38
|
+
createOutput: (coin) => ({ type: coin.type, value: coin.value }),
|
|
39
|
+
isCoinEqual: (a, b) => a.id === b.id,
|
|
40
|
+
coinSelection: chooseCoin, // Optional: defaults to smallest-first selection
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
console.log('Inputs to add:', recipe.inputs);
|
|
44
|
+
console.log('Outputs to create:', recipe.outputs);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Working with Imbalances
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { Imbalances } from '@midnight-ntwrk/wallet-sdk-capabilities';
|
|
51
|
+
|
|
52
|
+
// Create imbalances from entries
|
|
53
|
+
const imbalances = Imbalances.fromEntries([
|
|
54
|
+
['NIGHT', -500n], // Need 500 NIGHT (negative = deficit)
|
|
55
|
+
['TOKEN_A', 200n], // Have 200 TOKEN_A excess (positive = surplus)
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
// Merge imbalances
|
|
59
|
+
const merged = Imbalances.merge(imbalances1, imbalances2);
|
|
60
|
+
|
|
61
|
+
// Get value for a token type
|
|
62
|
+
const nightImbalance = Imbalances.getValue(imbalances, 'NIGHT');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Custom Coin Selection
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { getBalanceRecipe, CoinSelection } from '@midnight-ntwrk/wallet-sdk-capabilities';
|
|
69
|
+
|
|
70
|
+
// Custom strategy: prefer larger coins
|
|
71
|
+
const largestFirst: CoinSelection<MyCoin> = (coins, tokenType, amountNeeded, costModel) => {
|
|
72
|
+
return coins
|
|
73
|
+
.filter((coin) => coin.type === tokenType)
|
|
74
|
+
.sort((a, b) => Number(b.value - a.value))
|
|
75
|
+
.at(0);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const recipe = getBalanceRecipe({
|
|
79
|
+
// ... other options
|
|
80
|
+
coinSelection: largestFirst,
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Error Handling
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { getBalanceRecipe, InsufficientFundsError } from '@midnight-ntwrk/wallet-sdk-capabilities';
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const recipe = getBalanceRecipe({
|
|
91
|
+
/* ... */
|
|
92
|
+
});
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error instanceof InsufficientFundsError) {
|
|
95
|
+
console.log(`Cannot balance: insufficient ${error.tokenType}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Exports
|
|
101
|
+
|
|
102
|
+
### Balancer
|
|
103
|
+
|
|
104
|
+
- `getBalanceRecipe` - Main function to calculate inputs/outputs needed to balance a transaction
|
|
105
|
+
- `createCounterOffer` - Creates counter offers with cost model awareness
|
|
106
|
+
- `chooseCoin` - Default coin selection strategy (smallest coin first)
|
|
107
|
+
- `InsufficientFundsError` - Error thrown when balancing fails due to insufficient funds
|
|
108
|
+
- `BalanceRecipe` - Type for balance result with inputs and outputs
|
|
109
|
+
- `CoinSelection` - Type for coin selection function
|
|
110
|
+
|
|
111
|
+
### Imbalances
|
|
112
|
+
|
|
113
|
+
- `Imbalances` - Utilities for creating and manipulating token imbalance maps
|
|
114
|
+
- `Imbalance` - Type representing a single token imbalance `[TokenType, TokenValue]`
|
|
115
|
+
- `TokenType` - String type alias for token identifiers
|
|
116
|
+
- `TokenValue` - Bigint type alias for token amounts
|
|
117
|
+
- `CoinRecipe` - Interface for basic coin structure with `type` and `value`
|
|
118
|
+
|
|
119
|
+
### CounterOffer
|
|
120
|
+
|
|
121
|
+
- `CounterOffer` - Class for building balanced transactions with fee awareness
|
|
122
|
+
- `TransactionCostModel` - Interface defining `inputFeeOverhead` and `outputFeeOverhead`
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midnight-ntwrk/wallet-sdk-capabilities",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -24,25 +24,26 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@midnight-ntwrk/wallet-sdk-address-format": "3.0.0
|
|
28
|
-
"@midnight-ntwrk/wallet-sdk-hd": "3.0.0
|
|
29
|
-
"effect": "^3.
|
|
27
|
+
"@midnight-ntwrk/wallet-sdk-address-format": "3.0.0",
|
|
28
|
+
"@midnight-ntwrk/wallet-sdk-hd": "3.0.0",
|
|
29
|
+
"effect": "^3.19.14"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@midnight-ntwrk/ledger-
|
|
32
|
+
"@midnight-ntwrk/ledger-v7": "7.0.0",
|
|
33
33
|
"eslint": "^9.37.0",
|
|
34
34
|
"fast-check": "^4.2.0",
|
|
35
|
+
"prettier": "^3.7.0",
|
|
35
36
|
"publint": "~0.3.14",
|
|
36
37
|
"rimraf": "^6.0.1",
|
|
37
38
|
"typescript": "^5.9.3",
|
|
38
|
-
"vitest": "^
|
|
39
|
+
"vitest": "^4.0.16"
|
|
39
40
|
},
|
|
40
41
|
"scripts": {
|
|
41
42
|
"typecheck": "tsc -b ./tsconfig.json --noEmit",
|
|
42
43
|
"test": "vitest run",
|
|
43
44
|
"lint": "eslint --max-warnings 0",
|
|
44
|
-
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml}\"",
|
|
45
|
-
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml}\"",
|
|
45
|
+
"format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
46
|
+
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
46
47
|
"dist": "tsc -b ./tsconfig.build.json",
|
|
47
48
|
"dist:publish": "tsc -b ./tsconfig.publish.json",
|
|
48
49
|
"clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
|