-
Notifications
You must be signed in to change notification settings - Fork 191
/
test-vaultFactory.js
91 lines (76 loc) · 3.17 KB
/
test-vaultFactory.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
// @ts-check
import '@endo/init';
import anyTest from 'ava';
import path from 'path';
import { buildVatController, buildKernelBundles } from '@agoric/swingset-vat';
import bundleSource from '@endo/bundle-source';
import { E } from '@endo/eventual-send';
import zcfBundle from '@agoric/zoe/bundles/bundle-contractFacet.js';
import committeeBundle from '@agoric/governance/bundles/bundle-committee.js';
import contractGovernorBundle from '@agoric/governance/bundles/bundle-contractGovernor.js';
import binaryVoteCounterBundle from '@agoric/governance/bundles/bundle-binaryVoteCounter.js';
import liquidateMinimumBundle from '../../../bundles/bundle-liquidateMinimum.js';
import ammBundle from '../../../bundles/bundle-amm.js';
import vaultFactoryBundle from '../../../bundles/bundle-vaultFactory.js';
/** @type {import('ava').TestFn<{ data: { kernelBundles: any, config: any } }>} */
const test = anyTest;
/**
*
* @param {Record<string, unknown>} contractBundles
* @param {string[]} vatNames
* @returns {Promise<SwingSetConfig>}
*/
export const buildSwingSetConfig = async (contractBundles, vatNames) => {
const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);
const vatNameToSource = vatNames.map(name => {
const source = `${dirname}/vat-${name}.js`;
return [name, source];
});
const bootstrapSource = `${dirname}/bootstrap.js`;
vatNameToSource.push(['bootstrap', bootstrapSource]);
const bundles = await Promise.all(
vatNameToSource.map(([_, source]) => bundleSource(source)),
);
/** @type {SwingSetConfigDescriptor} */
const vats = {};
[...vatNames, 'bootstrap'].forEach(
(name, index) => (vats[name] = { bundle: bundles[index] }),
);
vats.bootstrap.parameters = { contractBundles };
const config = { bootstrap: 'bootstrap', vats };
config.bundles = { zcf: { bundle: zcfBundle } };
config.defaultManagerType = 'xs-worker';
return config;
};
test.before(async t => {
const kernelBundles = await buildKernelBundles();
const contractBundles = {
liquidateMinimum: liquidateMinimumBundle,
amm: ammBundle,
vaultFactory: vaultFactoryBundle,
committee: committeeBundle,
contractGovernor: contractGovernorBundle,
binaryVoteCounter: binaryVoteCounterBundle,
};
const vatNames = ['alice', 'zoe', 'priceAuthority', 'owner'];
const config = await buildSwingSetConfig(contractBundles, vatNames);
t.context.data = { kernelBundles, config };
});
async function main(t, argv) {
const { kernelBundles, config } = t.context.data;
const controller = buildVatController(config, argv, { kernelBundles });
await E(controller).run();
return E(controller).dump();
}
// NB: yarn build if changing any of the contract bundles under test
test.serial('vaultFactory', async t => {
const startingValues = [[100], [1000]]; // [aliceValues, ownerValues]
const dump = await main(t, ['oneLoanWithInterest', startingValues]);
t.deepEqual(dump.log, [
'=> alice and the vaultFactory are set up',
'=> alice.oneLoanWithInterest called',
'Alice owes {"brand":"[Alleged: RUN brand]","value":"[510000n]"} after borrowing',
'Alice owes {"brand":"[Alleged: RUN brand]","value":"[510035n]"} after interest',
]);
});