Skip to content

Commit

Permalink
Merge pull request #4522 from Agoric/4114-swapForEmpty
Browse files Browse the repository at this point in the history
fix: when trades for zero are requested don't throw
  • Loading branch information
mergify[bot] authored Feb 10, 2022
2 parents bb4ffc6 + 59b6da8 commit 428349e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 428349e

Please sign in to comment.