Skip to content

Commit

Permalink
feat: update exchange benchmark to re-use simpleExchange contract
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Aug 27, 2020
1 parent b1a6db0 commit 3ecbee0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 110 deletions.
98 changes: 34 additions & 64 deletions packages/swingset-runner/demo/exchangeBenchmark/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,8 @@
import { makeIssuerKit } from '@agoric/ertp';
import { E } from '@agoric/eventual-send';
import { makePrintLog } from './printLog';

/* eslint-disable-next-line import/no-unresolved, import/extensions */
import simpleExchangeBundle from './bundle-simpleExchange';

const log = makePrintLog();

function setupBasicMints() {
// prettier-ignore
const all = [
makeIssuerKit('moola'),
makeIssuerKit('simoleans'),
];
const mints = all.map(objs => objs.mint);
const issuers = all.map(objs => objs.issuer);
const amountMaths = all.map(objs => objs.amountMath);

return harden({
mints,
issuers,
amountMaths,
});
}

function makeVats(vats, zoe, installations, startingValues) {
const { mints, issuers, amountMaths } = setupBasicMints();
// prettier-ignore
function makePayments(values) {
return mints.map((mint, i) => mint.mintPayment(amountMaths[i].make(values[i])));
}
const [aliceValues, bobValues] = startingValues;

// Setup Alice
const alice = E(vats.alice).build(
zoe,
issuers,
makePayments(aliceValues),
installations,
);

// Setup Bob
const bob = E(vats.bob).build(
zoe,
issuers,
makePayments(bobValues),
installations,
);

const result = {
alice,
bob,
};

log(`=> alice and bob are set up`);
return harden(result);
}
import exchangeBundle from './bundle-simpleExchange';

