# week3 remix版 **Published by:** [mosheng](https://paragraph.com/@mscoin/) **Published on:** 2022-08-15 **URL:** https://paragraph.com/@mscoin/week3-remix ## Content 1.step1 将 Polygon Mumbai 添加到您的 Metamask 钱包 1.进入 mumbai.polygonscan.com 并向下滚动到页面底部。 您将看到 “添加多边形网络”按钮,单击它并确认您要将其添加到 Metamask。step2 获取免费的 Matic 以部署您的 NFT 智能合约获取 Test MATIC 非常简单,只需导航到以下水龙头之一:mumbaifaucet.comfaucet.polygon.technology (这个貌似一直有)将钱包地址复制到文本栏中,然后点击 “Send Me MATIC” : 参考原教程 一直到领到测试币 2.remix 新建 ChainBattles.sol 文件3.把代码复制到这个文件里// 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 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 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)); } } 编译5.小狐狸先切换到刚刚配置的matic的测试网络,然后切换到部署的选项卡,按图中的第二步切换到Injected 选项 然后按第三步的选择ChainBattles-Chain….sol 最后 单击 Deploy 部署合约 小狐狸确定交易6. 展开合约(部署合约的时候 把gasprice调高点 要不然可能不出来合约)7.先mint,mint之后 train函数输入1 调用一下复制合约地址 去opensea 查看https://testnets.opensea.io/ 9.提交opensea里你的nft链接 和 合约地址 ## Publication Information - [mosheng](https://paragraph.com/@mscoin/): Publication homepage - [All Posts](https://paragraph.com/@mscoin/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@mscoin): Subscribe to updates