tornado-contracts/contracts/Governance/libraries/SafeResolver.sol

60 lines
2.2 KiB
Solidity
Raw Permalink Normal View History

2025-01-04 18:39:49 +00:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
2025-01-05 09:29:24 +00:00
import { IAddressResolver, IAddrResolver, IContentHashResolver, INameResolver, ITextResolver } from '@ensdomains/ens-contracts/contracts/resolvers/Resolver.sol';
2025-01-04 18:39:49 +00:00
/**
* @dev Goal is to not revert on custom ENS resolvers
*/
library SafeResolver {
function safeaddr(IAddressResolver resolver, bytes32 node, uint256 coinType) internal view returns (address) {
2025-01-05 09:29:24 +00:00
(bool success, bytes memory data) = address(resolver).staticcall(
abi.encodeWithSelector(resolver.addr.selector, node, coinType)
);
2025-01-04 18:39:49 +00:00
if (!success || data.length == 0) {
return address(0);
}
return abi.decode(data, (address));
}
function safeaddr(IAddrResolver resolver, bytes32 node) internal view returns (address) {
2025-01-05 09:29:24 +00:00
(bool success, bytes memory data) = address(resolver).staticcall(
abi.encodeWithSelector(resolver.addr.selector, node)
);
2025-01-04 18:39:49 +00:00
if (!success || data.length == 0) {
return address(0);
}
return abi.decode(data, (address));
}
function safecontenthash(IContentHashResolver resolver, bytes32 node) internal view returns (bytes memory) {
2025-01-05 09:29:24 +00:00
(bool success, bytes memory data) = address(resolver).staticcall(
abi.encodeWithSelector(resolver.contenthash.selector, node)
);
2025-01-04 18:39:49 +00:00
if (!success || data.length == 0) {
return new bytes(0);
}
return abi.decode(data, (bytes));
}
function safename(INameResolver resolver, bytes32 node) internal view returns (string memory) {
2025-01-05 09:29:24 +00:00
(bool success, bytes memory data) = address(resolver).staticcall(
abi.encodeWithSelector(resolver.name.selector, node)
);
2025-01-04 18:39:49 +00:00
if (!success || data.length == 0) {
return '';
}
return abi.decode(data, (string));
}
function safetext(ITextResolver resolver, bytes32 node, string memory key) internal view returns (string memory) {
2025-01-05 09:29:24 +00:00
(bool success, bytes memory data) = address(resolver).staticcall(
abi.encodeWithSelector(resolver.text.selector, node, key)
);
2025-01-04 18:39:49 +00:00
if (!success || data.length == 0) {
return '';
}
return abi.decode(data, (string));
2025-01-05 09:29:24 +00:00
}
}