forked from trizin/NFT-Marketplace-on-Avalanche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFT.sol
41 lines (34 loc) · 1.47 KB
/
NFT.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
pragma solidity ^0.7.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract NFT is ERC721 {
uint _tokenCounter;
mapping (uint => uint) public itemValue;
uint maxValue = 10000; // Max value of an item
constructor() ERC721("Super NFT", "SPRNFT") {}
/**
Returns an random number
*/
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, block.number)));
}
function myItems() external view returns (uint[] memory items) {
// Returns an array of items that the user owns
items = new uint[](balanceOf(msg.sender));
uint _counter = 0;
for(uint i = 1; i < _tokenCounter; i++) { // i = 1 because the token counter is increased before the id is assigned
if(ownerOf(i) == msg.sender) { // if the user owns the item
items[_counter] = i; // add the item to the array
_counter++; // increase the counter
}
}
return items;
}
function getItem() payable public returns (uint256) {
require(msg.value == 0.5 ether); // 0.5 AVAX is the cost of an item
uint256 newItemId = _tokenCounter; // Get the current counter value
_tokenCounter++; // Increase the counter
_mint(msg.sender, newItemId); // Mint the new item to the player
itemValue[newItemId] = random() % maxValue; // Set the item value to a random number modulus used to make sure that the value isn't bigger than maxValue
return newItemId; // Return the new item id
}
}