Skip to content

Commit

Permalink
fix: add proper tests, require statements for sellTokenForToken
Browse files Browse the repository at this point in the history
  • Loading branch information
kimpers authored and asoong committed May 27, 2021
1 parent 06ca9b6 commit 5c84995
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion contracts/protocol/integration/exchange/ZeroExApiAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ contract ZeroExApiAdapter {
abi.decode(_data[4:], (bytes, uint256, uint256, address));
supportsRecipient = true;

require(encodedPath.length > 20, "UniswapV3 token path too short");
uint256 numHops = (encodedPath.length - 20)/UNISWAP_V3_SINGLE_HOP_OFFSET_SIZE;
require(numHops > 0, "At least 1 hop");
require(numHops > 0, "UniswapV3 token path too short");

if (numHops == 1) {
(inputToken, outputToken) = _decodePoolInfoFromPathWithOffset(encodedPath, 0);
Expand Down
108 changes: 108 additions & 0 deletions test/protocol/integration/exchange/zeroExApiAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,114 @@ describe("ZeroExApiAdapter", () => {
expect(_data).to.deep.eq(data);
});
}

it("rejects wrong input token", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([otherToken, destToken]),
sourceQuantity,
minDestinationQuantity,
destination,
]);
const tx = zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
await expect(tx).to.be.revertedWith("Mismatched input token");
});

it("rejects wrong output token", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([sourceToken, otherToken]),
sourceQuantity,
minDestinationQuantity,
destination,
]);
const tx = zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
await expect(tx).to.be.revertedWith("Mismatched output token");
});

it("rejects wrong input token quantity", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([sourceToken, destToken]),
otherQuantity,
minDestinationQuantity,
destination,
]);
const tx = zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
await expect(tx).to.be.revertedWith("Mismatched input token quantity");
});

it("rejects wrong output token quantity", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([sourceToken, destToken]),
sourceQuantity,
otherQuantity,
destination,
]);
const tx = zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
await expect(tx).to.be.revertedWith("Mismatched output token quantity");
});

it("rejects invalid uniswap path", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([sourceToken]),
sourceQuantity,
minDestinationQuantity,
destination,
]);
const tx = zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
await expect(tx).to.be.revertedWith("UniswapV3 token path too short");
});

it("rejects wrong destination", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([sourceToken, destToken]),
sourceQuantity,
minDestinationQuantity,
ADDRESS_ZERO,
]);
const tx = zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
await expect(tx).to.be.revertedWith("Mismatched recipient");
});
});
});
});

0 comments on commit 5c84995

Please sign in to comment.