Skip to content

Commit

Permalink
Merge pull request #169 from gildlab/163-test-all-erc1155-standard-fu…
Browse files Browse the repository at this point in the history
…nction-implementations-for-receipts

Test all erc1155 standard function implementations for receipts
  • Loading branch information
thedavidmeister authored Nov 5, 2024
2 parents 33d0f0d + 328e93c commit 67c67a3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "lib/rain.tier.interface"]
path = lib/rain.tier.interface
url = https://github.com/rainlanguage/rain.tier.interface
[submodule "lib/solady"]
path = lib/solady
url = https://github.com/Vectorized/solady
5 changes: 0 additions & 5 deletions erc1155Metadata/ReceiptMetadata.json

This file was deleted.

1 change: 1 addition & 0 deletions lib/solady
Submodule solady added at d223d4
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
4 changes: 2 additions & 2 deletions test/src/concrete/oracle/FtsoV2LTSFeedOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ contract FtsoV2LTSFeedOracleTest is OracleTest {
assertEq(oracle.price{value: fee}(), 0.0141082e18);
assertEq(ALICE.balance, 0);

vm.deal(ALICE, fee + 5);
vm.deal(ALICE, uint256(fee) + 5);
vm.prank(ALICE);
assertEq(oracle.price{value: fee + 5}(), 0.0141082e18);
assertEq(oracle.price{value: uint256(fee) + 5}(), 0.0141082e18);
assertEq(ALICE.balance, 5);

vm.deal(ALICE, fee);
Expand Down
36 changes: 35 additions & 1 deletion test/src/concrete/receipt/Receipt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,56 @@
// SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister
pragma solidity =0.8.25;

import {Receipt} 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";

contract ReceiptTest is ReceiptFactoryTest {
event ReceiptInformation(address sender, uint256 id, bytes information);

struct Metadata {
uint256 decimals;
string description;
string name;
}

function testInitialize() public {
TestReceiptOwner mockOwner = new TestReceiptOwner();
TestReceipt receipt = createReceipt(address(mockOwner));
assertEq(receipt.owner(), address(mockOwner));
}

function testReceiptURI(uint256 id) external {
// Deploy the Receipt contract
TestReceiptOwner mockOwner = new TestReceiptOwner();
TestReceipt receipt = createReceipt(address(mockOwner));

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.");
assertEq(metadataJson.decimals, 18);
assertEq(metadataJson.name, "Receipt");
}

// Test receipt sets owner
function testReceiptOwnerIsSet(uint256 fuzzedKeyAlice) external {
TestReceiptOwner mockOwner = new TestReceiptOwner();
Expand Down

0 comments on commit 67c67a3

Please sign in to comment.