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

Normalize BigNumber to string for output #20

Merged
merged 1 commit into from
Nov 2, 2022

Conversation

the-flagship
Copy link
Contributor

@the-flagship the-flagship commented Nov 2, 2022

Before:

{
  id: '0x123abc...',
  owner: '0x456def...',
  name: 'foo',
  email: '[email protected]',
  escrow: BigNumber { _hex: '0x0544ed520394774822', _isBigNumber: true },
  reserve: BigNumber { _hex: '0x3782dace9d900000', _isBigNumber: true },
  content: 'https://...',
  checksum: '0xdeadbeef...'
}

After:

{
  id: '0x123abc...',
  owner: '0x456def...',
  name: 'foo',
  email: '[email protected]',
  escrow: '90.0',
  reserve: '4.0',
  content: 'https://...',
  checksum: '0xdeadbeef...'
}

src/helpers.ts Outdated
.map((k) => [k, r[k]])
.map((k) => {
if (r[k] instanceof BigNumber) {
return [k, r[k].toString()];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd take this further by doing this:

const floatThreshold = parseUnits("1", 18);
if (r[k].gte(floatThreshold)) {
  return [k, formatUnits(r[k], 18)];
}

Copy link
Contributor Author

@the-flagship the-flagship Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this do what you want?

function normalizeBigNumber(n: BigNumber): number | string {
  try {
    return n.toNumber();
  } catch {
    return formatUnits(n, 18);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edited the above since I didn't actually understand what formatUnits was doing...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will overflow. 1.0 tokens = 1^18 bignumber.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The edited version should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it accomplish the goal of your original comment? I think maybe we should just always return formatUnits(n, 18)... not the best ergonomics to sometimes have a number in these fields and sometimes strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, but you can't apply it unconditionally to all bignumbers since some of them aren't fractional (e.g. 1 == 1, but 1.0 = 1^18).

Copy link
Contributor Author

@the-flagship the-flagship Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, how about 5e1fa71?

@the-flagship the-flagship force-pushed the bignumbers-as-strings branch from 3f9c2bf to 5e1fa71 Compare November 2, 2022 21:59
src/helpers.ts Outdated
@@ -43,6 +50,13 @@ export function normalizeHex(s: string | undefined): string {
}
}

function normalizeBigNumber(n: BigNumber): string {
if (n.gte(FLOAT_THRESHOLD)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think of it, I like the version with try+toNumber even better because it'll also work for values <1 (and should still have no false positives).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, a8c333d has try/catch with .toNumber().toString()

@the-flagship the-flagship force-pushed the bignumbers-as-strings branch from 5e1fa71 to a8c333d Compare November 2, 2022 22:06
@the-flagship the-flagship merged commit 2f9b878 into main Nov 2, 2022
@the-flagship the-flagship deleted the bignumbers-as-strings branch November 2, 2022 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants