Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unnatural Ada rounding #1442

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Testing inputOutputTransformer function', () => {
expect(result.addr).toEqual(
'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp'
);
expect(result.amount).toBe('1.00');
expect(result.amount).toBe('1');
expect(result.assetList.length).toEqual(0);
});

Expand All @@ -46,7 +46,7 @@ describe('Testing inputOutputTransformer function', () => {
expect(result.addr).toEqual(
'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp'
);
expect(result.amount).toBe('3.00');
expect(result.amount).toBe('3');
expect(result.assetList.length).toEqual(0);
});

Expand All @@ -66,7 +66,7 @@ describe('Testing inputOutputTransformer function', () => {
expect(result.addr).toEqual(
'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp'
);
expect(result.amount).toBe('3.00');
expect(result.amount).toBe('3');
expect(result.assetList.length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { walletBalanceTransformer } from '../transformers';
describe('Testing walletBalanceTransformer function', () => {
test('given a wallet balance in lovelace should return the balance in ada and undefined for fiat', () => {
const result = walletBalanceTransformer('10000000');
expect(result.coinBalance).toBe('10.00');
expect(result.coinBalance).toBe('10');
expect(result.fiatBalance).toBeUndefined();
});

test('given a wallet balance in lovelace and a fiat price should return the balance in ada and in fiat', () => {
const result = walletBalanceTransformer('10000000', 2);
expect(result.coinBalance).toBe('10.00');
expect(result.coinBalance).toBe('10');
expect(result.fiatBalance).toBe('20.00');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Testing availableCoinsTransformer function', () => {
const coins = availableCoinsTransformer(totalCoins.toString(), assets);
expect(coins).toEqual([
{
balance: '10.00',
balance: '10',
id: '1',
symbol: 'ADA'
},
Expand Down
3 changes: 0 additions & 3 deletions apps/browser-extension-wallet/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ export const CARDANO_COIN_SYMBOL: { [key in Wallet.Cardano.NetworkId]: ADASymbol
export const cardanoCoin: Wallet.CoinId = {
id: '1',
name: 'Cardano',
decimals: 6,
symbol: CARDANO_COIN_SYMBOL[Wallet.Cardano.NetworkId.Mainnet]
};

export const EPOCH_DURATION_DAYS = 5;

export const MIN_COIN_TO_SEND = 1;

type StaticBalanceTracker = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ export const cardanoStakePoolSelectedDetails = {
status: 'active',
ticker: 'STTST',
blocks: '20',
pledge: '2000.00'
pledge: '2000'
};

export const transformedStakePool = {
Expand All @@ -585,7 +585,7 @@ export const transformedStakePool = {
'stake_test1uqrw9tjymlm8wrwq7jk68n6v7fs9qz8z0tkdkve26dylmfc2ux2hj',
'stake_test1uq7g7kqeucnqfweqzgxk3dw34e8zg4swnc7nagysug2mm4cm77jrx'
],
pledge: '2000.00ADA',
pledge: '2000ADA',
retired: false,
saturation: '5.12',
size: '- %',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ describe('Testing tx transformers utils', () => {
deposit: undefined,
depositReclaim: undefined,
direction: 'Outgoing',
fee: '1.00',
fee: '1',
fiatAmount: '1.00 USD',
id: '6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad',
amount: '1.00 ADA',
amount: '1 ADA',
assets: [
{
id: '6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7',
Expand All @@ -141,17 +141,15 @@ describe('Testing tx transformers utils', () => {
});

describe('getFormattedFiatAmount', () => {
test('shoud return properly formatted fiat amount', () => {
const amount = new BigNumber('10');
test('should return properly formatted fiat amount', () => {
const amount = new BigNumber('5500000');
const fiatPrice = 2;
const fiatCurrency = { code: 'code', symbol: 'symbol' } as unknown as CurrencyInfo;
mockLovelacesToAdaString.mockImplementationOnce((val) => val);

expect(getFormattedFiatAmount({ amount, fiatPrice, fiatCurrency })).toEqual(
`${amount.times(new BigNumber(fiatPrice)).toString()} ${fiatCurrency.code}`
);
expect(getFormattedFiatAmount({ amount, fiatPrice, fiatCurrency })).toEqual(`11.00 ${fiatCurrency.code}`);
});
test('shoud return properly formatted fiat amount in case there is no fiat price', () => {
test('should return properly formatted fiat amount in case there is no fiat price', () => {
const amount = new BigNumber('10');
const fiatPrice = 0;
const fiatCurrency = { code: 'code', symbol: 'symbol' } as unknown as CurrencyInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getFormattedFiatAmount = ({
fiatCurrency: CurrencyInfo;
}): string => {
const fiatAmount = fiatPrice
? Wallet.util.lovelacesToAdaString(amount.times(new BigNumber(fiatPrice)).toString())
? Wallet.util.convertLovelaceToFiat({ lovelaces: amount.toString(), fiat: fiatPrice }).toString()
: '';
return fiatAmount ? `${fiatAmount} ${fiatCurrency.code}` : '-';
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('CoinInput util', () => {
describe('getADACoinProperties', () => {
test('returns 0 for availableADA and false for hasMaxBtn when balance is 0', () => {
expect(getADACoinProperties('0', '1000000', '0', '0')).toEqual({
availableADA: '0.00',
availableADA: '0',
max: '1',
hasMaxBtn: false,
hasReachedMaxAmount: false,
Expand All @@ -40,7 +40,7 @@ describe('CoinInput util', () => {
});
test('returns 0 for max and true for hasReachedMaxAmount when spendable coins is 0', () => {
expect(getADACoinProperties('1000000', '0', '0', '0')).toEqual({
availableADA: '1.00',
availableADA: '1',
max: '0',
hasMaxBtn: true,
hasReachedMaxAmount: true,
Expand All @@ -49,7 +49,7 @@ describe('CoinInput util', () => {
});
test('returns formatted balance as availableADA, and the spendable coin in ADA as max when there is no spending', () => {
expect(getADACoinProperties('20000000', '10000000', '0', '0')).toEqual({
availableADA: '20.00',
availableADA: '20',
max: '10',
hasMaxBtn: true,
hasReachedMaxAmount: false,
Expand All @@ -58,7 +58,7 @@ describe('CoinInput util', () => {
});
test('returns the calculated max amount when there is less spent coin than spendable coin', () => {
expect(getADACoinProperties('20000000', '10000000', '5', '2')).toEqual({
availableADA: '20.00',
availableADA: '20',
max: '7',
hasMaxBtn: true,
hasReachedMaxAmount: false,
Expand All @@ -67,7 +67,7 @@ describe('CoinInput util', () => {
});
test('returns max amount as 0 and hasReachedMaxAmount as true when there is more spent coin than spendable coin', () => {
expect(getADACoinProperties('20000000', '10000000', '10', '0')).toEqual({
availableADA: '20.00',
availableADA: '20',
max: '0',
hasMaxBtn: true,
hasReachedMaxAmount: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const useSelectedCoins = ({
},
formattedFiatValue: `= ${compactNumberWithUnit(fiatValue)} ${fiatCurrency?.code}`,
fiatValue: `= ${fiatValue} ${fiatCurrency?.code}`,
maxDecimals: cardanoCoin.decimals
maxDecimals: 6
} as AssetInputListProps['rows'][number];
}

Expand Down
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
1 change: 0 additions & 1 deletion packages/cardano/src/wallet/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type CoinId = {
id: string;
symbol: string;
name: string;
decimals: number;
};

export enum TransactionStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { stakePoolTransformer } from '../stake-pool-transformer';
const cardanoCoin: CoinId = {
id: '1',
name: 'Cardano',
decimals: 6,
symbol: 'ADA'
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
} from '../unit-converters';

describe('Testing lovelacesToAdaString function', () => {
test('should convert lovelaces to ada', async () => {
test('should convert lovelaces to ada - one million', async () => {
const result = lovelacesToAdaString('1000000');
expect(result).toBe('1.00');
expect(result).toBe('1');
});

test('should return ada value with 4 decimals', async () => {
const result = lovelacesToAdaString('1000000', 4);
expect(result).toBe('1.0000');
test('should convert lovelaces to ada - one million and one', async () => {
const result = lovelacesToAdaString('1000001');
expect(result).toBe('1.000001');
});
});

Expand Down Expand Up @@ -63,6 +63,6 @@ describe('Testing getFormattedAmount function', () => {
decimals: 2
};
const result = getFormattedAmount({ amount: '2000000', cardanoCoin });
expect(result).toBe('2.00 symbol');
expect(result).toBe('2 symbol');
});
});
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const transactionDate = '2021/09/10';
export const cardanoCoin: Wallet.CoinId = {
id: '1',
name: 'Cardano',
decimals: 6,
symbol: 'A'
};

Expand Down
Loading