Skip to content

Commit

Permalink
fix: when trades for zero are requested don't throw
Browse files Browse the repository at this point in the history
I thought no one needed to request zero prices, and so I threw in that
case.  #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.
  • Loading branch information
Chris-Hibbert committed Feb 10, 2022
1 parent cd65fff commit 4516e5b
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 4516e5b

Please sign in to comment.