Skip to content

Commit

Permalink
fix: price requests for zero should get zero as an answer (#2762)
Browse files Browse the repository at this point in the history
fixes #2760
  • Loading branch information
Chris-Hibbert authored Mar 30, 2021
1 parent 6e779bf commit 11285e7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/zoe/src/contracts/autoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ const start = async zcf => {
const inputReserve = getPoolAmount(amountIn.brand).value;
const outputReserve = getPoolAmount(brandOut).value;
assert(isNatValue(amountIn.value));
if (amountMath.isEmpty(amountIn)) {
return amountMath.makeEmpty(brandOut);
}
const outputValue = getInputPrice(
amountIn.value,
inputReserve,
Expand All @@ -362,6 +365,9 @@ const start = async zcf => {
const inputReserve = getPoolAmount(brandIn).value;
const outputReserve = getPoolAmount(amountOut.brand).value;
assert(isNatValue(amountOut.value));
if (amountMath.isEmpty(amountOut)) {
return amountMath.makeEmpty(brandIn);
}
const outputValue = getOutputPrice(
amountOut.value,
inputReserve,
Expand Down
12 changes: 12 additions & 0 deletions packages/zoe/src/contracts/multipoolAutoswap/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ export const makeAddPool = (zcf, isSecondary, initPool, centralBrand) => {
outputBrand,
);
assert(isNatValue(inputAmount.value));
if (amountMath.isEmpty(inputAmount)) {
return {
amountOut: amountMath.makeEmpty(outputBrand),
amountIn: amountMath.makeEmpty(inputAmount.brand),
};
}
const valueOut = getInputPrice(
inputAmount.value,
inputReserve,
Expand All @@ -126,6 +132,12 @@ export const makeAddPool = (zcf, isSecondary, initPool, centralBrand) => {
outputAmount.brand,
);
assert(isNatValue(outputAmount.value));
if (amountMath.isEmpty(outputAmount)) {
return {
amountOut: amountMath.makeEmpty(outputAmount.brand),
amountIn: amountMath.makeEmpty(inputBrand),
};
}
const valueIn = getOutputPrice(
outputAmount.value,
inputReserve,
Expand Down
59 changes: 59 additions & 0 deletions packages/zoe/test/unitTests/contracts/test-autoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,62 @@ test('autoSwap jig - swap varying amounts', async t => {

// attempt a trade with bad numbers
});

test('autoSwap price quote for zero', async t => {
const {
moolaIssuer,
simoleanIssuer,
moolaMint,
simoleanMint,
moola,
simoleans,
zoe,
} = setup();
const installation = await installationPFromSource(zoe, autoswap);

// Setup Alice
const aliceMoolaPayment = moolaMint.mintPayment(moola(10));
// Let's assume that simoleans are worth 2x as much as moola
const aliceSimoleanPayment = simoleanMint.mintPayment(simoleans(5));

// Alice creates an autoswap instance
const issuerKeywordRecord = harden({
Central: moolaIssuer,
Secondary: simoleanIssuer,
});
const startRecord = await zoe.startInstance(
installation,
issuerKeywordRecord,
);
/** @type {AutoswapPublicFacet} */
const publicFacet = startRecord.publicFacet;
const liquidityIssuerP = await E(publicFacet).getLiquidityIssuer();
const liquidityBrand = await E(liquidityIssuerP).getBrand();
const liquidity = value => amountMath.make(value, liquidityBrand);

// Alice adds liquidity
// 10 moola = 5 simoleans at the time of the liquidity adding
// aka 2 moola = 1 simolean
const aliceProposal = harden({
want: { Liquidity: liquidity(10) },
give: { Central: moola(10), Secondary: simoleans(5) },
});
const alicePayments = {
Central: aliceMoolaPayment,
Secondary: aliceSimoleanPayment,
};
const aliceInvitation = await publicFacet.makeAddLiquidityInvitation();
await zoe.offer(aliceInvitation, aliceProposal, alicePayments);

const simoleanAmounts = await E(publicFacet).getInputPrice(
moola(0),
simoleans(0).brand,
);
t.deepEqual(simoleanAmounts, simoleans(0), `currentPrice`);

const moolaAmounts = await E(publicFacet).getOutputPrice(
simoleans(0),
moola(0n).brand,
);
t.deepEqual(moolaAmounts, moola(0), `price 0`);
});
30 changes: 30 additions & 0 deletions packages/zoe/test/unitTests/contracts/test-autoswapPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,33 @@ test('pool getOutputPrice cenToCen', async t => {
},
);
});

test('pool getPrice zero', async t => {
const allocations = { central: 100n, secondary: 100n };
const { pool, centralBrand, central, secondary } = await setupPool(
allocations,
);
const valueIn = 0n;
const { amountOut, amountIn } = await E(pool).getPriceGivenAvailableInput(
secondary(valueIn),
centralBrand,
);
const expected = 0n;
t.deepEqual(amountOut, central(expected));
t.deepEqual(amountIn, secondary(valueIn));
});

test('pool getOutputPrice zero', async t => {
const poolBalances = { central: 100n, secondary: 100n };
const { pool, secondaryBrand, central, secondary } = await setupPool(
poolBalances,
);
const valueOut = 0n;
const { amountOut, amountIn } = await E(pool).getPriceGivenRequiredOutput(
secondaryBrand,
central(valueOut),
);
const expected = 0n;
t.deepEqual(amountOut, central(valueOut));
t.deepEqual(amountIn, secondary(expected));
});

0 comments on commit 11285e7

Please sign in to comment.