diff --git a/packages/zoe/src/contractFacet/contractFacet.js b/packages/zoe/src/contractFacet/contractFacet.js index 60e40fa1f8a..ddb767683e5 100644 --- a/packages/zoe/src/contractFacet/contractFacet.js +++ b/packages/zoe/src/contractFacet/contractFacet.js @@ -355,10 +355,12 @@ export function buildRootObject() { // First, evaluate the contract code bundle. const contractCode = evalContractBundle(bundle); + // Don't trigger Node.js's UnhandledPromiseRejectionWarning + contractCode.catch(() => {}); // Next, execute the contract code, passing in zcf - /** @type {Promise} */ - return E(contractCode) + /** @type {Promise} */ + const result = E(contractCode) .start(zcf) .then(({ creatorFacet, publicFacet, creatorInvitation }) => { return harden({ @@ -368,6 +370,8 @@ export function buildRootObject() { addSeatObj, }); }); + result.catch(() => {}); // Don't trigger Node.js's UnhandledPromiseRejectionWarning + return result; }; return harden({ executeContract }); diff --git a/packages/zoe/src/contractFacet/evalContractCode.js b/packages/zoe/src/contractFacet/evalContractCode.js index 273b688e641..d42ff0b5dcc 100644 --- a/packages/zoe/src/contractFacet/evalContractCode.js +++ b/packages/zoe/src/contractFacet/evalContractCode.js @@ -23,7 +23,7 @@ const evalContractBundle = (bundle, additionalEndowments = {}) => { const installation = importBundle(bundle, { endowments: fullEndowments, - }); + }).catch(() => {}); // Don't trigger Node.js's UnhandledPromiseRejectionWarning return installation; }; diff --git a/packages/zoe/src/internal-types.js b/packages/zoe/src/internal-types.js index 3e7f54e5586..5ce69b0fdab 100644 --- a/packages/zoe/src/internal-types.js +++ b/packages/zoe/src/internal-types.js @@ -59,7 +59,7 @@ * @typedef {Object} ZoeSeatAdmin * @property {(allocation: Allocation) => void} replaceAllocation * @property {() => void} exit - * @property {(reason: any) => never} kickOut called with the reason this seat + * @property {(reason: any) => void} kickOut called with the reason this seat * is being kicked out, where reason is normally an instanceof Error. * @property {() => Allocation} getCurrentAllocation */ @@ -173,7 +173,7 @@ * @param {Issuer} invitationIssuer * @param {ZoeInstanceAdmin} zoeInstanceAdmin * @param {InstanceRecord} instanceRecord - * @returns {ExecuteContractResult} + * @returns {Promise} * */ diff --git a/packages/zoe/test/swingsetTests/brokenContracts/test-crashingContract.js b/packages/zoe/test/swingsetTests/brokenContracts/test-crashingContract.js index 112c2135ec3..737ee300e45 100644 --- a/packages/zoe/test/swingsetTests/brokenContracts/test-crashingContract.js +++ b/packages/zoe/test/swingsetTests/brokenContracts/test-crashingContract.js @@ -60,7 +60,7 @@ const meterExceededInSecondOfferLog = [ 'counter: 2', ]; -test('ZCF metering crash on invitation exercise', async t => { +test('ZCF metering crash on second invitation', async t => { const dump = await main(['meterInSecondInvitation', [8, 0, 0]]); t.deepEqual(dump.log, meterExceededInSecondOfferLog); }); @@ -146,7 +146,7 @@ const thrownExceptionInMakeContractILog = [ 'newCounter: 2', ]; -test('ZCF metering crash in makeContract call', async t => { +test('throw in makeContract call', async t => { const dump = await main(['throwInMakeContract', [3, 0, 0]]); t.deepEqual(dump.log, thrownExceptionInMakeContractILog); }); diff --git a/packages/zoe/test/unitTests/contractSupport/test-bondingCurves.js b/packages/zoe/test/unitTests/contractSupport/test-bondingCurves.js index 80b2f57d58e..aa2a0c84f3c 100644 --- a/packages/zoe/test/unitTests/contractSupport/test-bondingCurves.js +++ b/packages/zoe/test/unitTests/contractSupport/test-bondingCurves.js @@ -86,7 +86,7 @@ test('getInputPrice ok 6', t => { testGetPrice(t, input, expectedOutput); }); -test('calculate value to mint - positive supply', t => { +test('calculate value to mint - positive supply 1', t => { const res = calcLiqValueToMint({ liqTokenSupply: 20, inputValue: 30, @@ -103,11 +103,14 @@ test('calculate value to mint - mispelled key', t => { inputValue: 30, inputReserve: 5, }), + { + message: /value required/, + }, `calcLiqValueToMint should throw if a key is misspelled`, ); }); -test('calculate value to mint - positive supply', t => { +test('calculate value to mint - positive supply 2', t => { const res = calcLiqValueToMint({ liqTokenSupply: 5, inputValue: 8, diff --git a/packages/zoe/test/unitTests/contracts/test-escrowToVote.js b/packages/zoe/test/unitTests/contracts/test-escrowToVote.js index a373a33ea7c..4838f0b4302 100644 --- a/packages/zoe/test/unitTests/contracts/test-escrowToVote.js +++ b/packages/zoe/test/unitTests/contracts/test-escrowToVote.js @@ -62,24 +62,27 @@ test('zoe - escrowToVote', async t => { const result = await E(voter).vote('YES'); t.is(result, `Successfully voted 'YES'`, `voter1 votes YES`); + return { voter, seat }; + }; - seat.getPayout('Assets').then(async moolaPayment => { - t.deepEqual( - await moolaIssuer.getAmountOf(moolaPayment), - moola(3), - `voter1 gets everything she escrowed back`, - ); - - console.log('EXPECTED ERROR ->>>'); - t.throws( - () => voter.vote('NO'), - { message: /the voter seat has exited/ }, - `voter1 voting fails once offer is withdrawn or amounts are reallocated`, - ); - }); + const voter1CollectsPayout = async ({ voter, seat }) => { + const moolaPayment = await seat.getPayout('Assets'); + t.deepEqual( + await moolaIssuer.getAmountOf(moolaPayment), + moola(3), + `voter1 gets everything she escrowed back`, + ); + + console.log('EXPECTED ERROR ->>>'); + t.throws( + () => voter.vote('NO'), + { message: /the voter seat has exited/ }, + `voter1 voting fails once offer is withdrawn or amounts are reallocated`, + ); }; - await voter1Votes(voterInvitation1); + const voter1Result = await voter1Votes(voterInvitation1); + const voter1DoneP = voter1CollectsPayout(voter1Result); // Voter 2 makes badly formed vote, then votes YES, then changes // vote to NO. Vote will be weighted by 5 (5 moola escrowed). @@ -106,24 +109,27 @@ test('zoe - escrowToVote', async t => { // Votes can be recast at any time const result2 = await E(voter).vote('NO'); t.is(result2, `Successfully voted 'NO'`, `voter 2 recast vote for NO`); + return { voter, seat }; + }; - seat.getPayout('Assets').then(async moolaPayment => { - t.deepEqual( - await moolaIssuer.getAmountOf(moolaPayment), - moola(5), - `voter2 gets everything she escrowed back`, - ); - - console.log('EXPECTED ERROR ->>>'); - t.throws( - () => voter.vote('NO'), - { message: /the voter seat has exited/ }, - `voter2 voting fails once offer is withdrawn or amounts are reallocated`, - ); - }); + const voter2CollectsPayout = async ({ voter, seat }) => { + const moolaPayment = await seat.getPayout('Assets'); + t.deepEqual( + await moolaIssuer.getAmountOf(moolaPayment), + moola(5), + `voter2 gets everything she escrowed back`, + ); + + console.log('EXPECTED ERROR ->>>'); + t.throws( + () => voter.vote('NO'), + { message: /the voter seat has exited/ }, + `voter2 voting fails once offer is withdrawn or amounts are reallocated`, + ); }; - await voter2Votes(voterInvitation2); + const voter2Result = await voter2Votes(voterInvitation2); + const voter2DoneP = voter2CollectsPayout(voter2Result); // Voter 3 votes NO and then exits the seat, retrieving their // assets, meaning that their vote should not be counted. They get @@ -145,6 +151,10 @@ test('zoe - escrowToVote', async t => { // not be counted. seat.tryExit(); + return { voter, seat }; + }; + + const voter3CollectsPayout = async ({ voter, seat }) => { const moolaPayment = await seat.getPayout('Assets'); t.deepEqual( @@ -161,7 +171,8 @@ test('zoe - escrowToVote', async t => { ); }; - await voter3Votes(voterInvitation3); + const voter3Result = await voter3Votes(voterInvitation3); + const voter3DoneP = voter3CollectsPayout(voter3Result); // Voter4 votes YES with a weight of 4 const voter4Votes = async invitation => { @@ -178,22 +189,26 @@ test('zoe - escrowToVote', async t => { t.is(result, `Successfully voted 'YES'`, `voter1 votes YES`); - seat.getPayout('Assets').then(async moolaPayment => { - t.deepEqual( - await moolaIssuer.getAmountOf(moolaPayment), - moola(4), - `voter4 gets everything she escrowed back`, - ); - }); + return seat; + }; + + const voter4CollectsPayout = async seat => { + const moolaPayment = seat.getPayout('Assets'); + t.deepEqual( + await moolaIssuer.getAmountOf(moolaPayment), + moola(4), + `voter4 gets everything she escrowed back`, + ); }; - await voter4Votes(voterInvitation4); + const voter4Seat = await voter4Votes(voterInvitation4); + const voter4DoneP = voter4CollectsPayout(voter4Seat); // Secretary closes election and tallies the votes. const electionResults = await E(secretary).closeElection(); t.deepEqual(electionResults, { YES: moola(7), NO: moola(5) }); // Once the election is closed, the voters get their escrowed funds - // back and can no longer vote. See the voter functions for the - // resolution of the payout promises for each voter. + // back and can no longer vote. + await Promise.all([voter1DoneP, voter2DoneP, voter3DoneP, voter4DoneP]); }); diff --git a/packages/zoe/test/unitTests/contracts/test-secondPriceAuction.js b/packages/zoe/test/unitTests/contracts/test-secondPriceAuction.js index 51c1be9d137..17f828100ab 100644 --- a/packages/zoe/test/unitTests/contracts/test-secondPriceAuction.js +++ b/packages/zoe/test/unitTests/contracts/test-secondPriceAuction.js @@ -53,7 +53,11 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => { const seat = await E(zoe).offer(sellInvitation, proposal, payments); - E(seat) + const makeBidInvitationObj = await E(seat).getOfferResult(); + return { seat, makeBidInvitationObj }; + }, + collectPayout: async seat => { + await E(seat) .getPayout('Asset') .then(moolaPurse.deposit) .then(amountDeposited => @@ -64,7 +68,7 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => { ), ); - E(seat) + await E(seat) .getPayout('Ask') .then(simoleanPurse.deposit) .then(amountDeposited => @@ -74,9 +78,6 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => { `Alice got the second price bid, Carol's bid, even though Bob won`, ), ); - - const makeBidInvitationObj = await E(seat).getOfferResult(); - return makeBidInvitationObj; }, }; }; @@ -129,19 +130,17 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => { await E(seat).getOfferResult(), 'The offer has been accepted. Once the contract has been completed, please check your payout', ); - - E(seat) + return seat; + }, + collectPayout: async seat => { + await E(seat) .getPayout('Asset') .then(moolaPurse.deposit) .then(amountDeposited => - t.deepEqual( - amountDeposited, - proposal.want.Asset, - `Bob wins the auction`, - ), + t.deepEqual(amountDeposited, moola(1), `Bob wins the auction`), ); - E(seat) + await E(seat) .getPayout('Bid') .then(simoleanPurse.deposit) .then(amountDeposited => @@ -175,15 +174,17 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => { await E(seat).getOfferResult(), 'The offer has been accepted. Once the contract has been completed, please check your payout', ); - - E(seat) + return seat; + }, + collectPayout: async seat => { + await E(seat) .getPayout('Asset') .then(moolaPurse.deposit) .then(amountDeposited => t.deepEqual(amountDeposited, moola(0), `didn't win the auction`), ); - E(seat) + await E(seat) .getPayout('Bid') .then(simoleanPurse.deposit) .then(amountDeposited => @@ -214,16 +215,25 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => { const { creatorInvitation } = await alice.startInstance(installation); - const makeInvitationsObj = await alice.offer(creatorInvitation); + const { seat: aliceSeat, makeBidInvitationObj } = await alice.offer( + creatorInvitation, + ); - const bidInvitation1 = E(makeInvitationsObj).makeBidInvitation(); - const bidInvitation2 = E(makeInvitationsObj).makeBidInvitation(); - const bidInvitation3 = E(makeInvitationsObj).makeBidInvitation(); + const bidInvitation1 = E(makeBidInvitationObj).makeBidInvitation(); + const bidInvitation2 = E(makeBidInvitationObj).makeBidInvitation(); + const bidInvitation3 = E(makeBidInvitationObj).makeBidInvitation(); - await bob.offer(bidInvitation1); - await carol.offer(bidInvitation2); - await dave.offer(bidInvitation3); + const bobSeat = await bob.offer(bidInvitation1); + const carolSeat = await carol.offer(bidInvitation2); + const daveSeat = await dave.offer(bidInvitation3); timer.tick(); + + await Promise.all([ + alice.collectPayout(aliceSeat), + bob.collectPayout(bobSeat), + carol.collectPayout(carolSeat), + dave.collectPayout(daveSeat), + ]); }); test('zoe - secondPriceAuction - alice tries to exit', async t => {