簡體   English   中英

通過地址獲取已部署的合約

[英]Get deployed contract by address

根據這個文檔。 我們可以使用ethers.getContract來獲取已部署的合約。

我已經在0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2部署了我的合同,我的創建者地址是0x6e0F5B57FEdc8911722c92dcD5D7D0cf69ceA385現在我正在做的合同

deployedContract = await ethers.getContract(
                              "0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2",
                              "0x6e0F5B57FEdc8911722c92dcD5D7D0cf69ceA385"
                              )

但是它的拋出錯誤

 Error: No Contract deployed with name 0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2

但是你可以看到合約已部署https://goerli.etherscan.io/address/0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2

有人可以幫我嗎?

0x33F4...合約部署在Goerli tes.net,僅在this.network可用。 您的帖子沒有 state 連接到 which.network 是您的 ethers provider連接的,但基於上下文我假設它是一個不同的.network - 例如本地模擬的hardhat .network(如果您不這樣做,這是默認選項t 指定任何提供商)或以太坊 mai.net。

getContract() function 僅存在於ethers package 的歷史版本中。根據您鏈接的文檔,您似乎使用的是0.0.1版(而且它似乎不是文檔頁面的錯誤配置,因為0.0.1 版本實際上存在)。 package 的當前版本(2023 年 1 月)是5.4 - 您可以在https://docs.ethers.io/v5/找到它的文檔。

最初鏈接的文檔中的第二個參數是contractInterface 這不是部署者地址(在您的示例中傳遞)而是 ABI(應用程序二進制接口)——合同及其事件的publicexternal方法的 JSON 格式規范。 此類 ABI JSON 的示例可以在Example標題下方的原始文檔中找到。

每個合約通常都有自己獨特的 ABI,這是在合約編譯期間從其源代碼生成的,但您也可以使用通用 ABI 來實現標准化功能。 例如,所有 ERC-20 代幣合約都具有 ERC-20 標准所需的相同功能(這些功能包含在通用 ERC-20 ABI 中),此外它們還可能在此之上實現一些其他功能(這些自定義功能是不在通用 ABI 中)。


下面是一個使用當前版本(0.5.4)的ethers與 Goerli.network 提供商連接的合約交互的示例:

const { ethers } = require("ethers");
// A 3rd party provider that is connected to the Goerli network
const provider = new ethers.providers.JsonRpcProvider("https://goerli.infura.io/v3/<your_api_key>");

const CONTRACT_ADDRESS = "0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2";
// ABI JSON of this specific contract, not included in the answer for readability
const CONTRACT_ABI = [/* ... */];

async function run() {
    const myContract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider);
    // `getEntryFee()` function defined in the ABI JSON as well as in the actual contract
    const entryFee = await myContract.getEntryFee();
    console.log(entryFee.toString());
}

run();

注意:以上是未使用 Hardhat 框架的獨立 NodeJS 腳本。 您還可以在 Hardhat 配置文件中添加 the.network,然后從他們的scripts文件夾中運行一個腳本,該腳本會自動包含ethers並連接到選定的提供商。

require("@nomicfoundation/hardhat-toolbox");

module.exports = {
    solidity: "0.8.17",
    networks: {
        goerli: {
            url: "https://goerli.infura.io/v3/<your_api_key>",
            accounts: [<private_key>, <private_key>], // for sending transactions
        }
    }
};

npx hardhat run --network goerli scripts/myScript.js

這很簡單。 您可以使用getContractAt function。

getContractAt: <T extends ethers.Contract>(
  nameOrAbi: string | any[],
  address: string,
  signer?: ethers.Signer | string
) => Promise<T>;

所以在你的情況下,它可能是:

const raffle = await ethers.getcontractAt(
  "Raffle",
  "0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2",
  "0x6e0F5B57FEdc8911722c92dcD5D7D0cf69ceA385"
);

此外,由於您使用合同地址作為合同名稱,因此出現錯誤。

  getContract: <T extends ethers.Contract>(
    name: string,
    signer?: ethers.Signer | string
  ) => Promise<T>;

將此添加到hardhat.config.js

networks: {
    goerli: {
      url: `https://goerli.infura.io/v3/${process.env.INFURA_API_KEY}`,
      accounts: [process.env.PRI_KEY],
    },
  },

create.env 並添加你的環境變量

// I am loading env variables 
env $(cat .env) npx hardhat run --network goerli scripts/yourScriptFile.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM