/ contract / deploy / execute-p2p.js
execute-p2p.js
 1  /**
 2   * @file
 3   *
 4   * @see {@link https://github.com/Agoric/agoric-sdk/discussions/6839}
 5   * @see {@link https://gist.github.com/dckc/1230bf4a62530b049c53ec7740844033#file-chainstorage-proposal-js}
 6   *
 7   * This is a script for use with swingset.CoreEval.
 8   *
 9   * It's a script, not a module, so we can't use `import`.
10   * But E, Far, etc. are in scope, provided by the
11   * `new Compartment(globals)` call in
12   * `bridgeCoreEval()` in packages/vats/src/core/chain-behaviors.js
13   */
14  // @ts-check
15  // uncomment the following line to typecheck, for example, in vs-code.
16  // import { E } from '@endo/far';
17  
18  const contractInfo = {
19    storagePath: 'p2p',
20    instanceName: 'p2pAgent',
21    bundleID:
22      /**  obtained by bundling + publishing  contract (agoric publish) */
23      'b1-c9394d83ba0cd5ddff283c40a076839ae83c7be099b19848db9d7d22bda2762028d6a339455d8e40d89d7d75ec6a2675f313931e9c9b26fc95ec45076c7771e7'
24  };
25  
26  const fail = reason => {
27    throw reason;
28  };
29  
30  /**
31   * Execute a proposal to start the p2p contract
32   *
33   * @param {BootstrapPowers & ChainStorageVatParams} powers
34   */
35  const executeProposal = async powers => {
36    const {
37      consume: { agoricNamesAdmin, board, chainStorage, chainTimerService, zoe },
38      produce: { p2pKit },
39      instance: {
40        produce: { [contractInfo.instanceName]: produceInstance }
41      }
42    } = powers;
43  
44    const chainStorageSettled =
45      (await chainStorage) || fail(Error('chainStorage is not available'));
46  
47    const chainTimerServiceSettled =
48      (await chainTimerService) ||
49      fail(Error('chainTimerService is not available'));
50  
51    const storageNode = E(chainStorageSettled).makeChildNode(
52      contractInfo.storagePath
53    );
54    const marshaller = await E(board).getReadonlyMarshaller();
55  
56    const vbankAssets = await E(
57      E(E(agoricNamesAdmin).readonly()).lookup('vbankAsset')
58    ).values();
59  
60    const issuerKeywordRecord = harden(
61      vbankAssets.reduce(
62        (acc, { issuer, issuerName }) =>
63          Object.assign(acc, { [issuerName]: issuer }),
64        {}
65      )
66    );
67  
68    const terms = harden({
69      timerService: chainTimerServiceSettled
70    });
71    const privateArgs = harden({
72      storageNode,
73      marshaller
74    });
75  
76    const installation = await E(zoe).installBundleID(contractInfo.bundleID);
77  
78    const facets = await E(zoe).startInstance(
79      installation,
80      issuerKeywordRecord,
81      terms,
82      // @ts-ignore
83      privateArgs
84    );
85  
86    produceInstance.resolve(facets.instance);
87  
88    // Share the publicFacet, creatorFacet, and adminFacet in the bootstrap space
89    // for use by other CoreEval behaviors.
90    p2pKit.resolve(facets);
91  
92    // Share instance widely via E(agoricNames).lookup('instance', 'p2pAgent')
93    const instanceAdmin = E(agoricNamesAdmin).lookupAdmin('instance');
94    await E(instanceAdmin).update(contractInfo.instanceName, facets.instance);
95  };
96  harden(executeProposal);
97  
98  // "export" the function as the script completion value
99  executeProposal;