-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathcontractSupport.js
201 lines (187 loc) · 6.22 KB
/
contractSupport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { AmountMath } from '@agoric/ertp';
import { makeStoredPublisherKit, makeStoredPublishKit } from '@agoric/notifier';
import { M } from '@agoric/store';
import {
makeScalarBigMapStore,
provide,
provideDurableSetStore,
} from '@agoric/vat-data';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
const { Fail, quote: q } = assert;
export const amountPattern = harden({ brand: M.remotable(), value: M.any() });
export const ratioPattern = harden({
numerator: amountPattern,
denominator: amountPattern,
});
/**
* Apply a delta to the `base` Amount, where the delta is represented as
* an amount to gain and an amount to lose. Typically one of those will
* be empty because gain/loss comes from the give/want for a specific asset
* on a proposal. We use two Amounts because an Amount cannot represent
* a negative number (so we use a "loss" that will be subtracted).
*
* @template {AssetKind} K
* @param {Amount<K>} base
* @param {Amount<K>} gain
* @param {Amount<K>} loss
* @returns {Amount<K>}
*/
export const addSubtract = (base, gain, loss) =>
AmountMath.subtract(AmountMath.add(base, gain), loss);
/**
* Verifies that every key in the proposal is in the provided list
*
* @param {ProposalRecord} proposal
* @param {string[]} keys
*/
export const assertOnlyKeys = (proposal, keys) => {
/** @param {AmountKeywordRecord} clause */
const onlyKeys = clause =>
Object.getOwnPropertyNames(clause).every(c => keys.includes(c));
onlyKeys(proposal.give) || Fail`extraneous terms in give: ${proposal.give}`;
onlyKeys(proposal.want) || Fail`extraneous terms in want: ${proposal.want}`;
};
/**
* Stage a transfer between `fromSeat` and `toSeat`, specified as the delta between
* the gain and a loss on the `fromSeat`. The gain/loss are typically from the
* give/want respectively of a proposal. The `key` is the allocation keyword.
*
* @deprecated Use atomicRearrange instead
* @param {ZCFSeat} fromSeat
* @param {ZCFSeat} toSeat
* @param {Amount} fromLoses
* @param {Amount} fromGains
* @param {Keyword} key
*/
export const stageDelta = (fromSeat, toSeat, fromLoses, fromGains, key) => {
// Must check `isEmpty`; can't subtract `empty` from a missing allocation.
if (!AmountMath.isEmpty(fromLoses)) {
toSeat.incrementBy(fromSeat.decrementBy(harden({ [key]: fromLoses })));
}
if (!AmountMath.isEmpty(fromGains)) {
fromSeat.incrementBy(toSeat.decrementBy(harden({ [key]: fromGains })));
}
};
/**
* @param {Amount<'nat'>} debtLimit
* @param {Amount<'nat'>} totalDebt
* @param {Amount<'nat'>} toMint
* @throws if minting would exceed total debt
*/
export const checkDebtLimit = (debtLimit, totalDebt, toMint) => {
const debtPost = AmountMath.add(totalDebt, toMint);
!AmountMath.isGTE(debtPost, debtLimit) ||
Fail`Minting ${q(toMint)} past ${q(
totalDebt,
)} would hit total debt limit ${q(debtLimit)}`;
};
/**
* @deprecated incompatible with durability; instead handle vstorage ephemerally on a durable PublishKit
* @template T
* @param {ERef<StorageNode>} storageNode
* @param {ERef<Marshaller>} marshaller
* @returns {MetricsPublisherKit<T>}
*/
export const makeMetricsPublisherKit = (storageNode, marshaller) => {
assert(
storageNode && marshaller,
'makeMetricsPublisherKit missing storageNode or marshaller',
);
/** @type {import('@agoric/notifier').StoredPublisherKit<T>} */
const kit = makeStoredPublisherKit(storageNode, marshaller, 'metrics');
return {
metricsPublication: kit.publisher,
metricsSubscription: kit.subscriber,
};
};
harden(makeMetricsPublisherKit);
/**
* @template T
* @typedef {object} MetricsPublisherKit<T>
* @property {IterationObserver<T>} metricsPublication
* @property {StoredSubscription<T>} metricsSubscription
*/
/**
* @template T
* @typedef {object} MetricsPublishKit<T>
* @property {Publisher<T>} metricsPublisher
* @property {StoredSubscriber<T>} metricsSubscriber
*/
/**
* @deprecated incompatible with durability; instead handle vstorage ephemerally on a durable PublishKit
* @template T
* @param {ERef<StorageNode>} storageNode
* @param {ERef<Marshaller>} marshaller
* @returns {MetricsPublishKit<T>}
*/
export const makeMetricsPublishKit = (storageNode, marshaller) => {
assert(
storageNode && marshaller,
'makeMetricsPublisherKit missing storageNode or marshaller',
);
const metricsNode = E(storageNode).makeChildNode('metrics');
/** @type {StoredPublishKit<T>} */
const kit = makeStoredPublishKit(metricsNode, marshaller);
return {
metricsPublisher: kit.publisher,
metricsSubscriber: kit.subscriber,
};
};
harden(makeMetricsPublishKit);
/**
* @template K Key
* @template {{}} E Ephemeral state
* @param {() => E} init
*/
export const makeEphemeraProvider = init => {
/** @type {Map<K, E>} */
const ephemeras = new Map();
/**
* Provide an object to hold state that need not (or cannot) be durable.
*
* @type {(key: K) => E}
*/
return key => {
if (ephemeras.has(key)) {
// @ts-expect-error cast
return ephemeras.get(key);
}
const newEph = init();
ephemeras.set(key, newEph);
return newEph;
};
};
harden(makeEphemeraProvider);
export const provideEmptySeat = (zcf, baggage, name) => {
return provide(baggage, name, () => zcf.makeEmptySeatKit().zcfSeat);
};
harden(provideEmptySeat);
/**
* For making singletons, so that each baggage carries a separate kind definition (albeit of the definer)
*
* @param {import('@agoric/vat-data').Baggage} baggage
* @param {string} category diagnostic tag
* @returns {import('@agoric/vat-data').Baggage}
*/
export const provideChildBaggage = (baggage, category) => {
const baggageSet = provideDurableSetStore(baggage, `${category}Set`);
return Far('childBaggageManager', {
/**
* @template {(baggage: import('@agoric/ertp').Baggage) => any} M Maker function
* @param {string} childName diagnostic tag
* @param {M} makeChild
* @returns {ReturnType<M>}
*/
addChild: (childName, makeChild) => {
const childStore = makeScalarBigMapStore(`${childName}${category}`, {
durable: true,
});
const result = makeChild(childStore);
baggageSet.add(childStore);
return result;
},
children: () => baggageSet.values(),
});
};
harden(provideChildBaggage);