Skip to content

Commit

Permalink
Merge pull request #3034 from Agoric/workAroundCollectFees
Browse files Browse the repository at this point in the history
test and workaround for a reallocate issue in zoe
  • Loading branch information
michaelfig authored May 4, 2021
2 parents 8ddc52a + 74b0d3d commit 126c71e
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 126c71e

Please sign in to comment.