-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
src/helpers.ts
Outdated
.map((k) => [k, r[k]]) | ||
.map((k) => { | ||
if (r[k] instanceof BigNumber) { | ||
return [k, r[k].toString()]; |
There was a problem hiding this comment.
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)];
}
There was a problem hiding this comment.
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);
}
}
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, how about 5e1fa71?
3f9c2bf
to
5e1fa71
Compare
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)) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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()
5e1fa71
to
a8c333d
Compare
Before:
After: