我必须用json格式的部署最新ABI更新我的前端文件。我在部署脚本中使用以下代码(在部署原始智能契约之后运行)
import { DeployFunction } from "hardhat-deploy/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import fs from "fs";
const updateUI: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { network, ethers } = hre;
console.log("Writing to front end...");
//Updating ABI on front end JSON file
const frontEndAbiFile = "path/to/store/abi.json";
const fundme = await ethers.getContract("FundMe"); //already deployed contract on hardhat/rinkeby
//I want to get the ABI here so that I can write and save it to frontEndAbiFile location.
fs.writeFileSync(frontEndAbiFile, 'How shoud I get ABI in json?' );
};
export default updateUI;我怎样才能得到json格式的FundMe合同中的ABI?请阅读我以上代码中的评论。注意:我使用的是TypeScript。
发布于 2022-07-31 07:40:06
如果您是通过命令行进行部署,则可以从以前构建的文件中读取,在“硬帽子”的情况下,有一个示例:
const fs = require("fs")
const path = require("path")
const getTheAbi = () => {
try {
const dir = path.resolve(
__dirname,
"./artifacts/contracts/HelloWorld.sol/HelloWorld.json"
)
const file = fs.readFileSync(dir, "utf8")
const json = JSON.parse(file)
const abi = json.abi
console.log(`abi`, abi)
return abi
} catch (e) {
console.log(`e`, e)
}
}发布于 2022-09-14 11:04:07
可以跳过ABI直接获得合同实例:
const contractInstance = await ethers.getContractAt("contracts/XYZ.sol:ContractName", contractAddress);发布于 2022-11-24 12:34:17
fs.writeFileSync(
frontEndAbiFile,
fundme.interface.format(ethers.utils.FormatTypes.json)
)部署时,这将更新前端文件。
https://ethereum.stackexchange.com/questions/131400
复制相似问题