Created
February 1, 2023 17:12
-
-
Save backseats/97027c2d1a31804903fe291f83fe40ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //SPDX-License-Identifier: MIT | |
| pragma solidity 0.8.15; | |
| contract ERC1155TokenURIHelper { | |
| function tokenURI(address _contractAddress, uint _tokenId) public view returns (string memory) { | |
| if (tokenURIDetector(_contractAddress, _tokenId)) { | |
| return IERC1155(_contractAddress).tokenURI(_tokenId); | |
| } else { | |
| return IERC1155(_contractAddress).uri(_tokenId); | |
| } | |
| } | |
| function tokenURIDetector(address _contractAddress, uint _tokenId) public view returns (bool) { | |
| bytes4 selector = bytes4(keccak256("tokenURI(uint256)")); | |
| bool success; | |
| bytes memory data = abi.encodeWithSelector(selector, _tokenId); | |
| assembly { | |
| success := staticcall( | |
| gas(), // gas remaining | |
| _contractAddress, // destination address | |
| add(data, 32), // input buffer (starts after the first 32 bytes in the `data` array) | |
| mload(data), // input length (loaded from the first 32 bytes in the `data` array) | |
| 0, // output buffer | |
| 0 // output length | |
| ) | |
| } | |
| return success; | |
| } | |
| } | |
| interface IERC1155 { | |
| function tokenURI(uint256) external view returns (string memory); | |
| function uri(uint256) external view returns (string memory); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment