Skip to content

Commit

Permalink
feat(contractStarter): build core-eval script / permit with rollup
Browse files Browse the repository at this point in the history
 - main
 - emitPermit
  • Loading branch information
dckc committed Jan 21, 2024
1 parent bc0b73c commit fc26194
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"make:help": "make list",
"start": "yarn docker:make clean start-contract print-key",
"build": "exit 0",
"build:deployer": "rollup -c rollup.config.mjs src/start-contractStarter.js",
"test": "ava --verbose",
"lint": "eslint '**/*.{js,jsx}'",
"lint-fix": "eslint --fix '**/*.{js,jsx}'",
Expand Down
40 changes: 40 additions & 0 deletions contract/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file rollup configuration to bundle core-eval script
*
* Supports developing core-eval script, permit as a module:
* - import { E } from '@endo/far'
* We can strip this declaration during bundling
* since the core-eval scope includes exports of @endo/far
* - `bundleID = ...` is replaced using updated/cached bundle hash
* - `main` export is appended as script completion value
* - `permit` export is emitted as JSON
*/
// @ts-check
import {
coreEvalGlobals,
moduleToScript,
configureBundleID,
emitPermit,
} from './tools/rollup-plugin-core-eval.js';
import { permit } from './src/start-contractStarter.js';

/** @type {import('rollup').RollupOptions} */
const config = {
output: {
globals: coreEvalGlobals,
file: 'bundles/deploy-starter.js',
format: 'es',
footer: 'main',
},
external: ['@endo/far'],
plugins: [
configureBundleID({
name: 'contractStarter',
rootModule: './src/contractStarter.js',
cache: 'bundles',
}),
moduleToScript(),
emitPermit({ permit, file: 'deploy-starter-permit.json' }),
],
};
export default config;
26 changes: 25 additions & 1 deletion contract/src/start-contractStarter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const installContractStarter = async (
) => {
const {
// rendering this template requires not re-flowing the next line
bundleID = Fail`bundleID required`,
bundleID = Fail`contractStarter bundleID required`,
} = options?.contractStarter || {};

const installation = await E(zoe).installBundleID(bundleID);
Expand Down Expand Up @@ -72,3 +72,27 @@ export const startContractStarter = async (
produceInstance.resolve(instance);
return instance;
};

export const permit = {
consume: {
zoe: true,
chainStorage: true,
chainTimerService: true,
priceAuthority: true,
board: true,
agoricNames: true,
namesByAddressAdmin: true,
},
produce: { contractStarterKit: true },
installation: {
produce: { contractStarter: true },
consume: { contractStarter: true },
},
instance: { produce: { contractStarter: true } },
brand: { consume: { IST: true } },
};

export const main = async powers => {
await installContractStarter(powers);
await startContractStarter(powers);
};
54 changes: 54 additions & 0 deletions contract/tools/rollup-plugin-core-eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @ts-check
import '@endo/init';
import { makeNodeBundleCache } from '@endo/bundle-source/cache.js';
export const coreEvalGlobals = {
E: 'E',
Far: 'Far',
};

const redactImportDecls = txt =>
txt.replace(/^\s*import\b\s*(.*)/gm, '// XMPORT: $1');
const omitExportKewords = txt => txt.replace(/^\s*export\b\s*/gm, '');
// cf. ses rejectImportExpressions
// https://github.com/endojs/endo/blob/ebc8f66e9498f13085a8e64e17fc2f5f7b528faa/packages/ses/src/transforms.js#L143
const hideImportExpr = txt => txt.replace(/\bimport\b/g, 'XMPORT');

export const moduleToScript = () => ({
name: 'module-to-script',
generateBundle: (_opts, bundle, _isWrite) => {
for (const fileName of Object.keys(bundle)) {
bundle[fileName].code = hideImportExpr(
redactImportDecls(omitExportKewords(bundle[fileName].code)),
);
}
},
});

export const configureBundleID = ({ name, rootModule, cache }) => {
const pattern = new RegExp(`bundleID\\b = Fail.*`, 'g');
const bundleCacheP = makeNodeBundleCache(cache, {}, s => import(s));
return {
name: 'configureBundleID',
transform: async (code, id) => {
const bundle = await bundleCacheP.then(c => c.load(rootModule, name));
const revised = code.replace(
pattern,
`bundleID = ${JSON.stringify(`b1-${bundle.endoZipBase64Sha512}`)},`,
);
if (revised === code) return null;
return { code: revised };
},
};
};

export const emitPermit = ({ permit, file }) => ({
name: 'emit-permit',
generateBundle(_opts, _bundle) {
console.log('@@@emit permit');
this.emitFile({
type: 'asset',
fileName: file,
source: JSON.stringify(permit, null, 2),
});
},
});

0 comments on commit fc26194

Please sign in to comment.