|
1 |
| -import { mustMatch, M } from '@agoric/store'; |
2 |
| -import { assertRightsConserved } from '../contractFacet/rightsConservation.js'; |
| 1 | +import { M } from '@agoric/store'; |
3 | 2 | import { AmountKeywordRecordShape, SeatShape } from '../typeGuards.js';
|
4 | 3 |
|
5 |
| -const { Fail, quote: q } = assert; |
6 |
| - |
7 | 4 | export const TransferPartShape = M.splitArray(
|
8 | 5 | harden([M.opt(SeatShape), M.opt(SeatShape), M.opt(AmountKeywordRecordShape)]),
|
9 | 6 | harden([M.opt(AmountKeywordRecordShape)]),
|
@@ -49,82 +46,7 @@ export const TransferPartShape = M.splitArray(
|
49 | 46 | * @param {TransferPart[]} transfers
|
50 | 47 | */
|
51 | 48 | 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); |
128 | 50 | };
|
129 | 51 |
|
130 | 52 | /**
|
|
0 commit comments