@aboutcircles/sdk-pathfinder 0.1.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.
@@ -0,0 +1,6 @@
1
+ import type { FlowMatrix, TransferStep, Address } from '@aboutcircles/sdk-types';
2
+ /**
3
+ * Create an ABI‑ready FlowMatrix object from a list of TransferSteps.
4
+ */
5
+ export declare function createFlowMatrix(from: Address, to: Address, value: bigint, transfers: TransferStep[]): FlowMatrix;
6
+ //# sourceMappingURL=flowMatrix.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flowMatrix.d.ts","sourceRoot":"","sources":["../src/flowMatrix.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,OAAO,EAAgC,MAAM,yBAAyB,CAAC;AAG/G;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,OAAO,EACb,EAAE,EAAE,OAAO,EACX,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,YAAY,EAAE,GACxB,UAAU,CAmEZ"}
@@ -0,0 +1,57 @@
1
+ import { packCoordinates, transformToFlowVertices } from './packing';
2
+ /**
3
+ * Create an ABI‑ready FlowMatrix object from a list of TransferSteps.
4
+ */
5
+ export function createFlowMatrix(from, to, value, transfers) {
6
+ const sender = from.toLowerCase();
7
+ const receiver = to.toLowerCase();
8
+ const { sorted: flowVertices, idx } = transformToFlowVertices(transfers, sender, receiver);
9
+ const flowEdges = transfers.map((t) => {
10
+ const isTerminal = t.to.toLowerCase() === receiver;
11
+ return {
12
+ streamSinkId: isTerminal ? 1 : 0,
13
+ amount: t.value
14
+ };
15
+ });
16
+ // Ensure at least one terminal edge
17
+ const hasTerminalEdge = flowEdges.some((e) => e.streamSinkId === 1);
18
+ if (!hasTerminalEdge) {
19
+ const lastEdgeIndex = transfers
20
+ .map((t) => t.to.toLowerCase())
21
+ .lastIndexOf(receiver);
22
+ const fallbackIndex = lastEdgeIndex === -1 ? flowEdges.length - 1 : lastEdgeIndex;
23
+ flowEdges[fallbackIndex].streamSinkId = 1;
24
+ }
25
+ const termEdgeIds = flowEdges
26
+ .map((e, i) => (e.streamSinkId === 1 ? i : -1))
27
+ .filter((i) => i !== -1);
28
+ const streams = [
29
+ {
30
+ sourceCoordinate: idx[sender],
31
+ flowEdgeIds: termEdgeIds,
32
+ data: new Uint8Array(0)
33
+ }
34
+ ];
35
+ const coords = [];
36
+ transfers.forEach((t) => {
37
+ coords.push(idx[t.tokenOwner.toLowerCase()]);
38
+ coords.push(idx[t.from.toLowerCase()]);
39
+ coords.push(idx[t.to.toLowerCase()]);
40
+ });
41
+ const packedCoordinates = packCoordinates(coords);
42
+ const expected = BigInt(value);
43
+ const terminalSum = flowEdges
44
+ .filter((e) => e.streamSinkId === 1)
45
+ .reduce((sum, e) => sum + BigInt(e.amount.toString()), BigInt(0));
46
+ const isBalanced = terminalSum === expected;
47
+ if (!isBalanced) {
48
+ throw new Error(`Terminal sum ${terminalSum} does not equal expected ${expected}`);
49
+ }
50
+ return {
51
+ flowVertices,
52
+ flowEdges,
53
+ streams,
54
+ packedCoordinates,
55
+ sourceCoordinate: idx[sender]
56
+ };
57
+ }
@@ -0,0 +1,4 @@
1
+ export * from './flowMatrix';
2
+ export * from './path';
3
+ export * from './packing';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC"}