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..d1d7edd1 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,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'; 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', () => { 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"