Skip to content

Commit a7a4fed

Browse files
committed
chore: simplify remain atomicRearrange helper; use forEach over map
1 parent 14ac202 commit a7a4fed

File tree

2 files changed

+3
-81
lines changed

2 files changed

+3
-81
lines changed

packages/zoe/src/contractFacet/zcfSeat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export const createSeatManager = (
375375
// Commit the new allocations (currentAllocation is replaced
376376
// for each of the seats) and inform Zoe of the new allocation.
377377

378-
newAllocations.map(([seat, allocation]) =>
378+
newAllocations.forEach(([seat, allocation]) =>
379379
activeZCFSeats.set(seat, allocation),
380380
);
381381

packages/zoe/src/contractSupport/atomicTransfer.js

+2-80
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { mustMatch, M } from '@agoric/store';
2-
import { assertRightsConserved } from '../contractFacet/rightsConservation.js';
1+
import { M } from '@agoric/store';
32
import { AmountKeywordRecordShape, SeatShape } from '../typeGuards.js';
43

5-
const { Fail, quote: q } = assert;
6-
74
export const TransferPartShape = M.splitArray(
85
harden([M.opt(SeatShape), M.opt(SeatShape), M.opt(AmountKeywordRecordShape)]),
96
harden([M.opt(AmountKeywordRecordShape)]),
@@ -49,82 +46,7 @@ export const TransferPartShape = M.splitArray(
4946
* @param {TransferPart[]} transfers
5047
*/
5148
export const atomicRearrange = (zcf, transfers) => {
52-
mustMatch(transfers, M.arrayOf(M.array()), 'transfers');
53-
const uniqueSeatSet = new Set();
54-
for (const [
55-
fromSeat = undefined,
56-
toSeat = undefined,
57-
fromAmounts = undefined,
58-
toAmounts = undefined,
59-
] of transfers) {
60-
if (fromSeat) {
61-
if (!fromAmounts) {
62-
throw Fail`Transfer from ${fromSeat} must say how much`;
63-
}
64-
uniqueSeatSet.add(fromSeat);
65-
if (toSeat) {
66-
// Conserved transfer between seats
67-
if (toAmounts) {
68-
// distinct amounts, so we check conservation.
69-
assertRightsConserved(
70-
Object.values(fromAmounts),
71-
Object.values(toAmounts),
72-
);
73-
} // else fromAmounts will be used as toAmounts
74-
uniqueSeatSet.add(toSeat);
75-
} else {
76-
// Transfer only from fromSeat
77-
!toAmounts ||
78-
Fail`Transfer without toSeat cannot have toAmounts ${toAmounts}`;
79-
}
80-
} else {
81-
toSeat || Fail`Transfer must have at least one of fromSeat or toSeat`;
82-
// Transfer only to toSeat
83-
!fromAmounts ||
84-
Fail`Transfer without fromSeat cannot have fromAmounts ${fromAmounts}`;
85-
toAmounts || Fail`Transfer to ${toSeat} must say how much`;
86-
uniqueSeatSet.add(toSeat);
87-
}
88-
}
89-
90-
const uniqueSeats = harden([...uniqueSeatSet.keys()]);
91-
for (const seat of uniqueSeats) {
92-
!seat.hasStagedAllocation() ||
93-
Fail`Cannot mix atomicRearrange with seat stagings: ${seat}`;
94-
}
95-
96-
// At this point the basic shape has been validated
97-
98-
try {
99-
for (const [
100-
fromSeat = undefined,
101-
toSeat = undefined,
102-
fromAmounts = undefined,
103-
toAmounts = toSeat && fromAmounts,
104-
] of transfers) {
105-
if (fromSeat && fromAmounts) {
106-
// testing both just to satisfy the type checker
107-
fromSeat.decrementBy(fromAmounts);
108-
}
109-
if (toSeat && toAmounts) {
110-
// testing both just to satisfy the type checker
111-
toSeat.incrementBy(toAmounts);
112-
}
113-
}
114-
115-
// Perhaps deprecate this >= 2 restriction?
116-
uniqueSeats.length >= 2 ||
117-
Fail`Can only commit a reallocation among at least 2 seats: ${q(
118-
uniqueSeats.length,
119-
)}`;
120-
// Take it apart and put it back together to satisfy the type checker
121-
const [seat0, seat1, ...restSeats] = uniqueSeats;
122-
zcf.reallocate(seat0, seat1, ...restSeats);
123-
} finally {
124-
for (const seat of uniqueSeats) {
125-
seat.clear();
126-
}
127-
}
49+
zcf.atomicRearrange(transfers);
12850
};
12951

13052
/**

0 commit comments

Comments
 (0)