Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(world-modules): properly concat baseURI and tokenURI in ERC721 module #2686

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Owners } from "./tables/Owners.sol";
import { TokenApproval } from "./tables/TokenApproval.sol";
import { TokenURI } from "./tables/TokenURI.sol";

import { _balancesTableId, _metadataTableId, _tokenUriTableId, _operatorApprovalTableId, _ownersTableId, _tokenApprovalTableId } from "./utils.sol";
import { _balancesTableId, _metadataTableId, _tokenUriTableId, _operatorApprovalTableId, _ownersTableId, _tokenApprovalTableId, uintToString } from "./utils.sol";

contract ERC721System is IERC721Mintable, System, PuppetMaster {
using WorldResourceIdInstance for ResourceId;
Expand Down Expand Up @@ -64,7 +64,7 @@ contract ERC721System is IERC721Mintable, System, PuppetMaster {

string memory baseURI = _baseURI();
string memory _tokenURI = TokenURI.get(_tokenUriTableId(_namespace()), tokenId);
_tokenURI = bytes(_tokenURI).length > 0 ? _tokenURI : string(abi.encodePacked(tokenId));
_tokenURI = bytes(_tokenURI).length > 0 ? _tokenURI : uintToString(tokenId);
Copy link
Contributor

@yonadaa yonadaa Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreeing with @alvrs, we can lift the Strings library that OZ uses here in their ERC721 implementation.

In MUD we've copied a lot of files.

return bytes(baseURI).length > 0 ? string.concat(baseURI, _tokenURI) : _tokenURI;
}

Expand Down
22 changes: 22 additions & 0 deletions packages/world-modules/src/modules/erc721-puppet/utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,25 @@ function _tokenUriTableId(bytes14 namespace) pure returns (ResourceId) {
function _erc721SystemId(bytes14 namespace) pure returns (ResourceId) {
return WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: namespace, name: ERC721_SYSTEM_NAME });
}

function uintToString(uint256 value) pure returns (string memory) {
// Base case
if (value == 0) {
return "0";
}
// Temporarily store the value to calculate the number of digits
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
// Allocate enough memory to store the string
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
16 changes: 15 additions & 1 deletion packages/world-modules/test/ERC721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PuppetModule } from "../src/modules/puppet/PuppetModule.sol";
import { ERC721Module } from "../src/modules/erc721-puppet/ERC721Module.sol";
import { ERC721MetadataData } from "../src/modules/erc721-puppet/tables/ERC721Metadata.sol";
import { IERC721Mintable } from "../src/modules/erc721-puppet/IERC721Mintable.sol";
import { IERC721Metadata } from "../src/modules/erc721-puppet/IERC721Metadata.sol";
import { registerERC721 } from "../src/modules/erc721-puppet/registerERC721.sol";
import { IERC721Errors } from "../src/modules/erc721-puppet/IERC721Errors.sol";
import { IERC721Events } from "../src/modules/erc721-puppet/IERC721Events.sol";
Expand Down Expand Up @@ -77,7 +78,11 @@ contract ERC721Test is Test, GasReporter, IERC721Events, IERC721Errors {
StoreSwitch.setStoreAddress(address(world));

// Register a new ERC721 token
token = registerERC721(world, "myERC721", ERC721MetadataData({ name: "Token", symbol: "TKN", baseURI: "" }));
token = registerERC721(
world,
"myERC721",
ERC721MetadataData({ name: "Token", symbol: "TKN", baseURI: "base-uri/" })
);
}

function _expectAccessDenied(address caller) internal {
Expand Down Expand Up @@ -164,6 +169,15 @@ contract ERC721Test is Test, GasReporter, IERC721Events, IERC721Errors {
assertEq(token.ownerOf(id), owner);
}

function testTokenURI(address owner) public {
vm.assume(owner != address(0));

token.mint(owner, 1);

IERC721Metadata tokenMetadata = IERC721Metadata(address(token));
assertEq(tokenMetadata.tokenURI(1), "base-uri/1");
}

function testMintRevertAccessDenied(uint256 id, address owner, address operator) public {
_assumeDifferentNonZero(owner, operator, address(this));

Expand Down
Loading