Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/components/staking/SavingsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default defineComponent({
if (
fromSavingAmount.value === '0' ||
fromSavingAmount.value === '' ||
Number(fromSavingAmount.value) >= assetToAmount(rexSavings.value)
assetToAmount(fromSavingAmount.value) > assetToAmount(rexSavings.value)
) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/stores/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ export const useAccountStore = defineStore('account', {
rexbal.rex_maturities.forEach((maturity) => {
const maturityYear = new Date(maturity.first).getFullYear();
const maturityTime = new Date(maturity.first).getTime();
if (maturityYear - thisYear > 1) {
savingsRex = tlosRexRatio * (Number(maturity.second) / 10000);
} else if (maturityTime - now < 0) {
const diffNow = maturityTime - now;
const diffMaturity = maturityYear - thisYear;
if (diffMaturity > 1) {
savingsRex += tlosRexRatio * (Number(maturity.second) / 10000);
} else if (diffNow < 0) {
maturedRex += tlosRexRatio * (Number(maturity.second) / 10000);
} else {
maturingRex = tlosRexRatio * (Number(maturity.second) / 10000);
Expand Down
32 changes: 23 additions & 9 deletions src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export function formatCurrency(
symbol?: string,
skipPrettyPrinting?: boolean,
): string {
if (+amount > 0 && +amount < 0.5) {

}
const floatingPointNumberRegex = /^-?(0|[1-9]\d*)(\.\d+)?$/;
const amountIsValid =
(typeof amount === 'number' || floatingPointNumberRegex.test(amount)) &&
Expand All @@ -43,20 +46,31 @@ export function formatCurrency(
throw `${amount} is not a valid number`;
}

let amountAsString = typeof amount === 'number' ? amount.toString() : amount;
const originalAsString = typeof amount === 'number' ? amount.toString() : amount;
let amountAsString = originalAsString;

// ensure correct precision
amountAsString = (+amountAsString).toFixed(precision);
if (amountAsString.indexOf('.') === -1) {
// only in case of integer
amountAsString = originalAsString + '.' + '0'.repeat(precision);
}

if (!skipPrettyPrinting) {
// commify
amountAsString = (() => {
const [integer, fraction] = amountAsString.split('.');
const [integer, fraction] = amountAsString.split('.');
let fixed_fraction = fraction;

return `${(+integer).toLocaleString('en')}.${fraction}`;
})();
if (fixed_fraction.length < precision) {
// if the fraction is shorter than the precision, pad it with zeroes
fixed_fraction += '0'.repeat(precision - fixed_fraction.length);
} else {
// if the fraction is longer than the precision or equal to it, trim it
fixed_fraction = fixed_fraction.slice(0, precision);
}

if (skipPrettyPrinting) {
amountAsString = `${integer}.${fixed_fraction}`;
} else {
// commify
amountAsString = `${(+integer).toLocaleString('en')}.${fixed_fraction}`;
}

if (+amountAsString === 0 && !skipPrettyPrinting) {
amountAsString = '0';
Expand Down
4 changes: 4 additions & 0 deletions test/jest/__tests__/utils/string-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ describe('string-utils utility functions', () => {

expect(formatCurrency(0, 2, 'USD')).toBe('0 USD');
expect(formatCurrency(0, 4, 'TLOS')).toBe('0 TLOS');

expect(formatCurrency('0.12345', 4, 'TLOS')).toBe('0.1234 TLOS');
expect(formatCurrency('0.0003456', 4, 'TLOS')).toBe('0.0003 TLOS');

});

it('should pretty print when skipPrettyPrinting is true', () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2680,10 +2680,10 @@
"@wharfkit/token" "^1.1.2"
tslib "^2.1.0"

"@wharfkit/[email protected].10":
version "1.0.10"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-1.0.10.tgz#5c63d6b67a4b62038575cd86ef47edaa8d1f57dd"
integrity sha512-12m4nD6+ZZl+mwzySuFkeiJCOrbXQT6mqg9amr1l/JtRewRdxeN9QTsEoPjE5MCTXbJc84X51KCT71ysglse0Q==
"@wharfkit/[email protected].13":
version "1.0.13"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-1.0.13.tgz#216b28da9d9a91ece9db273447f4419aa770657f"
integrity sha512-f4O5O8+6Bd5BHpMUHTmlWQmlhX5xYb4AfzT2NJweyqIPqQOstm+aInF42AtUhSALDa8fvoY80YZoqwM0L8TUyw==
dependencies:
bn.js "^4.11.9"
brorand "^1.1.0"
Expand Down
Loading