Skip to content

Commit

Permalink
Merge pull request #21 from Gearbox-protocol/token_compressor
Browse files Browse the repository at this point in the history
feat: token compressor
  • Loading branch information
0xmikko authored Sep 11, 2024
2 parents 561a27f + 7ea05af commit f8d2fa6
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion contracts/compressors/TokenCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,36 @@
// (c) Gearbox Foundation, 2024.
pragma solidity ^0.8.17;

contract TokenCompressor {}
import {TokenInfo} from "../types/MarketData.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {LibString} from "@solady/utils/LibString.sol";

contract TokenCompressor {
function getTokenInfo(address token) public view returns (TokenInfo memory result) {
result.token = token;
result.decimals = IERC20Metadata(token).decimals();

// Fallback to low-level call to handle bytes32 symbol
(bool success, bytes memory data) = token.staticcall(abi.encodeWithSignature("symbol()"));
if (success) {
if (data.length == 32) {
result.symbol = LibString.fromSmallString(bytes32(data));
} else {
result.symbol = abi.decode(data, (string));
}
} else {
revert("symbol retrieval failed");
}

(success, data) = token.staticcall(abi.encodeWithSignature("name()"));
if (success) {
if (data.length == 32) {
result.name = LibString.fromSmallString(bytes32(data));
} else {
result.name = abi.decode(data, (string));
}
} else {
revert("name retrieval failed");
}
}
}

0 comments on commit f8d2fa6

Please sign in to comment.