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:
The following code is used in runtime for stake calculation:
required_stake
is a floating point, and with usage of saturated_into is rounded to the nearest integer. While in the typesript codeIf 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: