From b306bb6f7bb3581c0a2cdcbca80f8666b901d0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viterbo=20Rodr=C3=ADguez?= Date: Tue, 25 Mar 2025 04:39:36 -0300 Subject: [PATCH 1/4] move from savings all your funds --- src/components/staking/SavingsTab.vue | 2 +- src/stores/account.ts | 8 +++++--- src/utils/string-utils.ts | 23 ++++++++++++++++++++--- yarn.lock | 8 ++++---- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/components/staking/SavingsTab.vue b/src/components/staking/SavingsTab.vue index a0a0fe0e..074555a4 100644 --- a/src/components/staking/SavingsTab.vue +++ b/src/components/staking/SavingsTab.vue @@ -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; } diff --git a/src/stores/account.ts b/src/stores/account.ts index 957fbb47..18a19105 100644 --- a/src/stores/account.ts +++ b/src/stores/account.ts @@ -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); diff --git a/src/utils/string-utils.ts b/src/utils/string-utils.ts index 8e1f3272..ccb001f9 100644 --- a/src/utils/string-utils.ts +++ b/src/utils/string-utils.ts @@ -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)) && @@ -43,16 +46,30 @@ 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); + const beforeLastDigit = originalAsString.substr(0, precision + 1); + // const beforeLastDigit = originalAsString.substr(0, precision + 2); + const zero = '0.' + '0'.repeat(precision - 1); + if (beforeLastDigit === zero) { + amountAsString = originalAsString.substr(0, precision + 2); + } else { + amountAsString = (+originalAsString).toFixed(precision); + } + + if (amountAsString === '0.0004') { + } + + if (amountAsString.indexOf('.') === -1) { + amountAsString += '.0'; + } if (!skipPrettyPrinting) { // commify amountAsString = (() => { const [integer, fraction] = amountAsString.split('.'); - return `${(+integer).toLocaleString('en')}.${fraction}`; })(); } diff --git a/yarn.lock b/yarn.lock index 0199b981..a463fc3b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2680,10 +2680,10 @@ "@wharfkit/token" "^1.1.2" tslib "^2.1.0" -"@wharfkit/antelope@1.0.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/antelope@1.0.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" From 3763760c27831b7550d44dbbeaccf4b67521f031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viterbo=20Rodr=C3=ADguez?= Date: Tue, 25 Mar 2025 04:43:43 -0300 Subject: [PATCH 2/4] remove unused if --- src/utils/string-utils.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/utils/string-utils.ts b/src/utils/string-utils.ts index ccb001f9..e60395b7 100644 --- a/src/utils/string-utils.ts +++ b/src/utils/string-utils.ts @@ -59,9 +59,6 @@ export function formatCurrency( amountAsString = (+originalAsString).toFixed(precision); } - if (amountAsString === '0.0004') { - } - if (amountAsString.indexOf('.') === -1) { amountAsString += '.0'; } From 33c005fe8503659bbc2f0d14ba3a36ba79061873 Mon Sep 17 00:00:00 2001 From: Viterbo Date: Tue, 25 Mar 2025 21:22:11 -0300 Subject: [PATCH 3/4] fix, fraction part on formatCurrency function --- src/utils/string-utils.ts | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/utils/string-utils.ts b/src/utils/string-utils.ts index e60395b7..357560d4 100644 --- a/src/utils/string-utils.ts +++ b/src/utils/string-utils.ts @@ -49,29 +49,30 @@ export function formatCurrency( const originalAsString = typeof amount === 'number' ? amount.toString() : amount; let amountAsString = originalAsString; - // ensure correct precision - const beforeLastDigit = originalAsString.substr(0, precision + 1); - // const beforeLastDigit = originalAsString.substr(0, precision + 2); - const zero = '0.' + '0'.repeat(precision - 1); - if (beforeLastDigit === zero) { - amountAsString = originalAsString.substr(0, precision + 2); - } else { - amountAsString = (+originalAsString).toFixed(precision); - } - - if (amountAsString.indexOf('.') === -1) { - amountAsString += '.0'; - } - if (!skipPrettyPrinting) { // commify + if (amountAsString.indexOf('.') === -1) { + // only in case of integer + amountAsString = originalAsString + '.' + '0'.repeat(precision); + } amountAsString = (() => { const [integer, fraction] = amountAsString.split('.'); - return `${(+integer).toLocaleString('en')}.${fraction}`; + let fixed_fraction = 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); + } + + return `${(+integer).toLocaleString('en')}.${fixed_fraction}`; })(); } + if (+amountAsString === 0 && !skipPrettyPrinting) { amountAsString = '0'; } From a0d4fde76b3274ce7636deb065a4e62d4d244b33 Mon Sep 17 00:00:00 2001 From: Viterbo Date: Tue, 25 Mar 2025 21:58:27 -0300 Subject: [PATCH 4/4] fixing tests --- src/utils/string-utils.ts | 39 +++++++++---------- .../jest/__tests__/utils/string-utils.spec.ts | 4 ++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/string-utils.ts b/src/utils/string-utils.ts index 357560d4..d1d7edd1 100644 --- a/src/utils/string-utils.ts +++ b/src/utils/string-utils.ts @@ -49,29 +49,28 @@ export function formatCurrency( const originalAsString = typeof amount === 'number' ? amount.toString() : amount; let amountAsString = originalAsString; - if (!skipPrettyPrinting) { - // commify - if (amountAsString.indexOf('.') === -1) { - // only in case of integer - amountAsString = originalAsString + '.' + '0'.repeat(precision); - } - amountAsString = (() => { - const [integer, fraction] = amountAsString.split('.'); - let fixed_fraction = 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); - } - - return `${(+integer).toLocaleString('en')}.${fixed_fraction}`; - })(); + if (amountAsString.indexOf('.') === -1) { + // only in case of integer + amountAsString = originalAsString + '.' + '0'.repeat(precision); } + const [integer, fraction] = amountAsString.split('.'); + let fixed_fraction = 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'; diff --git a/test/jest/__tests__/utils/string-utils.spec.ts b/test/jest/__tests__/utils/string-utils.spec.ts index 4e48b892..f287db99 100644 --- a/test/jest/__tests__/utils/string-utils.spec.ts +++ b/test/jest/__tests__/utils/string-utils.spec.ts @@ -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', () => {