Skip to content

Commit

Permalink
feat(zoe): allow specifying additional endowments for contracts (Agor…
Browse files Browse the repository at this point in the history
…ic#155)

* feat(zoe): allow specifying additional endowments for contracts

This is also added to the contractHost, so that SwingSet users
can endow a `require` function for bundled contract code in the
`getExport` module format.

* doc(zoe): declare that additionalEndowments should be pure-ish

Same for contractHost.
  • Loading branch information
michaelfig authored and katelynsills committed Oct 15, 2019
1 parent 49b6703 commit a209c90
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
40 changes: 27 additions & 13 deletions core/contractHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import { makeInviteConfig } from './config/inviteConfig';
import { makeMint } from './mint';
import makePromise from '../util/makePromise';

/** Make a reusable host that can reliably install and execute contracts. */
function makeContractHost(E, evaluate) {
/**
* Make a reusable host that can reliably install and execute contracts.
*
* @param E eventual-send method proxy
* @param evaluate function to evaluate with endowments
* @param additionalEndowments pure or pure-ish endowments to add to evaluator
*/
function makeContractHost(E, evaluate, additionalEndowments = {}) {
// Maps from seat identity to seats
const seats = makePrivateName();
// from seat identity to invite description.
Expand All @@ -40,20 +46,28 @@ No invites left`;
).then(_ => seats.get(seatIdentity));
}

const defaultEndowments = {
Nat,
harden,
console,
E,
makePromise,
// TODO: sameStructure is used in one check...() function. Figure out a
// more general approach to providing useful helpers.
// The best approach is to use the `getExport` moduleFormat, and
// bundle imported modules that implement the things we want to use.
sameStructure,
mustBeSameStructure,
};
const fullEndowments = Object.create(null, {
...Object.getOwnPropertyDescriptors(defaultEndowments),
...Object.getOwnPropertyDescriptors(additionalEndowments),
});

function evaluateStringToFn(functionSrcString) {
insist(typeof functionSrcString === 'string')`\n
"${functionSrcString}" must be a string, but was ${typeof functionSrcString}`;
const fn = evaluate(functionSrcString, {
Nat,
harden,
console,
E,
makePromise,
// TODO: sameStructure is used in one check...() function. Figure out a
// more general approach to providing useful helpers.
sameStructure,
mustBeSameStructure,
});
const fn = evaluate(functionSrcString, fullEndowments);
insist(typeof fn === 'function')`\n
"${functionSrcString}" must be a string for a function, but produced ${typeof fn}`;
return fn;
Expand Down
22 changes: 16 additions & 6 deletions core/zoe/zoe/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { makeSeatMint } from '../../seatMint';
import { makeEscrowReceiptConfig } from './escrowReceiptConfig';
import { makeMint } from '../../mint';

const makeZoe = async () => {
/**
* Create an instance of Zoe.
*
* @param additionalEndowments pure or pure-ish endowments to add to evaluator
*/
const makeZoe = async (additionalEndowments = {}) => {
// The escrowReceiptAssay is a long-lived identity over many
// contract instances
const {
Expand All @@ -42,14 +47,19 @@ const makeZoe = async () => {
);
const escrowReceiptAssay = escrowReceiptMint.getAssay();

const defaultEndowments = {
harden,
makePromise,
insist,
};
const fullEndowments = Object.create(null, {
...Object.getOwnPropertyDescriptors(defaultEndowments),
...Object.getOwnPropertyDescriptors(additionalEndowments),
});
const evaluateStringToFn = functionSrcString => {
insist(typeof functionSrcString === 'string')`\n
"${functionSrcString}" must be a string, but was ${typeof functionSrcString}`;
const fn = evaluate(functionSrcString, {
harden,
makePromise,
insist,
});
const fn = evaluate(functionSrcString, fullEndowments);
insist(typeof fn === 'function')`\n
"${functionSrcString}" must be a string for a function, but produced ${typeof fn}`;
return fn;
Expand Down

0 comments on commit a209c90

Please sign in to comment.