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

unstake and savings available inputs displaying incorrect value #715

Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/components/staking/SavingsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineComponent, ref, computed } from 'vue';
import { useStore } from 'src/store';
import ViewTransaction from 'src/components/ViewTransanction.vue';
import { API } from '@greymass/eosio';
import { assetToAmount } from 'src/utils/string-utils';
import { assetToAmount, formatNumberWithCommas } from 'src/utils/string-utils';

export default defineComponent({
name: 'SavingsTab',
Expand Down Expand Up @@ -104,6 +104,7 @@ export default defineComponent({
rexSavings,
transactionId,
transactionError,
formatNumberWithCommas,
formatDec,
moveToSavings,
moveFromSavings,
Expand All @@ -126,7 +127,7 @@ export default defineComponent({
<div class="col-9">STAKE TO SAVINGS</div>
<div class="col-3">
<div class="row items-center justify-end q-hoverable cursor-pointer" @click="setMaxSavingsValue">
<div class="text-weight-bold text-right balance-amount">{{ eligibleStaked }}</div>
<div class="text-weight-bold text-right balance-amount">{{ formatNumberWithCommas(eligibleStaked) }}</div>
<q-icon class="q-ml-xs" name="info"/>
<q-tooltip>Any balance currently maturing will be moved first, click to stake full amount</q-tooltip>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ export function formatCurrency(
export function assetToAmount(asset: string): number {
try {
const qty: string = asset.split(' ')[0];
return parseFloat(qty);
return parseFloat(qty.replace(/,/g, '')); // remove commas
} catch (error) {
return 0;
}
}

export function formatNumberWithCommas(num: number): string {
return num.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
}
donnyquixotic marked this conversation as resolved.
Show resolved Hide resolved

export function formatDate(date: string, showTime = true): string {
return showTime ?
new Date(date).toLocaleDateString('en-US', {
Expand Down
19 changes: 19 additions & 0 deletions test/jest/__tests__/utils/string-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isValidTransactionHex,
formatCurrency,
assetToAmount,
formatNumberWithCommas,
formatDate,
getRexHistoryAsset,
} from 'src/utils/string-utils';
Expand Down Expand Up @@ -162,6 +163,24 @@ describe('string-utils utility functions', () => {
});
});

describe('formatNumberWithCommas', () => {
it('passed a number less than one thousand returns the same number as a string', () => {
const testValue = 123;
const expectedResult = '123';
expect(formatNumberWithCommas(testValue)).toEqual(expectedResult);
});
it('inserts commas for numbers larger than four digits', () => {
const testValue = 123456789;
const expectedResult = '123,456,789';
expect(formatNumberWithCommas(testValue)).toEqual(expectedResult);
});
it('does not insert commas for trailing decimal places exceeding four digits', () => {
const testValue = 12345.6789;
const expectedResult = '12,345.6789';
expect(formatNumberWithCommas(testValue)).toEqual(expectedResult);
});
});

describe('formatDate', () => {
it('formats ISO date timestamp string to `<Month> <day>, <Year> at H:MM:SS <AM/PM>`', () => {
const testDate = '2023-03-21T21:26:16+01:23';
Expand Down
Loading