From 4a9119a96056bf6a89b7f2ba6b55fae6c4cd876b Mon Sep 17 00:00:00 2001 From: thisyahlen <104053934+thisyahlen-deriv@users.noreply.github.com> Date: Thu, 24 Aug 2023 16:23:25 +0800 Subject: [PATCH] thisyahlen/chore: add useTradingPlatformAccounts to deriv api package (#9807) --- packages/api/src/hooks/index.ts | 3 +- .../src/hooks/useTradingPlatformAccounts.ts | 48 ++++ packages/api/types.ts | 210 ++++++++++++++++++ 3 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 packages/api/src/hooks/useTradingPlatformAccounts.ts diff --git a/packages/api/src/hooks/index.ts b/packages/api/src/hooks/index.ts index 99207f06536b..6795a3ac3953 100644 --- a/packages/api/src/hooks/index.ts +++ b/packages/api/src/hooks/index.ts @@ -3,5 +3,6 @@ export { default as useActiveWalletAccounts } from './useActiveWalletAccounts'; export { default as useAuthorize } from './useAuthorize'; export { default as useBalance } from './useBalance'; export { default as useCurrencyConfig } from './useCurrencyConfig'; -export { default as useWalletAccountsList } from './useWalletAccountsList'; export { default as useMT5LoginList } from './useMT5LoginList'; +export { default as useTradingPlatformAccounts } from './useTradingPlatformAccounts'; +export { default as useWalletAccountsList } from './useWalletAccountsList'; diff --git a/packages/api/src/hooks/useTradingPlatformAccounts.ts b/packages/api/src/hooks/useTradingPlatformAccounts.ts new file mode 100644 index 000000000000..39e3d91d1f51 --- /dev/null +++ b/packages/api/src/hooks/useTradingPlatformAccounts.ts @@ -0,0 +1,48 @@ +import { useMemo } from 'react'; +import useFetch from '../useFetch'; + +/** A custom hook that gets the list of created other CFD accounts. */ +const useTradingPlatformAccounts = () => { + const { data: derivez_accounts, ...derivez_rest } = useFetch('trading_platform_accounts', { + payload: { platform: 'derivez' }, + }); + const { data: dxtrade_accounts, ...dxtrade_rest } = useFetch('trading_platform_accounts', { + payload: { platform: 'dxtrade' }, + }); + + /** Adding neccesary properties to derivez accounts */ + const modified_derivez_accounts = useMemo( + () => + derivez_accounts?.trading_platform_accounts?.map(account => ({ + ...account, + loginid: account.login, + })), + [derivez_accounts?.trading_platform_accounts] + ); + + /** Adding neccesary properties to dxtrade accounts */ + const modified_dxtrade_accounts = useMemo( + () => + dxtrade_accounts?.trading_platform_accounts?.map(account => ({ + ...account, + loginid: account.account_id, + })), + [dxtrade_accounts?.trading_platform_accounts] + ); + + const data = useMemo( + () => ({ + dxtrade_accounts: modified_dxtrade_accounts || [], + derivez_accounts: modified_derivez_accounts || [], + }), + [modified_dxtrade_accounts, modified_derivez_accounts] + ); + + return { + /** List of all created other CFD accounts */ + data, + ...{ ...derivez_rest, ...dxtrade_rest }, + }; +}; + +export default useTradingPlatformAccounts; diff --git a/packages/api/types.ts b/packages/api/types.ts index 500ba46ebe9f..6d3beef337e9 100644 --- a/packages/api/types.ts +++ b/packages/api/types.ts @@ -229,6 +229,216 @@ import type { import type { useMutation, useQuery } from '@tanstack/react-query'; type TPrivateSocketEndpoints = { + trading_platform_accounts: { + request: { + /** + * Must be `1` + */ + trading_platform_accounts: 1; + /** + * Trading platform name + */ + platform: 'dxtrade' | 'mt5' | 'derivez'; + /** + * [Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. + */ + passthrough?: { + [k: string]: unknown; + }; + /** + * [Optional] Used to map request to response. + */ + req_id?: number; + }; + response: { + /** + * Array containing Trading account objects. + */ + trading_platform_accounts?: { + /** + * ID of Trading account. + */ + account_id?: string; + /** + * Account type. + */ + account_type?: 'demo' | 'real'; + /** + * Balance of the Trading account. + */ + balance?: null | number; + /** + * Residence of the MT5 account. + */ + country?: string; + /** + * Currency of the Trading account. + */ + currency?: string; + /** + * Account balance, formatted to appropriate decimal places. + */ + display_balance?: null | string; + /** + * Email address of the MT5 account. + */ + email?: string; + /** + * Account enabled status + */ + enabled?: number; + /** + * Error in MT5 account details. + */ + error?: { + /** + * Error code string. + */ + code?: string; + /** + * Extra information about the error. + */ + details?: { + /** + * MT5 account type. + */ + account_type?: string; + /** + * MT5 account login ID. + */ + login?: string; + /** + * Trade server name of the MT5 account. + */ + server?: string; + /** + * Trade server information. + */ + server_info?: { + /** + * The environment. E.g. Deriv-Server. + */ + environment?: 'Deriv-Demo' | 'Deriv-Server' | 'Deriv-Server-02'; + /** + * Geographical location of the server. + */ + geolocation?: { + /** + * Internal server grouping. + */ + group?: string; + /** + * Sever location. + */ + location?: string; + /** + * Sever region. + */ + region?: string; + /** + * Sever sequence. + */ + sequence?: number; + }; + /** + * Server id. + */ + id?: string; + }; + }; + /** + * Error message. + */ + message_to_client?: string; + }; + /** + * Group type of the MT5 account, e.g. `demo\svg_financial` + */ + group?: string; + /** + * Landing company shortcode of the Trading account. + */ + landing_company_short?: 'bvi' | 'labuan' | 'malta' | 'maltainvest' | 'svg' | 'vanuatu' | 'seychelles'; + /** + * Leverage of the MT5 account (1 to 1000). + */ + leverage?: number; + /** + * Login name used to log in into Trading platform + */ + login?: string; + /** + * Market type + */ + market_type?: 'financial' | 'synthetic' | 'all'; + /** + * Name of the owner of the MT5 account. + */ + name?: string; + /** + * Name of trading platform. + */ + platform?: 'dxtrade' | 'mt5'; + /** + * Trade server name of the MT5 account. + */ + server?: string; + /** + * Trade server information. + */ + server_info?: { + /** + * The environment. E.g. Deriv-Server. + */ + environment?: 'Deriv-Demo' | 'Deriv-Server' | 'Deriv-Server-02'; + /** + * Geographical location of the server. + */ + geolocation?: { + /** + * Internal server grouping. + */ + group?: string; + /** + * Sever location. + */ + location?: string; + /** + * Sever region. + */ + region?: string; + /** + * Sever sequence. + */ + sequence?: number; + }; + /** + * Server id. + */ + id?: string; + }; + /** + * Sub account type + */ + sub_account_type?: 'financial' | 'financial_stp'; + }[]; + /** + * Echo of the request made. + */ + echo_req: { + [k: string]: unknown; + }; + /** + * Action name of the request made. + */ + msg_type: 'trading_platform_accounts'; + /** + * Optional field sent in request to map to response, present only when request contains `req_id`. + */ + req_id?: number; + [k: string]: unknown; + }; + }; cashier_payments: { request: { /**