Skip to content

Commit

Permalink
Use custom IUri instead of forcing IERC1155MetadataURI
Browse files Browse the repository at this point in the history
  • Loading branch information
matejos committed Apr 2, 2024
1 parent d869444 commit 297d49c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.13;

import {IERC1155MetadataURI} from "@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol";
import {IERC4906Agnostic} from "./IERC4906Agnostic.sol";
import {IUri} from "./IUri.sol";

/// @dev A standard ERC1155 that can be burned and has a special uri function accepting a custom base URI.
interface IInverseProjected1155 is IERC1155MetadataURI, IERC4906Agnostic {
Expand Down Expand Up @@ -32,9 +33,6 @@ interface IInverseProjected1155 is IERC1155MetadataURI, IERC4906Agnostic {
/// @dev Returns the token URI of specified `id` using a custom base URI.
function uri(uint256 id, string memory customBaseUri) external view returns (string memory);

/// @dev Returns the token URI of specified `id` using a call to contract implementing `IERC1155MetadataURI`.
function uri(
uint256 id,
IERC1155MetadataURI customUriInterface
) external view returns (string memory);
/// @dev Returns the token URI of specified `id` using a call to contract implementing `IUri`.
function uri(uint256 id, IUri customUriInterface) external view returns (string memory);
}
13 changes: 13 additions & 0 deletions packages/contracts/evm-contracts/contracts/token/IUri.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

/// @dev An interface exposing the `uri` function from IERC1155MetadataURI.
interface IUri {
/**
* @dev Returns the URI for token type `id`.
*
* If the `\{id\}` substring is present in the URI, it must be replaced by
* clients with the actual token type ID.
*/
function uri(uint256 id) external view returns (string memory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IInverseProjected1155} from "./IInverseProjected1155.sol";
import {IInverseAppProjected1155} from "./IInverseAppProjected1155.sol";
import {IUri} from "./IUri.sol";

struct MintEntry {
address minter;
Expand Down Expand Up @@ -149,11 +150,8 @@ contract InverseAppProjected1155 is IInverseAppProjected1155, ERC1155Supply, Own
return string(abi.encodePacked(URI, baseExtension));
}

/// @dev Returns the token URI of specified `id` using a call to contract implementing `IERC1155MetadataURI`.
function uri(
uint256 id,
IERC1155MetadataURI customUriInterface
) public view virtual returns (string memory) {
/// @dev Returns the token URI of specified `id` using a call to contract implementing `IUri`.
function uri(uint256 id, IUri customUriInterface) public view virtual returns (string memory) {
return customUriInterface.uri(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IInverseProjected1155} from "./IInverseProjected1155.sol";
import {IInverseBaseProjected1155} from "./IInverseBaseProjected1155.sol";
import {IUri} from "./IUri.sol";

/// @dev A Paima Inverse Projection ERC1155 token where initialization is handled by the base-layer.
/// A standard ERC1155 that accepts calldata in the mint function for any initialization data needed in a Paima dApp.
Expand Down Expand Up @@ -97,11 +98,8 @@ contract InverseBaseProjected1155 is IInverseBaseProjected1155, ERC1155Supply, O
return string(abi.encodePacked(URI, baseExtension));
}

/// @dev Returns the token URI of specified `id` using a call to contract implementing `IERC1155MetadataURI`.
function uri(
uint256 id,
IERC1155MetadataURI customUriInterface
) public view virtual returns (string memory) {
/// @dev Returns the token URI of specified `id` using a call to contract implementing `IUri`.
function uri(uint256 id, IUri customUriInterface) public view virtual returns (string memory) {
return customUriInterface.uri(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import {IERC4906Agnostic} from "../contracts/token/IERC4906Agnostic.sol";
import {InverseAppProjected1155} from "../contracts/token/InverseAppProjected1155.sol";
import {IInverseAppProjected1155} from "../contracts/token/IInverseAppProjected1155.sol";
import {IInverseProjected1155} from "../contracts/token/IInverseProjected1155.sol";
import {IUri} from "../contracts/token/IUri.sol";

contract MockTokenUri is IUri {
using Strings for uint256;

function uri(uint256 id) external view override returns (string memory) {
return string.concat("mock://", id.toString());
}
}

contract InverseAppProjected1155Test is CTest, ERC1155Holder {
using Strings for uint256;
Expand Down Expand Up @@ -154,6 +163,12 @@ contract InverseAppProjected1155Test is CTest, ERC1155Holder {
);
}

function test_TokenUriUsingCustomUriInterface() public {
IUri tokenUriInterface = new MockTokenUri();
string memory result = token.uri(ownedTokenId, tokenUriInterface);
assertEq(result, string.concat("mock://", ownedTokenId.toString()));
}

function test_SupportsInterface() public {
assertTrue(token.supportsInterface(type(IERC165).interfaceId));
assertTrue(token.supportsInterface(type(IERC1155).interfaceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import {IERC4906Agnostic} from "../contracts/token/IERC4906Agnostic.sol";
import {InverseBaseProjected1155} from "../contracts/token/InverseBaseProjected1155.sol";
import {IInverseBaseProjected1155} from "../contracts/token/IInverseBaseProjected1155.sol";
import {IInverseProjected1155} from "../contracts/token/IInverseProjected1155.sol";
import {IUri} from "../contracts/token/IUri.sol";

contract MockTokenUri is IUri {
using Strings for uint256;

function uri(uint256 id) external view override returns (string memory) {
return string.concat("mock://", id.toString());
}
}

contract InverseBaseProjected1155Test is CTest, ERC1155Holder {
using Strings for uint256;
Expand Down Expand Up @@ -106,6 +115,12 @@ contract InverseBaseProjected1155Test is CTest, ERC1155Holder {
);
}

function test_TokenUriUsingCustomUriInterface() public {
IUri tokenUriInterface = new MockTokenUri();
string memory result = token.uri(ownedTokenId, tokenUriInterface);
assertEq(result, string.concat("mock://", ownedTokenId.toString()));
}

function test_SupportsInterface() public {
assertTrue(token.supportsInterface(type(IERC165).interfaceId));
assertTrue(token.supportsInterface(type(IERC1155).interfaceId));
Expand Down

0 comments on commit 297d49c

Please sign in to comment.