# week3 remix版

By [mosheng](https://paragraph.com/@mscoin) · 2022-08-15

---

1.step1 将 Polygon Mumbai 添加到您的 Metamask 钱包

1.进入 [mumbai.polygonscan.com](https://mumbai.polygonscan.com/) 并向下滚动到页面底部。 您将看到 “添加多边形网络”按钮，单击它并确认您要将其添加到 Metamask。

### step2 获取免费的 Matic 以部署您的 NFT 智能合约

获取 Test MATIC 非常简单，只需导航到以下水龙头之一：

*   [mumbaifaucet.com](https://mumbaifaucet.com/)
    
*   [faucet.polygon.technology](https://faucet.polygon.technology/) （这个貌似一直有）
    

将钱包地址复制到文本栏中，然后点击 **“Send Me MATIC”** ：

参考原教程 一直到领到测试币

2.remix 新建 ChainBattles.sol 文件

![](https://storage.googleapis.com/papyrus_images/32bd8fd83309f6c0028fe48927b7eb409ba653efad0360bcf299fcfff2f9b76c.png)

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));
            }
    }
    

4.  编译
    

![](https://storage.googleapis.com/papyrus_images/4b3fae3588b453b3bab0790b18f0c813e327627e313ed141627baac2b81eb72d.png)

5.小狐狸先切换到刚刚配置的matic的测试网络，然后切换到部署的选项卡，按图中的第二步切换到Injected 选项 然后按第三步的选择ChainBattles-Chain….sol 最后 单击 Deploy 部署合约 小狐狸确定交易

![](https://storage.googleapis.com/papyrus_images/9ae2d8c0613b0999eb577a0fc66223264c6ad753ba70c8369e5722f8af8761e5.png)

6\. 展开合约（部署合约的时候 把gasprice调高点 要不然可能不出来合约）

![](https://storage.googleapis.com/papyrus_images/816978472904955e780ce0d282bd5f62d4324fda5d09f8006db100284e4b4fc0.png)

7.先mint，mint之后 train函数输入1 调用一下

![](https://storage.googleapis.com/papyrus_images/9bd4501d8c17cb3ce4d4372947ecddf5b3cf69bc5de3d9a2b4aec416d67fc77d.png)

8.  复制合约地址 去opensea 查看
    

[https://testnets.opensea.io/](https://testnets.opensea.io/)

9.提交opensea里你的nft链接 和 合约地址

---

*Originally published on [mosheng](https://paragraph.com/@mscoin/week3-remix)*
