@@ -3,6 +3,7 @@ pragma solidity >=0.8.0 <0.9.0;
3
3
4
4
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol " ;
5
5
import { KDOToken } from "./KDOToken.sol " ;
6
+ import { KDONft } from "./KDONft.sol " ;
6
7
7
8
contract KiddoPerks is Ownable {
8
9
event TaskCreated (string title );
@@ -27,8 +28,12 @@ contract KiddoPerks is Ownable {
27
28
);
28
29
error KiddoPerks__PerkAlreadyRedeemmed (uint256 id , address by );
29
30
error KiddoPerks__NoValidChild (address childAddr );
31
+ error KiddoPerks__CannotMintAnyNFTYet (address who );
32
+
33
+ uint256 constant MIN_TASK_COMPLETED_NFT = 5 ;
30
34
31
35
KDOToken token;
36
+ KDONft nft;
32
37
address public parent;
33
38
34
39
mapping (uint256 => Child) public s_children;
@@ -43,6 +48,8 @@ contract KiddoPerks is Ownable {
43
48
mapping (uint256 => Task) public s_tasks;
44
49
uint256 public s_taskNextId = 0 ;
45
50
mapping (address => mapping (uint256 => bool )) public s_completedTasksByUser;
51
+ mapping (address child = > uint256 numTasksCompleted ) public
52
+ s_childNumTasksCompleted;
46
53
47
54
struct Task {
48
55
uint256 id;
@@ -65,21 +72,18 @@ contract KiddoPerks is Ownable {
65
72
bool removed;
66
73
}
67
74
68
- constructor (
69
- KDOToken _token
70
- ) Ownable (msg .sender ) {
75
+ constructor (KDOToken _token , KDONft _nft ) Ownable (msg .sender ) {
71
76
setParent (msg .sender );
72
77
token = _token;
78
+ nft = _nft;
73
79
}
74
80
75
81
/**
76
82
* Set parent management address
77
83
*
78
84
* @param newParentAddress New parent address
79
85
*/
80
- function setParent (
81
- address newParentAddress
82
- ) public onlyOwner {
86
+ function setParent (address newParentAddress ) public onlyOwner {
83
87
parent = newParentAddress;
84
88
transferOwnership (newParentAddress);
85
89
@@ -98,9 +102,7 @@ contract KiddoPerks is Ownable {
98
102
emit TaskCreated (title);
99
103
}
100
104
101
- function taskBy (
102
- uint256 id
103
- ) public view returns (Task memory ) {
105
+ function taskBy (uint256 id ) public view returns (Task memory ) {
104
106
return s_tasks[id];
105
107
}
106
108
@@ -122,9 +124,7 @@ contract KiddoPerks is Ownable {
122
124
emit TokenMinted (by, taskCompleted.tokensReward);
123
125
}
124
126
125
- function removeTask (
126
- uint256 id
127
- ) public onlyOwner {
127
+ function removeTask (uint256 id ) public onlyOwner {
128
128
if (id >= s_taskNextId) {
129
129
revert KiddoPerks__NotValidId (id);
130
130
}
@@ -168,15 +168,11 @@ contract KiddoPerks is Ownable {
168
168
emit PerkCreated (title, tokensRequired);
169
169
}
170
170
171
- function perkBy (
172
- uint256 id
173
- ) public view returns (Perk memory ) {
171
+ function perkBy (uint256 id ) public view returns (Perk memory ) {
174
172
return s_perks[id];
175
173
}
176
174
177
- function removePerk (
178
- uint256 id
179
- ) public onlyOwner {
175
+ function removePerk (uint256 id ) public onlyOwner {
180
176
if (id >= s_perksNextId) {
181
177
revert KiddoPerks__NotValidId (id);
182
178
}
@@ -199,9 +195,7 @@ contract KiddoPerks is Ownable {
199
195
return allPerks;
200
196
}
201
197
202
- function redeemPerk (
203
- uint256 perkId
204
- ) public onlyValidChild (msg .sender ) {
198
+ function redeemPerk (uint256 perkId ) public onlyValidChild (msg .sender ) {
205
199
Perk memory perk = s_perks[perkId];
206
200
uint256 userTokenBalance = token.balanceOf (msg .sender );
207
201
if (userTokenBalance < perk.tokensRequired) {
@@ -234,15 +228,11 @@ contract KiddoPerks is Ownable {
234
228
emit ChildAdded (name, childAddr);
235
229
}
236
230
237
- function childBy (
238
- uint256 id
239
- ) public view returns (Child memory ) {
231
+ function childBy (uint256 id ) public view returns (Child memory ) {
240
232
return s_children[id];
241
233
}
242
234
243
- function removeChild (
244
- uint256 id
245
- ) public onlyOwner {
235
+ function removeChild (uint256 id ) public onlyOwner {
246
236
if (id >= s_childrenNextId) {
247
237
revert KiddoPerks__NotValidId (id);
248
238
}
@@ -266,9 +256,24 @@ contract KiddoPerks is Ownable {
266
256
return allChildren;
267
257
}
268
258
269
- modifier onlyValidChild (
270
- address childAddress
271
- ) {
259
+ /**
260
+ * NFT Mint
261
+ */
262
+ function mintNFTByTaskCompletion () public {
263
+ uint256 numTaskCompletedByChild = s_childNumTasksCompleted[msg .sender ];
264
+ if (numTaskCompletedByChild >= MIN_TASK_COMPLETED_NFT) {
265
+ revert KiddoPerks__CannotMintAnyNFTYet (msg .sender );
266
+ }
267
+
268
+ if (numTaskCompletedByChild >= 5 && numTaskCompletedByChild < 10 ) {
269
+ nft.mintNft (msg .sender , s_childNumTasksCompleted[msg .sender ]);
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Modifiers
275
+ */
276
+ modifier onlyValidChild (address childAddress ) {
272
277
if (s_validChildAddresses[childAddress] == false ) {
273
278
revert KiddoPerks__NoValidChild (childAddress);
274
279
}
0 commit comments