在 Moralis 分叉测试网上运行 npx hardhat test --network local 或 npx hardhat test --network kovan 时出现未经授权的错误

Posted

技术标签:

【中文标题】在 Moralis 分叉测试网上运行 npx hardhat test --network local 或 npx hardhat test --network kovan 时出现未经授权的错误【英文标题】:Unauthorized error when running npx hardhat test --network local or npx hardhat test --network kovan on Moralis forked testnet 【发布时间】:2022-01-05 05:43:35 【问题描述】:

我正在尝试学习如何使用 Moralis 配置安全帽,我编写了一个智能合约,现在我想对其进行测试。 我成功地分叉了一个 Moralis Kovan 网络和一个本地网络,我让它在我的计算机上运行。 现在我想在 Moralis 的分叉 kovan 或本地网络上运行一些测试,但它们似乎都不起作用。

运行 npx hardhat test --network local 或 npx hardhat test --network kovan 时出现以下错误:HardhatError: HH110: Invalid JSON-RPC response received: "error":"unauthorized"

这是我的 hardhat.config.js

require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-etherscan");

const ethers = require('ethers');

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.4",
  networks: 
    kovan: 
      url: "https://vqvzzpqhdvnc.usemoralis.com:2053/server",
      accounts: ["03c1bd1681bf0baf5f32c0e182a23d676d8e645df90ae97613f66b7a6d97a8a3"]
    ,
    local: 
      url: "https://7oqyifz8onne.usemoralis.com:2053/server",
      accounts: ["03c1bd1681bf0baf5f32c0e182a23d676d8e645df90ae97613f66b7a6d97a8a3"]
    
  ,
  etherscan: 
    apiKey: "9E45864SC7JZPH4F9U3MX9QXHE9EGXM2ZN"
  
;

const  expect, assert  = require("chai");
const  ethers  = require("hardhat");
const hre = require('hardhat');
const Moralis = require('moralis/node');

const NODE_URL = "https://speedy-nodes-nyc.moralis.io/6ee15640b2e5107f0e110d0e/eth/kovan";
const provider = new ethers.providers.JsonRpcProvider(NODE_URL);

const signer = provider.getSigner();

const serverUrl = "https://vqvzzpqhdvnc.usemoralis.com:2053/server";
const appId = "WApLAvxmeBmgniXlx50T6SvKMk4FomQHhvXKHQZj";
Moralis.start( serverUrl, appId );

describe("Greeter", function () 
  it("Should return the new greeting once it's changed", async function () 
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello, world!");
    await greeter.deployed();

    expect(await greeter.greet()).to.equal("Hello, world!");

    const setGreetingTx = await greeter.setGreeting("Hola, mundo!");

    // wait until the transaction is mined
    await setGreetingTx.wait();

    expect(await greeter.greet()).to.equal("Hola, mundo!");
  );
);

describe("UniSwap3", () => 
  let uniSwap3;

  beforeEach(async () => 
    const contractName = "UniSwap3";
    await hre.run("compile");
    const smartContract = await ethers.getContractFactory(contractName);
    uniSwap3 = await smartContract.deploy();
    await uniSwap3.deployed();
    console.log(`$contractName deployed to: $uniSwap3.address`);
  )

  it("should return version 1", async () => 
    const version = await uniSwap3.version();
    assert.equal(version, 1);
  );

  it("should return zero DAI balance", async () => 
    const daiBalance = uniSwap3.getDaiBalance();
    assert.equal(daiBalance, 0);
  )
)

【问题讨论】:

【参考方案1】:

尝试在 hardhat.config.js 中改用 Speedy Nodes

【讨论】:

以上是关于在 Moralis 分叉测试网上运行 npx hardhat test --network local 或 npx hardhat test --network kovan 时出现未经授权的错误的主要内容,如果未能解决你的问题,请参考以下文章

Moralis 克隆 dapp 抛出错误 (ReactJS)

捕获交易拒绝-moralis

来自 junit 测试的“分叉 Java VM 异常退出”错误

来自 junit 测试的“分叉 Java VM 异常退出”错误

未处理的拒绝(TypeError):moralis__WEBPACK_IMPORTED_MODULE_1__.Moralis.start 不是函数

在调试模式下运行 NPX 命令