|
| 1 | +// SPDX-License-Identifier: agpl-3.0 |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +interface IUniversalResolver { |
| 5 | + function reverseWithGateways( |
| 6 | + bytes calldata reverseName, |
| 7 | + uint256 coinType, |
| 8 | + string[] calldata gateways |
| 9 | + ) external view returns (string memory resolvedName, address resolver, address reverseResolver); |
| 10 | +} |
| 11 | + |
| 12 | +contract EnsGetter { |
| 13 | + struct ReverseLookupResult { |
| 14 | + string resolvedName; |
| 15 | + bool hasName; |
| 16 | + // True when the reverse lookup reverted with an EIP-3668 OffchainLookup, meaning the name |
| 17 | + // lives behind a CCIP-read gateway |
| 18 | + // The caller is expected to retry these addresses off-chain |
| 19 | + bool needsOffchainLookup; |
| 20 | + } |
| 21 | + |
| 22 | + // EIP-3668 |
| 23 | + bytes4 constant OFFCHAIN_LOOKUP_SELECTOR = 0x556f1830; |
| 24 | + |
| 25 | + function getNames( |
| 26 | + address universalResolver, |
| 27 | + address[] calldata addresses, |
| 28 | + uint256 coinType, |
| 29 | + string[] calldata gateways |
| 30 | + ) external view returns (ReverseLookupResult[] memory results) { |
| 31 | + uint256 len = addresses.length; |
| 32 | + results = new ReverseLookupResult[](len); |
| 33 | + |
| 34 | + for (uint256 i = 0; i < len; i++) { |
| 35 | + if (addresses[i] == address(0)) continue; |
| 36 | + |
| 37 | + try this.getReverseName(universalResolver, addresses[i], coinType, gateways) returns ( |
| 38 | + string memory resolvedName |
| 39 | + ) { |
| 40 | + if (bytes(resolvedName).length == 0) continue; |
| 41 | + |
| 42 | + results[i].resolvedName = resolvedName; |
| 43 | + results[i].hasName = true; |
| 44 | + } catch (bytes memory err) { |
| 45 | + // A missing/invalid reverse record reverts and should not fail the whole batch. |
| 46 | + // An OffchainLookup revert is surfaced so the caller can resolve it off-chain. |
| 47 | + if (err.length >= 4) { |
| 48 | + bytes4 selector; |
| 49 | + assembly { |
| 50 | + selector := mload(add(err, 0x20)) |
| 51 | + } |
| 52 | + if (selector == OFFCHAIN_LOOKUP_SELECTOR) results[i].needsOffchainLookup = true; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function getReverseName( |
| 59 | + address universalResolver, |
| 60 | + address lookupAddress, |
| 61 | + uint256 coinType, |
| 62 | + string[] calldata gateways |
| 63 | + ) external view returns (string memory resolvedName) { |
| 64 | + (resolvedName, , ) = IUniversalResolver(universalResolver).reverseWithGateways( |
| 65 | + abi.encodePacked(lookupAddress), |
| 66 | + coinType, |
| 67 | + gateways |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments