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 pnum #1935

Merged
merged 7 commits into from
Dec 5, 2024
Merged

Add pnum #1935

merged 7 commits into from
Dec 5, 2024

Conversation

JasonMHasperhoven
Copy link
Contributor

@JasonMHasperhoven JasonMHasperhoven commented Dec 4, 2024

Currently we have a bunch of conversion functions scattered around in:

  • packages/types/src/amount.ts
  • packages/types/src/lo-hi.ts
  • packages/types/src/round.ts
  • packages/types/src/value-view.ts

This aims to unify and simplify number conversions, see the function description at the top for context.

Copy link

changeset-bot bot commented Dec 4, 2024

🦋 Changeset detected

Latest commit: e6c0ac2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@penumbra-zone/types Minor
minifront Patch
node-status Patch
@penumbra-zone/crypto-web Major
@penumbra-zone/query Major
@penumbra-zone/services Major
@penumbra-zone/storage Major
@penumbra-zone/ui-deprecated Patch
@penumbra-zone/ui Patch
@penumbra-zone/wasm Major
@repo/tailwind-config Patch
@penumbra-zone/perspective Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@JasonMHasperhoven JasonMHasperhoven requested a review from a team December 4, 2024 15:36
Copy link
Contributor

@grod220 grod220 left a comment

Choose a reason for hiding this comment

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

Cool stuff!

Comment on lines 26 to 29
function pnum(
input: string | number | LoHi | bigint | Amount | ValueView | undefined,
exponentInput = 0,
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: this feels very class-like (private vars, exposed methods, constructor like initialization). Think we should convert it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did this on purpose as it's shorter: pnum(val) vs new PNum(val). My guess is that we'll use it quite often and this way it looks cleaner imo, especially if we need to do it inline inside a react component.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I can live with this, but probably just because it is a helper utility. Otherwise, think we should default to classes.

Comment on lines 43 to 44
input instanceof Amount ||
(typeof input === 'object' && 'lo' in input && 'hi' in input && input.lo && input.hi)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: Wouldn't this work the same?

else if (input instanceof Amount) {
    value = new BigNumber(joinLoHi(input.lo, input.hi).toString());
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case catches an instance of Amount and LoHi, in your check a LoHi will not be true.

Copy link
Contributor

Choose a reason for hiding this comment

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

No test appears to fail when removing that line. Can you add a test for coverage for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

Comment on lines 109 to 110
toAmount(): Amount {
return new Amount(splitLoHi(BigInt(value.toString())));
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: missing tests for this method

Comment on lines +1 to +3
---
'@penumbra-zone/types': minor
---
Copy link
Contributor

Choose a reason for hiding this comment

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

question: should we take those packages you've listed, create a new package, make those specific files private, and only expose pnum?

Copy link
Contributor

Choose a reason for hiding this comment

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

related: if we make the other stuff private, we'll need to find the previous usages and convert them to pnum

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think a rewrite would be quite a bit of effort and potentially introduce regression errors (you'd need to check all values if they are in base units) I'd say leave the existing code and adopt this for future code. Also it's internally using some of the other functions (splitLoHi etc)

*/
function pnum(
input: string | number | LoHi | bigint | Amount | ValueView | undefined,
exponentInput = 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: think we should consider an options obj as a second parameter so it's easier to expand in the future:

- exponentInput = 0,
+ options: { exponentInput?: number } = { exponentInput: 0 },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps wise indeed

Copy link
Contributor

Choose a reason for hiding this comment

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

think we can simplify the name too

- exponentInput = 0,
+ options: { exponent?: number } = { exponent: 0 },

* @param exponent
*/
function pnum(
input: string | number | LoHi | bigint | Amount | ValueView | undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

question: what are the usecases for an undefined input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This:
image

I guess you can make an argument against it, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see, it's kinda the default case. I wonder if explicitly defining it would be helpful:

function pnum(input: string | number | LoHi | bigint | Amount | ValueView = 0)
...
// and then removing this branch
 else {
    value = new BigNumber(0);
  }

Can you add tests for testing the default case if undefined is passed?

Copy link
Contributor

@grod220 grod220 left a comment

Choose a reason for hiding this comment

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

Feel free to merge after addressing the final comments 👍

Think it's mostly more tests we need

* @param exponent
*/
function pnum(
input: string | number | LoHi | bigint | Amount | ValueView | undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see, it's kinda the default case. I wonder if explicitly defining it would be helpful:

function pnum(input: string | number | LoHi | bigint | Amount | ValueView = 0)
...
// and then removing this branch
 else {
    value = new BigNumber(0);
  }

Can you add tests for testing the default case if undefined is passed?

*/
function pnum(
input: string | number | LoHi | bigint | Amount | ValueView | undefined,
exponentInput = 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

think we can simplify the name too

- exponentInput = 0,
+ options: { exponent?: number } = { exponent: 0 },

Comment on lines 26 to 29
function pnum(
input: string | number | LoHi | bigint | Amount | ValueView | undefined,
exponentInput = 0,
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I can live with this, but probably just because it is a helper utility. Otherwise, think we should default to classes.

packages/types/src/pnum.ts Outdated Show resolved Hide resolved
packages/types/src/pnum.test.ts Outdated Show resolved Hide resolved
Comment on lines 43 to 44
input instanceof Amount ||
(typeof input === 'object' && 'lo' in input && 'hi' in input && input.lo && input.hi)
Copy link
Contributor

Choose a reason for hiding this comment

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

No test appears to fail when removing that line. Can you add a test for coverage for that?

@TalDerei
Copy link
Contributor

TalDerei commented Dec 5, 2024

nice helper function, I've been wanting something like this for a while 👍

@@ -4,9 +4,10 @@ import { removeTrailingZeros } from './shortify.js';
export type RoundingMode = 'half-up' | 'up' | 'down';

export interface RoundOptions {
value: number;
value: number | string;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For @grod220 (because I know you want to know why) this is needed for a big number (caught by one of the tests)

@JasonMHasperhoven JasonMHasperhoven merged commit 712e7b1 into main Dec 5, 2024
1 check passed
@JasonMHasperhoven JasonMHasperhoven deleted the pnum branch December 5, 2024 13:27
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.

3 participants