Skip to content

Commit 5f41cb9

Browse files
committed
refactor(portfolio-contract): Make intra-Agoric links unidirectional
1 parent 06c1054 commit 5f41cb9

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

packages/portfolio-contract/src/network/buildGraph.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,14 @@ export const makeGraphFromDefinition = (
6666

6767
// Force the presence of particular edges.
6868
const edges = [...graph.edges] as Array<FlowEdge | undefined>;
69-
70-
// Ensure intra-Agoric links with 0 fee / 0 time.
71-
// Nodes: +agoric, <Cash>, <Deposit> on @agoric.
72-
const agoricHub: AssetPlaceRef = '@agoric';
73-
const seats: AssetPlaceRef[] = ['<Cash>', '<Deposit>'];
74-
const staging: AssetPlaceRef = '+agoric';
75-
const capacityDefault = 9_007_199_254_740_000; // slightly less than MAX_SAFE_INTEGER
69+
const capacityDefault = 9_007_199_254_740_000; // not quite MAX_SAFE_INTEGER
7670
const addOrReplaceEdge = (
7771
src: AssetPlaceRef,
7872
dest: AssetPlaceRef,
7973
customAttrs?: Omit<FlowEdge, 'id' | 'src' | 'dest'>,
8074
) => {
81-
if (!graph.nodes.has(src) || !graph.nodes.has(dest)) return;
75+
(graph.nodes.has(src) && graph.nodes.has(dest)) ||
76+
Fail`Graph missing nodes for link ${src}->${dest}`;
8277

8378
// Remove any existing edge for exact src->dest
8479
for (let i = 0; i < edges.length; i += 1) {
@@ -96,26 +91,20 @@ export const makeGraphFromDefinition = (
9691
edges.push({ id: 'TBD', src, dest, ...dataAttrs });
9792
};
9893

99-
// +agoric <-> @agoric
100-
addOrReplaceEdge(staging, agoricHub);
101-
addOrReplaceEdge(agoricHub, staging);
102-
103-
// seats <-> @agoric and seats <-> +agoric
104-
for (const seat of seats) {
105-
addOrReplaceEdge(seat, agoricHub);
106-
addOrReplaceEdge(agoricHub, seat);
107-
addOrReplaceEdge(seat, staging);
108-
addOrReplaceEdge(staging, seat);
109-
}
94+
// Ensure intra-Agoric links with 0 fee / 0 time:
95+
// <Deposit> -> +agoric -> @agoric -> <Cash>
96+
// eslint-disable-next-line github/array-foreach
97+
(['<Deposit>', '+agoric', '@agoric', '<Cash>'] as AssetPlaceRef[]).forEach(
98+
(dest, i, arr) => {
99+
const src: AssetPlaceRef | undefined = i === 0 ? undefined : arr[i - 1];
100+
if (!src || !graph.nodes.has(src) || !graph.nodes.has(dest)) return;
101+
addOrReplaceEdge(src, dest);
102+
},
103+
);
110104

111105
// Override the base graph with inter-hub links from spec.
112106
for (const link of spec.links) {
113-
const src = `@${link.src}` as AssetPlaceRef;
114-
const dest = `@${link.dest}` as AssetPlaceRef;
115-
(graph.nodes.has(src) && graph.nodes.has(dest)) ||
116-
Fail`Graph missing nodes for link ${src}->${dest}`;
117-
118-
addOrReplaceEdge(src, dest, {
107+
addOrReplaceEdge(`@${link.src}`, `@${link.dest}`, {
119108
capacity: Number(link.capacity ?? capacityDefault),
120109
variableFee: link.variableFeeBps ?? 0,
121110
fixedFee: link.flatFee === undefined ? undefined : Number(link.flatFee),

0 commit comments

Comments
 (0)