From 4516e5b6a2ab9176033956ee197687b5c6574647 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Wed, 9 Feb 2022 17:52:46 -0800 Subject: [PATCH] fix: when trades for zero are requested don't throw I thought no one needed to request zero prices, and so I threw in that case. https://github.com/agoric/agoric-sdk/issues/4114 reveals a situation where it will happen in ordinary circumstances. Change the AMM code to respond with an empty amount when an empty amount is offered, and vice versa. --- .../src/vpool-xyk-amm/constantProduct/swap.js | 8 ++++---- .../constantProduct/test-checkInvariants.js | 20 +++++++++++++------ .../test-compareBondingCurves.js | 17 +++++++++++++--- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/packages/run-protocol/src/vpool-xyk-amm/constantProduct/swap.js b/packages/run-protocol/src/vpool-xyk-amm/constantProduct/swap.js index 64111e0b7d1..8aa22402b42 100644 --- a/packages/run-protocol/src/vpool-xyk-amm/constantProduct/swap.js +++ b/packages/run-protocol/src/vpool-xyk-amm/constantProduct/swap.js @@ -189,10 +189,10 @@ export const swap = ( ); assertGreaterThanZero(poolAllocation.Central, 'poolAllocation.Central'); assertGreaterThanZero(poolAllocation.Secondary, 'poolAllocation.Secondary'); - assert( - isGreaterThanZero(amountGiven) || isGreaterThanZero(amountWanted), - X`amountGiven or amountWanted must be greater than 0: ${amountWanted} ${amountGiven}`, - ); + + if (!isGreaterThanZero(amountGiven) && !isGreaterThanZero(amountWanted)) { + return noTransaction; + } if (!isWantedAvailable(poolAllocation, amountWanted)) { return noTransaction; diff --git a/packages/run-protocol/test/amm/constantProduct/test-checkInvariants.js b/packages/run-protocol/test/amm/constantProduct/test-checkInvariants.js index 73880770145..a34885a58c1 100644 --- a/packages/run-protocol/test/amm/constantProduct/test-checkInvariants.js +++ b/packages/run-protocol/test/amm/constantProduct/test-checkInvariants.js @@ -308,13 +308,21 @@ test('getInputPrice zero input', t => { inputReserve: 320n, outputReserve: 50n, inputValue: 0n, + outputValue: 0n, + }; + testGetInputPriceNoTrade(t, input, true); + testGetInputPriceNoTrade(t, input, false); +}); + +test('getOutputPrice zero output', t => { + const input = { + inputReserve: 320n, + outputReserve: 50n, + inputValue: 0n, + outputValue: 0n, }; - const messageA = - 'amountGiven or amountWanted must be greater than 0: {"brand":"[Alleged: BLD brand]","value":"[0n]"} {"brand":"[Alleged: RUN brand]","value":"[0n]"}'; - testGetInputPriceThrows(t, input, messageA, true); - const messageB = - 'amountGiven or amountWanted must be greater than 0: {"brand":"[Alleged: RUN brand]","value":"[0n]"} {"brand":"[Alleged: BLD brand]","value":"[0n]"}'; - testGetInputPriceThrows(t, input, messageB, false); + testGetOutputPriceNoTrade(t, input, true); + testGetOutputPriceNoTrade(t, input, false); }); test('getInputPrice big product', t => { diff --git a/packages/run-protocol/test/amm/constantProduct/test-compareBondingCurves.js b/packages/run-protocol/test/amm/constantProduct/test-compareBondingCurves.js index 98f176eaf8c..711dd3e9b92 100644 --- a/packages/run-protocol/test/amm/constantProduct/test-compareBondingCurves.js +++ b/packages/run-protocol/test/amm/constantProduct/test-compareBondingCurves.js @@ -199,9 +199,20 @@ test('getInputPrice zero input', t => { outputReserve: 50n, inputValue: 0n, }; - const message = - 'amountGiven or amountWanted must be greater than 0: {"brand":"[Alleged: BLD brand]","value":"[0n]"} {"brand":"[Alleged: RUN brand]","value":"[0n]"}'; - getInputPriceThrows(t, input, message); + + const expectedOutput = 0n; + testInputGetPrice(t, input, expectedOutput); +}); + +test('getOutputPrice zero output', t => { + const input = { + inputReserve: 320n, + outputReserve: 50n, + outputValue: 0n, + }; + + const expectedInput = 0n; + testGetOutputPrice(t, input, expectedInput); }); test('getInputPrice big product', t => {