# Alchemy第三周 **Published by:** [tuzi](https://paragraph.com/@binancezh-2/) **Published on:** 2022-08-10 **URL:** https://paragraph.com/@binancezh-2/alchemy-2 ## Content 管理员运行ubuntu 运行创新新文件夹mkdir ChainBattled cd ChainBattled 运行创建成功之后运行npm install @openzeppelin/contracts 运行后界面打开codecode . 删除这两个文件夹下的2个文件修改 hardhat.config.js 内容为:修改 hardhat.config.js 内容为:require("dotenv").config(); require("@nomiclabs/hardhat-waffle"); require("@nomiclabs/hardhat-etherscan"); module.exports = { solidity: "0.8.10", networks: { mumbai: { url: process.env.TESTNET_RPC, accounts: [process.env.PRIVATE_KEY] }, }, etherscan: { apiKey: process.env.POLYGONSCAN_API_KEY } }; 在 contracts 文件夹中,创建一个新文件并将其命名为“ChainBattles.sol” 文件内容为:// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/utils/Strings.sol"; import "@openzeppelin/contracts/utils/Base64.sol"; contract ChainBattles is ERC721URIStorage { using Strings for uint256; using Counters for Counters.Counter; Counters.Counter private _tokenIds; mapping(uint256 => uint256) public tokenIdToLevels; constructor() ERC721 ("Chain Battles", "CBTLS"){ } function generateCharacter(uint256 tokenId) public view returns(string memory){ bytes memory svg = abi.encodePacked( '<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350">', '<style>.base { fill: white; font-family: serif; font-size: 14px; }</style>', '<rect width="100%" height="100%" fill="black" />', '<text x="50%" y="40%" class="base" dominant-baseline="middle" text-anchor="middle">',"Warrior",'</text>', '<text x="50%" y="50%" class="base" dominant-baseline="middle" text-anchor="middle">', "Levels: ",getLevels(tokenId),'</text>', '</svg>' ); return string( abi.encodePacked( "data:image/svg+xml;base64,", Base64.encode(svg) )); } function getLevels(uint256 tokenId) public view returns (string memory){ uint256 levels = tokenIdToLevels[tokenId]; return levels.toString(); } function getTokenURI(uint256 tokenId) public view returns (string memory){ bytes memory dataURI = abi.encodePacked( '{', '"name": "Chain Battles #', tokenId.toString(), '",', '"description": "Battles on chain",', '"image": "', generateCharacter(tokenId), '"', '}' ); return string( abi.encodePacked( "data:application/json;base64,", Base64.encode(dataURI) ) ); } function mint() public { _tokenIds.increment(); uint256 newItemId = _tokenIds.current(); _safeMint(msg.sender, newItemId); tokenIdToLevels[newItemId] = 0; _setTokenURI(newItemId, getTokenURI(newItemId)); } function train(uint256 tokenId) public { require(_exists(tokenId)); require(ownerOf(tokenId) == msg.sender, "You must own this token to train it"); uint256 currentLevel = tokenIdToLevels[tokenId]; tokenIdToLevels[tokenId] = currentLevel + 1; _setTokenURI(tokenId, getTokenURI(tokenId)); } } 运行touch .env 在code上修改.env内容为:TESTNET_RPC="" PRIVATE_KEY="" POLYGONSCAN_API_KEY="" 导航到alchemy.com并创建一个新的 Polygon Mumbai 应用程序,复制 API HTTP URL,并将 API 作为“ TESTNET_RPC ”PRIVATE_KEY为小狐狸私钥(新建账号) POLYGONSCAN_API_KEY在下面网址中注册账号 polygonscan.com 注册后在新增key并复制填写在code中。修改后如下图:运行npx hardhat compile 如出现下面报错,就运行,没遇到请忽略npm install dotenv npm install @nomiclabs/hardhat-waffle 运行成功npx hardhat compile后在scripts文件夹内新建文件 deploy.js内容为:const main = async () => { try { const nftContractFactory = await hre.ethers.getContractFactory( "ChainBattles" ); const nftContract = await nftContractFactory.deploy(); await nftContract.deployed(); console.log("Contract deployed to:", nftContract.address); process.exit(0); } catch (error) { console.log(error); process.exit(1); } }; main(); 保存后运行npx hardhat run scripts/deploy.js --network mumbai 运行成功后可以看到一个合约代码去mumbai.polygonscan.com搜索这个合约代码。 进入您的智能合约广告后,点击“标签”标签。 你会注意到不正常:回到终端运行验证码npx hardhat verify --network mumbai YOUR_SMARTCONTRACT_ADDRESS 通过仿制与您的智能合约现在智能合约已经通过验证,mumbai.polygonscan.com 将在其附近显示一个小项目: 要用作使用,请并使用**第一个 FT 品牌的“第一次承包商”的“N 品牌”**,然后单击“连接到 Web3”然后“min​​​t”函数并点击写:这将显示一个弹出窗口,要求您支付毒气,点击个性化按钮。 恭喜!您刚刚开始制造您的第一个动态 NFT - 让我们到 OpenSea 测试网来现场查看它。在 OpenSea 上查看您的动态 NFT复制智能合约地址,前往opensea.com,并将其部署到街头搜索。如果一切正常,您现在应该会看到 NFT 显示在 OpenSea 上,其中包含动态图像、标题和描述。 让我们回到终端。更新动态 NFT 图像训练 NFT导航回孟买.polygonscan.com,点击合同标签 > 写合同并寻找“火车”功能。 插入您的 NFT 的 ID - 这种情况下为“1”,因为我们只生成了一个,然后**点击写入:**然后回到testnets.opensea.com并刷新页面:完成任务填表。 重新刷新ft合同链接就可以了 alchemyapi.typeform.com/roadtoweekthree ## Publication Information - [tuzi](https://paragraph.com/@binancezh-2/): Publication homepage - [All Posts](https://paragraph.com/@binancezh-2/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@binancezh-2): Subscribe to updates - [Twitter](https://twitter.com/tututuzi11): Follow on Twitter