export function buildRootObject(_vatPowers, vatParameters) {
let alice;
Expand All @@ -68,27 +15,50 @@ export function buildRootObject(_vatPowers, vatParameters) {
);
const zoe = await E(vats.zoe).buildZoe(vatAdminSvc);

const installations = {
simpleExchange: await E(zoe).install(simpleExchangeBundle.bundle),
};
const exchange = await E(zoe).install(exchangeBundle.bundle);

const startingValues = [
const grubStake = [
[3, 0], // Alice: 3 moola, no simoleans
[0, 3], // Bob: no moola, 3 simoleans
];

({ alice, bob } = makeVats(vats, zoe, installations, startingValues));
const all = [makeIssuerKit('moola'), makeIssuerKit('simoleans')];
const mints = all.map(objs => objs.mint);
const issuers = all.map(objs => objs.issuer);
const amountMaths = all.map(objs => objs.amountMath);

function makePayments(values) {
return mints.map((mint, i) =>
mint.mintPayment(amountMaths[i].make(values[i])),
);
}

const [alicePayments, bobPayments] = grubStake.map(v => makePayments(v));

const [moolaIssuer, simoleanIssuer] = issuers;
const issuerKeywordRecord = harden({
Price: simoleanIssuer,
Asset: moolaIssuer,
});
const { publicFacet } = await E(zoe).startInstance(
exchange,
issuerKeywordRecord,
);

alice = E(vats.alice).build(zoe, issuers, alicePayments, publicFacet);
bob = E(vats.bob).build(zoe, issuers, bobPayments, publicFacet);

// Zoe appears to do some one-time setup the first time it's used, so this
// is a sacrifical benchmark round to prime the pump.
// is an optional, sacrifical benchmark round to prime the pump.
if (vatParameters.argv[0] === '--prime') {
await E(alice).initiateSimpleExchange(bob);
await E(bob).initiateSimpleExchange(alice);
await E(alice).initiateTrade(bob);
await E(bob).initiateTrade(alice);
}
},
async runBenchmarkRound() {
round += 1;
await E(alice).initiateSimpleExchange(bob);
await E(bob).initiateSimpleExchange(alice);
await E(alice).initiateTrade(bob);
await E(bob).initiateTrade(alice);
return `round ${round} complete`;
},
});
Expand Down
47 changes: 18 additions & 29 deletions packages/swingset-runner/demo/exchangeBenchmark/exchanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@ const log = makePrintLog();
* @param {ZoeService} zoe
* @param {Issuer[]} issuers
* @param {Payment[]} payments
* @param {Record<string,Installation>} installations
* @param {{ makeInvitation: () => Invitation }} publicAPI
*/
async function build(name, zoe, issuers, payments, installations) {
async function build(name, zoe, issuers, payments, publicAPI) {
const { moola, simoleans, purses } = await setupPurses(
zoe,
issuers,
payments,
);
const [moolaPurseP, simoleanPurseP] = purses;
const [moolaIssuer, simoleanIssuer] = issuers;
const issuerKeywordRecord = harden({
Price: simoleanIssuer,
Asset: moolaIssuer,
});
const inviteIssuer = await E(zoe).getInvitationIssuer();
const { simpleExchange } = installations;

const invitationIssuer = await E(zoe).getInvitationIssuer();

async function preReport() {
await showPurseBalance(moolaPurseP, `${name} moola before`, log);
Expand All @@ -49,16 +44,10 @@ async function build(name, zoe, issuers, payments, installations) {
await E(simoleanPurseP).deposit(simoleanPayout);
}

async function initiateSimpleExchange(otherP) {
async function initiateTrade(otherP) {
await preReport();

const { publicFacet } = await E(zoe).startInstance(
simpleExchange,
issuerKeywordRecord,
);
const publicAPI = /** @type {{ makeInvitation: () => Invitation }} */ (publicFacet);

const addOrderInvite = await E(publicAPI).makeInvitation();
const addOrderInvitation = await E(publicAPI).makeInvitation();

const mySellOrderProposal = harden({
give: { Asset: moola(1) },
Expand All @@ -69,24 +58,24 @@ async function build(name, zoe, issuers, payments, installations) {
Asset: await E(moolaPurseP).withdraw(moola(1)),
};
const seat = await E(zoe).offer(
addOrderInvite,
addOrderInvitation,
mySellOrderProposal,
paymentKeywordRecord,
);
const payoutP = E(seat).getPayouts();

const inviteP = E(publicAPI).makeInvitation();
await E(otherP).respondToSimpleExchange(inviteP);
const invitationP = E(publicAPI).makeInvitation();
await E(otherP).respondToTrade(invitationP);

await receivePayout(payoutP);
await postReport();
}

async function respondToSimpleExchange(inviteP) {
async function respondToTrade(invitationP) {
await preReport();

const invite = await inviteP;
const exclInvite = await E(inviteIssuer).claim(invite);
const invitation = await invitationP;
const exclInvitation = await E(invitationIssuer).claim(invitation);

const myBuyOrderProposal = harden({
want: { Asset: moola(1) },
Expand All @@ -98,7 +87,7 @@ async function build(name, zoe, issuers, payments, installations) {
};

const seatP = await E(zoe).offer(
exclInvite,
exclInvitation,
myBuyOrderProposal,
paymentKeywordRecord,
);
Expand All @@ -109,14 +98,14 @@ async function build(name, zoe, issuers, payments, installations) {
}

return harden({
initiateSimpleExchange,
respondToSimpleExchange,
initiateTrade,
respondToTrade,
});
}

export function buildRootObjectCommon(name, _vatPowers) {
export function buildRootObject(_vatPowers, vatParameters) {
return harden({
build: (zoe, issuers, payments, installations) =>
build(name, zoe, issuers, payments, installations),
build: (zoe, issuers, payments, publicAPI) =>
build(vatParameters.name, zoe, issuers, payments, publicAPI),
});
}
5 changes: 0 additions & 5 deletions packages/swingset-runner/demo/exchangeBenchmark/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ export async function setupPurses(zoe, issuers, payments) {
const simoleans = simoleanAmountMath.make;

return harden({
issuers: harden([moolaIssuer, simoleanIssuer]),
moolaIssuer,
simoleanIssuer,
moolaAmountMath,
simoleanAmountMath,
moola,
simoleans,
purses,
Expand Down
10 changes: 8 additions & 2 deletions packages/swingset-runner/demo/exchangeBenchmark/swingset.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
},
"vats": {
"alice": {
"sourceSpec": "vat-alice.js"
"sourceSpec": "exchanger.js",
"parameters": {
"name": "alice"
}
},
"bob": {
"sourceSpec": "vat-bob.js"
"sourceSpec": "exchanger.js",
"parameters": {
"name": "bob"
}
},
"zoe": {
"sourceSpec": "vat-zoe.js",
Expand Down
5 changes: 0 additions & 5 deletions packages/swingset-runner/demo/exchangeBenchmark/vat-alice.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/swingset-runner/demo/exchangeBenchmark/vat-bob.js

This file was deleted.

0 comments on commit 3ecbee0

Please sign in to comment.