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

Large nums as strings #225

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dagre": "^0.8.5",
"dayjs": "^1.11.1",
"i18next": "^21.6.16",
"lossless-json": "^4.0.2",
"pretty-bytes": "^6.0.0",
"react": "^17.0.2",
"react-copy-to-clipboard": "^5.1.0",
Expand Down Expand Up @@ -60,6 +61,7 @@
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/lossless-json": "^1.0.4",
"@types/node": "^17.0.25",
"@types/react": "^17.0.43",
"@types/react-copy-to-clipboard": "^5.0.2",
Expand Down
11 changes: 9 additions & 2 deletions src/utils/fetches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
ITokenTransferWithPool,
} from '../interfaces';

import { parse, isSafeNumber, LosslessNumber } from 'lossless-json';

export const fetchWithCredentials = (
resource: string,
options?: RequestInit
Expand All @@ -20,12 +22,17 @@ export const fetchWithCredentials = (
);
};

export function parseLargeNumbersAsStrings(value: any) {
return isSafeNumber(value, { approx: false })
? parseFloat(value) // Smaller numbers are kept as Javascript numbers
: new LosslessNumber(value).toString(); // Large numbers are safely stringified
}

Comment on lines +25 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

actually based on this example https://github.com/josdejong/lossless-json?tab=readme-ov-file#lossless-json couldn't we just call parse() and that's it?

Copy link
Contributor

@EnriqueL8 EnriqueL8 Sep 18, 2024

Choose a reason for hiding this comment

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

Or is it because lower down in the react component we are actually doing some manipulation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with just calling parse() is that small numbers all end up as LosslessNumbers as well and are therefore stringified in the UI. Which I don't think we want. So the extra number parser allows us to retain Javascript numbers for smaller numbers.

Copy link
Contributor

Choose a reason for hiding this comment

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

But they don't look like stringified here

// LosslessJSON.parse will preserve all numbers and even the formatting:
console.log(stringify(parse(text)))
// '{"decimal":2.370,"long":9123372036854000123,"big":2.3e+500}'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it's because we want the large numbers as a string (instead of a BigInt which would render in the UI as 1000000000000001n). In their example they are using their own stringify to render it as a large number without a string, but we would need to refactor a lot of the codebase to use that instead of our existing JSON serialisation logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got caught out that stringify is actually their own function

export const fetchCatcher = async (resource: string): Promise<any> => {
const response = await fetchWithCredentials(resource);
if (!response.ok) {
console.log(`error fetching ${resource}`);
} else {
return await response.json();
return parse(await response.text(), null, parseLargeNumbersAsStrings);
}
};

Expand Down
Loading