wighawag / hardhat-deploy

hardhat deployment plugin
MIT License
1.19k stars 292 forks source link

TypeError: Do not know how to serialize a BigInt #439

Closed jack-wombat closed 1 year ago

jack-wombat commented 1 year ago

Describe the bug Hardhat deploy uses JSON.stringify which does not know how to serialize bigint. See https://github.com/GoogleChromeLabs/jsbi/issues/30

I dont have visibility into which fields the pendingTransactions is using bigint. But one solution would be to use a string in place of bigint. In fact, this is the work around I'm using now.

This will become a bigger issue when people migrate to ether-v6 which uses bigint in place of big number.

Stack trace:

TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at DeploymentsManager.onPendingTx (/node_modules/hardhat-deploy/src/DeploymentsManager.ts:543:14)
    at _deploy (/node_modules/hardhat-deploy/src/helpers.ts:667:16)

Line#543 is when DeploymentsManager is saving the pending txn to file:

fs.writeFileSync(
        pendingTxPath,
        JSON.stringify(this.db.pendingTransactions, null, '  ')
      );

To Reproduce Deploy anything with bigint.

Expected behavior Expect it to works

versions

Additional context

Bolee95 commented 1 year ago

Out of the box, BigInt does not have support for JSON serialization. This can be hijacked, as every time serialization should be done at some time, toJSON is called. As BigInt does not implement this method, you can do the following:

// Enables JSON serialization of BigInt type
(BigInt.prototype as any).toJSON = function () {
  return this.toString();
};

Import this wherever you are using BigInt and you are good to go.

jack-wombat commented 1 year ago

I expect this to be a huge pain point for future user of ethers v6 which use bigint by default. So it's best to handle in the library. JSON.stringify provides a replacer which can stuff in this logic (mdn doc). i.e. if typeof key is bigint or BigInt, then call toString().

Also, the above workaround only works for BigInt and not bigint.

wighawag commented 1 year ago

@jack-wombat do you know on what field does the serialization fails

like which fields in the json are bigint ?

jack-wombat commented 1 year ago

@wighawag I didn't know which field it fails. I tried adding console.log in the files under node_modules, but it doesn't seem to catch up on the updates.

wighawag commented 1 year ago

I published a potential fix for it in v0.11.31 let me know if this solve your issue