Joystream / Joystream/joystream

Floating point values rounding

Open
#298 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.4k
Forks
116
PR merge metrics
No merged PRs in 30d

Description

The following code is used in runtime for stake calculation:

let required_stake = Perbill::from_rational_approximation(numerator, denominator) * total_issuance;
let balance: BalanceOf<T> = required_stake.saturated_into();

required_stake is a floating point, and with usage of saturated_into is rounded to the nearest integer. While in the typesript code

new BN((issuance * numerator) / denominator)

If sent using API, the value will be rounded down. This caused the issue when transaction gets rejected because of incorrect stake value provided, eg 101516 vs 101517.
With limited information about tx failure reason, such issue can cost time to find and solve.
Also, it is important to display the required stake correctly rounded in UI.

The solution used is to use explicit rounding function in typescript:

const stake = (issuance * numerator) / denominator;
const stakeBN = new BN(stake.toFixed(0));

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the runtime stake calculation using Perbill and saturated_into, then trace the corresponding TypeScript API and UI calculation using BN. Compare the rounding behavior for the reported 101516 versus 101517 case; done means submitted and displayed stake values use the same explicit rounding.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, typescript
Domain
api, blockchain, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.