|
| 1 | +import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js'; |
| 2 | + |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +import { E } from '@endo/eventual-send'; |
| 6 | +import bundleSource from '@endo/bundle-source'; |
| 7 | + |
| 8 | +import { M } from '@endo/patterns'; |
| 9 | +import { AmountShape } from '@agoric/ertp'; |
| 10 | +import { makeZoeForTest } from '../../../tools/setup-zoe.js'; |
| 11 | +import { setup } from '../setupBasicMints.js'; |
| 12 | +import { makeFakeVatAdmin } from '../../../tools/fakeVatAdmin.js'; |
| 13 | + |
| 14 | +const dirname = path.dirname(new URL(import.meta.url).pathname); |
| 15 | + |
| 16 | +const contractRoot = `${dirname}/zcfTesterContract.js`; |
| 17 | + |
| 18 | +test(`ProposalShapes mismatch`, async t => { |
| 19 | + const { moolaIssuer, simoleanIssuer, moola, moolaMint } = setup(); |
| 20 | + let testJig; |
| 21 | + const setJig = jig => { |
| 22 | + testJig = jig; |
| 23 | + }; |
| 24 | + const { admin: fakeVatAdminSvc, vatAdminState } = makeFakeVatAdmin(setJig); |
| 25 | + /** @type {ZoeService} */ |
| 26 | + const zoe = makeZoeForTest(fakeVatAdminSvc); |
| 27 | + |
| 28 | + // pack the contract |
| 29 | + const bundle = await bundleSource(contractRoot); |
| 30 | + // install the contract |
| 31 | + vatAdminState.installBundle('b1-zcftester', bundle); |
| 32 | + const installation = await E(zoe).installBundleID('b1-zcftester'); |
| 33 | + |
| 34 | + // Alice creates an instance |
| 35 | + const issuerKeywordRecord = harden({ |
| 36 | + Pixels: moolaIssuer, |
| 37 | + Money: simoleanIssuer, |
| 38 | + }); |
| 39 | + |
| 40 | + await E(zoe).startInstance(installation, issuerKeywordRecord); |
| 41 | + |
| 42 | + // The contract uses the testJig so the contractFacet |
| 43 | + // is available here for testing purposes |
| 44 | + /** @type {ZCF} */ |
| 45 | + // @ts-expect-error cast |
| 46 | + const zcf = testJig.zcf; |
| 47 | + |
| 48 | + const boring = () => { |
| 49 | + return 'ok'; |
| 50 | + }; |
| 51 | + |
| 52 | + const proposalShape = M.splitRecord({ |
| 53 | + give: { B: AmountShape }, |
| 54 | + exit: { deadline: M.any() }, |
| 55 | + }); |
| 56 | + const invitation = await zcf.makeInvitation( |
| 57 | + boring, |
| 58 | + 'seat1', |
| 59 | + {}, |
| 60 | + proposalShape, |
| 61 | + ); |
| 62 | + const { handle } = await E(zoe).getInvitationDetails(invitation); |
| 63 | + const shape = await E(zoe).getProposalShapeForInvitation(handle); |
| 64 | + t.deepEqual(shape, proposalShape); |
| 65 | + |
| 66 | + const proposal = harden({ |
| 67 | + give: { B: moola(5n) }, |
| 68 | + exit: { onDemand: null }, |
| 69 | + }); |
| 70 | + |
| 71 | + const fiveMoola = moolaMint.mintPayment(moola(5n)); |
| 72 | + await t.throwsAsync( |
| 73 | + () => |
| 74 | + E(zoe).offer(invitation, proposal, { |
| 75 | + B: fiveMoola, |
| 76 | + }), |
| 77 | + { |
| 78 | + message: |
| 79 | + '"seat1" proposal: exit: {"onDemand":null} - Must have missing properties ["deadline"]', |
| 80 | + }, |
| 81 | + ); |
| 82 | + t.falsy(vatAdminState.getHasExited()); |
| 83 | + // The moola was not deposited. |
| 84 | + t.true(await E(moolaIssuer).isLive(fiveMoola)); |
| 85 | +}); |
| 86 | + |
| 87 | +test(`ProposalShapes matched`, async t => { |
| 88 | + const { moolaIssuer, simoleanIssuer } = setup(); |
| 89 | + let testJig; |
| 90 | + const setJig = jig => { |
| 91 | + testJig = jig; |
| 92 | + }; |
| 93 | + const { admin: fakeVatAdminSvc, vatAdminState } = makeFakeVatAdmin(setJig); |
| 94 | + /** @type {ZoeService} */ |
| 95 | + const zoe = makeZoeForTest(fakeVatAdminSvc); |
| 96 | + |
| 97 | + // pack the contract |
| 98 | + const bundle = await bundleSource(contractRoot); |
| 99 | + // install the contract |
| 100 | + vatAdminState.installBundle('b1-zcftester', bundle); |
| 101 | + const installation = await E(zoe).installBundleID('b1-zcftester'); |
| 102 | + |
| 103 | + // Alice creates an instance |
| 104 | + const issuerKeywordRecord = harden({ |
| 105 | + Pixels: moolaIssuer, |
| 106 | + Money: simoleanIssuer, |
| 107 | + }); |
| 108 | + |
| 109 | + await E(zoe).startInstance(installation, issuerKeywordRecord); |
| 110 | + |
| 111 | + // The contract uses the testJig so the contractFacet |
| 112 | + // is available here for testing purposes |
| 113 | + /** @type {ZCF} */ |
| 114 | + // @ts-expect-error cast |
| 115 | + const zcf = testJig.zcf; |
| 116 | + |
| 117 | + const boring = () => { |
| 118 | + return 'ok'; |
| 119 | + }; |
| 120 | + |
| 121 | + const proposalShape = M.splitRecord({ exit: { onDemand: null } }); |
| 122 | + const invitation = await zcf.makeInvitation( |
| 123 | + boring, |
| 124 | + 'seat', |
| 125 | + {}, |
| 126 | + proposalShape, |
| 127 | + ); |
| 128 | + const { handle } = await E(zoe).getInvitationDetails(invitation); |
| 129 | + const shape = await E(zoe).getProposalShapeForInvitation(handle); |
| 130 | + t.deepEqual(shape, proposalShape); |
| 131 | + |
| 132 | + // onDemand is the default |
| 133 | + const seat = await E(zoe).offer(invitation); |
| 134 | + |
| 135 | + const result = await E(seat).getOfferResult(); |
| 136 | + t.is(result, 'ok', `userSeat1 offer result`); |
| 137 | + |
| 138 | + t.falsy(await E(seat).hasExited()); |
| 139 | + await E(seat).tryExit(); |
| 140 | + t.true(await E(seat).hasExited()); |
| 141 | + const payouts = await E(seat).getPayouts(); |
| 142 | + t.deepEqual(payouts, {}); |
| 143 | +}); |
0 commit comments