Skip to content

Commit

Permalink
updates (Agoric#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
katelynsills authored Jun 13, 2019
1 parent b464fac commit 7a29e63
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
51 changes: 8 additions & 43 deletions demo/gallery/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ function build(E, log) {
gallery.adminFacet.revokePixel(rawPixel);
E(aliceP).checkAfterRevoked();
}
async function testAliceSellsBack(aliceMaker, bobMaker, gallery, host) {
async function testAliceSellsBack(aliceMaker, bobMaker, gallery) {
log('starting testAliceSellsBack');
const aliceP = E(aliceMaker).make(gallery.userFacet);
await E(aliceP).doTapFaucetAndSell(host);
await E(aliceP).doTapFaucetAndSell();
}

const obj0 = {
Expand All @@ -47,73 +47,38 @@ function build(E, log) {
case 'tapFaucet': {
log('starting tapFaucet');
const aliceMaker = await E(vats.alice).makeAliceMaker();
const host = E(vats.host).makeHost();
const gallery = makeGallery(
E,
host,
log,
stateChangeHandler,
canvasSize,
);
const gallery = makeGallery(E, log, stateChangeHandler, canvasSize);
log('alice is made');
return testTapFaucet(aliceMaker, gallery);
}
case 'aliceChangesColor': {
log('starting aliceChangesColor');
const aliceMaker = await E(vats.alice).makeAliceMaker();
const host = E(vats.host).makeHost();
const gallery = makeGallery(
E,
host,
log,
stateChangeHandler,
canvasSize,
);
const gallery = makeGallery(E, log, stateChangeHandler, canvasSize);
log('alice is made');
return testAliceChangesColor(aliceMaker, gallery);
}
case 'aliceSendsOnlyUseRight': {
log('starting aliceSendsOnlyUseRight');
const aliceMaker = await E(vats.alice).makeAliceMaker();
const bobMaker = await E(vats.bob).makeBobMaker();
const host = E(vats.host).makeHost();
const gallery = makeGallery(
E,
host,
log,
stateChangeHandler,
canvasSize,
);
const gallery = makeGallery(E, log, stateChangeHandler, canvasSize);
log('alice is made');
return testAliceSendsOnlyUseRight(aliceMaker, bobMaker, gallery);
}
case 'galleryRevokes': {
log('starting galleryRevokes');
const aliceMaker = await E(vats.alice).makeAliceMaker();
const bobMaker = await E(vats.bob).makeBobMaker();
const host = E(vats.host).makeHost();
const gallery = makeGallery(
E,
host,
log,
stateChangeHandler,
canvasSize,
);
const gallery = makeGallery(E, log, stateChangeHandler, canvasSize);
return testGalleryRevokes(aliceMaker, bobMaker, gallery);
}
case 'aliceSellsBack': {
log('starting aliceSellsBack');
const aliceMaker = await E(vats.alice).makeAliceMaker();
const bobMaker = await E(vats.bob).makeBobMaker();
const host = E(vats.host).makeHost();
const gallery = makeGallery(
E,
host,
log,
stateChangeHandler,
canvasSize,
);
return testAliceSellsBack(aliceMaker, bobMaker, gallery, host);
const gallery = makeGallery(E, log, stateChangeHandler, canvasSize);
return testAliceSellsBack(aliceMaker, bobMaker, gallery);
}
default: {
throw new Error(`unrecognized argument value ${argv[0]}`);
Expand Down
14 changes: 8 additions & 6 deletions demo/gallery/vat-alice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import harden from '@agoric/harden';

import { insist } from '../../util/insist';
import { makeCollect } from '../../core/contractHost';

let storedUseRight;
let storedTransferRight;

function makeAliceMaker(E, log) {
const collect = makeCollect(E, log);

// TODO BUG: All callers should wait until settled before doing
// anything that would change the balance before show*Balance* reads
// it.
Expand Down Expand Up @@ -206,7 +203,7 @@ function makeAliceMaker(E, log) {
}`,
);
},
async doTapFaucetAndSell(host) {
async doTapFaucetAndSell() {
log('++ alice.doTapFaucetAndSell starting');
const pixelPaymentP = E(gallery).tapFaucet();
const { pixelIssuer, dustIssuer } = await E(gallery).getIssuers();
Expand All @@ -218,12 +215,17 @@ function makeAliceMaker(E, log) {
// terms of the amount parameter plus what the gallery is
// willing to offer for it
// sellToGallery returns an invite to the smart contract
const inviteP = E(gallery).sellToGallery(amount);
const { inviteP, host } = await E(gallery).sellToGallery(amount);
const seatP = E(host).redeem(inviteP);
E(seatP).offer(exclusivePixelPaymentP);
const dustPurseP = E(dustIssuer).makeEmptyPurse();
const pixelPurseP = E(pixelIssuer).makeEmptyPurse();
return collect(seatP, dustPurseP, pixelPurseP, 'alice escrow');
return E(gallery).collectFromGallery(
seatP,
dustPurseP,
pixelPurseP,
'alice escrow',
);
},
});
return alice;
Expand Down
32 changes: 22 additions & 10 deletions more/pixels/gallery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Nat from '@agoric/nat';
import harden from '@agoric/harden';
import evaluate from '@agoric/evaluate';

import {
makeCompoundPixelAssayMaker,
Expand All @@ -8,19 +9,19 @@ import {
} from './pixelAssays';
import { makeMint } from '../../core/issuers';
import { makeWholePixelList, insistPixelList } from './types/pixelList';
import { insistPixel } from './types/pixel';
import { makeMintController } from './pixelMintController';
import { makeLruQueue } from './lruQueue';

import { escrowExchangeSrc } from '../../core/escrow';
import { makeCollect } from '../../core/contractHost';
import { makeContractHost, makeCollect } from '../../core/contractHost';

function mockStateChangeHandler(_newState) {
// does nothing
}

export function makeGallery(
E,
contractHost,
log,
stateChangeHandler = mockStateChangeHandler,
canvasSize = 10,
Expand Down Expand Up @@ -238,7 +239,8 @@ export function makeGallery(
return getDistance(rawPixel, center);
}

function pricePixel(rawPixel) {
function pricePixelInternal(rawPixel) {
insistPixel(rawPixel, canvasSize);
const distance = getDistanceFromCenter(rawPixel);
// prices are simplistic for now
// they range from canvasSize / 2 to canvasSize
Expand All @@ -247,10 +249,11 @@ export function makeGallery(
}

function pricePixelAmount(pixelAmount) {
pixelAmount = pixelAssay.coerce(pixelAmount);
const rawPixelList = pixelAssay.quantity(pixelAmount);
let totalPriceInDust = 0;
for (const rawPixel of rawPixelList) {
totalPriceInDust += pricePixel(rawPixel);
totalPriceInDust += pricePixelInternal(rawPixel);
}
return dustAssay.make(totalPriceInDust);
}
Expand All @@ -273,21 +276,29 @@ export function makeGallery(
);
// dustPurse is dropped
const terms = harden([dustAmount, pixelAmount]);
const escrowExchangeInstallationP = E(contractHost).install(
const contractHost = makeContractHost(E, evaluate);
const escrowExchangeInstallationP = await E(contractHost).install(
escrowExchangeSrc,
);
const [galleryInviteP, userInviteP] = await E(
escrowExchangeInstallationP,
).spawn(terms);
const seatP = E(contractHost).redeem(galleryInviteP);
E(seatP).offer(dustPaymentP);
const dustPurseP = E(dustIssuer).makeEmptyPurse();
const pixelPurseP = E(pixelIssuer).makeEmptyPurse();
const dustPurseP = dustIssuer.makeEmptyPurse();
const pixelPurseP = pixelIssuer.makeEmptyPurse();
collect(seatP, pixelPurseP, dustPurseP, 'gallery escrow');
return userInviteP;
return {
inviteP: userInviteP,
host: contractHost,
};
});
}

function collectFromGallery(seatP, pixelPurseP, dustPurseP, name) {
return collect(seatP, pixelPurseP, dustPurseP, name);
}

function getIssuers() {
return {
pixelIssuer,
Expand All @@ -307,15 +318,16 @@ export function makeGallery(
getCanvasSize() {
return canvasSize;
},
pricePixel, // transparent pricing for now
pricePixelAmount, // transparent pricing for now
sellToGallery,
collectFromGallery,
};

const adminFacet = {
revokePixel,
getDistance,
getDistanceFromCenter,
pricePixel,
pricePixelAmount,
};

const readFacet = {
Expand Down
39 changes: 30 additions & 9 deletions test/more/pixels/test-gallery.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';

import { makeGallery } from '../../../more/pixels/gallery';

import { insistPixelList } from '../../../more/pixels/types/pixelList';

test('tapFaucet', t => {
Expand Down Expand Up @@ -234,15 +234,36 @@ test('getDistanceFromCenter', t => {
t.end();
});

test('pricePixel', t => {
test('pricePixel Internal', t => {
const { userFacet } = makeGallery();
// default canvasSize is 10
const { pricePixel } = userFacet;
t.deepEqual(pricePixel({ x: 0, y: 1 }), 4);
t.deepEqual(pricePixel({ x: 2, y: 1 }), 5);
t.deepEqual(pricePixel({ x: 2, y: 3 }), 7);
t.deepEqual(pricePixel({ x: 4, y: 1 }), 6);
t.deepEqual(pricePixel({ x: 0, y: 7 }), 5);
t.deepEqual(pricePixel({ x: 5, y: 5 }), 10);
const { pricePixelAmount, getIssuers } = userFacet;
const { dustIssuer, pixelIssuer } = getIssuers();
const dustAssay = dustIssuer.getAssay();
const pixelAssay = pixelIssuer.getAssay();
t.deepEqual(
pricePixelAmount(pixelAssay.make(harden([{ x: 0, y: 1 }]))),
dustAssay.make(4),
);
t.deepEqual(
pricePixelAmount(pixelAssay.make(harden([{ x: 2, y: 1 }]))),
dustAssay.make(5),
);
t.deepEqual(
pricePixelAmount(pixelAssay.make(harden([{ x: 2, y: 3 }]))),
dustAssay.make(7),
);
t.deepEqual(
pricePixelAmount(pixelAssay.make(harden([{ x: 4, y: 1 }]))),
dustAssay.make(6),
);
t.deepEqual(
pricePixelAmount(pixelAssay.make(harden([{ x: 0, y: 7 }]))),
dustAssay.make(5),
);
t.deepEqual(
pricePixelAmount(pixelAssay.make(harden([{ x: 5, y: 5 }]))),
dustAssay.make(10),
);
t.end();
});

0 comments on commit 7a29e63

Please sign in to comment.