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 comparison operators on Decimal type in @cosmjs/math #214

Closed
webmaster128 opened this issue Jun 10, 2020 · 0 comments · Fixed by #217
Closed

Add comparison operators on Decimal type in @cosmjs/math #214

webmaster128 opened this issue Jun 10, 2020 · 0 comments · Fixed by #217
Assignees

Comments

@webmaster128
Copy link
Member

webmaster128 commented Jun 10, 2020

There should be at least =, <, <=, >, >= on the Decimal type, which does not require unprecise conversion to JS numbers.

Since there is no operator overloading, I suggest interfaces like

a.equals(b)
a.isLessThan(b)
a.isGreaterThanOrEqual(b)

We need this here:

  /** true iff the distributor account needs a refill */
  public needsRefill(account: MinimalAccount, tickerSymbol: string): boolean {
    const meta = this.getTokenMeta(tickerSymbol);

    const balanceAmount = account.balance.find((b) => b.denom === meta.denom);

    const balance = balanceAmount
      ? Decimal.fromAtomics(balanceAmount.amount, meta.fractionalDigits)
      : Decimal.fromAtomics("0", 0);

    const thresholdAmount = this.refillThreshold(tickerSymbol);
    const threshold = Decimal.fromAtomics(thresholdAmount.amount, meta.fractionalDigits);

    // TODO: perform < operation on Decimal type directly
    // https://github.com/iov-one/iov-core/issues/1375
    return balance.toFloatApproximation() < threshold.toFloatApproximation();
  }

It would be good to have a compare funtion that we can use with sort like this

values.sort(Decimal.compare);

and use this to implement the other helpers


Bonus: Extract the function signatures in and interface that we can reuse for the integer types.

@webmaster128 webmaster128 added this to To do in CosmJS development via automation Jun 10, 2020
@willclarktech willclarktech moved this from To do to In progress in CosmJS development Jun 10, 2020
@willclarktech willclarktech self-assigned this Jun 10, 2020
@willclarktech willclarktech moved this from In progress to Awaiting review in CosmJS development Jun 10, 2020
CosmJS development automation moved this from Awaiting review to Done Jun 10, 2020
@okwme okwme removed this from Done in CosmJS development Oct 19, 2020
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 a pull request may close this issue.

2 participants