Skip to content

Commit

Permalink
🛋🤖 -> Migrations & Truffle reconfigured with existing smart contract …
Browse files Browse the repository at this point in the history
…for #3
  • Loading branch information
Gizmotronn committed Feb 8, 2022
1 parent 0e7516f commit 9df8d83
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
Binary file modified .DS_Store
Binary file not shown.
65 changes: 65 additions & 0 deletions contracts/GearToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract GearToken is ERC721, Ownable {
constructor(string memory _name, string memory _symbol)
ERC721(_name, _symbol)
{}

uint256 COUNTER; // Incremental function for indexing of NFTs

// Gas fee function
uint256 fee = 1 ether;

struct Gear {
string name;
uint256 id;
uint256 dna;
uint8 level;
uint8 rarity;
}

Gear[] public gears;

event NewGear(address indexed owner, uint256 id, uint256 dna);

// Helpers
function _createRandomNum(uint256 _mod) internal view returns(uint256) { // generate DNA
uint256 randomNum = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))); // Takes the string input into function _genRandomDna and hashes it into a random number (keccak256)
return randomNum % _mod;
}

function updateFee(uint256 _fee) external onlyOwner() {
fee = _fee;
}

function withdraw() external payable onlyOwner() { // Withdraw the (holding) gas fee that was paid, only users who have created NFTs can withdraw the gas they've put in
address payable _owner = payable(owner());
_owner.transfer(address(this).balance);
}

// Minting/creation stage of the token
function _createGear(string memory _name) internal {
uint8 randRarity = uint8(_createRandomNum(100)); // Modulus of 100 -> Random number from 1-100; converting/casting from uint256 to uint8
uint256 randDna = _createRandomNum(10**16);
Gear memory newGear = Gear(_name, COUNTER, randDna, 1, randRarity);
gears.push(newGear); // Adds the new gear onto the array
_safeMint(msg.sender, COUNTER); // Send the token to the account that initialised the transaction?, what is the identifier of the NFT that's being minted?
emit NewGear(msg.sender, COUNTER, randDna);
COUNTER++;
}

function createRandomGear(string memory _name) public payable {
require(msg.value >= fee, "The fee is not correct"); // msg.value => how much the address input and sent to the contract
_createGear(_name);
}

// Getter functions
function getGears() public view returns(Gear[] memory){
return gears;
}
}
27 changes: 13 additions & 14 deletions truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ module.exports = {
// tab if you use this network and you must also set the `host`, `port` and `network_id`
// options below to some value.
//
// development: {
// host: "127.0.0.1", // Localhost (default: none)
// port: 8545, // Standard Ethereum port (default: none)
// network_id: "*", // Any network (default: none)
// },
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
// Another network with more advanced options...
// advanced: {
// port: 8777, // Custom port
Expand Down Expand Up @@ -79,17 +79,16 @@ module.exports = {
},

// Configure your compilers
contract_build_directory: "./src/contracts",
compilers: {
solc: {
version: "0.8.11", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
version: "0.8.0", // Fetch exact version from solc-bin (default: truffle's version)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: false,
runs: 200
},
}
}
},

Expand Down

0 comments on commit 9df8d83

Please sign in to comment.