Skip to content

Commit

Permalink
feat: add getIssuerForBrand to zoe (#1276)
Browse files Browse the repository at this point in the history
* 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 <warner@lothar.com>
Co-authored-by: Kate Sills <kate@agoric.com>
  • Loading branch information
3 people authored Jul 20, 2020
1 parent 2c506a2 commit 4fe3c83
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
26 changes: 26 additions & 0 deletions packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion packages/zoe/src/contracts/multipoolAutoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
};

Expand Down
6 changes: 6 additions & 0 deletions packages/zoe/src/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
32 changes: 32 additions & 0 deletions packages/zoe/test/unitTests/contracts/test-zcf.js
Original file line number Diff line number Diff line change
@@ -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),
);
});
26 changes: 26 additions & 0 deletions packages/zoe/test/unitTests/contracts/zcfTesterContract.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 4fe3c83

Please sign in to comment.