forked from WiiQare/SmartContract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiiQareVoucherV1.sol
201 lines (180 loc) · 7.14 KB
/
WiiQareVoucherV1.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-License-Identifier: Unlicensed
//=============================================================================
// WiiQare NFT Voucher smart contract
//=============================================================================
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "./WiiQareSharedStructs.sol";
contract WiiQareVoucherV1 is ERC721, Pausable, Ownable, ERC721Burnable {
//=============================================================================
// State variables
//=============================================================================
uint256 private _voucherID;
//=============================================================================
//=============================================================================
// Structs
//=============================================================================
struct Voucher {
uint256 value;
string currencySymbol;
string ownerID;
string providerID;
string beneficiaryID;
string status;
}
//=============================================================================
//=============================================================================
// Mappings
//=============================================================================
mapping(uint256 => SharedStructs.Voucher) public vouchers;
//=============================================================================
//=============================================================================
// Events
//=============================================================================
event mintVoucherEvent(uint256 voucherID, SharedStructs.Voucher nftVoucer);
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);
//=============================================================================
constructor() ERC721("WiiQareVoucherV1", "WiiQare") {
_voucherID = 0;
}
//=============================================================================
// Functions
//=============================================================================
/**
* Allows the contract owner to mint a voucher when the contract is not paused
* @param voucher new voucher metadata
*/
function mintVoucher(
SharedStructs.Voucher memory voucher
) 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();
}
/**
* Allows the contract owner to transfer a voucher when the contract is not paused
* @param voucherID id of the target voucher
* @param ownerID new owner for the target voucher
*/
function transferVoucher(
uint256 voucherID,
string memory ownerID
) public whenNotPaused onlyOwner {
vouchers[voucherID].ownerID = ownerID;
emit transferVoucherEvent(voucherID, ownerID);
}
/**
* Allows the contract owner to alter data for a voucher when the contract is not paused
* @param voucherID id of the target voucher
* @param voucher new voucher data
*/
function alterVoucher(
uint256 voucherID,
SharedStructs.Voucher memory voucher
) public whenNotPaused onlyOwner {
vouchers[voucherID] = voucher;
emit alterVoucherEvent(voucherID, voucher);
}
/**
* Allows the contract owner to split a voucher into 2 new ones, target will be deleted when the contract is not paused
* @param voucherID id of the target voucher
* @param firstVoucher metadata for the first voucher of the split
* @param secondVoucher metadata for the second voucher of the split
*/
function splitVoucher(
uint256 voucherID,
SharedStructs.Voucher memory firstVoucher,
SharedStructs.Voucher memory secondVoucher
) public whenNotPaused onlyOwner {
require(firstVoucher.value > 0, "Invalid value for voucher 1");
require(secondVoucher.value > 0, "Invalid value for voucher 2");
require(
firstVoucher.value + secondVoucher.value ==
vouchers[voucherID].value,
"Sum of target voucher is different than sum of splitted vouchers"
);
mintVoucher(firstVoucher);
mintVoucher(secondVoucher);
_burn(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 {
delete vouchers[voucherID];
super._burn(voucherID);
emit burnVoucher(voucherID);
}
/**
* Returns all the minted vouchers
* @return Voucher[]
*/
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];
}
return vouchersArray;
}
/**
* Returns the last voucher ID that has been minted
* @return uint
*/
function getCurrentVoucherID() public view returns (uint256) {
return _voucherID;
}
/**
* Increments the voucher ID by 1
*/
function _incrementVoucherID() internal {
unchecked {
_voucherID += 1;
}
}
/**
* 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
*/
function pause() public onlyOwner {
_pause();
}
/**
* Allows the contract owner to unpause the execution of the contract
*/
function unpause() public onlyOwner {
_unpause();
}
//=============================================================================
}