Skip to content

Commit

Permalink
fix: sunc effective_rate calculation b/w FE & BE
Browse files Browse the repository at this point in the history
  • Loading branch information
likhith-deriv committed Jul 6, 2022
1 parent 9364c9d commit 6783fec
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
9 changes: 4 additions & 5 deletions packages/p2p/src/components/floating-rate/floating-rate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { InputField, Text } from '@deriv/components';
import { formatMoney, isMobile, mobileOSDetect } from '@deriv/shared';
import { localize } from 'Components/i18next';
import { useStores } from 'Stores';
import { setDecimalPlaces, removeTrailingZeros } from 'Utils/format-value.js';
import { setDecimalPlaces, removeTrailingZeros, percentOf, roundOffDecimal } from 'Utils/format-value.js';
import './floating-rate.scss';

const FloatingRate = ({
Expand All @@ -24,9 +24,8 @@ const FloatingRate = ({
const os = mobileOSDetect();
const { name, value, required } = props;

const market_feed = value
? parseFloat(floating_rate_store.exchange_rate * (1 + value / 100))
: floating_rate_store.exchange_rate;
const market_feed = value ? percentOf(floating_rate_store.exchange_rate, value) : floating_rate_store.exchange_rate;
const decimal_place = setDecimalPlaces(market_feed, 6);

// Input mask for formatting value on blur of floating rate field
const onBlurHandler = e => {
Expand Down Expand Up @@ -121,7 +120,7 @@ const FloatingRate = ({
>
{localize('Your rate is')} ={' '}
{removeTrailingZeros(
formatMoney(local_currency, market_feed, true, setDecimalPlaces(market_feed, 6))
formatMoney(local_currency, roundOffDecimal(market_feed, decimal_place), true, decimal_place)
)}{' '}
{local_currency}
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods, touched
text: my_profile_store.getPaymentMethodDisplayName(value),
},
]);
touched(true);
if (typeof touched === 'function') touched(true);
}
};

Expand All @@ -47,7 +47,7 @@ const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods, touched
...payment_methods_list.filter(payment_method => payment_method.value !== value),
selected_edit_method,
]);
touched(true);
if (typeof touched === 'function') touched(true);
}
};

Expand All @@ -58,7 +58,7 @@ const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods, touched
setSelectedMethods([...selected_methods, value]);
setPaymentMethodsList(payment_methods_list.filter(payment_method => payment_method.value !== value));
}
touched(true);
if (typeof touched === 'function') touched(true);
}
};

Expand Down
6 changes: 2 additions & 4 deletions packages/p2p/src/components/my-ads/create-ad-summary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { buy_sell } from 'Constants/buy-sell';
import { Localize } from 'Components/i18next';
import { ad_type } from 'Constants/floating-rate';
import { useStores } from 'Stores';
import { removeTrailingZeros, roundOffDecimal } from 'Utils/format-value.js';
import { removeTrailingZeros, roundOffDecimal, percentOf } from 'Utils/format-value.js';

const CreateAdSummary = ({ offer_amount, price_rate, type }) => {
const { floating_rate_store, general_store } = useStores();
Expand All @@ -19,9 +19,7 @@ const CreateAdSummary = ({ offer_amount, price_rate, type }) => {
let display_total = '';

if (price_rate) {
display_price_rate = market_feed
? roundOffDecimal(parseFloat(market_feed * (1 + price_rate / 100)), 6)
: price_rate;
display_price_rate = market_feed ? roundOffDecimal(percentOf(market_feed, price_rate), 6) : price_rate;
}

if (offer_amount && price_rate) {
Expand Down
6 changes: 2 additions & 4 deletions packages/p2p/src/components/my-ads/edit-ad-summary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { buy_sell } from 'Constants/buy-sell';
import { Localize } from 'Components/i18next';
import { ad_type } from 'Constants/floating-rate';
import { useStores } from 'Stores';
import { removeTrailingZeros, roundOffDecimal } from 'Utils/format-value.js';
import { removeTrailingZeros, roundOffDecimal, percentOf } from 'Utils/format-value.js';

const EditAdSummary = ({ offer_amount, price_rate, type }) => {
const { floating_rate_store, general_store, my_ads_store } = useStores();
Expand All @@ -19,9 +19,7 @@ const EditAdSummary = ({ offer_amount, price_rate, type }) => {
let display_total = '';

if (price_rate) {
display_price_rate = market_feed
? roundOffDecimal(parseFloat(market_feed * (1 + price_rate / 100)), 6)
: price_rate;
display_price_rate = market_feed ? roundOffDecimal(percentOf(market_feed, price_rate), 6) : price_rate;
}

if (offer_amount && price_rate) {
Expand Down
17 changes: 14 additions & 3 deletions packages/p2p/src/utils/format-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export const roundOffDecimal = (number, decimal_place = 2) => {
if (number === null || number === undefined) {
return 0;
}
return parseFloat(Math.round(number * Math.pow(10, decimal_place)) / Math.pow(10, decimal_place));

return parseFloat(number).toFixed(decimal_place);
// TODO: Uncomment the below line once BE has resolved the rounding issue
// return parseFloat(Math.round(number * Math.pow(10, decimal_place)) / Math.pow(10, decimal_place));
};

export const setDecimalPlaces = (value, expected_decimal_place) => {
Expand All @@ -18,6 +21,13 @@ export const setDecimalPlaces = (value, expected_decimal_place) => {
return actual_decimal_place > expected_decimal_place ? expected_decimal_place : actual_decimal_place;
};

export const percentOf = (number, percent) => {
// This method is used for computing the effective percent of a number
const parsed_number = parseFloat(number);
const parsed_percent = parseFloat(percent);
return parsed_number + parsed_number * (parsed_percent / 100);
};

export const generateEffectiveRate = ({
price = 0,
rate = 0,
Expand All @@ -31,9 +41,10 @@ export const generateEffectiveRate = ({
effective_rate = price;
display_effective_rate = formatMoney(local_currency, effective_rate, true);
} else {
effective_rate = parseFloat(exchange_rate * (1 + rate / 100));
effective_rate = percentOf(exchange_rate, rate);
const decimal_place = setDecimalPlaces(effective_rate, 6);
display_effective_rate = removeTrailingZeros(
formatMoney(local_currency, effective_rate, true, setDecimalPlaces(effective_rate, 6))
formatMoney(local_currency, roundOffDecimal(effective_rate, decimal_place), true, decimal_place)
);
}
return { effective_rate, display_effective_rate };
Expand Down

0 comments on commit 6783fec

Please sign in to comment.