Created
February 24, 2024 16:32
-
-
Save Mexidense/a90523b0b9e9bcd96b0dfc9dd14a7b92 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.24+commit.e11b9ed9.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol"; | |
import "@openzeppelin/[email protected]/access/Ownable.sol"; | |
contract ArtToken is ERC721, Ownable { | |
uint256 COUNTER; | |
uint256 public fee = 5 ether; | |
struct Art { | |
string name; | |
uint256 id; | |
uint256 dna; | |
uint8 level; | |
uint8 rarity; | |
} | |
Art[] public artWorks; | |
event NewArtWork(address indexed owner, uint256 id, uint256 dna); | |
constructor (string memory _name, string memory _symbol) ERC721(_name, _symbol) {} | |
function _createRandomNumber(uint256 _mod) internal view returns (uint256) { | |
bytes32 hasRandomNumber = keccak256(abi.encodePacked(block.timestamp, msg.sender)); | |
uint256 randomNumber = uint256(hasRandomNumber); | |
return randomNumber & _mod; | |
} | |
function _createArtWork(string memory _name) internal { | |
uint8 randomRarity = uint8(_createRandomNumber(1000)); | |
uint256 randomDna = _createRandomNumber(10**16); | |
Art memory newArtWork = Art(_name, COUNTER, randomDna, 1, randomRarity); | |
artWorks.push(newArtWork); | |
_safeMint(msg.sender, COUNTER); | |
emit NewArtWork(msg.sender, COUNTER, randomDna); | |
COUNTER++; | |
} | |
function updateFee(uint256 _fee) external onlyOwner { | |
fee = _fee; | |
} | |
function infoSmartContract() public view returns (address, uint256) { | |
address smartContractAddress = address(this); | |
uint256 smartContractMoney = address(this).balance / 10**18; | |
return (smartContractAddress, smartContractMoney); | |
} | |
function getArtWorks() public view returns (Art[] memory) { | |
return artWorks; | |
} | |
function getOwnerArtWork(address _owner) public view returns (Art[] memory) { | |
Art[] memory result = new Art[](balanceOf(_owner)); | |
uint256 counterOwner = 0; | |
for (uint256 i=0; i < artWorks.length; i++) { | |
if (ownerOf(i) == _owner) { | |
result[counterOwner] = artWorks[i]; | |
counterOwner++; | |
} | |
} | |
return result; | |
} | |
function createRandomArtWork(string memory _name) public payable { | |
require(msg.value >= fee, "[Art work] insufficient fee"); | |
_createArtWork(_name); | |
} | |
function withdraw() external payable onlyOwner { | |
address payable _owner = payable(owner()); | |
_owner.transfer(address(this).balance); | |
} | |
function levelUp(uint256 _artId) public { | |
require(ownerOf(_artId) == msg.sender, "[Art Work] Forbiden level art work up. Only owner"); | |
Art storage art = artWorks[_artId]; | |
art.level++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment