Migrate code to use the native BigInt type wherever possible.#607
Open
Migrate code to use the native BigInt type wherever possible.#607
BigInt type wherever possible.#607Conversation
Contributor
|
@Shaptic Please do not merge it so far. It's possible to calculate rational prices using bigints as well, so we'll be able to say farewell to the |
Contributor
|
@Shaptic Ok, here is the solution: /**
* Divide two numbers with 20 digits precision (result is floored)
* @param {String|Number|BigInt} a
* @param {String|Number|BigInt} b
* @return {String}
**/
function div(a, b) {
const val = (BigInt(a) * (10n**20n) / BigInt(b)).toString().padStart(20, '0')
const int = val.substring(0, val.length - 20) || '0'
const fract = val.substring(val.length - 20)
return int + '.' + fract
}It behaves the same as new BigNumber(2).div(3).toString() // '0.66666666666666666667'
div(2, 3) // '0.66666666666666666666'
new BigNumber(2).div(300000).toString() // '0.00000666666666666667'
div(2, 300000) // '0.00000666666666666666'
new BigNumber(200000).div(3).toString() // '66666.66666666666666666667'
div(200000, 3) // '66666.66666666666666666666'
('200000'/'3').toString() // '66666.66666666667' - regular Numbers division with limited precision |
27 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Minimizes usage of the external dependency
BigNumber, preferring the nativeBigInt.Why
Per @orbitlens' in stellar/js-stellar-sdk#792 (comment), this is an attempt to minimize usage of the
bignumber.jsexternal dependency.Unfortunately, a full replacement of the library is not possible for a key reason: we need big rationals for price management. Anything to do with offers supports passing real values as strings (that may exceed
number) and we need to convert those to fractions for the XDR. This necessitates big rationals, whichBigIntdoes not support, obviously. Until there's also a nativeBigRat, we will needbignumber.jsin these (not really) niche cases.Known Limitations
Honestly, I'm not certain about the utility of this change given its potential bug surface. Since we still do need
bignumber.jsfor big rational values, and since this is imported byoperation.js, this will still need to be used by a giant portion of downstream consumers. (The number of people using the SDK but not usingOperationis probably minuscule.)