diff --git a/src/components/modals/AssetTransferModal.vue b/src/components/modals/AssetTransferModal.vue index 2e1490b92..bccba874c 100644 --- a/src/components/modals/AssetTransferModal.vue +++ b/src/components/modals/AssetTransferModal.vue @@ -326,8 +326,9 @@ export default defineComponent({ get: () => { if (_cryptoAmount.value !== 0) return _cryptoAmount.value; if (!estimate.value || !p.value?.currencyCrypto) return 0; - if (estimate.value.from.asset !== p.value.currencyCrypto) return 0; - return capDecimals(estimate.value.from.amount + estimate.value.from.fee, estimate.value.from.asset); + const { asset, amount, fee } = estimate.value.from; + if (asset !== p.value.currencyCrypto) return 0; + return capDecimals({ amount: amount + fee, asset, decimals: p.value.displayedDecimalsCrypto?.value }); }, set: (value: number) => { _fiatAmount.value = 0; diff --git a/src/components/modals/BuyCryptoModal.vue b/src/components/modals/BuyCryptoModal.vue index b39bfdcc9..fd2ae0f32 100644 --- a/src/components/modals/BuyCryptoModal.vue +++ b/src/components/modals/BuyCryptoModal.vue @@ -435,9 +435,9 @@ export default defineComponent({ get: () => { if (_cryptoAmount.value !== 0) return _cryptoAmount.value; if (!estimate.value) return 0; - - if (estimate.value.to.asset !== activeCurrency.value.toUpperCase()) return 0; - return capDecimals(estimate.value.to.amount - estimate.value.to.fee, estimate.value.to.asset); + const { asset, amount, fee } = estimate.value.to; + if (asset !== activeCurrency.value.toUpperCase()) return 0; + return capDecimals({ amount: amount - fee, asset }); }, set: (value: number) => { _fiatAmount.value = 0; diff --git a/src/components/modals/SellCryptoModal.vue b/src/components/modals/SellCryptoModal.vue index 5d6524b84..312cf268f 100644 --- a/src/components/modals/SellCryptoModal.vue +++ b/src/components/modals/SellCryptoModal.vue @@ -420,8 +420,10 @@ export default defineComponent({ if (_cryptoAmount.value !== 0) return _cryptoAmount.value; if (!estimate.value) return 0; + const { asset, amount, fee } = estimate.value.from; if (estimate.value.from.asset !== activeCurrency.value.toUpperCase()) return 0; - return capDecimals(estimate.value.from.amount + estimate.value.from.fee, estimate.value.from.asset); + if (asset !== activeCurrency.value.toUpperCase()) return 0; + return capDecimals({ amount: amount + fee, asset }); }, set: (value: number) => { _fiatAmount.value = 0; diff --git a/src/composables/asset-transfer/types.ts b/src/composables/asset-transfer/types.ts index 6330764a8..d0570ee89 100644 --- a/src/composables/asset-transfer/types.ts +++ b/src/composables/asset-transfer/types.ts @@ -45,6 +45,7 @@ export interface AssetTransferParams { exchangeRate: Computed; decimalsCrypto: Computed; + displayedDecimalsCrypto: Computed; decimalsFiat: Computed; fiatFees: FundingFees; diff --git a/src/composables/asset-transfer/useSinpeMovilSwap.ts b/src/composables/asset-transfer/useSinpeMovilSwap.ts index f2d33ae31..6e24e8edf 100644 --- a/src/composables/asset-transfer/useSinpeMovilSwap.ts +++ b/src/composables/asset-transfer/useSinpeMovilSwap.ts @@ -41,7 +41,6 @@ import { import { captureException } from '@sentry/vue'; import router, { RouteName } from '@/router'; import { useSinpeMovilStore } from '@/stores/SinpeMovil'; -import { useSettingsStore } from '@/stores/Settings'; import { SwapLimits, useSwapLimits } from '../useSwapLimits'; import SinpeUserInfo from '../../components/SinpeUserInfo.vue'; import AddressSelector from '../../components/AddressSelector.vue'; @@ -76,7 +75,6 @@ export async function useSinpeMovilSwap(options: AssetTransferOptions): Promise< const fiatCurrency = isFiatCurrency(leftAsset) ? leftAsset : rightAsset; const cryptoCurrency = isCryptoCurrency(leftAsset) ? leftAsset : rightAsset; - useSettingsStore().setDecimals(2); // Code in the wallet rely on the global "selectedFiatCurrency" for swaps selectedFiatCurrency.value = fiatCurrency.toLocaleLowerCase() as FiatSwapCurrency; @@ -484,6 +482,7 @@ export async function useSinpeMovilSwap(options: AssetTransferOptions): Promise< exchangeRate, decimalsCrypto, + displayedDecimalsCrypto: computed(() => 2), decimalsFiat, limits: computed(() => limits.value) as unknown as SwapLimits, diff --git a/src/lib/swap/utils/CommonUtils.ts b/src/lib/swap/utils/CommonUtils.ts index 61255668f..b59767f6d 100644 --- a/src/lib/swap/utils/CommonUtils.ts +++ b/src/lib/swap/utils/CommonUtils.ts @@ -75,10 +75,8 @@ export function useCurrentLimitCrypto(currentLimitFiat: Ref) { const rate = exchangeRates.value[activeCurrency.value][selectedFiatCurrency.value]; if (!rate) return null; - return capDecimals( - (currentLimitFiat.value / rate) * (activeCurrency.value === CryptoCurrency.NIM ? 1e5 : 1e8), - activeCurrency.value.toUpperCase() as SwapAsset, - ); + const amount = (currentLimitFiat.value / rate) * (activeCurrency.value === CryptoCurrency.NIM ? 1e5 : 1e8); + return capDecimals({ amount, asset: activeCurrency.value.toUpperCase() as SwapAsset }); }); } @@ -127,7 +125,13 @@ export async function fetchAssets() { assets.value = await getAssets(); } -export function capDecimals(amount: number, asset: SwapAsset) { +export interface CapDecimalsOptions { + amount: number; + asset: SwapAsset; + decimals?: number; +} + +export function capDecimals({ amount, asset, decimals }: CapDecimalsOptions) { if (!amount) return 0; const numberSign = amount / Math.abs(amount); // 1 or -1 @@ -135,7 +139,7 @@ export function capDecimals(amount: number, asset: SwapAsset) { amount = Math.abs(amount); const currencyDecimals = asset === SwapAsset.NIM ? 5 : btcUnit.value.decimals; - const displayDecimals = calculateDisplayedDecimals(amount, asset.toLowerCase() as CryptoCurrency); + const displayDecimals = decimals || calculateDisplayedDecimals(amount, asset.toLowerCase() as CryptoCurrency); const roundingFactor = 10 ** (currencyDecimals - displayDecimals); return Math.floor(amount / roundingFactor) * roundingFactor * numberSign;