-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JSON serializability to BigNumber #288
Comments
A BigNumber is a complex type which, in general, cannot be serialized using JSON. For example, in JavaScript this also does not work: > new RegExp(JSON.parse(JSON.stringify(new RegExp(/Hello/))))
/[object Object]/ To serialize and deserialize a BigNumber, the hex is probably the easiest thing to use: let serialized = value.toHexString();
let deserialized = ethers.utils.bigNumberify(serialized); That said, supporting the behaviour you have above seems useful, and fairly easy (+ safe) to add. I will leave this ticket open as an enhancement for the library and get to it soon. Thanks! |
Thank you! |
This has been added in Let me know if you have any issues with it. Thanks! :) |
Merge commit '3736a1571480a0f69d632d6fc3bde549cbe46162' into fix/tslib * commit '3736a1571480a0f69d632d6fc3bde549cbe46162': (41 commits) Updated dist files. Added automatic event parsing for contract transaction receipts from tx.wait. Added ability to wait for a specific number of confirmations (ethers-io#229). Fix for geth-etc (official geth is fine), which returns Receipts before the blockHash is synced to the database. Fixed confirmations tests and bootstrap fast blockNumber. Added confirmations to TransactionResponse (ethers-io#156, ethers-io#238). Fixed nested errors for providers that were masking true error (ethers-io#292). Updated dist files. Added version to errors. Fixed French and Spanish for browsers without Uint8Array.forEach. Added French and Spanish includes to phantomjs test page. Increased timeout for querying npm registry. Updated dist files. Added French and Spanish wordlist dist files. Added French and Spanish BIP-39 wordlists (ethers-io#191). Added support for JSON serialized BigNumbers in the constructor (ethers-io#288). Fixed scrypt for long passwords (ethers-io#223). Updated dist files. Added chainId as supported override for contract transactions. Fixed wildcard events and made nested events more robust (ethers-io#289). ... Conflicts: dist/ethers.min.js dist/ethers.min.js.map package-lock.json
bigNumberify didn't work (threw an error) but BigNumber.from did the job. |
just in case import { BigNumber } from "ethers";
Object.defineProperties(BigNumber.prototype, {
toJSON: {
value: function (this: BigNumber) {
return this.toString();
},
},
});
console.log(JSON.stringify(BigNumber.from("1"))); // "1" |
I was expecting this:
ethers.utils.bigNumberify(JSON.parse(JSON.stringify(ethers.utils.bigNumberify("1"))))
to generate a
BigNumber
. Instead, it throws:When I:
JSON.parse(JSON.stringify(ethers.utils.bigNumberify("1")))
the result is:
{_hex: "0x01"}
where as:
ethers.utils.bigNumberify("1")
returns:
{_hex: "0x01", _ethersType: "BigNumber"}
so it looks like
JSON.stringify
is losing the_ethersType
, which is preventingethers.utils.bigNumberify
from correctly parsing it.The text was updated successfully, but these errors were encountered: