From 2f8f40144522714a2938caf146e6a7d49dfcc5a5 Mon Sep 17 00:00:00 2001 From: thedavidmeister Date: Tue, 5 Nov 2024 16:42:19 +0400 Subject: [PATCH] fix receipt test --- erc1155Metadata/ReceiptMetadata.json | 5 ----- src/concrete/receipt/Receipt.sol | 12 ++++++++--- test/src/concrete/receipt/Receipt.t.sol | 28 +++++++++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) delete mode 100644 erc1155Metadata/ReceiptMetadata.json diff --git a/erc1155Metadata/ReceiptMetadata.json b/erc1155Metadata/ReceiptMetadata.json deleted file mode 100644 index 2533cf84..00000000 --- a/erc1155Metadata/ReceiptMetadata.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "Receipt", - "decimals": 18, - "description": "A receipt for a ReceiptVault." -} diff --git a/src/concrete/receipt/Receipt.sol b/src/concrete/receipt/Receipt.sol index 42bff38f..b37c5c90 100644 --- a/src/concrete/receipt/Receipt.sol +++ b/src/concrete/receipt/Receipt.sol @@ -12,8 +12,14 @@ import {ERC1155Upgradeable as ERC1155} from import {OwnableUpgradeable as Ownable} from "openzeppelin-contracts-upgradeable/contracts/access/OwnableUpgradeable.sol"; import {StringsUpgradeable as Strings} from "openzeppelin-contracts-upgradeable/contracts/utils/StringsUpgradeable.sol"; -/// @dev the ERC1155 URI is always the pinned metadata on ipfs. -string constant RECEIPT_METADATA_URI = "ipfs://bafkreih7cvpjocgrk7mgdel2hvjpquc26j4jo2jkez5y2qdaojfil7vley"; +/// @dev The prefix for data URIs as base64 encoded JSON. +string constant DATA_URI_BASE64_PREFIX = "data:application/json;base64,"; + +// @notice The URI for the metadata of the `Receipt` contract. +// Decodes to a simple generic receipt metadata object. +// `{"name":"Receipt","decimals":18,"description":"A receipt for a ReceiptVault."}` +string constant RECEIPT_METADATA_DATA_URI = + "eyJuYW1lIjoiUmVjZWlwdCIsImRlY2ltYWxzIjoxOCwiZGVzY3JpcHRpb24iOiJBIHJlY2VpcHQgZm9yIGEgUmVjZWlwdFZhdWx0LiJ9"; /// @title Receipt /// @notice The `IReceiptV1` for a `ReceiptVault`. Standard implementation allows @@ -30,7 +36,7 @@ contract Receipt is IReceiptV1, Ownable, ERC1155, ICloneableV2 { /// implementation in `ReceiptFactory`. function initialize(bytes memory data) external override initializer returns (bytes32) { __Ownable_init(); - __ERC1155_init(RECEIPT_METADATA_URI); + __ERC1155_init(string.concat(DATA_URI_BASE64_PREFIX, RECEIPT_METADATA_DATA_URI)); address initialOwner = abi.decode(data, (address)); _transferOwnership(initialOwner); diff --git a/test/src/concrete/receipt/Receipt.t.sol b/test/src/concrete/receipt/Receipt.t.sol index 5a69d89a..72b394fd 100644 --- a/test/src/concrete/receipt/Receipt.t.sol +++ b/test/src/concrete/receipt/Receipt.t.sol @@ -2,21 +2,22 @@ // SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister pragma solidity =0.8.25; -import {Receipt, RECEIPT_METADATA_URI} from "src/concrete/receipt/Receipt.sol"; +import {Receipt, RECEIPT_METADATA_DATA_URI, DATA_URI_BASE64_PREFIX} from "src/concrete/receipt/Receipt.sol"; import {IReceiptOwnerV1} from "src/interface/IReceiptOwnerV1.sol"; import {TestReceipt} from "test/concrete/TestReceipt.sol"; import {TestReceiptOwner} from "test/concrete/TestReceiptOwner.sol"; import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol"; import {ReceiptFactoryTest, Vm} from "test/abstract/ReceiptFactoryTest.sol"; import {Base64} from "solady/utils/Base64.sol"; +import {console2} from "forge-std/Test.sol"; contract ReceiptTest is ReceiptFactoryTest { event ReceiptInformation(address sender, uint256 id, bytes information); struct Metadata { - string name; uint256 decimals; string description; + string name; } function testInitialize() public { @@ -25,17 +26,26 @@ contract ReceiptTest is ReceiptFactoryTest { assertEq(receipt.owner(), address(mockOwner)); } - function testReceiptURI() external { + function testReceiptURI(uint256 id) external { // Deploy the Receipt contract TestReceiptOwner mockOwner = new TestReceiptOwner(); TestReceipt receipt = createReceipt(address(mockOwner)); - string memory uri = receipt.uri(0); - - assertEq(uri, RECEIPT_METADATA_URI); - - bytes memory uriDecoded = Base64.decode(uri); - bytes memory uriJsonData = vm.parseJson(string(uriDecoded)); + string memory uri = receipt.uri(id); + + uint256 uriLength = bytes(uri).length; + assembly ("memory-safe") { + mstore(uri, 29) + } + assertEq(uri, DATA_URI_BASE64_PREFIX); + assembly ("memory-safe") { + uri := add(uri, 29) + mstore(uri, sub(uriLength, 29)) + } + assertEq(uri, RECEIPT_METADATA_DATA_URI); + + string memory uriDecoded = string(Base64.decode(uri)); + bytes memory uriJsonData = vm.parseJson(uriDecoded); Metadata memory metadataJson = abi.decode(uriJsonData, (Metadata)); assertEq(metadataJson.description, "A receipt for a ReceiptVault.");