From bb3dcb5b325c9bd9726f09fd4783224ced0b227b Mon Sep 17 00:00:00 2001 From: bt3gl <1130416+bt3gl@users.noreply.github.com> Date: Wed, 23 Mar 2022 06:56:46 -0700 Subject: [PATCH] Add code from erc721 101 class (#5) --- erc721-solidity-101/.DS_Store | Bin 0 -> 6148 bytes erc721-solidity-101/.env_sample | 5 +++ erc721-solidity-101/README.md | 20 +++++++++ erc721-solidity-101/contracts/MiaNFT.sol | 28 ++++++++++++ erc721-solidity-101/hardhat.config.js | 19 ++++++++ erc721-solidity-101/package.json | 23 ++++++++++ .../scripts/deploy-contract.js | 14 ++++++ erc721-solidity-101/scripts/mint-nft.js | 41 ++++++++++++++++++ 8 files changed, 150 insertions(+) create mode 100644 erc721-solidity-101/.DS_Store create mode 100644 erc721-solidity-101/.env_sample create mode 100644 erc721-solidity-101/README.md create mode 100644 erc721-solidity-101/contracts/MiaNFT.sol create mode 100644 erc721-solidity-101/hardhat.config.js create mode 100644 erc721-solidity-101/package.json create mode 100644 erc721-solidity-101/scripts/deploy-contract.js create mode 100644 erc721-solidity-101/scripts/mint-nft.js diff --git a/erc721-solidity-101/.DS_Store b/erc721-solidity-101/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f7d5e4f908a65c9a6d957a8b7de3e0e71fc57006 GIT binary patch literal 6148 zcmeHKO>fgM7=FDKmN1ZtG$c46MdDhOR%J|xOX#`-2QCYO1E7*Dr3=gADoNKvRVnxJ zUv}Of;lFT#=e0elU*m|-=2v?C`0I)7$MWl#h(vcBw~6XR=WSOxw!1^Dc`IJ1&6oZGqcE03a7Mrh^G@CM%etqi%fjA&Af z4C68yrAbk?T0h0cX6?@1Eyr=ToiD+$oCIZ9j*D)Xf8x@6DbwgGA4VUN!K7c`*_T-v zCfOiYgd`rI%jb_t7RyOjj+(0)tuIu*ePo~osotF)-{c3mC@TTq7UZ>&hb#`Yn z*Ln2p`Rl{}NqU;eb2WJq2v*7-7`%cX$gC0LEXcD|W<#_x^C2B0%;gj-EU9Va`$h>3 zQToo|Z$Z8bu;%5vPa_p5Pmm62pgFt+i#+jkiRaQG9bhwh^bXjhQ;L@uyu$gBwy41D za?EU`!}9#R#IL{izA0O}B*M~mLW5gs<`_8Jw{-n>*;WCo!1W4neDL6mzQIbP3LQw) z698C4wKC-Shk-esLEm7d5iKyGsX$E?=7}LR9rd2+=NqgvYB~w?_z-4fVO}UgjgIeo zsyhi^qfM;>R)IwYHg&Pi_y585@Bc-TEm;Mu0yjzlQR@Z0E|z5O)|JKaUF*TW!r8cA nrBP9knCn<&_$q!3SB5r^GeF;9r4cnS`y-%au!&XRpDOSNe1p>n literal 0 HcmV?d00001 diff --git a/erc721-solidity-101/.env_sample b/erc721-solidity-101/.env_sample new file mode 100644 index 0000000..f93641e --- /dev/null +++ b/erc721-solidity-101/.env_sample @@ -0,0 +1,5 @@ +API_URL = "https://eth-rinkeby.alchemyapi.io/v2/" +PUBLIC_KEY = +PRIVATE_KEY = +METADATA_URL = " +CONTRACT_ADDRESS = diff --git a/erc721-solidity-101/README.md b/erc721-solidity-101/README.md new file mode 100644 index 0000000..85395a3 --- /dev/null +++ b/erc721-solidity-101/README.md @@ -0,0 +1,20 @@ +# One: A special summer + + +1. Compile contract: + +``` +npx hardhat compile +``` + +2. Deploy contract: + +``` +npx hardhat run scripts/deploy-contract.js --network rinkeby +``` + +3. Mint NFT: + +``` +node scripts/mint-nft.js +``` \ No newline at end of file diff --git a/erc721-solidity-101/contracts/MiaNFT.sol b/erc721-solidity-101/contracts/MiaNFT.sol new file mode 100644 index 0000000..1bb8d51 --- /dev/null +++ b/erc721-solidity-101/contracts/MiaNFT.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.7.3 <0.9.0; + +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; +import "@openzeppelin/contracts/utils/Counters.sol"; +import "@openzeppelin/contracts/access/Ownable.sol"; + + +contract MiaNFT is ERC721, Ownable { + + using Counters for Counters.Counter; + Counters.Counter private _tokenIds; + + constructor() public ERC721("Mia's NFT, "NFT") {} + + function mintNFT(address recipient, string memory tokenURI) + public onlyOwner + returns (uint256) + { + _tokenIds.increment(); + + uint256 newItemId = _tokenIds.current(); + _mint(recipient, newItemId); + _setTokenURI(newItemId, tokenURI); + + return newItemId; + } +} \ No newline at end of file diff --git a/erc721-solidity-101/hardhat.config.js b/erc721-solidity-101/hardhat.config.js new file mode 100644 index 0000000..8b52766 --- /dev/null +++ b/erc721-solidity-101/hardhat.config.js @@ -0,0 +1,19 @@ +/** +* @type import('hardhat/config').HardhatUserConfig +*/ +require('dotenv').config(); +require("@nomiclabs/hardhat-ethers"); + +const { API_URL, PRIVATE_KEY } = process.env; + +module.exports = { + solidity: "0.7.3", + defaultNetwork: "rinkeby", + networks: { + hardhat: {}, + rinkeby: { + url: API_URL, + accounts: [`0x${PRIVATE_KEY}`] + } + }, +} diff --git a/erc721-solidity-101/package.json b/erc721-solidity-101/package.json new file mode 100644 index 0000000..a863d5e --- /dev/null +++ b/erc721-solidity-101/package.json @@ -0,0 +1,23 @@ +{ + "name": "MiaNFT", + "version": "1.0.0", + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Mia von Steinkirch", + "license": "MIT", + "devDependencies": { + "@nomiclabs/hardhat-ethers": "^2.0.2", + "@nomiclabs/hardhat-waffle": "^2.0.1", + "@openzeppelin/contracts": "^3.1.0-solc-0.7", + "chai": "^4.3.4", + "ethereum-waffle": "^3.4.0", + "ethers": "^5.4.6", + "hardhat": "^2.6.2" + }, + "dependencies": { + "@alch/alchemy-web3": "^1.1.4", + "dotenv": "^10.0.0" + } +} diff --git a/erc721-solidity-101/scripts/deploy-contract.js b/erc721-solidity-101/scripts/deploy-contract.js new file mode 100644 index 0000000..630b540 --- /dev/null +++ b/erc721-solidity-101/scripts/deploy-contract.js @@ -0,0 +1,14 @@ +async function main() { + const nft = await ethers.getContractFactory("MiaNFT"); + + const nft_deploy = await nft.deploy(); + console.log(" ⛓🧱✨ Contract address:", nft_deploy.address); + console.log(" ➡️ (Please add this string to .env)"); +} + +main() + .then(() => process.exit(0)) + .catch(e => { + console.error(e); + process.exit(1); + }); diff --git a/erc721-solidity-101/scripts/mint-nft.js b/erc721-solidity-101/scripts/mint-nft.js new file mode 100644 index 0000000..f4333b9 --- /dev/null +++ b/erc721-solidity-101/scripts/mint-nft.js @@ -0,0 +1,41 @@ +require('dotenv').config(); + +const PUBLIC_KEY = process.env.PUBLIC_KEY; +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const API_URL = process.env.API_URL; +const METADATA_URL = process.env.METADATA_URL; +const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS; +const { createAlchemyWeb3 } = require("@alch/alchemy-web3"); +const web3 = createAlchemyWeb3(API_URL); +const contract = require("../artifacts/contracts/MiaNFT.sol/MiaNFT.json"); +const nftContract = new web3.eth.Contract(contract.abi, CONTRACT_ADDRESS); + +async function mintNFT(tokenURI) { + + const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, 'latest'); + + const transaction = { + 'from': PUBLIC_KEY, + 'to': CONTRACT_ADDRESS, + 'nonce': nonce, + 'gas': 500000, + 'maxPriorityFeePerGas': 1999999987, + 'data': nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI() + }; + + const sign = web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY); + sign.then((signedTransaction) => { + + web3.eth.sendSignedTransaction(signedTransaction.rawTransaction, function(e, hash) { + if (!e) { + console.log("💾 Transaction hash: ", hash); + } else { + console.log("ERROR:", e) + } + }); + }).catch((e) => { + console.log("ERROR:", e); + }); +} + +mintNFT(METADATA_URL);