-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathsupports.js
277 lines (251 loc) · 8.67 KB
/
supports.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { AmountMath } from '@agoric/ertp';
import binaryVoteCounterBundle from '@agoric/governance/bundles/bundle-binaryVoteCounter.js';
import committeeBundle from '@agoric/governance/bundles/bundle-committee.js';
import contractGovernorBundle from '@agoric/governance/bundles/bundle-contractGovernor.js';
import puppetContractGovernorBundle from '@agoric/governance/bundles/bundle-puppetContractGovernor.js';
import * as utils from '@agoric/vats/src/core/utils.js';
import { makePromiseSpace, makeAgoricNamesAccess } from '@agoric/vats';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
import { makeMockChainStorageRoot } from '@agoric/internal/src/storage-test-utils.js';
import { setUpZoeForTest as generalSetUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { E } from '@endo/far';
import { makeTracer } from '@agoric/internal';
import {
makeNotifierFromAsyncIterable,
observeIteration,
subscribeEach,
} from '@agoric/notifier';
import { produceDiagnostics } from '@agoric/vats/src/core/basic-behaviors.js';
export { makeMockChainStorageRoot };
/** Common six-decimal places denom */
export const DENOM_UNIT = 1_000_000n;
/**
* @param {any} t
* @param {string} sourceRoot
* @param {string} bundleName
* @returns {Promise<SourceBundle>}
*/
export const provideBundle = (t, sourceRoot, bundleName) => {
assert(
t.context && t.context.bundleCache,
'must set t.context.bundleCache in test.before()',
);
const { bundleCache } = t.context;
return bundleCache.load(sourceRoot, bundleName);
};
harden(provideBundle);
/**
* Returns promises for `zoe` and the `feeMintAccess`.
*
* @param {() => void} [setJig]
*/
export const setUpZoeForTest = async (setJig = () => {}) =>
generalSetUpZoeForTest({
setJig,
feeIssuerConfig: utils.feeIssuerConfig,
});
harden(setUpZoeForTest);
/**
* @param {any} t
* @param {import('@agoric/time').TimerService} [optTimer]
*/
export const setupBootstrap = async (t, optTimer) => {
const trace = makeTracer('PromiseSpace', false);
const space = /** @type {any} */ (makePromiseSpace(trace));
const { produce, consume } = /**
* @type {import('../src/proposals/econ-behaviors.js').EconomyBootstrapPowers &
* BootstrapPowers}
*/ (space);
await produceDiagnostics(space);
const timer = optTimer || buildManualTimer(t.log);
produce.chainTimerService.resolve(timer);
produce.chainStorage.resolve(makeMockChainStorageRoot());
produce.board.resolve(makeFakeBoard());
const { zoe, feeMintAccess, run } = t.context;
produce.zoe.resolve(zoe);
produce.feeMintAccess.resolve(feeMintAccess);
const { agoricNames, agoricNamesAdmin, spaces } =
await makeAgoricNamesAccess();
produce.agoricNames.resolve(agoricNames);
produce.agoricNamesAdmin.resolve(agoricNamesAdmin);
const { brand, issuer } = spaces;
brand.produce.IST.resolve(run.brand);
issuer.produce.IST.resolve(run.issuer);
return { produce, consume, modules: { utils: { ...utils } }, ...spaces };
};
export const installGovernance = (zoe, produce) => {
produce.committee.resolve(E(zoe).install(committeeBundle));
produce.contractGovernor.resolve(E(zoe).install(contractGovernorBundle));
produce.binaryVoteCounter.resolve(E(zoe).install(binaryVoteCounterBundle));
};
/**
* Install governance contracts, with a "puppet" governor for use in tests.
*
* @param {ERef<ZoeService>} zoe
* @param {Space['installation']['produce']} produce
*/
export const installPuppetGovernance = (zoe, produce) => {
produce.committee.resolve(E(zoe).install(committeeBundle));
produce.contractGovernor.resolve(
E(zoe).install(puppetContractGovernorBundle),
);
// ignored by puppetContractGovernor but expected by something
produce.binaryVoteCounter.resolve(E(zoe).install(binaryVoteCounterBundle));
};
/**
* @param {bigint} value
* @param {{
* centralSupply: ERef<
* Installation<import('@agoric/vats/src/centralSupply.js').start>
* >;
* feeMintAccess: ERef<FeeMintAccess>;
* zoe: ERef<ZoeService>;
* }} powers
* @returns {Promise<Payment<'nat'>>}
*/
export const mintRunPayment = async (
value,
{ centralSupply, feeMintAccess: feeMintAccessP, zoe },
) => {
const feeMintAccess = await feeMintAccessP;
const { creatorFacet: bootstrapSupplier } = await E(zoe).startInstance(
centralSupply,
{},
{ bootstrapPaymentValue: value },
{ feeMintAccess },
);
return E(bootstrapSupplier).getBootstrapPayment();
};
/**
* @typedef {import('../src/proposals/econ-behaviors.js').EconomyBootstrapPowers} Space
* @param {Space} space
* @param {Record<
* keyof Space['installation']['produce'],
* Promise<Installation>
* >} installations
*/
export const produceInstallations = (space, installations) => {
for (const [key, installation] of Object.entries(installations)) {
space.installation.produce[key].resolve(installation);
}
};
export const scale6 = x => BigInt(Math.round(x * 1_000_000));
/** @param {Pick<IssuerKit<'nat'>, 'brand' | 'issuer' | 'mint'>} kit */
export const withAmountUtils = kit => {
const decimalPlaces = kit.issuer.getDisplayInfo?.()?.decimalPlaces ?? 6;
return {
...kit,
/** @param {NatValue} v */
make: v => AmountMath.make(kit.brand, v),
makeEmpty: () => AmountMath.makeEmpty(kit.brand),
/**
* @param {NatValue} n
* @param {NatValue} [d]
*/
makeRatio: (n, d) => makeRatio(n, kit.brand, d),
/** @param {number} n */
units: n =>
AmountMath.make(kit.brand, BigInt(Math.round(n * 10 ** decimalPlaces))),
};
};
/** @typedef {ReturnType<typeof withAmountUtils>} AmountUtils */
/** @param {ERef<StoredSubscription<unknown> | StoredSubscriber<unknown>>} subscription */
export const subscriptionKey = subscription => {
return E(subscription)
.getStoreKey()
.then(storeKey => {
const [space, unique] = storeKey.storeSubkey.split(':');
assert(
space === 'fake',
'subscriptionKey only works with makeFakeStorageKit',
);
return unique;
});
};
/**
* @param {ERef<{
* getPublicTopics: () => import('@agoric/zoe/src/contractSupport/index.js').TopicsRecord;
* }>} hasTopics
* @param {string} subscriberName
*/
export const topicPath = (hasTopics, subscriberName) => {
return E(hasTopics)
.getPublicTopics()
.then(topics => topics[subscriberName])
.then(t => t.storagePath);
};
/** @type {<T>(subscriber: ERef<Subscriber<T>>) => Promise<T>} */
export const headValue = async subscriber => {
await eventLoopIteration();
const record = await E(subscriber).subscribeAfter();
return record.head.value;
};
/**
* CAVEAT: the head may lag and you need to explicitly
* getUpdateSince(lastUpdateCount)
*
* @type {<T>(subscription: ERef<Subscription<T>>) => Promise<T>}
*/
export const headValueLegacy = async subscription => {
await eventLoopIteration();
const notifier = makeNotifierFromAsyncIterable(subscription);
const record = await notifier.getUpdateSince();
// @ts-expect-error bad types for legacy utils
return record.value.current;
};
/**
* @param {import('ava').ExecutionContext} t
* @param {ERef<{
* getPublicTopics: () => import('@agoric/zoe/src/contractSupport/index.js').TopicsRecord;
* }>} hasTopics
* @param {string} topicName
* @param {string} path
* @param {string[]} [dataKeys]
*/
export const assertTopicPathData = async (
t,
hasTopics,
topicName,
path,
dataKeys,
) => {
const topic = await E(hasTopics)
.getPublicTopics()
.then(topics => topics[topicName]);
t.is(await topic?.storagePath, path, 'topic storagePath must match');
const latest = /** @type {Record<string, unknown>} */ (
await headValue(topic.subscriber)
);
if (dataKeys !== undefined) {
// TODO consider making this a shape instead
t.deepEqual(Object.keys(latest), dataKeys, 'keys in topic feed must match');
}
};
/**
* Sequence currents from a wallet UpdateRecord publication feed. Note that
* local state may not reflect the wallet's state if the initial currents are
* missed.
*
* If this proves to be a problem we can add an option to this or a related
* utility to reset state from RPC.
*
* @param {ERef<
* Subscriber<
* import('@agoric/smart-wallet/src/smartWallet.js').CurrentWalletRecord
* >
* >} currents
* @returns {import('@agoric/smart-wallet/src/smartWallet.js').CurrentWalletRecord[]}
* array that grows as the subscription feeds
*/
export const sequenceCurrents = currents => {
const sequence = [];
void observeIteration(subscribeEach(currents), {
updateState: updateRecord => {
sequence.push(updateRecord);
},
});
return sequence;
};