Skip to content

Commit

Permalink
chore: add Errors lib
Browse files Browse the repository at this point in the history
  • Loading branch information
codenamejason committed Oct 29, 2023
1 parent 18fa5d4 commit 452fcae
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
3 changes: 1 addition & 2 deletions script/Will4USNFT.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ contract Will4USNFTScript is Script {
// assertEq(url, "https://arb-goerli.g.alchemy.com/v2/RqTiyvS7OspxaAQUQupKKCTjmf94JL-I");
vm.startBroadcast(deployerPrivateKey);

Will4USNFT nftContract =
new Will4USNFT(deployerAddress, deployerAddress, deployerAddress, 5);
new Will4USNFT(deployerAddress, deployerAddress, deployerAddress, 5);

// nftContract.awardCampaignItem(deployerAddress, 1);

Expand Down
31 changes: 20 additions & 11 deletions src/IVOccurrenceManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.20;
import { IVStaffManager } from "./IVStaffManager.sol";
import { Enums } from "./library/Enums.sol";
import { Structs } from "./library/Structs.sol";
import { Errors } from "./library/Errors.sol";

// import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";

Expand All @@ -17,6 +18,7 @@ contract IVOccurrenceManager is IVStaffManager {
bytes32 public constant CREATOR_ROLE = keccak256("CREATOR_ROLE");

mapping(bytes32 => Structs.Occurrence) public occurrences;
uint256 private _occurrenceCount;

modifier onlyCreator() {
require(hasRole(CREATOR_ROLE, msg.sender), "IVOccurrenceManager: caller is not a creator");
Expand Down Expand Up @@ -64,6 +66,7 @@ contract IVOccurrenceManager is IVStaffManager {
});

occurrences[_occurenceId.id] = _occurenceId;
_occurrenceCount++;

return _occurenceId.id;
}
Expand Down Expand Up @@ -114,7 +117,7 @@ contract IVOccurrenceManager is IVStaffManager {
return occurrences[_occurenceIdId];
}

function getStaffMember(bytes32 _occurenceIdId, address _member)
function getStaffMemberByOccurrenceId(bytes32 _occurenceIdId, address _member)
external
view
returns (Structs.Staff memory)
Expand All @@ -127,28 +130,34 @@ contract IVOccurrenceManager is IVStaffManager {
return staff[_member];
}

function getStaffMembers(bytes32 _occurenceIdId)
function getStaffMembersForOccurrence(bytes32 _occurenceId)
external
view
returns (Structs.Staff[] memory)
{
require(
occurrences[_occurenceIdId].id == _occurenceIdId,
"IVOccurrenceManager: occurrence does not exist"
);
if (occurrences[_occurenceId].id != _occurenceId) {
revert Errors.OccurrenceDoesNotExist(_occurenceId);
}

Structs.Occurrence memory _occurenceId = occurrences[_occurenceIdId];
Structs.Staff[] memory _staff = new Structs.Staff[](_occurenceId.staff.length);
Structs.Occurrence memory occurrence = occurrences[_occurenceId];
Structs.Staff[] memory _staff = new Structs.Staff[](occurrence.staff.length);

for (uint256 i = 0; i < _occurenceId.staff.length; i++) {
_staff[i] = staff[_occurenceId.staff[i]];
for (uint256 i = 0; i < occurrence.staff.length; i++) {
_staff[i] = staff[occurrence.staff[i]];
}

return _staff;
}

function getOccurrences() external view returns (Structs.Occurrence[] memory) {
// TODO:
Structs.Occurrence[] memory _occurrences = new Structs.Occurrence[](_occurrenceCount);

for (uint256 i = 0; i < _occurrenceCount; i++) {
// FIXME: this is not the correct way to do this
_occurrences[i] = occurrences[keccak256(abi.encodePacked(i))];
}

return _occurrences;
}

function getOccurrenceById(bytes32 _occurenceIdId)
Expand Down
1 change: 1 addition & 0 deletions src/IVStaffManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.20;

import { Enums } from "./library/Enums.sol";
import { Structs } from "../src/library/Structs.sol";
import { Errors } from "../src/library/Errors.sol";

import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";

Expand Down
23 changes: 8 additions & 15 deletions src/Will4USNFT.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity 0.8.20;

import { Errors } from "./library/Errors.sol";

import { ERC721URIStorage } from
"openzeppelin-contracts/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import { ERC721 } from "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";
Expand Down Expand Up @@ -40,15 +42,6 @@ contract Will4USNFT is ERC721URIStorage, AccessControl {
string metadata; // this is a pointer to json object that contains the metadata for this class
}

/**
* Errors ************
*/
error InvalidTokenId(uint256 tokenId);
error MaxMintablePerClassReached(address recipient, uint256 classId, uint256 maxMintable);
error AlreadyRedeemed(uint256 eventId, uint256 tokenId);
error Unauthorized(address sender);
error NewSupplyTooLow(uint256 minted, uint256 supply);

/**
* Events ************
*/
Expand All @@ -72,7 +65,7 @@ contract Will4USNFT is ERC721URIStorage, AccessControl {
*/
modifier onlyCampaingnMember(address sender) {
if (!hasRole(MINTER_ROLE, sender)) {
revert Unauthorized(sender);
revert Errors.Unauthorized(sender);
}
_;
}
Expand Down Expand Up @@ -139,7 +132,7 @@ contract Will4USNFT is ERC721URIStorage, AccessControl {
returns (uint256)
{
if (mintedPerClass[_recipient][_classId] > maxMintablePerClass) {
revert MaxMintablePerClassReached(_recipient, _classId, maxMintablePerClass);
revert Errors.MaxMintablePerClassReached(_recipient, _classId, maxMintablePerClass);
}

uint256 tokenId = _mintCampaingnItem(_recipient, _classId);
Expand Down Expand Up @@ -190,11 +183,11 @@ contract Will4USNFT is ERC721URIStorage, AccessControl {
*/
function redeem(uint256 _eventId, uint256 _tokenId) external onlyCampaingnMember(msg.sender) {
if (super.ownerOf(_tokenId) == address(0)) {
revert InvalidTokenId(_tokenId);
revert Errors.InvalidTokenId(_tokenId);
}

if (redeemed[_eventId][_tokenId]) {
revert AlreadyRedeemed(_eventId, _tokenId);
revert Errors.AlreadyRedeemed(_eventId, _tokenId);
}

redeemed[_eventId][_tokenId] = true;
Expand Down Expand Up @@ -259,7 +252,7 @@ contract Will4USNFT is ERC721URIStorage, AccessControl {

return tokenURI(_tokenId);
} else {
revert InvalidTokenId(_tokenId);
revert Errors.InvalidTokenId(_tokenId);
}
}

Expand All @@ -280,7 +273,7 @@ contract Will4USNFT is ERC721URIStorage, AccessControl {
// if the new supply is less than the current supply, we need to check if the new supply is less than the minted
// if it is, then we need to revert
if (_supply < minted) {
revert NewSupplyTooLow(minted, _supply);
revert Errors.NewSupplyTooLow(minted, _supply);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/library/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ library Errors {
error MaxMintablePerClassReached(address recipient, uint256 classId, uint256 maxMintable);
error AlreadyRedeemed(uint256 eventId, uint256 tokenId);
error NewSupplyTooLow(uint256 minted, uint256 supply);
error OccurrenceDoesNotExist(bytes32 occurrenceId);
}
11 changes: 11 additions & 0 deletions test/Will4USNFTTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,18 @@ contract Will4USNFTTest is Test {
string memory imagePointer,
string memory metadataPointer
) = nftContract.classes(2);

assertEq(name, "name2", "Class name should be name");
assertEq(description, "description", "Class description should be description");
assertEq(imagePointer, "imagePointer", "Class imagePointer should be imagePointer");
assertEq(
metadataPointer,
"https://a_new_pointer_to_json_object.io",
"Class metadataPointer should be metadataPointer"
);
assertEq(supply, 1e7, "Class supply should be 1e7");
assertEq(minted, 0, "Class minted should be 0");
assertEq(id, 2, "Class id should be 2");
}

function test_revert_addClass_Unauthorized() public {
Expand Down

0 comments on commit 452fcae

Please sign in to comment.