Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when trades for zero are requested don't throw #4522

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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