From 4fe3c83a70000cb9f933bf6e158cc2bc1862bae9 Mon Sep 17 00:00:00 2001 From: Dean Tribble Date: Mon, 20 Jul 2020 12:04:30 -0700 Subject: [PATCH] feat: add getIssuerForBrand to zoe (#1276) * zoe: add getIssuerForBrand * zoeHelpers: print more info when offer safety is violated * zoe: better assert when offerHandles is not an Array * feat: add getIssuerFromBrand to zoe * feat: add getIssuerFromBrand to zoe * feat: `addPool` now returns useful pool controls `addPool` returns the secondary token issuers, new liquidity issuers and such. * chore: es-lint fixes. * chore: add test Co-authored-by: Brian Warner Co-authored-by: Kate Sills --- .../zoe/src/contractSupport/zoeHelpers.js | 26 +++++++++++++++ .../zoe/src/contracts/multipoolAutoswap.js | 9 +++++- packages/zoe/src/zoe.js | 6 ++++ .../contracts/test-multipoolAutoswap.js | 8 ++--- .../zoe/test/unitTests/contracts/test-zcf.js | 32 +++++++++++++++++++ .../unitTests/contracts/zcfTesterContract.js | 26 +++++++++++++++ 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 packages/zoe/test/unitTests/contracts/test-zcf.js create mode 100644 packages/zoe/test/unitTests/contracts/zcfTesterContract.js diff --git a/packages/zoe/src/contractSupport/zoeHelpers.js b/packages/zoe/src/contractSupport/zoeHelpers.js index 4c9d8be22fd..4f0a31bcdc4 100644 --- a/packages/zoe/src/contractSupport/zoeHelpers.js +++ b/packages/zoe/src/contractSupport/zoeHelpers.js @@ -262,6 +262,32 @@ export const makeZoeHelpers = (zcf) => { rightAllocation, ); if (!(offerSafeForLeft && offerSafeForRight)) { + console.log( + `currentLeftAllocation`, + zcf.getCurrentAllocation(keepLeft.offerHandle), + ); + console.log( + `currentRightAllocation`, + zcf.getCurrentAllocation(tryRight.offerHandle), + ); + console.log(`proposed left reallocation`, leftAllocation); + console.log(`proposed right reallocation`, rightAllocation); + // show the contraints + console.log( + `left want`, + zcf.getOffer(keepLeft.offerHandle).proposal.want, + ); + console.log( + `right want`, + zcf.getOffer(tryRight.offerHandle).proposal.want, + ); + + if (!offerSafeForLeft) { + console.log(`offer not safe for left`); + } + if (!offerSafeForRight) { + console.log(`offer not safe for right`); + } return rejectOffer(tryRight.offerHandle); } zcf.reallocate( diff --git a/packages/zoe/src/contracts/multipoolAutoswap.js b/packages/zoe/src/contracts/multipoolAutoswap.js index 66ac85a3e07..f836cc968aa 100644 --- a/packages/zoe/src/contracts/multipoolAutoswap.js +++ b/packages/zoe/src/contracts/multipoolAutoswap.js @@ -141,7 +141,14 @@ const makeContract = zcf => { }), newTokenIssuerRecord.brand, ); - return `liquidity pool for ${newTokenKeyword} added`; + return harden({ + tokenIssuer: newTokenIssuer, + tokenBrand: newTokenIssuerRecord.brand, + liquidityIssuer, + liquidityBrand, + tokenKeyword: newTokenKeyword, + liquidityKeyword: newLiquidityKeyword, + }); }); }; diff --git a/packages/zoe/src/zoe.js b/packages/zoe/src/zoe.js index 93409b5177d..dd011818b43 100644 --- a/packages/zoe/src/zoe.js +++ b/packages/zoe/src/zoe.js @@ -237,6 +237,7 @@ import { makeTables } from './state'; * @property {(offerHandles: OfferHandle[], brandKeywordRecords?: BrandKeywordRecord[]) => Allocation[]} getCurrentAllocations * @property {() => InstanceRecord} getInstanceRecord * @property {(issuer: Issuer) => Brand} getBrandForIssuer + * @property {(brand: Brand) => Issuer} getIssuerForBrand * @property {(brand: Brand) => AmountMath} getAmountMath * * @callback Reallocate @@ -506,6 +507,10 @@ function makeZoe(additionalEndowments = {}, vatPowers = {}) { }, complete: offerHandles => { + assert( + Array.isArray(offerHandles), + details`zcf.complete(offerHandles) must be Array, not ${offerHandles}`, + ); assertOffersHaveInstanceHandle(offerHandles, instanceHandle); return completeOffers(instanceHandle, offerHandles); }, @@ -613,6 +618,7 @@ function makeZoe(additionalEndowments = {}, vatPowers = {}) { }, getInstanceRecord: () => instanceTable.get(instanceHandle), getBrandForIssuer: issuer => issuerTable.brandFromIssuer(issuer), + getIssuerForBrand: brand => issuerTable.get(brand).issuer, getAmountMath: getAmountMathForBrand, }); return contractFacet; diff --git a/packages/zoe/test/unitTests/contracts/test-multipoolAutoswap.js b/packages/zoe/test/unitTests/contracts/test-multipoolAutoswap.js index 9f40609b9c3..65181408a3f 100644 --- a/packages/zoe/test/unitTests/contracts/test-multipoolAutoswap.js +++ b/packages/zoe/test/unitTests/contracts/test-multipoolAutoswap.js @@ -81,8 +81,8 @@ test('multipoolAutoSwap with valid offers', async t => { 'Moola', ); t.equals( - addMoolaPoolResult, - `liquidity pool for Moola added`, + addMoolaPoolResult.tokenIssuer, + moolaR.issuer, `adding pool for moola`, ); const moolaLiquidityIssuer = await E(publicAPI).getLiquidityIssuer( @@ -98,8 +98,8 @@ test('multipoolAutoSwap with valid offers', async t => { 'Simoleans', ); t.equals( - addSimoleansPoolResult, - `liquidity pool for Simoleans added`, + addSimoleansPoolResult.tokenIssuer, + simoleanR.issuer, `adding pool for simoleans`, ); const simoleanLiquidityIssuer = await E(publicAPI).getLiquidityIssuer( diff --git a/packages/zoe/test/unitTests/contracts/test-zcf.js b/packages/zoe/test/unitTests/contracts/test-zcf.js new file mode 100644 index 00000000000..9baad9d8d51 --- /dev/null +++ b/packages/zoe/test/unitTests/contracts/test-zcf.js @@ -0,0 +1,32 @@ +/* global harden */ + +import '@agoric/install-ses'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { test } from 'tape-promise/tape'; +// eslint-disable-next-line import/no-extraneous-dependencies +import bundleSource from '@agoric/bundle-source'; + +import { makeZoe } from '../../../src/zoe'; +import { setup } from '../setupBasicMints'; + +const contractRoot = `${__dirname}/zcfTesterContract`; + +test('zoe - test zcf', async t => { + t.plan(1); + const { moolaIssuer, simoleanIssuer } = setup(); + const zoe = makeZoe(); + + // pack the contract + const bundle = await bundleSource(contractRoot); + // install the contract + const installationHandle = await zoe.install(bundle); + + // Alice creates an instance + const issuerKeywordRecord = harden({ + Pixels: moolaIssuer, + Money: simoleanIssuer, + }); + t.doesNotReject(() => + zoe.makeInstance(installationHandle, issuerKeywordRecord), + ); +}); diff --git a/packages/zoe/test/unitTests/contracts/zcfTesterContract.js b/packages/zoe/test/unitTests/contracts/zcfTesterContract.js new file mode 100644 index 00000000000..a81377f69e9 --- /dev/null +++ b/packages/zoe/test/unitTests/contracts/zcfTesterContract.js @@ -0,0 +1,26 @@ +/* global harden */ +// @ts-check + +import { assert } from '@agoric/assert'; + +/** + * Tests ZCF + * + * @typedef {import('../../../src/zoe').ContractFacet} ContractFacet + * @typedef {import('@agoric/ERTP').Amount} Amount + * @param {ContractFacet} zcf + */ +const makeContract = zcf => { + const { brandKeywordRecord, issuerKeywordRecord } = zcf.getInstanceRecord(); + Object.keys(brandKeywordRecord).forEach(keyword => { + // TODO: import tap/tape and do t.equals + assert.equal( + zcf.getIssuerForBrand(brandKeywordRecord[keyword]), + issuerKeywordRecord[keyword], + ); + }); + return zcf.makeInvitation(() => {}, 'test'); +}; + +harden(makeContract); +export { makeContract };