Skip to content
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

Closed
briarsweetbriar opened this issue Sep 26, 2018 · 5 comments
Closed

Add JSON serializability to BigNumber #288

briarsweetbriar opened this issue Sep 26, 2018 · 5 comments
Assignees
Labels
enhancement New feature or improvement.

Comments

@briarsweetbriar
Copy link

I was expecting this:

ethers.utils.bigNumberify(JSON.parse(JSON.stringify(ethers.utils.bigNumberify("1"))))

to generate a BigNumber. Instead, it throws:

Uncaught Error: invalid BigNumber value (arg="value", value={"_hex":"0x01"})
at Object.throwError (ethers.js:13663)
at new BigNumber (ethers.js:13213)
at Object.bigNumberify (ethers.js:13290)
at :1:14

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 preventing ethers.utils.bigNumberify from correctly parsing it.

@ricmoo
Copy link
Member

ricmoo commented Sep 26, 2018

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!

@ricmoo ricmoo changed the title Ethers BigNumber cannot JSON.stringify -> JSON.parse -> ether.utils.bigNumberify Add JSON serializability to BigNumber Sep 26, 2018
@ricmoo ricmoo self-assigned this Sep 26, 2018
@ricmoo ricmoo added enhancement New feature or improvement. on-deck This Enhancement or Bug is currently being worked on. v4.0 labels Sep 26, 2018
@briarsweetbriar
Copy link
Author

Thank you!

@ricmoo ricmoo removed the v4.0 label Sep 30, 2018
@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Oct 4, 2018
@ricmoo
Copy link
Member

ricmoo commented Oct 4, 2018

This has been added in 4.0.2.

Let me know if you have any issues with it.

Thanks! :)

@ricmoo ricmoo closed this as completed Oct 4, 2018
asnov added a commit to asnov/ethers.js that referenced this issue Oct 14, 2018
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
@iradofurioso
Copy link

bigNumberify

bigNumberify didn't work (threw an error) but BigNumber.from did the job.
in case anyone wats to convert to real value to print on screen: ethers.utils.formatEther

@TrejGun
Copy link

TrejGun commented Apr 12, 2022

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement.
Projects
None yet
Development

No branches or pull requests

4 participants