Skip to content

Commit 243cd9e

Browse files
committed
WIPW
1 parent 490654a commit 243cd9e

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

packages/foundry/contracts/KDONft.sol

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ contract KDONft is ERC721 {
88

99
error KDONft__NftDoesNotExist(uint256 tokenId);
1010

11+
enum TasksThreshold {
12+
FIVE,
13+
TEN,
14+
TWENTY,
15+
FIFTY,
16+
HUNDRED
17+
}
18+
1119
uint256 public s_nextTokenId;
20+
mapping(address child => TasksThreshold tasksCompleted) public
21+
s_childLastNftMinted;
1222

1323
constructor() ERC721("KiddoPerks NFT", "KDONft") {
1424
s_nextTokenId = 0;
1525
}
1626

17-
function mintNft(
18-
address to
19-
) public {
27+
function mintNft(address to) public {
2028
_safeMint(to, s_nextTokenId);
2129
emit KDONftMinted(to, s_nextTokenId);
22-
s_nextTokenId++;
30+
s_childToTokenId[s_nextTokenId] = s_nextTokenId++;
2331
}
2432

2533
function tokenURI(

packages/foundry/contracts/KiddoPerks.sol

+35-30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pragma solidity >=0.8.0 <0.9.0;
33

44
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
55
import { KDOToken } from "./KDOToken.sol";
6+
import { KDONft } from "./KDONft.sol";
67

78
contract KiddoPerks is Ownable {
89
event TaskCreated(string title);
@@ -27,8 +28,12 @@ contract KiddoPerks is Ownable {
2728
);
2829
error KiddoPerks__PerkAlreadyRedeemmed(uint256 id, address by);
2930
error KiddoPerks__NoValidChild(address childAddr);
31+
error KiddoPerks__CannotMintAnyNFTYet(address who);
32+
33+
uint256 constant MIN_TASK_COMPLETED_NFT = 5;
3034

3135
KDOToken token;
36+
KDONft nft;
3237
address public parent;
3338

3439
mapping(uint256 => Child) public s_children;
@@ -43,6 +48,8 @@ contract KiddoPerks is Ownable {
4348
mapping(uint256 => Task) public s_tasks;
4449
uint256 public s_taskNextId = 0;
4550
mapping(address => mapping(uint256 => bool)) public s_completedTasksByUser;
51+
mapping(address child => uint256 numTasksCompleted) public
52+
s_childNumTasksCompleted;
4653

4754
struct Task {
4855
uint256 id;
@@ -65,21 +72,18 @@ contract KiddoPerks is Ownable {
6572
bool removed;
6673
}
6774

68-
constructor(
69-
KDOToken _token
70-
) Ownable(msg.sender) {
75+
constructor(KDOToken _token, KDONft _nft) Ownable(msg.sender) {
7176
setParent(msg.sender);
7277
token = _token;
78+
nft = _nft;
7379
}
7480

7581
/**
7682
* Set parent management address
7783
*
7884
* @param newParentAddress New parent address
7985
*/
80-
function setParent(
81-
address newParentAddress
82-
) public onlyOwner {
86+
function setParent(address newParentAddress) public onlyOwner {
8387
parent = newParentAddress;
8488
transferOwnership(newParentAddress);
8589

@@ -98,9 +102,7 @@ contract KiddoPerks is Ownable {
98102
emit TaskCreated(title);
99103
}
100104

101-
function taskBy(
102-
uint256 id
103-
) public view returns (Task memory) {
105+
function taskBy(uint256 id) public view returns (Task memory) {
104106
return s_tasks[id];
105107
}
106108

@@ -122,9 +124,7 @@ contract KiddoPerks is Ownable {
122124
emit TokenMinted(by, taskCompleted.tokensReward);
123125
}
124126

125-
function removeTask(
126-
uint256 id
127-
) public onlyOwner {
127+
function removeTask(uint256 id) public onlyOwner {
128128
if (id >= s_taskNextId) {
129129
revert KiddoPerks__NotValidId(id);
130130
}
@@ -168,15 +168,11 @@ contract KiddoPerks is Ownable {
168168
emit PerkCreated(title, tokensRequired);
169169
}
170170

171-
function perkBy(
172-
uint256 id
173-
) public view returns (Perk memory) {
171+
function perkBy(uint256 id) public view returns (Perk memory) {
174172
return s_perks[id];
175173
}
176174

177-
function removePerk(
178-
uint256 id
179-
) public onlyOwner {
175+
function removePerk(uint256 id) public onlyOwner {
180176
if (id >= s_perksNextId) {
181177
revert KiddoPerks__NotValidId(id);
182178
}
@@ -199,9 +195,7 @@ contract KiddoPerks is Ownable {
199195
return allPerks;
200196
}
201197

202-
function redeemPerk(
203-
uint256 perkId
204-
) public onlyValidChild(msg.sender) {
198+
function redeemPerk(uint256 perkId) public onlyValidChild(msg.sender) {
205199
Perk memory perk = s_perks[perkId];
206200
uint256 userTokenBalance = token.balanceOf(msg.sender);
207201
if (userTokenBalance < perk.tokensRequired) {
@@ -234,15 +228,11 @@ contract KiddoPerks is Ownable {
234228
emit ChildAdded(name, childAddr);
235229
}
236230

237-
function childBy(
238-
uint256 id
239-
) public view returns (Child memory) {
231+
function childBy(uint256 id) public view returns (Child memory) {
240232
return s_children[id];
241233
}
242234

243-
function removeChild(
244-
uint256 id
245-
) public onlyOwner {
235+
function removeChild(uint256 id) public onlyOwner {
246236
if (id >= s_childrenNextId) {
247237
revert KiddoPerks__NotValidId(id);
248238
}
@@ -266,9 +256,24 @@ contract KiddoPerks is Ownable {
266256
return allChildren;
267257
}
268258

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) {
272277
if (s_validChildAddresses[childAddress] == false) {
273278
revert KiddoPerks__NoValidChild(childAddress);
274279
}

0 commit comments

Comments
 (0)