Skip to content

Commit

Permalink
test cyclo receipt uri
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Nov 3, 2024
1 parent 5f3ef9e commit f47b8cd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 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
1 change: 1 addition & 0 deletions lib/solady
Submodule solady added at 4ac044
5 changes: 4 additions & 1 deletion src/concrete/receipt/CycloReceipt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {FIXED_POINT_ONE} from "rain.math.fixedpoint/lib/FixedPointDecimalConstan
/// @dev The SVG of Cyclo logo is pinned on IPFS.
string constant CYCLO_RECEIPT_SVG_URI = "ipfs://bafybeidjgkxfpk7nujlnx7jwvjvmtcbkfg53vnlc2cc6ftqfhapqkmtahq";

/// @dev The prefix for data URIs as base64 encoded JSON.
string constant DATA_URI_BASE64_PREFIX = "data:application/json;base64,";

contract CycloReceipt is Receipt {
function uri(uint256 id) public view virtual override returns (string memory) {
bytes memory json = abi.encodePacked(
Expand All @@ -30,6 +33,6 @@ contract CycloReceipt is Receipt {
"\"}"
);

return string(abi.encodePacked("data:application/json;base64,", Base64.encode(json)));
return string(abi.encodePacked(DATA_URI_BASE64_PREFIX, Base64.encode(json)));
}
}
41 changes: 41 additions & 0 deletions test/src/concrete/receipt/CycloReceipt.metadata.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: LicenseRef-DCL-1.0
// SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister
pragma solidity ^0.8.25;

import {Test} from "forge-std/Test.sol";
import {Base64} from "solady/utils/Base64.sol";
import {CycloReceipt, DATA_URI_BASE64_PREFIX, CYCLO_RECEIPT_SVG_URI} from "src/concrete/receipt/CycloReceipt.sol";

contract CycloReceiptMetadataTest is Test {
struct URIJson {
string description;
string image;
string name;
}

function testCycloReceiptURI() external {
CycloReceipt receipt = new CycloReceipt();

string memory uri = receipt.uri(0.01544e18);
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))
}

bytes memory uriDecoded = Base64.decode(uri);
bytes memory uriJsonData = vm.parseJson(string(uriDecoded));

URIJson memory uriJson = abi.decode(uriJsonData, (URIJson));
assertEq(
uriJson.description,
"1 of these receipts can be burned alongside 1 cysFLR to redeem 64.766839378238341968 sFLR. Reedem at https://cyclo.finance."
);
assertEq(uriJson.image, CYCLO_RECEIPT_SVG_URI);
assertEq(uriJson.name, "Receipt for cyclo lock at 0.01544 USD per sFLR.");
}
}
15 changes: 0 additions & 15 deletions test/src/concrete/receipt/Receipt.metadata.t.sol

This file was deleted.

0 comments on commit f47b8cd

Please sign in to comment.