Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voucher contract audit fixes #6

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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