Solidity

如何更改使用乙太幣和安全帽本地網路部署智能合約的地址?

  • September 1, 2022

我想更改使用乙太幣在本地安全帽網路上部署契約的帳戶。目前程式碼:

const main = async () => {
 const [owner, addr1, addr2] = await ethers.getSigners();
 addr1.connect(ethers.provider);
 const BFactory = await ethers.getContractFactory('B');
 const b = await BFactory.deploy({
   value: ethers.utils.parseEther('0.1'),
 });

 await b.deployed();

 console.log('Contract deployed to:', b.address);

 await b.onlyMe();
};

部署腳本: npx hardhat run scripts/deploy.ts

結果

Error: cannot alter JSON-RPC Signer connection (operation="connect", code=UNSUPPORTED_OPERATION, version=providers/5.6.8)

首先,指定您要使用的帳戶的私鑰,然後連接到合約以使用該錢包部署它。

const main = async () => {
  const signer = new ethers.Wallet(your_private_key_string);
  const BFactory = await ethers.getContractFactory('B');
  const b = await BFactory.connect(signer).deploy({
   value: ethers.utils.parseEther('0.1'),
  });

 await b.deployed();

 console.log('Contract deployed to:', b.address);

 await b.onlyMe();
}

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