-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUniV3Path.sol
44 lines (40 loc) · 1.47 KB
/
UniV3Path.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;
/// @title Path
/// @author DELV
/// @notice This is a library for interacting with Uniswap's multi-hop paths.
library UniV3Path {
/// @dev Returns the input token of a Uniswap swap.
/// @param _path The Uniswap path for a multi-hop fill.
/// @return tokenIn_ The input token of a Uniswap swap.
function tokenIn(
bytes memory _path
) internal pure returns (address tokenIn_) {
// Look up the `tokenIn` as the first part of the path.
assembly {
tokenIn_ := div(
mload(add(add(_path, 0x20), 0x0)),
0x1000000000000000000000000
)
}
return tokenIn_;
}
/// @dev Returns the output token of a Uniswap swap.
/// @param _path The Uniswap path for a multi-hop fill.
/// @return tokenOut_ The output token of a Uniswap swap.
function tokenOut(
bytes memory _path
) internal pure returns (address tokenOut_) {
// Look up the `tokenOut` as the last address in the path.
assembly {
tokenOut_ := div(
// NOTE: We add the path pointer to the path length plus 12
// because this will point us 20 bytes from the end of the path.
// This gives us the last address in the path.
mload(add(add(_path, mload(_path)), 12)),
0x1000000000000000000000000
)
}
return tokenOut_;
}
}