Skip to content

Commit

Permalink
fix receipt test
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Nov 5, 2024
1 parent 815ee56 commit 2f8f401
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
5 changes: 0 additions & 5 deletions erc1155Metadata/ReceiptMetadata.json

This file was deleted.

12 changes: 9 additions & 3 deletions src/concrete/receipt/Receipt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
28 changes: 19 additions & 9 deletions test/src/concrete/receipt/Receipt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.");
Expand Down

0 comments on commit 2f8f401

Please sign in to comment.