javascript 我尝试在以太坊区块链上创建令牌,但每次运行代码时,它都会给我一个“ReferenceError”

eh57zj3b  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(213)

下面是我的代码:

const { expect } = require("chai");
const hre = require("hardhat");

describe("JaiToken contract", function() {

    let Token;
    let jaiToken;
    let owner;
    let addr1;
    let addr2;
    let tokenCap = 50000000;
    let tokenBlockReward = 20;

    beforeEach(async function () {

        Token = await ethers.getContractFactory("JaiToken");
        [owner, addr1, addr2] = await hre.ethers.getSigners();

        jaiToken = await Token.deploy(tokenCap, tokenBlockReward);
    });

    describe("Deployment", function () {
        it("Should set the right owner", async function () {
          expect(await jaiToken.owner()).to.equal(owner.address);
        });
     
        it("Should assign the total supply of tokens to the owner", async function () {
          const ownerBalance = await jaiToken.balanceOf(owner.address);
          expect(await jaiToken.totalSupply()).to.equal(ownerBalance);
        });
    
        it("Should set the max capped supply to the argument provided during deployment", async function () {
          const cap = await jaiToken.cap();
          expect(Number(hre.ethers.utils.formatEther(cap))).to.equal(tokenCap);
        });
    
        it("Should set the blockReward to the argument provided during deployment", async function () {
          const blockReward = await jaiToken.blockReward();
          expect(Number(hre.ethers.utils.formatEther(blockReward))).to.equal(tokenBlockReward);
        });
      });

      describe("Transactions", function () {
        it("Should transfer tokens between accounts", async function () {
          // Transfer 50 tokens from owner to addr1
          await jaiToken.transfer(addr1.address, 50);
          const addr1Balance = await jaiToken.balanceOf(addr1.address);
          expect(addr1Balance).to.equal(50);

          // Transfer 50 tokens from addr1 to addr2
          // We use .connect(signer) to send a transaction from another account
          await jaiToken.connect(addr1).transfer(addr2.address, 50);
          const addr2Balance = await jaiToken.balanceOf(addr2.address);
          expect(addr2Balance).to.equal(50);
        });

        it("Should fail if sender doesn't have enough tokens", async function () {
          const initialOwnerBalance = await jaiToken.balanceOf(owner.address);
          // Try to send 1 token from addr1 (0 tokens) to owner (1000000 tokens).
          // `require` will evaluate false and revert the transaction.
          await expect(
            jaiToken.connect(addr1).transfer(owner.address, 1)
          ).to.be.revertedWith("ERC20: transfer amount exceeds balance");

          // Owner balance shouldn't have changed.
          expect(await jaiToken.balanceOf(owner.address)).to.equal(
            initialOwnerBalance
          );
        });

        it("Should update balances after transfers", async function () {
          const initialOwnerBalance = await jaiToken.balanceOf(owner.address);

          // Transfer 100 tokens from owner to addr1.
          await jaiToken.transfer(addr1.address, 100);

          // Transfer another 50 tokens from owner to addr2.
          await jaiToken.transfer(addr2.address, 50);

          // Check balances.
          const finalOwnerBalance = await jaiToken.balanceOf(owner.address);
          expect(finalOwnerBalance).to.equal(initialOwnerBalance.sub(150));

          const addr1Balance = await jaiToken.balanceOf(addr1.address);
          expect(addr1Balance).to.equal(100);

          const addr2Balance = await jaiToken.balanceOf(addr2.address);
          expect(addr2Balance).to.equal(50);
      });
    });

  });

这是我的错误:

describe("JaiToken contract", function() {
^

ReferenceError: describe is not defined
    at Object.<anonymous> (/Users/owner/Desktop/HTML/JaiToken/Jai-Token/test/JaiToken.js:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.4.0

我期望运行的所有7个测试都将在终端中通过并 checkout ,但它抛出了描述错误。我多次查看了我的代码并研究了此错误,但无法找出错误所在。我正在使用VS代码作为我的编辑器,或者可能我没有下载正确的东西。此合同用于将功能应用到我的令牌,如设置余额、上限、确定是Owner,并为在区块链上添加交易的人设置区块奖励。我也已经检查了这个文件在测试文件中,这些都是我正在运行的测试。请帮助!

soat7uwm

soat7uwm1#

您当前正在使用 const hre = require(“hardhat”) 导入 hardhat 库,但您根本没有导入ethers库。要修复此错误,您可以尝试在文件顶部的任何其他代码之前添加以下代码行:

const ethers = require("ethers");

相关问题