Skip to content

Commit

Permalink
Merge pull request #6 from WiiQare/fix/audit-wiiqare-1
Browse files Browse the repository at this point in the history
Voucher contract audit fixes
  • Loading branch information
zigabe committed Nov 20, 2023
2 parents 0243110 + 8288c12 commit c3aebfe
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 409 deletions.
58 changes: 15 additions & 43 deletions contracts/WiiQareVoucherV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ contract WiiQareVoucherV1 is ERC721, Pausable, Ownable, ERC721Burnable {
uint256 private _voucherID;
//=============================================================================

//=============================================================================
// Structs
//=============================================================================
struct Voucher {
uint256 value;
string currencySymbol;
string ownerID;
string providerID;
string beneficiaryID;
string status;
}
//=============================================================================

//=============================================================================
// Mappings
//=============================================================================
Expand All @@ -40,20 +27,19 @@ contract WiiQareVoucherV1 is ERC721, Pausable, Ownable, ERC721Burnable {
//=============================================================================
// Events
//=============================================================================
event mintVoucherEvent(uint256 voucherID, SharedStructs.Voucher nftVoucer);
event mintVoucherEvent(uint256 voucherID, SharedStructs.Voucher nftVoucher);
event transferVoucherEvent(uint256 voucherID, string ownerID);
event splitVoucherEvent(
uint256 voucherID,
SharedStructs.Voucher firstVoucher,
SharedStructs.Voucher secondVoucher
);
event alterVoucherEvent(uint256 voucherID, SharedStructs.Voucher voucher);
event burnVoucher(uint256 voucherID);
event burnVoucherEvent(uint256 voucherID);

//=============================================================================

constructor() ERC721("WiiQareVoucherV1", "WiiQare") {
_voucherID = 0;
constructor() ERC721("WiiQareVoucherV1", "WiiQare") {
}

//=============================================================================
Expand All @@ -69,9 +55,9 @@ contract WiiQareVoucherV1 is ERC721, Pausable, Ownable, ERC721Burnable {
) public onlyOwner whenNotPaused {
require(voucher.value > 0, "Value of voucher must be greater than 0");
vouchers[_voucherID] = voucher;
_safeMint(msg.sender, _voucherID);
emit mintVoucherEvent(_voucherID, voucher);
_incrementVoucherID();
_safeMint(msg.sender, _voucherID);
}

/**
Expand Down Expand Up @@ -120,28 +106,32 @@ contract WiiQareVoucherV1 is ERC721, Pausable, Ownable, ERC721Burnable {
);
mintVoucher(firstVoucher);
mintVoucher(secondVoucher);
_burn(voucherID);
_burnVoucher(voucherID);
emit splitVoucherEvent(voucherID, firstVoucher, secondVoucher);
}

/**
* Allows the contract owner to destroy a voucher when the contract is not paused
* @param voucherID id of the target voucher
*/
function _burn(
uint256 voucherID
) internal override(ERC721) whenNotPaused onlyOwner {
function _burnVoucher(uint256 voucherID) internal whenNotPaused onlyOwner {
require(ownerOf(voucherID) != address(0), "Voucher doesn't exist!");
delete vouchers[voucherID];
super._burn(voucherID);
emit burnVoucher(voucherID);
emit burnVoucherEvent(voucherID);
}

/**
* Returns all the minted vouchers
* @return Voucher[]
*/
function getAllVouchers() public view returns (SharedStructs.Voucher[] memory) {
SharedStructs.Voucher[] memory vouchersArray = new SharedStructs.Voucher[](_voucherID);
function getAllVouchers()
public
view
returns (SharedStructs.Voucher[] memory)
{
SharedStructs.Voucher[]
memory vouchersArray = new SharedStructs.Voucher[](_voucherID);
require(vouchersArray.length > 0, "No vouchers have been minted!");
for (uint256 index = 0; index < vouchersArray.length; index++) {
vouchersArray[index] = vouchers[index];
Expand All @@ -166,24 +156,6 @@ contract WiiQareVoucherV1 is ERC721, Pausable, Ownable, ERC721Burnable {
}
}

/**
* Substracts 1 from the voucherID
*/
function _decrementVoucherID() internal {
uint256 value = _voucherID;
require(value > 0, "Counter: decrement overflow");
unchecked {
_voucherID = _voucherID - 1;
}
}

/**
* Sets the voucherID to 0
*/
function _resetVoucherID() internal onlyOwner {
_voucherID = 0;
}

/**
* Allows the contract owner to pause the execution of the contract
*/
Expand Down
Loading

0 comments on commit c3aebfe

Please sign in to comment.