-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathInteractionHelper.sol
97 lines (90 loc) · 4.42 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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.24;
// 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";
// Libraries
import {PanopticMath} from "@libraries/PanopticMath.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.
/// @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 of the Uniswap Pool
/// @param token1 The token1 of 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 hundredths of 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) {
string memory symbol0 = PanopticMath.safeERC20Symbol(token0);
string memory symbol1 = PanopticMath.safeERC20Symbol(token1);
unchecked {
return
string.concat(
prefix,
" ",
isToken0 ? symbol0 : symbol1,
" LP on ",
symbol0,
"/",
symbol1,
" ",
PanopticMath.uniswapFeeToString(fee)
);
}
}
/// @notice Returns collateral token symbol as `prefix` + `underlying token symbol`.
/// @param token The address of the underlying token used to compute the symbol
/// @param prefix A constant string prepended to the symbol of the underlying token to create the final symbol
/// @return The symbol of the collateral token
function computeSymbol(
address token,
string memory prefix
) external view returns (string memory) {
return string.concat(prefix, PanopticMath.safeERC20Symbol(token));
}
/// @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 metadata 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;
}
}
}