From 7a29e63b30a95a5eeb9c735c29d9fa16c9c32138 Mon Sep 17 00:00:00 2001 From: Kate Sills Date: Thu, 13 Jun 2019 17:46:18 +0200 Subject: [PATCH] updates (#16) --- demo/gallery/bootstrap.js | 51 +++++--------------------------- demo/gallery/vat-alice.js | 14 +++++---- more/pixels/gallery.js | 32 +++++++++++++------- test/more/pixels/test-gallery.js | 39 ++++++++++++++++++------ 4 files changed, 68 insertions(+), 68 deletions(-) diff --git a/demo/gallery/bootstrap.js b/demo/gallery/bootstrap.js index 64cdaacb8b7..ed9cabecb76 100644 --- a/demo/gallery/bootstrap.js +++ b/demo/gallery/bootstrap.js @@ -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 = { @@ -47,28 +47,14 @@ 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); } @@ -76,14 +62,7 @@ function build(E, log) { 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); } @@ -91,29 +70,15 @@ function build(E, log) { 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]}`); diff --git a/demo/gallery/vat-alice.js b/demo/gallery/vat-alice.js index 9d4c62b0495..2c73c3bb514 100644 --- a/demo/gallery/vat-alice.js +++ b/demo/gallery/vat-alice.js @@ -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. @@ -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(); @@ -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; diff --git a/more/pixels/gallery.js b/more/pixels/gallery.js index a69565cb599..d1bf1b9d108 100644 --- a/more/pixels/gallery.js +++ b/more/pixels/gallery.js @@ -1,5 +1,6 @@ import Nat from '@agoric/nat'; import harden from '@agoric/harden'; +import evaluate from '@agoric/evaluate'; import { makeCompoundPixelAssayMaker, @@ -8,11 +9,12 @@ 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 @@ -20,7 +22,6 @@ function mockStateChangeHandler(_newState) { export function makeGallery( E, - contractHost, log, stateChangeHandler = mockStateChangeHandler, canvasSize = 10, @@ -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 @@ -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); } @@ -273,7 +276,8 @@ 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( @@ -281,13 +285,20 @@ export function makeGallery( ).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, @@ -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 = { diff --git a/test/more/pixels/test-gallery.js b/test/more/pixels/test-gallery.js index 75eef7df694..3fda3f8d93f 100644 --- a/test/more/pixels/test-gallery.js +++ b/test/more/pixels/test-gallery.js @@ -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 => { @@ -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(); });