# BUILDSPACE第一课

By [Coinman.eth](https://paragraph.com/@coinman-eth) · 2022-11-27

---

大家好，我是帝哥（推特：@CoinmanLabs），今天我们来介绍一个新项目，其实帝哥曾把这个buildspace的全部课程都做了一遍，也可以说从中学到了很多东西，只不过帝哥做的时候是没有NFT奖励的，今天帝哥就带着大家一起从头再来做一次吧。

首先我们看下项目的介绍：

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

那就话不多说，直接开始上干货了，首先考虑到很多同学都不是程序员出身，没有专门的编译器，我们这里就直接使用replit了。

[https://replit.com/~](https://replit.com/~)

去到buildspace的网站开始我们的任务，可以直接使用谷歌邮箱登录。

[https://buildspace.so/](https://buildspace.so/)

我们登录后可以看到首页面是这样的，已经开始和可以做的。那我们就从第一个solidity的开始吧。

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

step1:开始编码

因为现在hardhat已经被普遍使用了，所以buildspace也是采用了他们，需要具备一个node的环境-这里就直接使用replit来进行编码。

step2:在replit新建一个项目

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

当项目环境准备完成后，帝哥在这里再次跟大家介绍下，我们replit的操作页面，后面就直接说在哪个区域操作了。

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

这里大家输入命令的时候一定要切换到shell区便于操作

    # 在shell区输入下面代码
    mkdir my-wave-portal
    cd my-wave-portal
    npm init -y
    npm install --save-dev hardhat@latest
    # 等待上面完成后，输入下面的命令
    npx hardhat
    # 再次等待完成
    npm install --save-dev chai @nomiclabs/hardhat-ethers ethers @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-chai-matchers
    

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

### 当执行完上面的命令，文件的目录应该是这样的，然后将hardhat.config.js用下面的文件替换

    require("@nomicfoundation/hardhat-toolbox");
    
    // This is a sample Hardhat task. To learn how to create your own go to
    // https://hardhat.org/guides/create-task.html
    task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
        const accounts = await hre.ethers.getSigners();
    
        for (const account of accounts) {
            console.log(account.address);
        }
    });
    
    // You need to export an object to set up your config
    // Go to https://hardhat.org/config/ to learn more
    
    /**
     * @type import('hardhat/config').HardhatUserConfig
     */
    module.exports = {
        solidity: "0.8.17",
    };
    

最后我们运行node命令和compile将结果输出

    npx hardhat node
    npx hardhat compile
    npx hardhat test
    

重点来了，我们第一步需要提交的就是将我们的test的结果截图后提交

![提交的结果](https://storage.googleapis.com/papyrus_images/3c7345dd3a1999977bf266bf3fec0fcd84f406b9dbd755e33a52892d69273fec.png)

提交的结果

当我们将结果提交后，将test文件夹下Lock.js、scripts的deploy.js、contracts下面的Lock.sol的文件都删除了。

step3:新建合约

在 contracts下面新建 `WavePortal.sol` 文件，将下面的代码写入文件里面

    // SPDX-License-Identifier: UNLICENSED
    
    pragma solidity ^0.8.17;
    
    import "hardhat/console.sol";
    
    contract WavePortal {
        constructor() {
            console.log("Yo yo, I am a contract and I am smart");
        }
    }
    

将代码的截图提交做了第二步的提交答案。

![第二步截图](https://storage.googleapis.com/papyrus_images/f948b832346df0b2ca97a586656c38571148a79a3665ad5f801437fd8ade3aa2.png)

第二步截图

step4.新建运行脚本

在test文件夹下新建一个run.js，输入下面的内容

    const hre = require("hardhat")
    const main = async () => {
      const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
      const waveContract = await waveContractFactory.deploy();
      await waveContract.deployed();
      console.log("Contract deployed to:", waveContract.address);
    };
    
    const runMain = async () => {
      try {
        await main();
        process.exit(0); // exit Node process without error
      } catch (error) {
        console.log(error);
        process.exit(1); // exit Node process while indicating 'Uncaught Fatal Exception' error
      }
      // Read more about Node exit ('process.exit(num)') status codes here: https://stackoverflow.com/a/47163396/7974948
    };
    
    runMain();
    

当我们完成后在shell区中输入下面的命令查看结果：

    npx hardhat run scripts/run.js
    

![第三步提交截图](https://storage.googleapis.com/papyrus_images/fc8a22f14c0453c88f85614370398df08a594593fb0442dc8eb6c73ea38c0840.png)

第三步提交截图

step5:修改合约

我们将合约替换为下面的内容

    // SPDX-License-Identifier: UNLICENSED
    
    pragma solidity ^0.8.17;
    
    import "hardhat/console.sol";
    
    contract WavePortal {
        uint256 totalWaves;
    
        constructor() {
            console.log("Yo yo, I am a contract and I am smart");
        }
    
        function wave() public {
            totalWaves += 1;
            console.log("%s has waved!", msg.sender);
        }
    
        function getTotalWaves() public view returns (uint256) {
            console.log("We have %d total waves!", totalWaves);
            return totalWaves;
        }
    }
    

同时将run.js修改为下面的内容

    const main = async () => {
      const [owner, randomPerson] = await hre.ethers.getSigners();
      const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
      const waveContract = await waveContractFactory.deploy();
      await waveContract.deployed();
    
      console.log("Contract deployed to:", waveContract.address);
      console.log("Contract deployed by:", owner.address);
    
      await waveContract.getTotalWaves();
    
      const waveTxn = await waveContract.wave();
      await waveTxn.wait();
    
      await waveContract.getTotalWaves();
    };
    
    const runMain = async () => {
      try {
        await main();
        process.exit(0);
      } catch (error) {
        console.log(error);
        process.exit(1);
      }
    };
    
    runMain();
    

再次运行上面的脚本执行命令

    npx hardhat run scripts/run.js
    

但是他在这一步给了我们类似一个小的挑战，我们来看看是啥？需要我们将收件人的地址存储在一个数据里面。

![挑战](https://storage.googleapis.com/papyrus_images/bfe36725330ccc5526821cefadef195ff70ced917aee92b14d439784980e825e.png)

挑战

所以我们修改后的合约就是这样的，

    // SPDX-License-Identifier: UNLICENSED
    
    pragma solidity ^0.8.17;
    
    import "hardhat/console.sol";
    
    contract WavePortal {
        uint256 totalWaves;
        address[] public waves;
    
        constructor() {
            console.log("Yo yo, I am a contract and I am smart");
        }
    
        function wave() public {
            totalWaves += 1;
            waves.push(msg.sender);
            console.log("%s has waved!", msg.sender);
        }
    
        function getTotalWaves() public view returns (uint256) {
            console.log("We have %d total waves!", totalWaves);
            return totalWaves;
        }
    }
    

    # 运行命令输出结果截图
    npx hardhat run scripts/run.js
    

![第四步截图](https://storage.googleapis.com/papyrus_images/2488b85dbf0c475c78b507e8e0f7e07dcd028578f7baf4bb3caf6c61fda26ac3.png)

第四步截图

step6.部署合约

我们在scripts新建一个deploy.js将下面的代码复制进去

    const main = async () => {
      const [deployer] = await hre.ethers.getSigners();
      const accountBalance = await deployer.getBalance();
    
      console.log("Deploying contracts with account: ", deployer.address);
      console.log("Account balance: ", accountBalance.toString());
    
      const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
      const waveContract = await waveContractFactory.deploy();
      await waveContract.deployed();
    
      console.log("WavePortal address: ", waveContract.address);
    };
    
    const runMain = async () => {
      try {
        await main();
        process.exit(0);
      } catch (error) {
        console.log(error);
        process.exit(1);
      }
    };
    
    runMain();
    

当粘贴完成后，运行下面的脚本截图，首先需要我们去新开一个shell

    cd my-wave-portal/
    npx hardhat node
    

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

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

然后在原来的shell输入下面的命令

    npx hardhat run scripts/deploy.js --network localhost
    

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

将上面的截图作为作业提交，这就是第一阶段创建一个后续了，关注帝哥为你第一时间带来最新的区块链动向。

---

*Originally published on [Coinman.eth](https://paragraph.com/@coinman-eth/buildspace)*
