我正在开发两个智能合约,它们的部署顺序很重要。首先是一个ERC20令牌,然后将其部署地址传递给ERC721智能合约的构造函数。两个联系人的互动已在Hardhat上成功测试。然而,在学习Hardhat教程时,我试图使用以下脚本将这些智能合约部署到RSK regtest本地节点:
const totalSuply = 1e6;
async function main() {
const ERC20 = await ethers.getContractFactory('ERC20');
const erc20 = await ERC20.deploy(totalSuply);
const ERC721 = await ethers.getContractFactory('ERC721');
const erc721 = await ERC721.deploy(erc20.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
我收到以下错误:
Error:
transaction failed
[ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ](transactionHash="0xac83bdd94bf43eaec2e930497aa9a637883ad870749e5ee14cf8ba4ff332a810",
transaction {
"hash":"0xac83bdd94bf43eaec2e930497aa9a637883ad870749e5ee14cf8ba4ff332a810",
"type":0,
"accessList":null,
"blockHash":null,
"blockNumber":null,
"transactionIndex":null,
"confirmations":0,
"from":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
"gasPrice":{"type":"BigNumber","hex":"0x00"},
"gasLimit":{"type":"BigNumber","hex":"0x1204a5"},
"to":null,"value":{"type":"BigNumber","hex":"0x00"},
"nonce":736,"data":...
根据指定的链接,最有可能的原因是:
如果您未能等到部署合约,则可能会发生这种情况。
既然我已经在await
-等待部署,这怎么可能呢?有人在通过Hardhat在RSK上部署智能合约时遇到过类似的问题吗?
作为参考,这是我正在使用的hardhat.config.js
文件:
require('@nomiclabs/hardhat-waffle');
module.exports = {
solidity: '0.8.1',
defaultNetwork: 'rskregtest',
networks: {
hardhat: {},
rskregtest: {
url: 'http://localhost:4444',
},
},
};
问题是您没有等待部署完成。根据Hardhat文档,您应该按照以下部署智能合约
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
具体地说,.deploy()
后面必须跟.deployed()
,并且需要同时跟await
。修改您的部署脚本如下所示:
const totalSuply = 1e6;
async function main() {
const ERC20 = await ethers.getContractFactory('ERC20');
const erc20 = await ERC20.deploy(totalSuply);
await erc20.deployed(); // here
const ERC721 = await ethers.getContractFactory('ERC721');
const erc721 = await ERC721.deploy(erc20.address);
await erc721.deployed(); // and here
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});