-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontractcode.sol
105 lines (82 loc) · 3.35 KB
/
contractcode.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract DegenToken is ERC20, Ownable {
enum ItemType { Ring, Chain, Bracelet }
// Item prices in AVAX
mapping(ItemType => uint256) public itemPrices;
// Item quantities
mapping(ItemType => uint256) public itemQuantities;
// Item quantities owned by each account
mapping(address => mapping(ItemType => uint256)) public accountItems;
constructor(address initialOwner) Ownable(initialOwner) ERC20("Degen", "DGN") {
itemPrices[ItemType.Ring] = 200;
itemPrices[ItemType.Chain] = 300;
itemPrices[ItemType.Bracelet] = 500;
itemQuantities[ItemType.Ring] = 100;
itemQuantities[ItemType.Chain] = 100;
itemQuantities[ItemType.Bracelet] = 100;
}
function mint(address to, uint256 amount) external onlyOwner {
_mint(to, amount);
}
function transferTokens(address recipient, uint256 amount) external {
_transfer(msg.sender, recipient, amount);
}
function redeemTokens(ItemType itemType, uint256 quantity) external {
require(balanceOf(msg.sender) >= itemPrices[itemType] * quantity, "Insufficient balance for redemption");
require(itemQuantities[itemType] >= quantity, "Insufficient quantity of the item");
// Update account items
accountItems[msg.sender][itemType] += quantity;
_burn(msg.sender, itemPrices[itemType] * quantity);
itemQuantities[itemType] -= quantity;
}
function burnTokens(uint256 amount) external {
_burn(msg.sender, amount);
}
function viewInStoreItems() external view returns (string memory output) {
uint256[3] memory quantities;
quantities[0] = itemQuantities[ItemType.Ring];
quantities[1] = itemQuantities[ItemType.Chain];
quantities[2] = itemQuantities[ItemType.Bracelet];
output = string(abi.encodePacked(
"Ring: ", toString(quantities[0]),
", Chain: ", toString(quantities[1]),
", Bracelet: ", toString(quantities[2])
));
return output;
}
// View items owned by an account
function viewOwnedItems(address account) external view returns (string memory output) {
uint256[3] memory ownedQuantities;
ownedQuantities[0] = accountItems[account][ItemType.Ring];
ownedQuantities[1] = accountItems[account][ItemType.Chain];
ownedQuantities[2] = accountItems[account][ItemType.Bracelet];
output = string(abi.encodePacked(
" Ring: ", toString(ownedQuantities[0]),
", Chain: ", toString(ownedQuantities[1]),
", Bracelet: ", toString(ownedQuantities[2])
));
return output;
}
// Helper function to convert uint256 to string
function toString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
}