@totemsdk/omnia-router 0.3.0 → 0.4.1
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/LICENSE +202 -21
- package/NOTICE +60 -0
- package/dist/bounded-search.d.ts +36 -0
- package/dist/bounded-search.js +164 -0
- package/dist/cjs/bounded-search.d.ts +36 -0
- package/dist/cjs/bounded-search.js +174 -0
- package/dist/cjs/execute.js +63 -7
- package/dist/cjs/graph.d.ts +8 -0
- package/dist/cjs/graph.js +42 -3
- package/dist/cjs/pathfind.d.ts +11 -12
- package/dist/cjs/pathfind.js +87 -189
- package/dist/cjs/rates.d.ts +9 -0
- package/dist/cjs/rates.js +27 -0
- package/dist/cjs/route-validation.d.ts +24 -0
- package/dist/cjs/route-validation.js +91 -0
- package/dist/cjs/routing-engine.d.ts +86 -0
- package/dist/cjs/routing-engine.js +154 -0
- package/dist/cjs/types.d.ts +8 -1
- package/dist/execute.js +63 -7
- package/dist/graph.d.ts +8 -0
- package/dist/graph.js +40 -2
- package/dist/pathfind.d.ts +11 -12
- package/dist/pathfind.js +84 -186
- package/dist/rates.d.ts +9 -0
- package/dist/rates.js +22 -0
- package/dist/route-validation.d.ts +24 -0
- package/dist/route-validation.js +84 -0
- package/dist/routing-engine.d.ts +86 -0
- package/dist/routing-engine.js +147 -0
- package/dist/types.d.ts +8 -1
- package/package.json +9 -7
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { bfsLayers, bfsRoute, computeLayers, defaultEdgeFee, isZeroFeeToken, reconstructFromLayers, routeFromLayers, } from './bounded-search.js';
|
|
2
|
+
import { getGraphRevisions } from './graph.js';
|
|
3
|
+
export const DEFAULT_MAX_HOPS = 8;
|
|
4
|
+
export function createRoutingGraphSnapshot(graph) {
|
|
5
|
+
return { graph, revision: getGraphRevisions(graph) };
|
|
6
|
+
}
|
|
7
|
+
export const FIXED_AMOUNT_COST_MODEL = {
|
|
8
|
+
id: 'fixed-amount-v1',
|
|
9
|
+
nonNegative: true,
|
|
10
|
+
additive: true,
|
|
11
|
+
amountDependent: false,
|
|
12
|
+
edgeFee: defaultEdgeFee,
|
|
13
|
+
compare(a, b) {
|
|
14
|
+
if (a.fees < b.fees)
|
|
15
|
+
return -1;
|
|
16
|
+
if (a.fees > b.fees)
|
|
17
|
+
return 1;
|
|
18
|
+
return a.hops - b.hops;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
export class UnsupportedRoutingModeError extends Error {
|
|
22
|
+
constructor(mode) {
|
|
23
|
+
super(`Routing mode unsupported: ${mode}`);
|
|
24
|
+
this.mode = mode;
|
|
25
|
+
this.name = 'UnsupportedRoutingModeError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function resolveMaxHops(query) {
|
|
29
|
+
if (query.maxHops === undefined)
|
|
30
|
+
return DEFAULT_MAX_HOPS;
|
|
31
|
+
if (query.maxHops === null)
|
|
32
|
+
throw new UnsupportedRoutingModeError('unbounded');
|
|
33
|
+
if (!Number.isInteger(query.maxHops) || query.maxHops < 0) {
|
|
34
|
+
throw new Error(`maxHops must be a non-negative integer (got ${String(query.maxHops)})`);
|
|
35
|
+
}
|
|
36
|
+
return query.maxHops;
|
|
37
|
+
}
|
|
38
|
+
class LayeredRoutingEngine {
|
|
39
|
+
constructor() {
|
|
40
|
+
this.id = 'layered-bounded-v1';
|
|
41
|
+
this.capabilities = {
|
|
42
|
+
bounded: true,
|
|
43
|
+
unbounded: false,
|
|
44
|
+
oneToMany: true,
|
|
45
|
+
incremental: false,
|
|
46
|
+
crossToken: true,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
findRoute(snapshot, query, costModel = FIXED_AMOUNT_COST_MODEL) {
|
|
50
|
+
const maxHops = resolveMaxHops(query);
|
|
51
|
+
if (query.amount <= 0n || !query.destination)
|
|
52
|
+
return null;
|
|
53
|
+
if (isZeroFeeToken(snapshot.graph, query.tokenId, query.amount, costModel.edgeFee)) {
|
|
54
|
+
return bfsRoute(snapshot.graph, query.source, query.destination, query.amount, query.tokenId, maxHops);
|
|
55
|
+
}
|
|
56
|
+
const layers = computeLayers(snapshot.graph, query.source, query.amount, query.tokenId, maxHops, costModel.edgeFee);
|
|
57
|
+
return routeFromLayers(layers, query.destination, query.amount, query.tokenId, maxHops);
|
|
58
|
+
}
|
|
59
|
+
computeTree(snapshot, query, costModel = FIXED_AMOUNT_COST_MODEL) {
|
|
60
|
+
const maxHops = resolveMaxHops(query);
|
|
61
|
+
let layers;
|
|
62
|
+
if (query.amount <= 0n) {
|
|
63
|
+
layers = Array.from({ length: maxHops + 1 }, () => new Map());
|
|
64
|
+
}
|
|
65
|
+
else if (isZeroFeeToken(snapshot.graph, query.tokenId, query.amount, costModel.edgeFee)) {
|
|
66
|
+
// Zero-fee trees carry shortest-hop labels only (longer zero-fee paths are never better).
|
|
67
|
+
layers = bfsLayers(snapshot.graph, query.source, query.amount, query.tokenId, maxHops);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
layers = computeLayers(snapshot.graph, query.source, query.amount, query.tokenId, maxHops, costModel.edgeFee);
|
|
71
|
+
}
|
|
72
|
+
return createRoutingTree(snapshot, query, costModel, maxHops, layers);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function createRoutingTree(snapshot, query, costModel, maxHops, layers) {
|
|
76
|
+
const destinations = new Set();
|
|
77
|
+
for (const layer of layers) {
|
|
78
|
+
for (const node of layer.keys())
|
|
79
|
+
destinations.add(node);
|
|
80
|
+
}
|
|
81
|
+
const labelAt = (destination, hops) => {
|
|
82
|
+
if (!Number.isInteger(hops) || hops < 0 || hops > maxHops)
|
|
83
|
+
return null;
|
|
84
|
+
const label = layers[hops]?.get(destination);
|
|
85
|
+
if (!label)
|
|
86
|
+
return null;
|
|
87
|
+
const routeHops = reconstructFromLayers(layers, hops, destination, query.amount, query.tokenId);
|
|
88
|
+
return {
|
|
89
|
+
hops,
|
|
90
|
+
totalFees: label.totalFees,
|
|
91
|
+
route: {
|
|
92
|
+
hops: routeHops,
|
|
93
|
+
totalFees: label.totalFees,
|
|
94
|
+
tokenIn: query.tokenId,
|
|
95
|
+
tokenOut: query.tokenId,
|
|
96
|
+
estimatedBlocks: routeHops.length * 2,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
source: query.source,
|
|
102
|
+
amount: query.amount,
|
|
103
|
+
tokenId: query.tokenId,
|
|
104
|
+
maxHops,
|
|
105
|
+
revision: snapshot.revision,
|
|
106
|
+
costModelId: costModel.id,
|
|
107
|
+
destinations: [...destinations].sort(),
|
|
108
|
+
labelAt,
|
|
109
|
+
routeTo(destination) {
|
|
110
|
+
let bestH = -1;
|
|
111
|
+
let bestFees = 0n;
|
|
112
|
+
for (let h = 0; h <= maxHops; h++) {
|
|
113
|
+
const label = layers[h]?.get(destination);
|
|
114
|
+
if (!label)
|
|
115
|
+
continue;
|
|
116
|
+
if (bestH === -1 ||
|
|
117
|
+
label.totalFees < bestFees ||
|
|
118
|
+
(label.totalFees === bestFees && h < bestH)) {
|
|
119
|
+
bestH = h;
|
|
120
|
+
bestFees = label.totalFees;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (bestH === -1)
|
|
124
|
+
return null;
|
|
125
|
+
return labelAt(destination, bestH)?.route ?? null;
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const LAYERED_ENGINE = new LayeredRoutingEngine();
|
|
130
|
+
export function selectRoutingEngine(_snapshot) {
|
|
131
|
+
return LAYERED_ENGINE;
|
|
132
|
+
}
|
|
133
|
+
/** Cache key includes every input that materially affects correctness. */
|
|
134
|
+
export function routeCacheKey(snapshot, query, costModel = FIXED_AMOUNT_COST_MODEL) {
|
|
135
|
+
return [
|
|
136
|
+
snapshot.revision.topology,
|
|
137
|
+
snapshot.revision.economics,
|
|
138
|
+
snapshot.revision.live,
|
|
139
|
+
query.source,
|
|
140
|
+
query.destination ?? '*',
|
|
141
|
+
query.amount.toString(),
|
|
142
|
+
query.tokenId,
|
|
143
|
+
query.targetToken ?? '',
|
|
144
|
+
query.maxHops === null ? 'unbounded' : String(query.maxHops ?? 'default'),
|
|
145
|
+
costModel.id,
|
|
146
|
+
].join('|');
|
|
147
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -123,6 +123,8 @@ export interface RoutingHop {
|
|
|
123
123
|
to: string;
|
|
124
124
|
amount: bigint;
|
|
125
125
|
tokenId: string;
|
|
126
|
+
/** Edge fee rate (per SCALE); populated by the routing engine. */
|
|
127
|
+
feeRate?: bigint;
|
|
126
128
|
/** Populated during payment execution */
|
|
127
129
|
htlcId?: string;
|
|
128
130
|
}
|
|
@@ -189,12 +191,17 @@ export interface PaymentResult {
|
|
|
189
191
|
/** Hex preimage revealed during settlement (present on success) */
|
|
190
192
|
preimage?: string;
|
|
191
193
|
error?: string;
|
|
194
|
+
/** Machine-readable failure code (e.g. ROUTE_REVALIDATION_FAILED). */
|
|
195
|
+
errorCode?: string;
|
|
192
196
|
/** htlcIds that were successfully settled */
|
|
193
197
|
settledHops: string[];
|
|
194
198
|
/** Verifiable fee proofs for each settled hop — feeds recordPoolFee earn-proofs. */
|
|
195
199
|
feeProofs?: RouterFeeProof[];
|
|
196
200
|
}
|
|
197
201
|
export interface RouteOptions {
|
|
198
|
-
/**
|
|
202
|
+
/**
|
|
203
|
+
* Maximum number of hops for the whole route (default: 8). For cross-token
|
|
204
|
+
* routes this is a global end-to-end budget.
|
|
205
|
+
*/
|
|
199
206
|
maxHops?: number;
|
|
200
207
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totemsdk/omnia-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Multi-hop payment routing over Omnia channels — single-token and cross-token paths",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"types": "dist/cjs/index.d.ts",
|
|
@@ -19,14 +19,15 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
|
21
21
|
"README.md",
|
|
22
|
-
"LICENSE"
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"NOTICE"
|
|
23
24
|
],
|
|
24
25
|
"dependencies": {
|
|
25
|
-
"@totemsdk/core": "^1.2
|
|
26
|
-
"@totemsdk/storage": "^0.
|
|
26
|
+
"@totemsdk/core": "^1.4.2",
|
|
27
|
+
"@totemsdk/storage": "^0.2.2"
|
|
27
28
|
},
|
|
28
29
|
"peerDependencies": {
|
|
29
|
-
"@totemsdk/omnia": "^2.
|
|
30
|
+
"@totemsdk/omnia": "^2.4.0"
|
|
30
31
|
},
|
|
31
32
|
"peerDependenciesMeta": {
|
|
32
33
|
"@totemsdk/omnia": {
|
|
@@ -46,7 +47,7 @@
|
|
|
46
47
|
"publishConfig": {
|
|
47
48
|
"access": "public"
|
|
48
49
|
},
|
|
49
|
-
"license": "
|
|
50
|
+
"license": "Apache-2.0",
|
|
50
51
|
"author": "Totem SDK",
|
|
51
52
|
"homepage": "https://docs.totem.ing",
|
|
52
53
|
"bugs": {
|
|
@@ -79,6 +80,7 @@
|
|
|
79
80
|
"build": "tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', JSON.stringify({type:'commonjs'})+'\\n')\"",
|
|
80
81
|
"build:go": "cd go && go build -o ../bin/omnia-router ./cmd/omnia-router",
|
|
81
82
|
"clean": "rm -rf dist bin",
|
|
82
|
-
"test": "jest"
|
|
83
|
+
"test": "jest",
|
|
84
|
+
"bench": "node bench/route-bench.mjs"
|
|
83
85
|
}
|
|
84
86
|
}
|