-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathERC20Extensions.sol
33 lines (29 loc) · 1.1 KB
/
ERC20Extensions.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
// Interface that includes the decimals method
interface ExtendedIERC20 is IERC20 {
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
}
// Library to extend the functionality of IERC20
library ERC20Extensions {
function safeDecimals(IERC20 token) internal view returns (uint8) {
// Try casting the token to the extended interface with decimals()
try ExtendedIERC20(address(token)).decimals() returns (uint8 tokenDecimals) {
return tokenDecimals;
} catch {
// Return a default value if decimals() is not implemented
return 18;
}
}
function safeSymbol(IERC20 token) internal view returns (string memory) {
// Try casting the token to the extended interface with symbol()
try ExtendedIERC20(address(token)).symbol() returns (string memory tokenSymbol) {
return tokenSymbol;
} catch {
// Return a default value if symbol() is not implemented
return "";
}
}
}