Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

likhith/incorporated market rate calculation with 6 decimals #5770

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8a09eb5
feat: incorporated market rate calculation with 6 decimals
likhith-deriv Jun 20, 2022
d2ae702
ref: refactored code to generate effective rate
likhith-deriv Jun 20, 2022
00aef79
ref: removed commented code
likhith-deriv Jun 20, 2022
eadbd57
fix: round off decimal calculation of effective market rate
likhith-deriv Jun 21, 2022
50abd43
fix: formatting decimals in effective rate
likhith-deriv Jun 22, 2022
572b789
fix: set the form to be dirty when PM is selected
likhith-deriv Jun 22, 2022
4e2c510
fix: checking for change when PM methods are modified
likhith-deriv Jun 22, 2022
2f73dba
fix: enable button for edit ad
likhith-deriv Jun 22, 2022
117ed80
fix: enabling submit method for buy ads
likhith-deriv Jun 22, 2022
276225e
Merge branch 'floating-rate' of github.com:binary-com/deriv-app into …
likhith-deriv Jun 28, 2022
778987b
fix: decimal round off issue in buy-sell
likhith-deriv Jun 28, 2022
e19117f
fix: display min of 2 decimal points
likhith-deriv Jun 28, 2022
940fd27
Merge branch 'floating-rate' of github.com:binary-com/deriv-app into …
likhith-deriv Jun 29, 2022
a946fea
fix: displying initial value of buy-sell ad
likhith-deriv Jun 29, 2022
093d9e4
Trigger build
likhith-deriv Jun 29, 2022
275cf8e
fix: removed unnecessary spacing
likhith-deriv Jun 29, 2022
1bfffff
Trigger build
likhith-deriv Jun 29, 2022
d49947b
fix: effective rate round off in Buy/sell
likhith-deriv Jun 29, 2022
a3d5ebd
Trigger build
likhith-deriv Jun 29, 2022
2d1e327
fix: setting correct ad type
likhith-deriv Jun 30, 2022
c77d578
fix: display order rate amount
likhith-deriv Jun 30, 2022
2ae4742
fix: removed formatted text usage
likhith-deriv Jun 30, 2022
8fe2feb
fix: removed incorrect round off
likhith-deriv Jun 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import PropTypes from 'prop-types';
import React from 'react';
import { Button, Table, Text } from '@deriv/components';
import { formatMoney, isMobile } from '@deriv/shared';
import { isMobile } from '@deriv/shared';
import { observer } from 'mobx-react-lite';
import { useStores } from 'Stores';
import { buy_sell } from 'Constants/buy-sell';
import { ad_type } from 'Constants/floating-rate';
import { localize, Localize } from 'Components/i18next';
import { generateEffectiveRate } from 'Utils/format-value.js';
import './advertiser-page.scss';

