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

maryia/87383/feat: connected proposal API + updated trade params #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions packages/core/src/Stores/contract-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BARRIER_LINE_STYLES,
isBarrierSupported,
getEndTime,
isTurbosContract,
} from '@deriv/shared';
import { getChartConfig } from './Helpers/logic';
import { setLimitOrderBarriers, getLimitOrder } from './Helpers/limit-orders';
Expand Down Expand Up @@ -122,8 +123,9 @@ export default class ContractStore extends BaseStore {
}

const is_multiplier = isMultiplierContract(this.contract_info.contract_type);
const is_turbos = isTurbosContract(this.contract_info.contract_type);

if (is_multiplier && contract_info.contract_id && contract_info.limit_order) {
if ((is_multiplier || is_turbos) && contract_info.contract_id && contract_info.limit_order) {
this.populateContractUpdateConfig(this.contract_info);
}
}
Expand Down Expand Up @@ -212,7 +214,9 @@ export default class ContractStore extends BaseStore {
}

updateLimitOrder() {
const limit_order = getLimitOrder(this);
const limit_order = isTurbosContract(this.contract_info.contract_type)
? { take_profit: getLimitOrder(this).take_profit }
: getLimitOrder(this);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for Turbos we need only take_profit, not entire limit_order object


WS.contractUpdate(this.contract_id, limit_order).then(response => {
if (response.error) {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/utils/constants/barriers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const CONTRACT_SHADES = {
ASIAND: 'BELOW',
MULTUP: 'ABOVE',
MULTDOWN: 'BELOW',
TURBOSLONG: 'ABOVE',
TURBOSSHORT: 'BELOW',
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added barriers for Turbos here so that they appear on the chart

};

// Default non-shade according to number of barriers
Expand Down
17 changes: 12 additions & 5 deletions packages/shared/src/utils/constants/contract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,24 @@ export const getContractTypesConfig: TGetContractTypesConfig = symbol => ({
],
config: { hide_duration: true },
}, // hide Duration for Multiplier contracts for now
turbos: {
title: localize('Turbos'),
trade_types: ['TURBOSLONG', 'TURBOSSHORT'],
turboslong: {
title: localize('Long'),
trade_types: ['TURBOSLONG'],
basis: ['stake'],
barrier_count: 1,
components: ['take_profit'],
components: ['barrier', 'take_profit'],
},
turbosshort: {
title: localize('Short'),
trade_types: ['TURBOSSHORT'],
basis: ['stake'],
barrier_count: 1,
components: ['barrier', 'take_profit'],
},
});

export const getContractCategoriesConfig = () => ({
Turbos: { name: localize('Turbos'), categories: ['turbos'] },
Turbos: { name: localize('Turbos'), categories: ['turboslong', 'turbosshort'] },
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'turboslong' and 'turbosshort' are separate categories of Turbos trade type

Multipliers: { name: localize('Multipliers'), categories: ['multiplier'] },
'Ups & Downs': {
name: localize('Ups & Downs'),
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/utils/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const hasContractEntered = (contract_info: TContractInfo) => !!contract_i

export const isMultiplierContract = (contract_type: string) => /MULT/i.test(contract_type);

export const isTurbosContract = (contract_type: string) => /TURBOS/i.test(contract_type);

export const isCryptoContract = (underlying: string) => /^cry/.test(underlying);

type TGetCurrentTick = TContractInfo & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const PurchaseFieldset = ({
is_multiplier,
is_proposal_empty,
is_proposal_error,
is_turbos,
purchased_states_arr,
onClickPurchase,
onHoverPurchase,
Expand All @@ -38,7 +39,7 @@ const PurchaseFieldset = ({
<PurchaseButton
buy_info={buy_info}
currency={currency}
info={info}
info={!is_turbos && info}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hiding info from purchase button for Turbos

index={index}
has_deal_cancellation={is_multiplier && has_cancellation}
is_disabled={is_disabled}
Expand Down Expand Up @@ -154,6 +155,7 @@ PurchaseFieldset.propTypes = {
is_multiplier: PropTypes.bool,
is_proposal_empty: PropTypes.bool,
is_proposal_error: PropTypes.bool,
is_turbos: PropTypes.bool,
onClickPurchase: PropTypes.func,
onHoverPurchase: PropTypes.func,
purchased_states_arr: PropTypes.array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const Amount = ({
is_multiplier,
is_nativepicker,
is_single_currency,
is_turbos,
has_equals_only,
onChange,
setCurrentFocus,
Expand Down Expand Up @@ -95,7 +96,7 @@ const Amount = ({
return (
<Fieldset
className='trade-container__fieldset center-text'
header={is_multiplier ? localize('Stake') : undefined}
header={is_multiplier || is_turbos ? localize('Stake') : undefined}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Showing Stake above the stake input for Turbos as well

header_tooltip={
is_multiplier ? (
<Localize i18n_default_text='Your gross profit is the percentage change in market price times your stake and the multiplier chosen here.' />
Expand Down Expand Up @@ -191,6 +192,7 @@ Amount.propTypes = {
is_multiplier: PropTypes.bool,
is_nativepicker: PropTypes.bool,
is_single_currency: PropTypes.bool,
is_turbos: PropTypes.bool,
has_equals_only: PropTypes.bool,
setCurrentFocus: PropTypes.func,
onChange: PropTypes.func,
Expand All @@ -212,6 +214,7 @@ export default connect(({ modules, client, ui }) => ({
is_equal: modules.trade.is_equal,
is_single_currency: client.is_single_currency,
is_multiplier: modules.trade.is_multiplier,
is_turbos: modules.trade.is_turbos,
has_equals_only: modules.trade.has_equals_only,
stop_out: modules.trade.stop_out,
onChange: modules.trade.onChange,
Expand Down
4 changes: 4 additions & 0 deletions packages/trader/src/Modules/Trading/Containers/purchase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Purchase = ({
purchased_states_arr,
// is_purchase_locked,
is_trade_enabled,
is_turbos,
onClickPurchase,
onHoverPurchase,
// togglePurchaseLock,
Expand Down Expand Up @@ -62,6 +63,7 @@ const Purchase = ({
// is_purchase_confirm_on={is_purchase_confirm_on}
is_proposal_empty={is_proposal_empty}
is_proposal_error={is_proposal_error}
is_turbos={is_turbos}
purchased_states_arr={purchased_states_arr}
// is_purchase_locked={is_purchase_locked}
// togglePurchaseLock={togglePurchaseLock}
Expand Down Expand Up @@ -96,6 +98,7 @@ Purchase.propTypes = {
// is_purchase_confirm_on : PropTypes.bool,
is_purchase_locked: PropTypes.bool,
is_trade_enabled: PropTypes.bool,
is_turbos: PropTypes.bool,
onClickPurchase: PropTypes.func,
onHoverPurchase: PropTypes.func,
proposal_info: PropTypes.object,
Expand All @@ -115,6 +118,7 @@ export default connect(({ modules, ui }) => ({
is_purchase_enabled: modules.trade.is_purchase_enabled,
is_trade_enabled: modules.trade.is_trade_enabled,
is_multiplier: modules.trade.is_multiplier,
is_turbos: modules.trade.is_turbos,
onClickPurchase: modules.trade.onPurchase,
onHoverPurchase: modules.trade.onHoverPurchase,
proposal_info: modules.trade.proposal_info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const CONTRACT_SHADES = {
ASIAND: 'BELOW',
MULTUP: 'ABOVE',
MULTDOWN: 'BELOW',
TURBOSLONG: 'ABOVE',
TURBOSSHORT: 'BELOW',
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added barriers for Turbos here so that they appear on the chart

};

// Default non-shade according to number of barriers
Expand Down
13 changes: 12 additions & 1 deletion packages/trader/src/Stores/Modules/Trading/Helpers/proposal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDecimalPlaces, getPropertyValue, convertToUnix, toMoment } from '@deriv/shared';
import { getDecimalPlaces, getPropertyValue, convertToUnix, isTurbosContract, toMoment } from '@deriv/shared';

const isVisible = elem => !(!elem || (elem.offsetWidth === 0 && elem.offsetHeight === 0));

Expand Down Expand Up @@ -41,6 +41,10 @@ export const getProposalInfo = (store, response, obj_prev_contract_basis) => {

const commission = proposal.commission;
const cancellation = proposal.cancellation;
const turbos_details = {
barrier_choices: proposal.barrier_choices || response?.error?.details?.barrier_choices,
number_of_contracts: proposal.number_of_contracts,
};
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

values specific for Turbos that we extract from proposal response


return {
commission,
Expand All @@ -58,6 +62,7 @@ export const getProposalInfo = (store, response, obj_prev_contract_basis) => {
profit: profit.toFixed(getDecimalPlaces(store.currency)),
returns: `${returns.toFixed(2)}%`,
stake,
...turbos_details,
};
};

Expand Down Expand Up @@ -90,6 +95,7 @@ const setProposalMultiplier = (store, obj_multiplier) => {
const createProposalRequestForContract = (store, type_of_contract) => {
const obj_expiry = {};
const obj_multiplier = {};
let limit_order;

if (store.expiry_type === 'endtime') {
const expiry_date = toMoment(store.expiry_date);
Expand All @@ -100,6 +106,10 @@ const createProposalRequestForContract = (store, type_of_contract) => {
setProposalMultiplier(store, obj_multiplier);
}

if (isTurbosContract(store.contract_type) && store.has_take_profit && store.take_profit) {
limit_order = { take_profit: +store.take_profit || 0 };
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting take profit for Turbos to send it in proposal request

}

return {
proposal: 1,
subscribe: 1,
Expand All @@ -120,5 +130,6 @@ const createProposalRequestForContract = (store, type_of_contract) => {
}),
...(store.barrier_count === 2 && { barrier2: store.barrier_2 }),
...obj_multiplier,
limit_order,
};
};
28 changes: 27 additions & 1 deletion packages/trader/src/Stores/Modules/Trading/trade-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getBarrierPipSize,
isBarrierSupported,
removeBarrier,
isTurbosContract,
} from '@deriv/shared';
import { localize } from '@deriv/translations';
import { getValidationRules, getMultiplierValidationRules } from 'Stores/Modules/Trading/Constants/validation-rules';
Expand Down Expand Up @@ -131,6 +132,7 @@ export default class TradeStore extends BaseStore {
cancellation_range_list = [];

// Turbos trade params
number_of_contracts = 0;
turbos_barrier_choices = [];

// Mobile
Expand Down Expand Up @@ -275,6 +277,7 @@ export default class TradeStore extends BaseStore {
requestProposal: action.bound,
forgetAllProposal: action.bound,
setMarketStatus: action.bound,
number_of_contracts: observable,
onProposalResponse: action.bound,
onChartBarrierChange: action.bound,
onAllowEqualsChange: action.bound,
Expand All @@ -297,6 +300,7 @@ export default class TradeStore extends BaseStore {
chartStateChange: action.bound,
has_alternative_source: computed,
is_multiplier: computed,
is_turbos: computed,
getFirstOpenMarket: action.bound,
});

Expand Down Expand Up @@ -343,7 +347,7 @@ export default class TradeStore extends BaseStore {
() => [this.contract_type],
() => {
this.root_store.portfolio.setContractType(this.contract_type);
if (this.contract_type === 'multiplier') {
if (this.contract_type === 'multiplier' || this.is_turbos) {
// when switching back to Multiplier contract, re-apply Stop loss / Take profit validation rules
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validating take profit field for Turbos as well as for Multipliers

Object.assign(this.validation_rules, getMultiplierValidationRules());
} else {
Expand Down Expand Up @@ -680,6 +684,7 @@ export default class TradeStore extends BaseStore {
// create barrier only when it's available in response
this.main_barrier = new ChartBarrierStore(barrier || high_barrier, low_barrier, this.onChartBarrierChange, {
color,
not_draggable: isTurbosContract(contract_type),
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made barrier on the chart non-draggable for Turbos

});
// this.main_barrier.updateBarrierShade(true, contract_type);
} else {
Expand Down Expand Up @@ -1039,6 +1044,23 @@ export default class TradeStore extends BaseStore {
this.stop_out = limit_order?.stop_out?.order_amount;
}

if (this.is_turbos && this.proposal_info) {
const contract_key = this.contract_type.toUpperCase();
if (this.proposal_info[contract_key]) {
const { barrier_choices, number_of_contracts } = this.proposal_info[contract_key];
this.turbos_barrier_choices = barrier_choices || [];
this.number_of_contracts = number_of_contracts ?? 0;
if (barrier_choices && !barrier_choices.includes(this.barrier_1)) {
this.onChange({
target: {
name: 'barrier_1',
value: barrier_choices[0],
},
});
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracting necessary data for Turbos from proposal and setting them in trade-store.
Initial barrier taken from contracts_for gets outdated quickly so we have to check if it's applicable, i.e. present among barrier_choices. If it's not, we set the first value from barrier_choices as the barrier value.

}
}

if (!this.main_barrier || !(this.main_barrier.shade !== 'NONE_SINGLE')) {
this.setMainBarrier(response.echo_req);
}
Expand Down Expand Up @@ -1361,6 +1383,10 @@ export default class TradeStore extends BaseStore {
return this.contract_type === 'multiplier';
}

get is_turbos() {
return isTurbosContract(this.contract_type);
}

async getFirstOpenMarket(markets_to_search) {
if (this.active_symbols?.length) {
return findFirstOpenMarket(this.active_symbols, markets_to_search);
Expand Down