Skip to content

Commit

Permalink
test: add some tests for the swap helper
Browse files Browse the repository at this point in the history
move setupZCFTest to a common location and share it.
use makeOffer helper suggested by Kate
  • Loading branch information
Chris-Hibbert committed Sep 14, 2020
1 parent 12f5911 commit 2a8b0fc
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 188 deletions.
165 changes: 0 additions & 165 deletions packages/zoe/test/unitTests/contractSupport/test-zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,171 +82,6 @@ test.skip('ZoeHelpers assertKeywords', t => {
);
});

test.skip('ZoeHelpers swap ok', t => {
t.plan(4);
const { moolaR, simoleanR, moola, simoleans } = setup();
const leftOfferHandle = harden({});
const rightOfferHandle = harden({});
const cantTradeRightOfferHandle = harden({});
const mockZCFBuilder = makeMockTradingZcfBuilder();
mockZCFBuilder.addBrand(moolaR);
mockZCFBuilder.addBrand(simoleanR);
mockZCFBuilder.addAllocation(leftOfferHandle, { Asset: moola(10) });
mockZCFBuilder.addAllocation(rightOfferHandle, { Price: simoleans(6) });
mockZCFBuilder.addAllocation(cantTradeRightOfferHandle, {
Price: simoleans(6),
});
mockZCFBuilder.addOffer(leftOfferHandle, {
proposal: {
give: { Asset: moola(10) },
want: { Price: simoleans(4) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addOffer(rightOfferHandle, {
proposal: {
give: { Price: simoleans(6) },
want: { Asset: moola(7) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addOffer(cantTradeRightOfferHandle, {
proposal: {
give: { Price: simoleans(6) },
want: { Asset: moola(100) },
exit: { onDemand: null },
},
});
const mockZCF = mockZCFBuilder.build();
// eslint-disable-next-line no-undef
const { swap } = makeZoeHelpers(mockZCF);
t.truthy(
swap(leftOfferHandle, rightOfferHandle, 'prior offer no longer available'),
);
t.deepEqual(
mockZCF.getReallocatedHandles(),
harden([leftOfferHandle, rightOfferHandle]),
`both handles reallocated`,
);
t.deepEqual(
mockZCF.getReallocatedAmountObjs(),
[
{ Asset: moola(3), Price: simoleans(4) },
{ Price: simoleans(2), Asset: moola(7) },
],
`amounts reallocated passed to reallocate were as expected`,
);
t.deepEqual(
mockZCF.getCompletedHandles(),
harden([leftOfferHandle, rightOfferHandle]),
`both handles were completed`,
);
});

test.skip('ZoeHelpers swap keep inactive', t => {
t.plan(4);
const { moola, simoleans } = setup();
const leftOfferHandle = harden({});
const rightOfferHandle = harden({});
const cantTradeRightOfferHandle = harden({});
const mockZCFBuilder = makeMockTradingZcfBuilder();
mockZCFBuilder.addOffer(leftOfferHandle, {
proposal: {
give: { Asset: moola(10) },
want: { Price: simoleans(4) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addOffer(rightOfferHandle, {
proposal: {
give: { Price: simoleans(6) },
want: { Asset: moola(7) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addOffer(cantTradeRightOfferHandle, {
proposal: {
give: { Price: simoleans(6) },
want: { Asset: moola(100) },
exit: { onDemand: null },
},
});
mockZCFBuilder.setOffersInactive();
const mockZCF = mockZCFBuilder.build();
// eslint-disable-next-line no-undef
const { swap } = makeZoeHelpers(mockZCF);
t.throws(
() =>
swap(
leftOfferHandle,
rightOfferHandle,
'prior offer no longer available',
),
/Error: prior offer no longer available/,
`throws if keepHandle offer is not active`,
);
const reallocatedHandles = mockZCF.getReallocatedHandles();
t.deepEqual(reallocatedHandles, harden([]), `nothing reallocated`);
const reallocatedAmountObjs = mockZCF.getReallocatedAmountObjs();
t.deepEqual(reallocatedAmountObjs, harden([]), `no amounts reallocated`);
t.deepEqual(
mockZCF.getCompletedHandles(),
harden([]),
`no offers were completed`,
);
});

test.skip(`ZoeHelpers swap - can't trade with`, t => {
t.plan(4);
const { moolaR, simoleanR, moola, simoleans } = setup();
const leftOfferHandle = harden({});
const rightOfferHandle = harden({});
const cantTradeHandle = harden({});

const mockZCFBuilder = makeMockTradingZcfBuilder();
mockZCFBuilder.addBrand(moolaR);
mockZCFBuilder.addBrand(simoleanR);
mockZCFBuilder.addOffer(leftOfferHandle, {
proposal: {
give: { Asset: moola(10) },
want: { Price: simoleans(4) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addOffer(rightOfferHandle, {
proposal: {
give: { Price: simoleans(6) },
want: { Asset: moola(7) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addOffer(cantTradeHandle, {
proposal: {
give: { Price: simoleans(6) },
want: { Asset: moola(100) },
exit: { onDemand: null },
},
});
mockZCFBuilder.addAllocation(leftOfferHandle, { Asset: moola(10) });
mockZCFBuilder.addAllocation(rightOfferHandle, { Price: simoleans(6) });
mockZCFBuilder.addAllocation(cantTradeHandle, { Price: simoleans(6) });
const mockZcf = mockZCFBuilder.build();
// eslint-disable-next-line no-undef
const { swap } = makeZoeHelpers(mockZcf);
t.throws(
() =>
swap(leftOfferHandle, cantTradeHandle, 'prior offer no longer available'),
/Error: The offer was invalid. Please check your refund./,
`throws if can't trade with left and right`,
);
const reallocatedHandles = mockZcf.getReallocatedHandles();
t.deepEqual(reallocatedHandles, harden([]), `nothing reallocated`);
const reallocatedAmountObjs = mockZcf.getReallocatedAmountObjs();
t.deepEqual(reallocatedAmountObjs, harden([]), `no amounts reallocated`);
const completedHandles = mockZcf.getCompletedHandles();
t.deepEqual(completedHandles, harden([]), `no offers were completed`);
});

test.skip('ZoeHelpers isOfferSafe', t => {
t.plan(5);
const { moolaR, simoleanR, moola, simoleans } = setup();
Expand Down
26 changes: 26 additions & 0 deletions packages/zoe/test/unitTests/zcf/setupZcfTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { E } from '@agoric/eventual-send';
import bundleSource from '@agoric/bundle-source';

// noinspection ES6PreferShortImport
import { makeZoe } from '../../../src/zoeService/zoe';
import { makeFakeVatAdmin } from '../contracts/fakeVatAdmin';

const contractRoot = `${__dirname}/zcfTesterContract`;

export const setupZCFTest = async (issuerKeywordRecord, terms) => {
/** @type ContractFacet */
let zcf;
const setZCF = jig => {
zcf = jig.zcf;
};
// The contract provides the `zcf` via `setTestJig` upon `start`.
const zoe = makeZoe(makeFakeVatAdmin(setZCF));
const bundle = await bundleSource(contractRoot);
const installation = await E(zoe).install(bundle);
const { creatorFacet, instance } = await E(zoe).startInstance(
installation,
issuerKeywordRecord,
terms,
);
return { zoe, zcf, instance, installation, creatorFacet };
};
24 changes: 1 addition & 23 deletions packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,11 @@ import test from 'ava';

import { MathKind } from '@agoric/ertp';
import { E } from '@agoric/eventual-send';
import bundleSource from '@agoric/bundle-source';

// noinspection ES6PreferShortImport
import { makeZoe } from '../../../src/zoeService/zoe';
import { setup } from '../setupBasicMints';
import { makeFakeVatAdmin } from '../contracts/fakeVatAdmin';
import buildManualTimer from '../../../tools/manualTimer';

const contractRoot = `${__dirname}/zcfTesterContract`;

const setupZCFTest = async (issuerKeywordRecord, terms) => {
/** @type {ContractFacet} */
let zcf;
const setZCF = jig => {
zcf = jig.zcf;
};
// The contract provides the `zcf` via `setTestJig` upon `start`.
const zoe = makeZoe(makeFakeVatAdmin(setZCF));
const bundle = await bundleSource(contractRoot);
const installation = await zoe.install(bundle);
const { creatorFacet, instance } = await E(zoe).startInstance(
installation,
issuerKeywordRecord,
terms,
);
return { zoe, zcf, instance, installation, creatorFacet };
};
import { setupZCFTest } from './setupZcfTest';

// TODO: Still to be tested:
// * @property {Reallocate} reallocate
Expand Down
97 changes: 97 additions & 0 deletions packages/zoe/test/unitTests/zcf/test-zoeHelpersWZcf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import '@agoric/install-ses';
import { E } from '@agoric/eventual-send';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';

import { setup } from '../setupBasicMints';
import { swap } from '../../../src/contractSupport';
import { assertPayoutAmount } from '../../zoeTestHelpers';
import { setupZCFTest } from './setupZcfTest';

const makeOffer = async (zoe, zcf, proposal, payments) => {
let zcfSeat;
const getSeat = seat => {
zcfSeat = seat;
};
const invitation = await zcf.makeInvitation(getSeat, 'seat');
const userSeat = await E(zoe).offer(invitation, proposal, payments);
return { zcfSeat, userSeat };
};

test(`zoeHelper with zcf - swap`, async t => {
const {
moolaIssuer,
moola,
moolaMint,
simoleanIssuer,
simoleanMint,
simoleans,
} = setup();
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer };
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord);

const { zcfSeat: aZcfSeat, userSeat: aUserSeat } = await makeOffer(
zoe,
zcf,
harden({ want: { A: moola(3) }, give: { B: simoleans(7) } }),
{ B: simoleanMint.mintPayment(simoleans(7)) },
);
const { zcfSeat: bZcfSeat, userSeat: bUserSeat } = await makeOffer(
zoe,
zcf,
harden({ want: { B: simoleans(3) }, give: { A: moola(5) } }),
{ A: moolaMint.mintPayment(moola(5)) },
);
const message = await swap(zcf, aZcfSeat, bZcfSeat);
t.is(
message,
'The offer has been accepted. Once the contract has been completed, please check your payout',
);
assertPayoutAmount(t, moolaIssuer, await aUserSeat.getPayout('A'), moola(3));
const seat1PayoutB = await aUserSeat.getPayout('B');
assertPayoutAmount(t, simoleanIssuer, seat1PayoutB, simoleans(4));
const seat2PayoutB = await bUserSeat.getPayout('B');
assertPayoutAmount(t, simoleanIssuer, seat2PayoutB, simoleans(3));
assertPayoutAmount(t, moolaIssuer, await bUserSeat.getPayout('A'), moola(2));
});

test(`zoeHelper with zcf - swap no match`, async t => {
const {
moolaIssuer,
moola,
moolaMint,
simoleanIssuer,
simoleanMint,
simoleans,
} = setup();
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer };
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord);

const { zcfSeat: aZcfSeat, userSeat: aUserSeat } = await makeOffer(
zoe,
zcf,
harden({ want: { A: moola(20) }, give: { B: simoleans(3) } }),
{ B: simoleanMint.mintPayment(simoleans(3)) },
);
const { zcfSeat: bZcfSeat, userSeat: bUserSeat } = await makeOffer(
zoe,
zcf,
harden({ want: { B: simoleans(43) }, give: { A: moola(5) } }),
{ A: moolaMint.mintPayment(moola(5)) },
);
t.throws(
() => swap(zcf, aZcfSeat, bZcfSeat),
{
message:
'The trade between left [object Object] and right [object Object] failed. Please check the log for more information',
},
'mismatched offers',
);
assertPayoutAmount(t, moolaIssuer, await aUserSeat.getPayout('A'), moola(0));
const seat1PayoutB = await aUserSeat.getPayout('B');
assertPayoutAmount(t, simoleanIssuer, seat1PayoutB, simoleans(3));
const seat2PayoutB = await bUserSeat.getPayout('B');
assertPayoutAmount(t, simoleanIssuer, seat2PayoutB, simoleans(0));
assertPayoutAmount(t, moolaIssuer, await bUserSeat.getPayout('A'), moola(5));
});

0 comments on commit 2a8b0fc

Please sign in to comment.