Skip to content

Commit f8d2fa6

Browse files
authored
Merge pull request #21 from Gearbox-protocol/token_compressor
feat: token compressor
2 parents 561a27f + 7ea05af commit f8d2fa6

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

contracts/compressors/TokenCompressor.sol

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,36 @@
33
// (c) Gearbox Foundation, 2024.
44
pragma solidity ^0.8.17;
55

6-
contract TokenCompressor {}
6+
import {TokenInfo} from "../types/MarketData.sol";
7+
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
8+
import {LibString} from "@solady/utils/LibString.sol";
9+
10+
contract TokenCompressor {
11+
function getTokenInfo(address token) public view returns (TokenInfo memory result) {
12+
result.token = token;
13+
result.decimals = IERC20Metadata(token).decimals();
14+
15+
// Fallback to low-level call to handle bytes32 symbol
16+
(bool success, bytes memory data) = token.staticcall(abi.encodeWithSignature("symbol()"));
17+
if (success) {
18+
if (data.length == 32) {
19+
result.symbol = LibString.fromSmallString(bytes32(data));
20+
} else {
21+
result.symbol = abi.decode(data, (string));
22+
}
23+
} else {
24+
revert("symbol retrieval failed");
25+
}
26+
27+
(success, data) = token.staticcall(abi.encodeWithSignature("name()"));
28+
if (success) {
29+
if (data.length == 32) {
30+
result.name = LibString.fromSmallString(bytes32(data));
31+
} else {
32+
result.name = abi.decode(data, (string));
33+
}
34+
} else {
35+
revert("name retrieval failed");
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)