如果您想从视频中学习,我们的 YouTube 上有本教程的录音。单击下面的屏幕截图观看视频,或继续阅读教程!
https://www.youtube.com/watch?v=uwnAXAsd428
为了构建智能合约,我们将使用Hardhat。Hardhat 是专为全栈开发而设计的以太坊开发环境和框架。简单来说,您可以编写智能合约、部署它们、运行测试和调试代码。
mkdir NFT-Tutorial
cd NFT-Tutorial
npm init --yes
npm install --save-dev hardhat
npx hardhat
现在你有一个Hardhat项目准备好了!
如果您不在 Mac 上,请执行此额外步骤并安装这些库:)
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
让我们安装 Open Zeppelin 合约,在终端窗口中执行这个命令
npm install @openzeppelin/contracts
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Import the openzepplin contracts
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// GameItem is ERC721 signifies that the contract we are creating imports ERC721 and follows ERC721 contract from openzeppelin
contract GameItem is ERC721 {
constructor() ERC721("GameItem", "ITM") {
// mint an NFT to yourself
_mint(msg.sender, 1);
}
}
npx hardhat compile
// Import ethers from Hardhat package
const { ethers } = require("hardhat");
async function main() {
/*
A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts,
so nftContract here is a factory for instances of our GameItem contract.
*/
const nftContract = await ethers.getContractFactory("GameItem");
// here we deploy the contract
const deployedNFTContract = await nftContract.deploy();
// print the address of the deployed contract
console.log("NFT Contract Address:", deployedNFTContract.address);
}
// Call the main function and catch if there is any error
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
# Go to https://www.alchemyapi.io, sign up, create
# a new App in its dashboard and select the network as Rinkeby, and replace "add-the-alchemy-key-url-here" with its key url
ALCHEMY_API_KEY_URL="add-the-alchemy-key-url-here"
# Replace this private key with your RINKEBY account private key
# To export your private key from Metamask, open Metamask and
# go to Account Details > Export Private Key
# Be aware of NEVER putting real Ether into testing accounts
RINKEBY_PRIVATE_KEY="add-the-rinkeby-private-key-here"
您可以将 Alchemy 视为区块链的 AWS EC2。它是一个节点提供者。它通过为我们提供节点来帮助我们与区块链连接,以便我们可以读取和写入区块链。Alchemy 帮助我们将合约部署到 rinkeby。
npm install dotenv
require("@nomiclabs/hardhat-waffle");
require("dotenv").config({ path: ".env" });
const ALCHEMY_API_KEY_URL = process.env.ALCHEMY_API_KEY_URL;
const RINKEBY_PRIVATE_KEY = process.env.RINKEBY_PRIVATE_KEY;
module.exports = {
solidity: "0.8.4",
networks: {
rinkeby: {
url: ALCHEMY_API_KEY_URL,
accounts: [RINKEBY_PRIVATE_KEY],
},
},
};
npx hardhat run scripts/deploy.js --network rinkeby
[1] 4 级: https://github.com/LearnWeb3DAO/Crypto-Wallets
[2] 下载: https://nodejs.org/en/download/
[3] Openzeppelin 的 ERC721 Contract: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol
[4] www.rinkebyfaucet.com/: http://www.rinkebyfaucet.com/
[5] Rinkeby Etherscan: https://rinkeby.etherscan.io/
留言与评论(共有 0 条评论) “” |