跳转到主要内容
本指南介绍如何使用 Hardhat 在 Plasma 兼容 EVM 的测试网上搭建环境、部署合约并与之交互。本文假设你已对 Ethereum 工具链和智能合约开发有基本了解。

前置条件

  • Node.js 20+
  • Ethereum 开发的基本知识
  • 浏览器钱包

环境搭建

使用 Hardhat 构建和部署合约。

Hardhat 设置

  1. 创建一个新的项目目录并初始化:
    mkdir my-plasma-project
    cd my-plasma-project
    npm init -y
    npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
    npm install dotenv
    npx hardhat init
    
  2. 按提示创建 JavaScript 项目。可以将项目保存在任意位置。在本快速开始中,你可能不需要 .gitignore 文件。
  3. 配置 hardhat.config.js 以使用 Plasma 测试网:
    require("@nomicfoundation/hardhat-toolbox");
    require("dotenv").config();
    
    /** @type import('hardhat/config').HardhatUserConfig */
    module.exports = {
      solidity: "0.8.28",
      networks: {
        plasmaTestnet: {
          url: process.env.RPC_ENDPOINT,
          chainId: 9746,
          accounts: [process.env.PRIVATE_KEY] // We'll set this up next.
        }
      }
    };
    

钱包与测试网设置

将 Plasma Testnet 添加到浏览器钱包

  1. 打开浏览器钱包(MetaMask、Trust Wallet 或 Rabby)并点击网络选择器。
  2. 选择添加自定义网络的选项(例如 Add NetworkAdd manuallyAdd custom network)。
  3. 输入以下信息:
    • Network Name:Plasma Testnet
    • New RPC URLhttps://testnet-rpc.plasma.to
    • Chain ID9746
    • Currency SymbolXPL
    • Block Explorer URLhttps://testnet.plasmascan.to
  4. 保存该网络,并在钱包中切换到 Plasma Testnet。

获取测试网代币

  1. 从浏览器钱包中复制你的钱包地址。
  2. 访问 openfaucet.org
  3. 输入地址并申请测试网代币。
  4. 等待交易完成(应在几秒内出现在钱包中)。

设置环境变量

  1. 使用浏览器钱包的账户设置导出你账户的私钥。
  2. 在项目根目录中创建 .env 文件:
    PRIVATE_KEY=your_private_key_here
    RPC_ENDPOINT=https://testnet-rpc.plasma.to
    
切勿将私钥或助记词提交到版本控制系统。请将 .env 添加到 .gitignore 文件中。

编写一个简单的智能合约

  1. 创建一个基础的存储合约 contracts/SimpleStorage.sol
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.28;
    
    contract SimpleStorage {
        string private storedData;
        
        event DataStored(string data);
        
        constructor() {
            storedData = "Hello, Plasma!";
        }
        
        function setData(string memory data) public {
            storedData = data;
            emit DataStored(data);
        }
        
        function getData() public view returns (string memory) {
            return storedData;
        }
    }
    
  2. 编译合约:
    npx hardhat compile
    
    输出类似如下:
    Downloading compiler 0.8.28
    Downloading compiler 0.8.28
    Compiled 2 Solidity files successfully (evm target: paris).
    

测试

  1. 创建测试脚本 test/SimpleStorage.js
    const { expect } = require("chai");
    
    describe("SimpleStorage", function () {
      let simpleStorage;
    
      beforeEach(async function () {
        const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
        simpleStorage = await SimpleStorage.deploy();
        await simpleStorage.waitForDeployment();
      });
    
      it("Should return the initial greeting", async function () {
        expect(await simpleStorage.getData()).to.equal("Hello, Plasma!");
      });
    
      it("Should update the stored data", async function () {
        await simpleStorage.setData("Updated data");
        expect(await simpleStorage.getData()).to.equal("Updated data");
      });
    });
    
  2. 运行测试:
    npx hardhat test
    
    输出类似如下:
    Lock
      Deployment
        ✔ Should set the right unlockTime (454ms)
        ✔ Should set the right owner
        ✔ Should receive and store the funds to lock
        ✔ Should fail if the unlockTime is not in the future
      Withdrawals
        Validations
          ✔ Should revert with the right error if called too soon
          ✔ Should revert with the right error if called from another account
          ✔ Shouldn't fail if the unlockTime has arrived and the owner calls it
        Events
          ✔ Should emit an event on withdrawals
        Transfers
          ✔ Should transfer the funds to the owner
    
    SimpleStorage
      ✔ Should return the initial greeting
      ✔ Should update the stored data
    
    
    11 passing (541ms)
    

部署到 Plasma Testnet

  1. 先创建一个 scripts 目录:
    mkdir scripts
    
  2. 创建部署脚本 scripts/deploy.js
    async function main() {
      const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
      
      console.log("Deploying SimpleStorage...");
      const simpleStorage = await SimpleStorage.deploy();
      await simpleStorage.waitForDeployment();
      
      const address = await simpleStorage.getAddress();
      console.log("SimpleStorage deployed to:", address);
    }
    
    main()
      .then(() => process.exit(0))
      .catch((error) => {
        console.error(error);
        process.exit(1);
      });
    
  3. 部署合约:
    npx hardhat run scripts/deploy.js --network plasmaTestnet
    
    输出类似如下:
    Deploying SimpleStorage...
    SimpleStorage deployed to: 0xaa4FB2A8eD6953A4e57b8097BAf048d7919e6262
    
  4. 记下 deployed to: 后面的地址。这就是你部署在 Plasma 测试网上的合约地址。

验证部署

  1. 复制合约地址。
  2. 访问 Plasma 测试网区块浏览器
  3. 搜索你的合约地址,查看部署交易。

故障排查

问题解决方案
Gas 估算错误确保钱包有足够的 XPL
网络连接错误检查 RPC URL 和测试网可用性
私钥错误确保 .env 配置正确(如有要求,不要带 0x 前缀)
浏览器延迟稍等几分钟;浏览器可能正在同步