Skip to content

Commit

Permalink
fix: test and workaround for a reallocate issue in zoe
Browse files Browse the repository at this point in the history
issue 3033 shows that reallocate fails unnecessarily if asked to
reallocate to seats that had no previous allocation, even if the new
allocations are empty.

This works around that for the collectFees contract until a fix can be prepared.
  • Loading branch information
Chris-Hibbert authored and michaelfig committed May 4, 2021
1 parent 97ce4dd commit f56ff7a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/zoe/src/contracts/newSwap/collectFees.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import { amountMath } from '@agoric/ertp';
export const makeMakeCollectFeesInvitation = (zcf, feeSeat, centralBrand) => {
const collectFees = seat => {
const allocation = feeSeat.getAmountAllocated('RUN', centralBrand);
zcf.reallocate(
seat.stage({ RUN: allocation }),
feeSeat.stage({ RUN: amountMath.makeEmpty(centralBrand) }),
);

// This check works around
// https://github.com/Agoric/agoric-sdk/issues/3033
// when that bug is fixed, the reallocate can be moved outside the check and
// the check dropped.
if (!amountMath.isEmpty(allocation)) {
zcf.reallocate(
seat.stage({ RUN: allocation }),
feeSeat.stage({ RUN: amountMath.makeEmpty(centralBrand) }),
);
}
seat.exit();
return `paid out ${allocation.value}`;
};
Expand Down
42 changes: 42 additions & 0 deletions packages/zoe/test/unitTests/contracts/newSwap/test-newSwap-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ test('newSwap doubleSwap', async t => {
publicFacet,
).makeAddLiquidityInvitation();

const collectFeesInvitation2 = E(creatorFacet).makeCollectFeesInvitation();
const collectFeesSeat2 = await zoe.offer(
collectFeesInvitation2,
undefined,
undefined,
);

const moolaLiquidityIssuer = await E(publicFacet).addPool(
moolaR.issuer,
'Moola',
Expand Down Expand Up @@ -902,3 +909,38 @@ test('newSwap jig - swapOut uneven', async t => {
RUN: amountMath.makeEmpty(centralR.brand),
});
});

// This demonstrates that we've worked around
// https://github.com/Agoric/agoric-sdk/issues/3033. When that is fixed, the
// work-around in collectFees should be cleaned up.
test('newSwap workaround zoe Bug', async t => {
const zoe = makeZoe(fakeVatAdmin);

// Set up central token
const centralR = makeIssuerKit('central');

// Alice creates an autoswap instance
const bundle = await bundleSource(newSwapRoot);

const installation = await zoe.install(bundle);
// This timer is only used to build quotes. Let's make it non-zero
const fakeTimer = buildManualTimer(console.log, 30);
const { creatorFacet } = await zoe.startInstance(
installation,
harden({ Central: centralR.issuer }),
{ timer: fakeTimer, poolFee: 24n, protocolFee: 6n },
);

const collectFeesInvitation2 = E(creatorFacet).makeCollectFeesInvitation();
const collectFeesSeat2 = await zoe.offer(
collectFeesInvitation2,
undefined,
undefined,
);

const payout = await E(collectFeesSeat2).getPayout('RUN');
const result = await E(collectFeesSeat2).getOfferResult();

t.deepEqual(payout, undefined);
t.deepEqual(result, 'paid out 0');
});

0 comments on commit f56ff7a

Please sign in to comment.