-
Notifications
You must be signed in to change notification settings - Fork 3
/
SafeMetadata.sol
31 lines (27 loc) · 1.17 KB
/
SafeMetadata.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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.4;
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IERC20Metadata} from '@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol';
library SafeMetadata {
function safeName(IERC20 token) internal view returns (string memory) {
(bool success, bytes memory data) = address(token).staticcall(
abi.encodeWithSelector(IERC20Metadata.name.selector)
);
if (success) return abi.decode(data, (string));
return 'Token';
}
function safeSymbol(IERC20 token) internal view returns (string memory) {
(bool success, bytes memory data) = address(token).staticcall(
abi.encodeWithSelector(IERC20Metadata.symbol.selector)
);
if (success) return abi.decode(data, (string));
return 'TKN';
}
function safeDecimals(IERC20 token) internal view returns (uint8) {
(bool success, bytes memory data) = address(token).staticcall(
abi.encodeWithSelector(IERC20Metadata.decimals.selector)
);
if (success && data.length >= 32) return abi.decode(data, (uint8));
return 18;
}
}