我正在尝试用安全帽实施智能合约。从hardhat文档中,我看到我们可以使用以下命令来部署合约 -
npx hardhat run scripts/deploy.js
在scripts/deploy.js中,
const { ethers } = require("hardhat");
async function main() {
const Referral = await ethers.getContractFactory("Referral");
const referral = await Referral.deploy("0", "1", "2", "3");
let contractAddress = referral.address;
console.log("Contract deployed to address:", contractAddress);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
我想将合约部署在 Node.js 中某处的触发器上,并将合约地址存储在某个存储中。但是使用上面的命令,我会在终端中获得输出,并且必须使用nodejs的
exec
将其提取出来。我想在scripts/deploy.js 中有一个函数,我可以用它来部署合约,并可以从nodejs 中的某个地方调用它。此外,它还有助于将一些参数传递给 Referral.deploy
将接受 0, 1, 2, 3... 的函数
我对 web3 生态系统非常陌生,并试图看看上面的事情是否可以实现。
要使用param进行调用,请将其提取到函数中,例如:
const { ethers } = require("hardhat");
async function deployContract(param1, param2, param3, param4) {
const Referral = await ethers.getContractFactory("Referral");
const referral = await Referral.deploy(param1, param2, param3, param4);
return referral.address;
}
module.exports = deployContract;
然后从其他地方调用它:
const deployContract = require("./scripts/deploy");
async function main() {
const contractAddress = await deployContract("0", "1", "2", "3");
console.log("Contract deployed to address:", contractAddress);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
如果以太坊IDE能够提供高质量的部署并且你也可以在本地网络上测试它,那么为什么你要实施安全帽呢?