Skip to content

Commit

Permalink
fix: round Ada naturally
Browse files Browse the repository at this point in the history
Ada naturally supports 6 decimal points, and we should be supporting this precision
  • Loading branch information
rhyslbw committed Sep 22, 2024
1 parent 48a74ad commit 61c99c7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const getADACoinProperties = (
): ADARow => {
// Convert to ADA
const availableADA = Wallet.util.lovelacesToAdaString(balance);
const spendableCoinInAda = Wallet.util.lovelacesToAdaString(spendableCoin, undefined, BigNumber.ROUND_DOWN);
const spendableCoinInAda = Wallet.util.lovelacesToAdaString(spendableCoin);
// Calculate max amount in ADA
const max = getMaxSpendableAmount(spendableCoinInAda, spentCoins, currentSpendingAmount);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import {
describe('Testing lovelacesToAdaString function', () => {
test('should convert lovelaces to ada', async () => {
const result = lovelacesToAdaString('1000000');
expect(result).toBe('1.00');
});

test('should return ada value with 4 decimals', async () => {
const result = lovelacesToAdaString('1000000', 4);
expect(result).toBe('1.0000');
expect(result).toBe('1');
});
});

Expand Down
7 changes: 2 additions & 5 deletions packages/cardano/src/wallet/util/unit-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import { CoinId } from '../types';
const LOVELACE_VALUE = 1_000_000;
const DEFAULT_DECIMALS = 2;

export const lovelacesToAdaString = (
lovelaces: string,
decimalValues: number = DEFAULT_DECIMALS,
roudingMode: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP
): string => new BigNumber(lovelaces).dividedBy(LOVELACE_VALUE).toFixed(decimalValues, roudingMode);
export const lovelacesToAdaString = (lovelaces: string): string =>
new BigNumber(lovelaces).dividedBy(LOVELACE_VALUE).toString();

export const adaToLovelacesString = (ada: string): string => new BigNumber(ada).multipliedBy(LOVELACE_VALUE).toString();

Expand Down

0 comments on commit 61c99c7

Please sign in to comment.