-
Notifications
You must be signed in to change notification settings - Fork 14
/
InteractionHelper.sol
116 lines (110 loc) · 5.14 KB
/
InteractionHelper.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
// Interfaces
import {CollateralTracker} from "@contracts/CollateralTracker.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IERC20Partial} from "@tokens/interfaces/IERC20Partial.sol";
import {SemiFungiblePositionManager} from "@contracts/SemiFungiblePositionManager.sol";
// OpenZeppelin libraries
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
/// @title InteractionHelper - contains helper functions for external interactions such as approvals.
/// @notice Used to delegate logic with multiple external calls.
/// @dev Generally employed when there is a need to save or reuse bytecode size
// on a core contract (calls take a significant amount of logic).
/// @author Axicon Labs Limited
library InteractionHelper {
/// @notice Function that performs approvals on behalf of the PanopticPool for CollateralTracker and SemiFungiblePositionManager.
/// @param sfpm The SemiFungiblePositionManager being approved for both token0 and token1
/// @param ct0 The CollateralTracker (token0) being approved for token0
/// @param ct1 The CollateralTracker (token1) being approved for token1
/// @param token0 The token0 (in Uniswap) being approved for
/// @param token1 The token1 (in Uniswap) being approved for
function doApprovals(
SemiFungiblePositionManager sfpm,
CollateralTracker ct0,
CollateralTracker ct1,
address token0,
address token1
) external {
// Approve transfers of Panoptic Pool funds by SFPM
IERC20Partial(token0).approve(address(sfpm), type(uint256).max);
IERC20Partial(token1).approve(address(sfpm), type(uint256).max);
// Approve transfers of Panoptic Pool funds by Collateral token
IERC20Partial(token0).approve(address(ct0), type(uint256).max);
IERC20Partial(token1).approve(address(ct1), type(uint256).max);
}
/// @notice Computes the name of a CollateralTracker based on the token composition and fee of the underlying Uniswap Pool.
/// @dev Some tokens do not have proper symbols so error handling is required - this logic takes up significant bytecode size, which is why it is in a library.
/// @param token0 The token0 in the Uniswap Pool
/// @param token1 The token1 in the Uniswap Pool
/// @param isToken0 Whether the collateral token computing the name is for token0 or token1
/// @param fee The fee of the Uniswap pool in basis points
/// @param prefix A constant string appended to the start of the token name
/// @return The complete name of the collateral token calling this function
function computeName(
address token0,
address token1,
bool isToken0,
uint24 fee,
string memory prefix
) external view returns (string memory) {
// get the underlying token symbols
// it's not guaranteed that they support the metadata extension
// so we need to let them fail and return placeholder if not
string memory symbol0;
string memory symbol1;
try IERC20Metadata(token0).symbol() returns (string memory _symbol) {
symbol0 = _symbol;
} catch {
symbol0 = "???";
}
try IERC20Metadata(token1).symbol() returns (string memory _symbol) {
symbol1 = _symbol;
} catch {
symbol1 = "???";
}
unchecked {
return
string.concat(
prefix,
" ",
isToken0 ? symbol0 : symbol1,
" LP on ",
symbol0,
"/",
symbol1,
" ",
Strings.toString(fee),
"bps"
);
}
}
/// @notice Returns symbol as prefixed symbol of underlying token.
/// @param token The address of the underlying token used to compute the symbol
/// @param prefix A constant string appended to the symbol of the underlying token to create the final symbol
/// @return The symbol of the token
function computeSymbol(
address token,
string memory prefix
) external view returns (string memory) {
// not guaranteed that token supports metadada extension
// so we need to let call fail and return placeholder if not
try IERC20Metadata(token).symbol() returns (string memory tokenSymbol) {
return string.concat(prefix, tokenSymbol);
} catch {
return string.concat(prefix, "???");
}
}
/// @notice Returns decimals of underlying token (0 if not present).
/// @param token The address of the underlying token used to compute the decimals
/// @return The decimals of the token
function computeDecimals(address token) external view returns (uint8) {
// not guaranteed that token supports metadada extension
// so we need to let call fail and return placeholder if not
try IERC20Metadata(token).decimals() returns (uint8 _decimals) {
return _decimals;
} catch {
return 0;
}
}
}