const AdvertiserPageRow = ({ row: advert, showAdPopup }) => {
Expand All @@ -25,8 +25,13 @@ const AdvertiserPageRow = ({ row: advert, showAdPopup }) => {
const is_buy_advert = advertiser_page_store.counterparty_type === buy_sell.BUY;
const is_my_advert = advertiser_page_store.advertiser_details_id === general_store.advertiser_id;

const display_effective_rate =
rate_type === ad_type.FIXED ? price_display : parseFloat(floating_rate_store.exchange_rate * (1 + rate / 100));
const { display_effective_rate } = generateEffectiveRate({
price: price_display,
rate_type,
rate,
local_currency,
exchange_rate: floating_rate_store.exchange_rate,
});

const showAdForm = () => {
buy_sell_store.setSelectedAdState(advert);
Expand All @@ -48,7 +53,7 @@ const AdvertiserPageRow = ({ row: advert, showAdPopup }) => {

<div className='advertiser-page__adverts-price'>
<Text color='profit-success' size='s' weight='bold' line_height='m'>
{formatMoney(local_currency, display_effective_rate, true)} {local_currency}
{display_effective_rate} {local_currency}
</Text>
</div>
<div className='advertiser-page__cell-limit'>
Expand Down Expand Up @@ -95,7 +100,7 @@ const AdvertiserPageRow = ({ row: advert, showAdPopup }) => {
<Table.Cell>{`${min_order_amount_limit_display}-${max_order_amount_limit_display} ${currency}`}</Table.Cell>
<Table.Cell className='advertiser-page__adverts-price'>
<Text color='profit-success' line-height='m' size='xs' weight='bold'>
{formatMoney(local_currency, display_effective_rate, true)} {local_currency}
{display_effective_rate} {local_currency}
</Text>
</Table.Cell>
<Table.Cell>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { Text } from '@deriv/components';
import { getFormattedText } from '@deriv/shared';
import { formatMoney } from '@deriv/shared';
import { Localize } from 'Components/i18next';
import { roundOffDecimal } from 'Utils/format-value.js';
import { useStores } from 'Stores';

const BuySellFormReceiveAmount = () => {
Expand All @@ -17,7 +18,12 @@ const BuySellFormReceiveAmount = () => {
)}
</Text>
<Text as='p' color='general' line_height='m' size='xs' weight='bold'>
{getFormattedText(buy_sell_store?.receive_amount, buy_sell_store?.advert?.local_currency)}
{formatMoney(
buy_sell_store?.advert?.local_currency,
roundOffDecimal(buy_sell_store?.receive_amount),
true
)}{' '}
{buy_sell_store?.advert?.local_currency}
</Text>
</React.Fragment>
);
Expand Down
31 changes: 16 additions & 15 deletions packages/p2p/src/components/buy-sell/buy-sell-form.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { Formik, Field, Form } from 'formik';
import { HintBox, Icon, Input, Text } from '@deriv/components';
import { getRoundedNumber, getFormattedText, isDesktop, isMobile, useIsMounted } from '@deriv/shared';
import { getRoundedNumber, isDesktop, isMobile, useIsMounted } from '@deriv/shared';
import { reaction } from 'mobx';
import { observer } from 'mobx-react-lite';
import { localize, Localize } from 'Components/i18next';
Expand All @@ -11,7 +12,7 @@ import { useStores } from 'Stores';
import BuySellFormReceiveAmount from './buy-sell-form-receive-amount.jsx';
import PaymentMethodCard from '../my-profile/payment-methods/payment-method-card/payment-method-card.jsx';
import { floatingPointValidator } from 'Utils/validations';
import classNames from 'classnames';
import { generateEffectiveRate, setDecimalPlaces, roundOffDecimal, removeTrailingZeros } from 'Utils/format-value.js';

const BuySellForm = props => {
const isMounted = useIsMounted();
Expand Down Expand Up @@ -44,8 +45,15 @@ const BuySellForm = props => {
cursor: should_disable_field ? 'not-allowed' : 'pointer',
};

const effective_rate =
rate_type === ad_type.FLOAT ? parseFloat(floating_rate_store.exchange_rate * (1 + rate / 100)) : price;
const { effective_rate, display_effective_rate } = generateEffectiveRate({
price,
rate_type,
rate,
local_currency,
exchange_rate: floating_rate_store.exchange_rate,
});

const calculated_rate = removeTrailingZeros(roundOffDecimal(effective_rate, setDecimalPlaces(effective_rate, 6)));

React.useEffect(
() => {
Expand All @@ -69,7 +77,7 @@ const BuySellForm = props => {

advertiser_page_store.setFormErrorMessage('');
buy_sell_store.setShowRateChangePopup(rate_type === ad_type.FLOAT);
buy_sell_store.setInitialReceiveAmount(getRoundedNumber(effective_rate, buy_sell_store.account_currency));
buy_sell_store.setInitialReceiveAmount(calculated_rate);

return () => {
buy_sell_store.payment_method_ids = [];
Expand Down Expand Up @@ -155,7 +163,7 @@ const BuySellForm = props => {
/>
</Text>
<Text as='p' color='general' line_height='m' size='xs'>
{getFormattedText(effective_rate, local_currency)}
{display_effective_rate} {local_currency}
</Text>
</div>
</div>
Expand Down Expand Up @@ -352,17 +360,10 @@ const BuySellForm = props => {
event.target.value,
buy_sell_store.account_currency
);
setFieldValue('amount', input_amount);

setFieldValue('amount', getRoundedNumber(input_amount));
buy_sell_store.setReceiveAmount(
getRoundedNumber(
input_amount *
getRoundedNumber(
effective_rate,
buy_sell_store.account_currency
),
buy_sell_store.account_currency
)
input_amount * calculated_rate
);
}
}}
Expand Down
17 changes: 11 additions & 6 deletions packages/p2p/src/components/buy-sell/buy-sell-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Table, Text, Button, Icon } from '@deriv/components';
import { formatMoney, isMobile } from '@deriv/shared';
import { isMobile } from '@deriv/shared';
import { observer } from 'mobx-react-lite';
import { buy_sell } from 'Constants/buy-sell';
import { Localize, localize } from 'Components/i18next';
import UserAvatar from 'Components/user/user-avatar';
import { ad_type } from 'Constants/floating-rate';
import { useStores } from 'Stores';
import { generateEffectiveRate } from 'Utils/format-value.js';
import './buy-sell-row.scss';
import TradeBadge from '../trade-badge';

Expand Down Expand Up @@ -49,8 +49,13 @@ const BuySellRow = ({ row: advert }) => {
const is_my_advert = advert.advertiser_details.id === general_store.advertiser_id;
const is_buy_advert = counterparty_type === buy_sell.BUY;
const { name: advertiser_name } = advert.advertiser_details;
const display_effective_rate =
rate_type === ad_type.FIXED ? price_display : parseFloat(floating_rate_store.exchange_rate * (1 + rate / 100));
const { display_effective_rate } = generateEffectiveRate({
price: price_display,
rate_type,
rate,
local_currency,
exchange_rate: floating_rate_store.exchange_rate,
});

if (isMobile()) {
return (
Expand Down Expand Up @@ -97,7 +102,7 @@ const BuySellRow = ({ row: advert }) => {
/>
</Text>
<Text as='div' color='profit-success' line_height='m' size='s' weight='bold'>
{formatMoney(local_currency, display_effective_rate, true)} {local_currency}
{display_effective_rate} {local_currency}
</Text>
<Text as='div' color='less-prominent' line_height='m' size='xxs'>
<Localize
Expand Down Expand Up @@ -182,7 +187,7 @@ const BuySellRow = ({ row: advert }) => {
</Table.Cell>
<Table.Cell>
<Text color='profit-success' size='xs' line-height='m' weight='bold'>
{formatMoney(local_currency, display_effective_rate, true)} {local_currency}
{display_effective_rate} {local_currency}
</Text>
</Table.Cell>
<Table.Cell>
Expand Down
10 changes: 8 additions & 2 deletions packages/p2p/src/components/floating-rate/floating-rate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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 './floating-rate.scss';

const FloatingRate = ({
Expand Down Expand Up @@ -92,7 +93,8 @@ const FloatingRate = ({
line_height='xs'
className='floating-rate__mkt-rate--msg'
>
1 {fiat_currency} = {formatMoney(local_currency, floating_rate_store.exchange_rate, true)}{' '}
1 {fiat_currency} ={' '}
{removeTrailingZeros(formatMoney(local_currency, floating_rate_store.exchange_rate, true, 6))}{' '}
{local_currency}
</Text>
</div>
Expand All @@ -117,7 +119,11 @@ const FloatingRate = ({
line_height='xs'
className='floating-rate__hint'
>
{localize('Your rate is')} = {formatMoney(local_currency, market_feed, true)} {local_currency}
{localize('Your rate is')} ={' '}
{removeTrailingZeros(
formatMoney(local_currency, market_feed, true, setDecimalPlaces(market_feed, 6))
)}{' '}
{local_currency}
</Text>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { localize } from 'Components/i18next';
import PropTypes from 'prop-types';
import './buy-ad-payment-methods-list.scss';

const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods }) => {
const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods, touched }) => {
const { my_ads_store, my_profile_store } = useStores();
const [selected_edit_method, setSelectedEditMethod] = React.useState();
const [payment_methods_list, setPaymentMethodsList] = React.useState([]);
Expand All @@ -33,6 +33,7 @@ const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods }) => {
text: my_profile_store.getPaymentMethodDisplayName(value),
},
]);
touched(true);
}
};

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

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

Expand Down
27 changes: 13 additions & 14 deletions packages/p2p/src/components/my-ads/create-ad-summary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { observer } from 'mobx-react-lite';
import { Text } from '@deriv/components';
import { buy_sell } from 'Constants/buy-sell';
import { Localize } from 'Components/i18next';
import { useStores } from 'Stores';
import { ad_type } from 'Constants/floating-rate';
import { useStores } from 'Stores';
import { removeTrailingZeros, roundOffDecimal } from 'Utils/format-value.js';

const CreateAdSummary = ({ offer_amount, price_rate, type }) => {
const { floating_rate_store, general_store } = useStores();
Expand All @@ -17,20 +18,16 @@ const CreateAdSummary = ({ offer_amount, price_rate, type }) => {
let display_price_rate = '';
let display_total = '';

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

if (market_feed && offer_amount && price_rate) {
display_total = formatMoney(
local_currency_config.currency,
offer_amount * parseFloat(market_feed * (1 + price_rate / 100)),
true
);
} else if (offer_amount && price_rate) {
display_total = formatMoney(local_currency_config.currency, offer_amount * price_rate, true);
if (offer_amount && price_rate) {
display_total = market_feed
? formatMoney(local_currency_config.currency, offer_amount * display_price_rate, true)
: formatMoney(local_currency_config.currency, offer_amount * price_rate, true);
}

if (offer_amount) {
Expand All @@ -43,7 +40,9 @@ const CreateAdSummary = ({ offer_amount, price_rate, type }) => {
Object.assign(values, {
local_amount: display_total,
local_currency: local_currency_config.currency,
price_rate: formatMoney(local_currency_config.currency, display_price_rate, true),
price_rate: removeTrailingZeros(
formatMoney(local_currency_config.currency, display_price_rate, true, 6)
),
});

if (type === buy_sell.BUY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ const EditAdFormPaymentMethods = ({ is_sell_advert, selected_methods, setSelecte
);
}

return <BuyAdPaymentMethodsList selected_methods={selected_methods} setSelectedMethods={setSelectedMethods} />;
return (
<BuyAdPaymentMethodsList
selected_methods={selected_methods}
setSelectedMethods={setSelectedMethods}
touched={touched}
/>
);
};

export default observer(EditAdFormPaymentMethods);
10 changes: 8 additions & 2 deletions packages/p2p/src/components/my-ads/edit-ad-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const EditAdForm = () => {
const [is_cancel_edit_modal_open, setIsCancelEditModalOpen] = React.useState(false);
const [is_payment_method_touched, setIsPaymentMethodTouched] = React.useState(false);

const set_initial_ad_rate = () => {
const setInitialAdRate = () => {
if (my_ads_store.required_ad_type !== my_ads_store.selected_ad_type) {
if (my_ads_store.required_ad_type === ad_type.FLOAT) {
return is_buy_advert ? '-0.01' : '+0.01';
Expand Down Expand Up @@ -110,6 +110,12 @@ const EditAdForm = () => {
my_ads_store.payment_method_ids.push(pm[0]);
});
}
if (my_ads_store.required_ad_type !== rate_type) {
const is_payment_method_available =
!!Object.keys({ ...payment_method_details }).length ||
!!Object.values({ ...payment_method_names }).length;
setIsPaymentMethodTouched(is_payment_method_available);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand All @@ -130,7 +136,7 @@ const EditAdForm = () => {
max_transaction: max_order_amount_display,
min_transaction: min_order_amount_display,
offer_amount: amount_display,
rate_type: set_initial_ad_rate(),
rate_type: setInitialAdRate(),
type,
is_active:
rate_type !== floating_rate_store.rate_type && floating_rate_store.reached_target_date
Expand Down
Loading