fix too aggressively flooring BN math (#277)

* fix too aggressively flooring BN math

* use i80f48 for division

Co-authored-by: Maximilian Schneider <mail@maximilianschneider.net>
This commit is contained in:
microwavedcola1 2022-10-28 13:43:24 +02:00 committed by GitHub
parent cc220f56e3
commit 73792dd6ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 2 deletions

View File

@ -34,9 +34,11 @@ export function toUiDecimals(
nativeAmount: BN | I80F48 | number,
decimals: number,
): number {
// TODO: remove BN and upgrade to bigint https://github.com/solana-labs/solana/issues/27440
if (nativeAmount instanceof BN) {
return nativeAmount.div(new BN(Math.pow(10, decimals))).toNumber();
} else if (nativeAmount instanceof I80F48) {
nativeAmount = I80F48.fromU64(nativeAmount);
}
if (nativeAmount instanceof I80F48) {
return nativeAmount
.div(I80F48.fromNumber(Math.pow(10, decimals)))
.toNumber();