Solidity

分叉時未找到合約,但存在於遠端網路上

  • October 30, 2022

我在本地項目中創建了多邊形孟買網路,這樣我就可以通過編寫測試來解決 Ethernaut 挑戰(以提高我對 ethers.js、測試和解決問題等的理解)

我認為分叉網路會創建一個與原始狀態完全相同的複製,但我似乎無法證實這一點……

我用 hardhat.config.js 做這個:

networks: {
   hardhat: {
     accounts,
     forking: {
       url: "https://polygon-mumbai.g.alchemy.com/v2/<key>",
       blockNumber: 0,
     },
   },
}

我通過執行在本地執行分叉:

npx hardhat node --fork https://polygon-mumbai.g.alchemy.com/v2/<key> --port 7545 --verbose

我有 ethernaut ABI,然後是 ethernaut 地址,然後是我的 before 鉤子:


const ETHERNAUT_ABI = [
 {
   anonymous: false,
   inputs: [
     {
       indexed: true,
       internalType: "address",
       name: "player",
       type: "address",
     },
     {
       indexed: false,
       internalType: "contract Level",
       name: "level",
       type: "address",
     },
   ],
   name: "LevelCompletedLog",
   type: "event",
   signature:
     "0x9dfdf7e3e630f506a3dfe38cdbe34e196353364235df33e5a3b588488d9a1e78",
 },
 {
   anonymous: false,
   inputs: [
     {
       indexed: true,
       internalType: "address",
       name: "player",
       type: "address",
     },
     {
       indexed: false,
       internalType: "address",
       name: "instance",
       type: "address",
     },
   ],
   name: "LevelInstanceCreatedLog",
   type: "event",
   signature:
     "0x7bf7f1ed7f75e83b76de0ff139966989aff81cb85aac26469c18978d86aac1c2",
 },
 {
   anonymous: false,
   inputs: [
     {
       indexed: true,
       internalType: "address",
       name: "previousOwner",
       type: "address",
     },
     {
       indexed: true,
       internalType: "address",
       name: "newOwner",
       type: "address",
     },
   ],
   name: "OwnershipTransferred",
   type: "event",
   signature:
     "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0",
 },
 {
   inputs: [
     {
       internalType: "contract Level",
       name: "_level",
       type: "address",
     },
   ],
   name: "createLevelInstance",
   outputs: [],
   stateMutability: "payable",
   type: "function",
   payable: true,
   signature: "0xdfc86b17",
 },
 {
   inputs: [],
   name: "owner",
   outputs: [
     {
       internalType: "address",
       name: "",
       type: "address",
     },
   ],
   stateMutability: "view",
   type: "function",
   constant: true,
   signature: "0x8da5cb5b",
 },
 {
   inputs: [
     {
       internalType: "contract Level",
       name: "_level",
       type: "address",
     },
   ],
   name: "registerLevel",
   outputs: [],
   stateMutability: "nonpayable",
   type: "function",
   signature: "0x202023d4",
 },
 {
   inputs: [],
   name: "renounceOwnership",
   outputs: [],
   stateMutability: "nonpayable",
   type: "function",
   signature: "0x715018a6",
 },
 {
   inputs: [
     {
       internalType: "address payable",
       name: "_instance",
       type: "address",
     },
   ],
   name: "submitLevelInstance",
   outputs: [],
   stateMutability: "nonpayable",
   type: "function",
   signature: "0xc882d7c2",
 },
 {
   inputs: [
     {
       internalType: "address",
       name: "newOwner",
       type: "address",
     },
   ],
   name: "transferOwnership",
   outputs: [],
   stateMutability: "nonpayable",
   type: "function",
   signature: "0xf2fde38b",
 },
];

ETHERNAUT_ADDRESS = `0xe04f955e4Cf9858F8f8d60C09aBf16DF23D4672b`;

before(async () => {
 const ethernaut = await ethers.getContractAt(
   ETHERNAUT_ABI,
   ETHERNAUT_ADDRESS
 );
}

但是,我收到錯誤:

0xe04f955e4Cf9858F8f8d60C09aBf16DF23D4672b is not a contract account.

這個地址肯定是一個合約賬戶,因為它是孟買的 Ethernaut 合約,如下所示:

https://mumbai.polygonscan.com/address/0xe04f955e4Cf9858F8f8d60C09aBf16DF23D4672b

很明顯,這是 Mumbai testnet 上的合約賬戶,那為什麼不在我的 Mumbai testnet 分支上呢?

當你寫:

     forking: {
       url: "https://polygon-mumbai.g.alchemy.com/v2/<key>",
       blockNumber: 0,
     },

blockNumber: 0意味著你在區塊號0docs)分叉,所以當然契約還沒有部署。

我相信刪除它應該可以解決您的問題:

     forking: {
       url: "https://polygon-mumbai.g.alchemy.com/v2/<key>",
     },

引用自:https://ethereum.stackexchange.com/questions/138424