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

Rewards show more then 6 decimals #3376

Closed
faboweb opened this issue Jan 8, 2020 · 1 comment · Fixed by #3396
Closed

Rewards show more then 6 decimals #3376

faboweb opened this issue Jan 8, 2020 · 1 comment · Fixed by #3396
Assignees
Labels
bug 🐛 issues related to unhandled errors in the code that need to be fixed

Comments

@faboweb
Copy link
Collaborator

faboweb commented Jan 8, 2020

Describe the bug

image

To Reproduce

Expected behavior

Should only show 6 decimals.
This should probably be handled in the API.

Screenshots

Desktop (please complete the following information):

  • OS:
  • Browser:
  • Version:

Smartphone (please complete the following information):

  • Device:
  • OS:
  • Browser
  • Version

Additional context

@faboweb faboweb added the bug 🐛 issues related to unhandled errors in the code that need to be fixed label Jan 8, 2020
@mariopino
Copy link
Contributor

mariopino commented Jan 10, 2020

I think in this case api is returning rewards as expected (6 decimal points):

Query:

{
  rewards(networkId: "cosmos-hub-mainnet", delegatorAddress: "cosmos16d7lk68ruw5556wumhc3tle9vj333lhzalnfmf") {
    validator {
      operatorAddress
    }
    amount
  }
}

Response:

{
  "data": {
    "rewards": [
      {
        "validator": {
          "operatorAddress": "cosmosvaloper1qwl879nx9t6kef4supyazayf7vjhennyh568ys"
        },
        "amount": "0.000492"
      },
      {
        "validator": {
          "operatorAddress": "cosmosvaloper15r4tc0m6hc7z8drq3dzlrtcs6rq2q9l2nvwher"
        },
        "amount": "0.000048"
      },
      {
        "validator": {
          "operatorAddress": "cosmosvaloper1k9a0cs97vul8w2vwknlfmpez6prv8klv03lv3d"
        },
        "amount": "0.000079"
      },
      {
        "validator": {
          "operatorAddress": "cosmosvaloper1uutuwrwt3z2a5z8z3uasml3rftlpmu25aga5c6"
        },
        "amount": "0.000052"
      }
    ]
  }
}

The problem is at frontend, the following code is returning 0,0006709999999999999 (classic javascript floating point issue):

totalRewards() {
      return this.rewards.reduce((sum, { amount }) => sum + Number(amount), 0)
    },

So to fix this behaviour we can change it to:

    totalRewards() {
      return this.rewards
        .reduce((sum, { amount }) => sum + Number(amount), 0)
        .toFixed(6)
    },

Which returns 0,000671 as desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 issues related to unhandled errors in the code that need to be fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants