Skip to content

Commit

Permalink
feat: verify uniswap path for single hops WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kimpers authored and asoong committed May 27, 2021
1 parent c25665b commit 7905d73
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
34 changes: 25 additions & 9 deletions contracts/protocol/integration/exchange/ZeroExApiAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,12 @@ contract ZeroExApiAdapter {
inputTokenAmount = fillData.sellAmount;
} else if (selector == 0x6af479b2) {
// sellTokenForTokenToUniswapV3()
{
bytes memory encodedPath;
(encodedPath, inputTokenAmount, minOutputTokenAmount, recipient) =
abi.decode(_data[4:], (bytes, uint256, uint256, address));
require(encodedPath.length >= UNISWAP_V3_SINGLE_HOP_PATH_SIZE, "Uniswap token path too short");
}
// TODO(kimpers): Need to decode the path here
bytes memory encodedPath;
(encodedPath, inputTokenAmount, minOutputTokenAmount, recipient) =
abi.decode(_data[4:], (bytes, uint256, uint256, address));
supportsRecipient = true;
inputToken = _sourceToken;
outputToken = _destinationToken;

(inputToken, outputToken) = _decodePoolInfoFromPath(encodedPath);
}
else {
revert("Unsupported 0xAPI function selector");
Expand All @@ -188,4 +184,24 @@ contract ZeroExApiAdapter {
_data
);
}

// Return the first input token, output token, and fee of an encoded uniswap path.
function _decodePoolInfoFromPath(bytes memory encodedPath)
private
pure
returns (
address inputToken,
address outputToken
)
{
require(encodedPath.length >= UNISWAP_V3_SINGLE_HOP_PATH_SIZE, "Uniswap token path too shor too shortt");
assembly {
let p := add(encodedPath, 32)
inputToken := shr(96, mload(p))
p := add(p, 20)
// account for fee
p := add(p, 3)
outputToken := shr(96, mload(p))
}
}
}
19 changes: 19 additions & 0 deletions test/protocol/integration/exchange/zeroExApiAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,25 @@ describe("ZeroExApiAdapter", () => {
expect(value).to.deep.eq(ZERO);
expect(_data).to.deep.eq(data);
});
it("multiple hops: validates data", async () => {
const data = zeroExMock.interface.encodeFunctionData("sellTokenForTokenToUniswapV3", [
encodePath([sourceToken, otherToken, destToken]),
sourceQuantity,
minDestinationQuantity,
destination,
]);
const [target, value, _data] = await zeroExApiAdapter.getTradeCalldata(
sourceToken,
destToken,
destination,
sourceQuantity,
minDestinationQuantity,
data,
);
expect(target).to.eq(zeroExMock.address);
expect(value).to.deep.eq(ZERO);
expect(_data).to.deep.eq(data);
});
});
});
});

0 comments on commit 7905d73

Please sign in to comment.