Skip to content

Commit

Permalink
Create unit tests and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
geovgy committed Aug 18, 2024
1 parent e74c60e commit 38fcacf
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 67 deletions.
19 changes: 0 additions & 19 deletions script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

36 changes: 26 additions & 10 deletions src/POD.sol → src/PODv2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";

contract ProofOfDrinkV2 is ERC1155, Ownable {
/**
* @title PODv2
* @author geovgy
* @notice The Proof of Drink contract, 2nd edition
*/
contract PODv2 is ERC1155, Ownable {
mapping(uint256 id => bytes32 root) internal _merkleRoots;
mapping(uint256 id => string uri) internal _tokenURIs;
mapping(uint256 id => mapping(string claimCode => bool)) internal _claimed;
Expand All @@ -18,6 +23,14 @@ contract ProofOfDrinkV2 is ERC1155, Ownable {

constructor(address _owner) ERC1155("") Ownable(_owner) {}

function merkleRootOf(uint256 id) external view returns (bytes32) {
return _merkleRoots[id];
}

function uri(uint256 id) public view override virtual returns (string memory) {
return _tokenURIs[id];
}

function claim(
address account,
uint256 id,
Expand All @@ -35,28 +48,31 @@ contract ProofOfDrinkV2 is ERC1155, Ownable {
emit Claimed(account, id, claimCode);
}

function create(bytes32 root, string calldata uri) external onlyOwner {
function create(bytes32 root, string calldata uri_) external onlyOwner returns (uint256 id) {
_idCounter++;
_merkleRoots[_idCounter] = root;
_tokenURIs[_idCounter] = uri;
emit Created(_idCounter, root, uri);
id = _idCounter;
_setMerkleRoot(id, root);
_setTokenURI(id, uri_);
emit Created(id, root, uri_);
}

function setMerkleRoot(uint256 id, bytes32 root) external onlyOwner {
require(id <= _idCounter, "ProofOfDrink: Invalid id");
_setMerkleRoot(id, root);
emit MerkleRootSet(id, root);
}

function setTokenURI(uint256 id, string calldata uri) external onlyOwner {
_setTokenURI(id, uri);
emit TokenURISet(id, uri);
function setTokenURI(uint256 id, string calldata uri_) external onlyOwner {
require(id <= _idCounter, "ProofOfDrink: Invalid id");
_setTokenURI(id, uri_);
emit TokenURISet(id, uri_);
}

function _setMerkleRoot(uint256 id, bytes32 root) internal {
_merkleRoots[id] = root;
}

function _setTokenURI(uint256 id, string calldata uri) internal {
_tokenURIs[id] = uri;
function _setTokenURI(uint256 id, string calldata uri_) internal {
_tokenURIs[id] = uri_;
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

89 changes: 89 additions & 0 deletions test/PODv2.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {Test, console} from "forge-std/Test.sol";
import {PODv2} from "../src/PODv2.sol";

contract PODv2Test is Test {
PODv2 internal pod;
address internal owner = address(0x1);

function setUp() public {
pod = new PODv2(owner);
}

function test_owner() public view {
assertEq(pod.owner(), owner);
}

function test_create() public {
bytes32 root = keccak256("merkleroot");
string memory uri = "ipfs://testtesttest";

// Revert if non-owner
vm.expectRevert();
pod.create(root, uri);

// Should execute and emit event
vm.startPrank(owner);
vm.expectEmit();
emit PODv2.Created(1, root, uri);

uint256 id = pod.create(root, uri);
vm.stopPrank();

vm.assertEq(id, 1);
vm.assertEq(pod.merkleRootOf(1), root);
vm.assertEq(pod.uri(1), uri);
}

function test_setMerkleRoot() public {
vm.prank(owner);
pod.create(keccak256("merkleroot"), "ipfs://testtesttest");

bytes32 newRoot = keccak256("new_merkleroot");

// Revert if non-owner
vm.expectRevert();
pod.setMerkleRoot(1, newRoot);

// Revert if id not created yet
vm.expectRevert();
pod.setMerkleRoot(2, newRoot);

// Should execute and emit event
vm.startPrank(owner);
vm.expectEmit();
emit PODv2.MerkleRootSet(1, newRoot);

pod.setMerkleRoot(1, newRoot);
vm.stopPrank();

vm.assertEq(pod.merkleRootOf(1), newRoot);
}

function test_setTokenURI() public {
vm.prank(owner);
pod.create(keccak256("merkleroot"), "ipfs://testtesttest");

string memory newUri = "ipfs://newnewnewnew";

// Revert if non-owner
vm.expectRevert();
pod.setTokenURI(1, newUri);

// Revert if id not created yet
vm.expectRevert();
pod.setTokenURI(2, newUri);

// Should execute and emit event
vm.startPrank(owner);
vm.expectEmit();
emit PODv2.TokenURISet(1, newUri);

pod.setTokenURI(1, newUri);
vm.stopPrank();

vm.assertEq(pod.uri(1), newUri);
}
}

0 comments on commit 38fcacf

Please sign in to comment.