Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lesson 10: Tip about ethers.utils.FormatTypes.json #781

pyromaus started this conversation in Ideas
Discussion options

At this point of the course I was following the typing out of this code.

async function updateAbi() {
 const raffle = await ethers.getContract("Raffle")
 fs.writeFileSync(
 FRONTEND_ABI_FILE,
 raffle.interface.format(ethers.utils.FormatTypes.json)

As the last line is getting typed out, the video quickly cuts to the Ethers docs. At the same time, VScode suggests I replace json with JSON by hitting Enter. I do so (in VScode we trust).

I run yarn hardhat node to produce our .json files. I get the following error:

TypeError: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Array
 at Object.writeFileSync (node:fs:2163:5)

I try adding .toString() on the end. It goes through but the abi.json looks all weird and unformatted. Nope, next.
After tinkering around and googling for like 15 minutes, while looking at the Ethers docs I try putting it back to lowercase .json. It goes through and my abi.json looks beauuutiful!!

Just a heads-up for anyone potentially searching the Discussions for this error (I didn't find any); might save them some time and frustration, it made my eyes roll how a VScode suggestion made the code break like this lol.

Take note when VScode suggests you something. You could overlook it and it can break your code later.
Cheers

You must be logged in to vote

Replies: 3 comments 1 reply

Comment options

Bruv I used .json in lowercase itself, but I'm not getting an abi.json output. Do you have any ideas why? My contract addresses JSON file is all perfect, but I'm getting errors here. Do you have any idea?

You must be logged in to vote
1 reply
Comment options

No idea mate, show some of your code, like your updateAbi() function, your FRONT_END_ABI_FILE variable, etc. we can start there

Comment options

instead of this

fs.writeFileSync(FRONT_END_ABI_FILE, raffle.interface.format(ethers.utils.FormatTypes.JSON));

try this

 fs.writeFileSync(FRONT_END_ABI_FILE, JSON.stringify(raffle.interface));
You must be logged in to vote
0 replies
Comment options

In ethers 6, you can use the formatJson() function to get the same thing that Patrick.
The doc : https://docs.ethers.org/v6/api/abi/#Interface-formatJson

The line to write the contract ABI :

fs.writeFileSync(FRONT_END_ABI_FILE, raffle.interface.formatJson());
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /