diff --git a/src/components/App/Alerts/AlertList.vue b/src/components/App/Alerts/AlertList.vue index af29102f4..3f1966645 100644 --- a/src/components/App/Alerts/AlertList.vue +++ b/src/components/App/Alerts/AlertList.vue @@ -53,7 +53,7 @@ import { Component, Mixins } from 'vue-property-decorator'; import { ZeroStringValue } from '@/consts'; import { getter, mutation, state } from '@/store/decorators'; -import { calcPriceChange, showMostFittingValue } from '@/utils'; +import { calcPriceChange, showMostFittingValue, toPrecision } from '@/utils'; import type { AccountAsset } from '@sora-substrate/util/build/assets/types'; import type { Alert, WhitelistIdsBySymbol } from '@soramitsu/soraneo-wallet-web/lib/types/common'; @@ -118,7 +118,7 @@ export default class AlertList extends Mixins( const asset = this.getAsset(this.whitelistIdsBySymbol[alert.token]); const currentPrice = FPNumber.fromCodecValue(this.getAssetFiatPrice(asset) ?? ZeroStringValue); const priceChange = calcPriceChange(desiredPrice, currentPrice); - const priceChangeFormatted = priceChange.dp(2).toString(); + const priceChangeFormatted = toPrecision(priceChange, 2).toString(); const currentPriceFormatted = showMostFittingValue(currentPrice); return `${priceChangeFormatted}% ยท ${this.t('alerts.currentPrice')}: $${currentPriceFormatted}`; diff --git a/src/components/shared/PriceChange.vue b/src/components/shared/PriceChange.vue index 2ed9b589d..d727b6c49 100644 --- a/src/components/shared/PriceChange.vue +++ b/src/components/shared/PriceChange.vue @@ -10,6 +10,8 @@ import { FPNumber } from '@sora-substrate/util'; import { components, WALLET_CONSTS } from '@soramitsu/soraneo-wallet-web'; import { Component, Prop, Vue } from 'vue-property-decorator'; +import { toPrecision } from '@/utils'; + @Component({ components: { FormattedAmount: components.FormattedAmount, @@ -39,9 +41,9 @@ export default class PriceChange extends Vue { get formatted(): string { const value = this.increased ? this.value : this.value.mul(new FPNumber(-1)); - const number = value.toFixed(2); + const number = toPrecision(value, 2); - return new FPNumber(number).toLocaleString(); + return number.toLocaleString(); } } diff --git a/src/store/soraCard/actions.ts b/src/store/soraCard/actions.ts index 7927a1753..636f8183b 100644 --- a/src/store/soraCard/actions.ts +++ b/src/store/soraCard/actions.ts @@ -3,7 +3,7 @@ import { api, ScriptLoader } from '@soramitsu/soraneo-wallet-web'; import { defineActions } from 'direct-vuex'; import { soraCardActionContext } from '@/store/soraCard'; -import { waitForAccountPair, waitForSoraNetworkFromEnv } from '@/utils'; +import { waitForAccountPair, waitForSoraNetworkFromEnv, toPrecision } from '@/utils'; import { defineUserStatus, getXorPerEuroRatio, @@ -22,7 +22,7 @@ const actions = defineActions({ const euroToPay = FPNumber.HUNDRED.add(FPNumber.ONE).sub(totalXorBalance.mul(xorPerEuro)); const euroToPayInXor = euroToPay.div(xorPerEuro); - commit.setXorPriceToDeposit(euroToPayInXor.dp(3)); // TODO: round up number + commit.setXorPriceToDeposit(toPrecision(euroToPayInXor, 3)); // TODO: round up number }, async calculateXorBalanceInEuros(context, { xorPerEuro, xorTotalBalance }): Promise { diff --git a/src/utils/index.ts b/src/utils/index.ts index 1cf2d1096..67b224327 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -98,6 +98,11 @@ export const getMaxValue = ( return getMaxBalance(asset, fee, { isExternalBalance, isExternalNative, isBondedBalance }).toString(); }; +/** Change FPNumber precision (`FPNumber.dp()` has issues) */ +export const toPrecision = (value: FPNumber, precision: number): FPNumber => { + return new FPNumber(value.toFixed(precision), precision); +}; + /** * Returns formatted value in most suitable form * @param value @@ -112,7 +117,7 @@ export const showMostFittingValue = ( const [integer, decimal = '00'] = value.toString().split('.'); const precision = parseInt(integer) > 0 ? 2 : Math.min(decimal.search(/[1-9]/) + 2, precisionForLowCostAsset); - return value.dp(precision).toLocaleString(); + return toPrecision(value, precision).toLocaleString(); }; export const hasInsufficientBalance = ( diff --git a/src/views/Bridge.vue b/src/views/Bridge.vue index 553a3727c..654a7d4c2 100644 --- a/src/views/Bridge.vue +++ b/src/views/Bridge.vue @@ -351,6 +351,7 @@ import { getAssetBalance, asZeroValue, delay, + toPrecision, } from '@/utils'; import type { IBridgeTransaction, CodecString } from '@sora-substrate/util'; @@ -485,16 +486,16 @@ export default class Bridge extends Mixins( if (!(this.asset && this.isRegisteredAsset)) return ZeroStringValue; const fee = this.isSoraToEvm ? this.soraNetworkFee : this.externalNetworkFee; - const maxBalance = getMaxBalance(this.asset, fee, { + let maxBalance = getMaxBalance(this.asset, fee, { isExternalBalance: !this.isSoraToEvm, isExternalNative: this.isNativeTokenSelected, }); - if (this.transferMaxAmount) { - if (FPNumber.gt(maxBalance, this.transferMaxAmount)) return this.transferMaxAmount.toString(); + if (this.transferMaxAmount && FPNumber.gt(maxBalance, this.transferMaxAmount)) { + maxBalance = this.transferMaxAmount; } - return maxBalance.toString(); + return toPrecision(maxBalance, this.amountDecimals).toString(); } get isMaxAvailable(): boolean {