I am implementing an app in react-native using ethers.js to interact with blockchain. I needed human readable ABI to create a new instance of a smart contract. But I initially deployed my smart contracts using Brownie python package. Is there a way to get the ABI's from brownie?
1 Answer
brownie compile
outputs JSON ABIs in the build
directory specified in your brownie-config.yml
. However, Ethers.js cannot consume these files because the output JSON includes other content alongside the desired abi. Here's a solution using Ethers.js:
// read and parse the JSON output written by brownie
const jsonAbi = fs.readFileSync('brownie-out/MyContract.json');
const parsed = JSON.parse(jsonAbi.toString());
// create the Ethers.js Interface used for translation,
// and pass only the desired ABI content
const iface = new Interface(parsed.abi);
// perform the translation
const formattedAbi = iface.format(FormatTypes.full).toString();
const translatedAbi = JSON.stringify(JSON.parse(formattedAbi));
// write the translated ABI to disk
fs.writeFileSync(
path.join(process.cwd(), 'abis/', 'MyContract.abi'),
translatedAbi
);
Here's some relevant documentation: https://docs.ethers.org/v5/api/utils/abi/formats/#abi-formats--converting-between-formats