Skip to content

Commit

Permalink
chore: add options to capDecimals fn
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Aug 21, 2024
1 parent 72cb58e commit ce869e9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/components/modals/AssetTransferModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 + fee, { asset, decimals: p.value.displayedDecimalsCrypto?.value });
},
set: (value: number) => {
_fiatAmount.value = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/BuyCryptoModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 - fee, { asset });
},
set: (value: number) => {
_fiatAmount.value = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/components/modals/SellCryptoModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 + fee, { asset });
},
set: (value: number) => {
_fiatAmount.value = 0;
Expand Down
1 change: 1 addition & 0 deletions src/composables/asset-transfer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface AssetTransferParams {
exchangeRate: Computed<number>;

decimalsCrypto: Computed<number>;
displayedDecimalsCrypto: Computed<number>;
decimalsFiat: Computed<number>;

fiatFees: FundingFees;
Expand Down
3 changes: 1 addition & 2 deletions src/composables/asset-transfer/useSinpeMovilSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions src/lib/swap/utils/CommonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ export function useCurrentLimitCrypto(currentLimitFiat: Ref<number | null>) {
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 });
});
}

Expand Down Expand Up @@ -127,15 +125,20 @@ export async function fetchAssets() {
assets.value = await getAssets();
}

export function capDecimals(amount: number, asset: SwapAsset) {
export interface CapDecimalsOptions {
asset: SwapAsset;
decimals?: number;
}

export function capDecimals(amount: number, { asset, decimals }: CapDecimalsOptions) {
if (!amount) return 0;

const numberSign = amount / Math.abs(amount); // 1 or -1

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;
Expand Down

0 comments on commit ce869e9

Please sign in to comment.