# Alchemy第一周教程

By [xiaoge](https://paragraph.com/@xiaoge) · 2022-11-11

---

特别声明： 每周任务前，请参看 任务准备 ，无论此周任务需要用到多少！！！

准备工作
----

### 1\. 通过

[https://mirror.xyz/0x6e780c7DA5f8A26c64C72203042735305EC29F17/VVzmSF5pooV\_8ZhdczrLVJlGUYHs\_pvvrzw3-xcspy8](https://mirror.xyz/0x6e780c7DA5f8A26c64C72203042735305EC29F17/VVzmSF5pooV_8ZhdczrLVJlGUYHs_pvvrzw3-xcspy8)

### 的步骤，在 Alchemy 上创建一个 ETH Goerli测试网 的APP，并保留此页面

### 2\. 将Alchemy Goerli网络添加到Metamask钱包

1）点开钱包，选择网络，点击 添加网络

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

2）填写 Goerli 网络和 前面在Alchemy上生成的RPC URL 信息，并保存

![](https://storage.googleapis.com/papyrus_images/6a64bff4e20ad64b1749a087274f4317c905338b963ef307984be53aa7b3c2b7.png)

_记得将钱包切换到_ _Alchemy_ _Goerli测试网_

### 3\. 在 领水网站上，输入钱包地址领取测试GETH

![](https://storage.googleapis.com/papyrus_images/718c88f17d9a244399913c4a8883dd1436237d2761af87f0a90c49e16c6559c4.png)

4\. 在IPFS上上传NFT和元数据
-------------------

1）进入 [https://filebase.com/](https://filebase.com/) ，注册。登录后，单击左侧菜单 _Buckets_ 按钮，然后选 _Create Bucket_ ，创建一个新存储桶(自己取名字，重名不行)

![](https://storage.googleapis.com/papyrus_images/67bfc3a6eb93c58edb4b0ac1b40804d2d0aa9aa8f5feb8de2ca8311df7faaeac.png)

![](https://storage.googleapis.com/papyrus_images/39b615fab18a256af1939d55aa206c38504079e97e5ff1889deca238bbdaa740.png)

2）点击进入刚创建的Bucket，单击右上的 _Upload_ 按钮 ，然后上传您要用于 NFT 的图像

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

![](https://storage.googleapis.com/papyrus_images/604d45b90daef170689b2810d3cbfb701f82f27a0f512bbf3d5ca49805fc929e.png)

_a.可上传本地图片，或者网上下载的免费图片（ 比如_ [_https://www.pexels.com/zh-cn/_](https://www.pexels.com/zh-cn/) _）_

_b.上传时会显示进度条，等待完成后刷新页面_

3）单击名字，进入详细页面，复制 _IPFS 网关 URL_，暂时保留页面

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

4）转到电脑桌面，右键新建创建文本文档，重命名为“metadata.json”（连带后缀全改）,复制以下代码进去，保存

> _a. 如果看不到文件后缀，比如系统是win10，请自行百度或者谷歌搜索 win10如何显示文件后缀名 ，进行设置更改_
> 
> _b. 将上一步复制的URL，粘贴替换下面的 ”image“后面_

    { 
      "description": "This NFT proves I've created and deployed my first ERC721 smart contract on Goerli Testnet with Alchemy",
      "external_url": "Alchemy.com",
      "image": "https://ipfs.filebase.io/ipfs/QmPXigwDiUevneEJtgSDWt1GEm82TT42gw2D4WXChZyRZf",
      "name": "A Beautiful NFT", 
      "attributes": [
        {
          "trait_type": "Base", 
          "value": "Starfish"
        }, 
        {
          "trait_type": "Eyes", 
          "value": "Big"
        }, 
        {
          "trait_type": "Mouth", 
          "value": "Surprised"
        }, 
        {
          "trait_type": "Level", 
          "value": 5
        }, 
        {
          "trait_type": "Stamina", 
          "value": 1.4
        }, 
        {
          "trait_type": "Personality", 
          "value": "Sad"
        }, 
        {
          "display_type": "boost_number", 
          "trait_type": "Aqua Power", 
          "value": 40
        }, 
        {
          "display_type": "boost_percentage", 
          "trait_type": "Stamina Increase", 
          "value": 10
        }, 
        {
          "display_type": "number", 
          "trait_type": "Generation", 
          "value": 2
        }
      ]
    }
    

5）回到 Filebase ，退到上一页，回到图片的桶页面，然后再次点击 _Upload_ 进行上传，这次选择的是桌面的metadata.json

![](https://storage.googleapis.com/papyrus_images/2cdd4bdf6a160f2c958b6500f50301e1a6e6c687d96c4ad7024d7c8ea4cc1e1e.png)

6）上传成功后，复制一下metadata.json的 _CID_，后面需要用到

项目代码初始化
-------

### 1\. 进入 Open Zeppelin 智能合约向导页面，将看到以下编辑器

![](https://storage.googleapis.com/papyrus_images/89c5e2b7460f6a790a13ada1300c7c47c62d316c0702c9b48265fe91b0bcf023.png)

### 2\. 点击左上角 ERC721 按钮，输入 Name 和 Symbol

![](https://storage.googleapis.com/papyrus_images/4532b42c2bd68f1ed718d0f08bf5d470f709cf9cfe0845c981340ec5ea905cac.png)

### 3\. 勾选如下的NFT功能

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

### 4\. 在 OpenZeppelin Wizard 编辑器的顶部，点击“Open in Remix”按钮，在浏览器的新选项卡中打开 REMIX IDE

![](https://storage.googleapis.com/papyrus_images/6a68b3a379d3016e1c81d7978538f479e40d8cc4ca796a669f78fb87bfb0cfa5.png)

### 5\. 打开后，等一小会会自动跳到智能合约代码界面

![](https://storage.googleapis.com/papyrus_images/9598ede5784bbc174312aedae5316e1ea1bab2fe652c142816ec3c653a3116e4.png)

1）在第14行填入

      uint256 MAX_SUPPLY = 100000;
    

![](https://storage.googleapis.com/papyrus_images/134af556ef9583e29ef13ba144e18da37c14cb0b4a1bfc3e13bf272dcbbe8535.png)

2）删除17行的 onlyOwner ，否则只允许智能合约的所有者（部署智能合约的钱包地址）铸造 NFT

![](https://storage.googleapis.com/papyrus_images/77b8fb7ccc3453837d0a75fbb9051b13f421e9951fb1888393542b77d98ccf22.png)

3）在第19行添加

    require(_tokenIdCounter.current() <= MAX_SUPPLY, "I'm sorry we reached the cap");
    

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

部署智能合约
------

### 1\. 回到 Remix，点击页面左侧的Solidity compiler，版本选0.8.4，勾选Auto compile，然后点击“Compile”按钮

![](https://storage.googleapis.com/papyrus_images/8307e76081c844b4a976b18b2572bbe19d81852cc474cfee185329ba12176cb5.png)

### 2\. 左边栏点击“Deploy & Run Transactions”菜单

> _Environment下拉菜单中选择 Injected provider- Metamask_
> 
> _contract选择 Alchemy-contracts 这个_
> 
> _点击 Deploy_

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

### 3\. 将出现 Metamask 弹出窗口，需支付一些 Gas 费用，单击“确认”

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

### 4\. 如果一切都按预期工作，大约20 秒后，您应该会在左下的 Deployed Contracts 处看到该合约

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

> _若部署合约出现下面类似的错，请检查钱包的网络是否选对_
> 
> {
> 
> "code": -32000,
> 
> "message": "execution reverted"
> 
> }

测试智能合约功能
--------

### 1\. 点开合约

![](https://storage.googleapis.com/papyrus_images/45213e01e65f413de11b5e97d7bdd997dc04cb4044d390333aaf91195cad9f8f.png)

### 2\. 点击 safeMint 下拉框

> _to 填入 你的钱包地址_
> 
> _uri 填入_ _ipfs://准备工作部分metadata.json的CID_

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

1）点击transact，会花一点手续费，等一会成功了会在右侧显示绿勾

![](https://storage.googleapis.com/papyrus_images/3268ab484c5a46ca159608bcb9876f5fae191a49758002a2b6842e2a263814d4.png)

### 3\. 将钱包地址复制并粘贴到 balanceOf 方法输入中点击call，它应该显示您有 1 个 NFT

![](https://storage.googleapis.com/papyrus_images/6f5e6aea29884d0503dc0e73fe55fa8248f1e88022697dd5057b218163228aec.png)

### 4\. 在tokenUri 插入“0”作为 id 参数，点击call，它应该显示你的 tokenURI

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

展示NFT
-----

1\. 进入

[https://goerli.pixxiti.com/](https://goerli.pixxiti.com/)

或者

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

，登录自己钱包，然后单击 _View your NFTs_ ，您应该会在那里看到您新铸造的 NFT

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

1）红圈图片为刚mint的图片

![](https://storage.googleapis.com/papyrus_images/65c88c0e7117660c3fdc1004b1e14b46554fcdcdde5d4bf9876a2e9b11da6dbb.png)

### 2\. 如果图像尚不可见，点击进去，然后单击右下的“Refresh”按钮，大约需要几分钟

![](https://storage.googleapis.com/papyrus_images/7a095782eb36e8bc297b8b8b76e3d40a997e7912f00bce6df83facb01c71faa7.png)

额外挑战（可选做）
---------

> _挑战课题： 如何限制每个钱包的 mint 数量（11月初好像必须要做挑战任务了）_

### 1\. 在REMIX的代码中加入以下内容

1）在 uint256 MAX\_SUPPLY = 10000; 下面添加：

    uint256 walletLimit = 2; //limit for per wallet
    mapping(address => uint) public walletMints;
    

2）在 require(tokenId <= MAX\_SUPPLY, "I'm sorry we reached the cap"); 下面添加：

    require(walletMints[msg.sender] < walletLimit, "Max Mint per wallet reached");
    walletMints[msg.sender]++;
    

如图：

![](https://storage.googleapis.com/papyrus_images/31294666b5c6c4763ba8c5d60c3cfa7b725dee117097120b4f65156ddb489962.png)

> _上面 walletLimt 的值可以自行修改，后面测试符合此值即可_

### 2\. 按 部署智能合约 流程以及 测试智能合约功能 的 safeMint 流程操作

1）此处测试需结合上一步设置的 walletLimit 数量。比如设置为2，在to地址和url不改变的情况下，当连续mint成功两次后，查看Metamask上的活动

![](https://storage.googleapis.com/papyrus_images/0197a748f498ba85ada38875469b9519afa5aae4a3feffed9f7853fcbcf3e72b.png)

2）第三次点transact就会出现以下错误（红框处和代码中的提醒一致）

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

> 如何证明完成了额外挑战：
> 
> _a. 给出合约地址，即可通过区块浏览器查看到进行了多少次safe mint_
> 
> _b. 将设置了walletLimit数量的代码、合约中几次safe mint、以及最后一次出错的几个截图发送到twitter上，并将此twitter链接填在提交表格中的 ”Share the URL of your reflection!“处_

提交任务
----

10月末提交任务需要在官方DC服务器写小作文了

---

*Originally published on [xiaoge](https://paragraph.com/@xiaoge/alchemy)*
