Skip to content

Commit

Permalink
fix: mismatch duration values
Browse files Browse the repository at this point in the history
  • Loading branch information
akmal-deriv committed Dec 2, 2024
1 parent 07c6de8 commit e42dbe1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,26 @@ const DayInput = ({
const [trigger_date, setTriggerDate] = useState(false);
const [is_disabled, setIsDisabled] = useState(false);
const [end_date_input, setEndDateInput] = useState(end_date);
const [payout_per_point, setPayoutPerPoint] = useState<number | undefined>();
const [barrier_value, setBarrierValue] = useState<string | undefined>();
const { common } = useStore();
const [day, setDay] = useState<number | null>(null);
const { server_time } = common;
const {
barrier_1,
contract_type,
duration_min_max,
duration_units_list,
duration,
expiry_date,
market_open_times,
is_turbos,
market_close_times,
duration_units_list,
market_open_times,
start_date,
start_time,
duration_min_max,
trade_types,
contract_type,
duration,
tick_data,
symbol,
barrier_1,
tick_data,
trade_types,
} = useTraderStore();
const trade_store = useTraderStore();
const { addSnackbar } = useSnackbar();
Expand All @@ -72,6 +75,8 @@ const DayInput = ({
basis: 'stake',
amount: '5',
symbol,
...(payout_per_point && { payout_per_point }),
...(barrier_value && { barrier: barrier_value }),
};

const proposal_req = getProposalRequestObject({
Expand All @@ -81,11 +86,11 @@ const DayInput = ({
});

const { data: response } = useDtraderQuery<ProposalResponse>(
['proposal', JSON.stringify(day)],
['proposal', JSON.stringify(day), JSON.stringify(payout_per_point), JSON.stringify(barrier_value)],
{
...proposal_req,
symbol,
...(barrier_1 ? { barrier: Math.round(tick_data?.quote as number) } : {}),
...(barrier_1 && !is_turbos && !barrier_value ? { barrier: Math.round(tick_data?.quote as number) } : {}),
},
{
enabled: trigger_date,
Expand All @@ -94,6 +99,24 @@ const DayInput = ({

useEffect(() => {
if (response) {
if (response?.error?.code === 'ContractBuyValidationError') {
const details = response.error.details;

if (details?.field === 'payout_per_point' && details?.payout_per_point_choices) {
const suggested_payout = details?.payout_per_point_choices[0];
setPayoutPerPoint(suggested_payout);
setTriggerDate(true);
return;
}

if (details?.field === 'barrier' && details?.barrier_choices) {
const suggested_barrier = details?.barrier_choices[0];
setBarrierValue(suggested_barrier);
setTriggerDate(true);
return;
}
}

if (response?.error?.message && response?.error?.details?.field === 'duration') {
addSnackbar({
message: <Localize i18n_default_text={response?.error?.message} />,
Expand All @@ -115,7 +138,12 @@ const DayInput = ({
);
}

invalidateDTraderCache(['proposal', JSON.stringify(day)]);
invalidateDTraderCache([
'proposal',
JSON.stringify(day),
JSON.stringify(payout_per_point),
JSON.stringify(barrier_value),
]);
setTriggerDate(false);
}
}, [response, setExpiryTimeInput]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export const createProposalRequestForContract = (store: TTradeStore, type_of_con
...((store.barrier_count > 0 || store.form_components.indexOf('last_digit') !== -1) &&
!isAccumulatorContract(type_of_contract) &&
!isTurbosContract(type_of_contract) && {
barrier: store.barrier_1 || store.last_digit,
barrier: store.barrier || store.barrier_1 || store.last_digit,
}),
...(store.barrier_count === 2 && !isAccumulatorContract(type_of_contract) && { barrier2: store.barrier_2 }),
...(isTurbosContract(type_of_contract) && {
Expand Down
4 changes: 3 additions & 1 deletion packages/trader/src/Stores/Modules/Trading/trade-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export type ProposalResponse = PriceProposalResponse & {
message: string;
details?: {
payout_per_point_choices?: number[];
barrier_choices?: string[];
[k: string]: unknown;
};
};
Expand Down Expand Up @@ -256,6 +257,7 @@ export default class TradeStore extends BaseStore {
expiry_type: string | null = 'duration';

// Barrier
barrier = '';
barrier_1 = '';
barrier_2 = '';
barrier_count = 0;
Expand Down Expand Up @@ -1374,7 +1376,7 @@ export default class TradeStore extends BaseStore {
const is_crypto = isCryptocurrency(this.currency ?? '');
const default_crypto_value = getMinPayout(this.currency ?? '') ?? '';
this.setV2ParamsInitialValues({
value: is_crypto ? default_crypto_value : this.default_stake ?? '',
value: is_crypto ? default_crypto_value : (this.default_stake ?? ''),
name: 'stake',
});
obj_new_values.amount = is_crypto ? default_crypto_value : this.default_stake;
Expand Down

0 comments on commit e42dbe1

Please sign in to comment.