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

thisyahlen/chore: add useAvailableWallets in deriv/api #9818

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/api/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { default as useSettings } from './useSettings';
export { default as useTradingAccountsList } from './useTradingAccountsList';
export { default as useTradingPlatformAccounts } from './useTradingPlatformAccounts';
export { default as useWalletAccountsList } from './useWalletAccountsList';
export { default as useAvailableWallets } from './useAvailableWallets';
78 changes: 78 additions & 0 deletions packages/api/src/hooks/useAvailableWallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import useFetch from '../useFetch';
import useWalletAccountsList from './useWalletAccountsList';
import useAuthorize from './useAuthorize';
import useCurrencyConfig from './useCurrencyConfig';

const useAvailableWallets = () => {
const { data } = useAuthorize();
const { data: account_type_data, ...rest } = useFetch('get_account_types', {
thisyahlen-deriv marked this conversation as resolved.
Show resolved Hide resolved
payload: {
// This is temporary fix, will be changed by BE
company: data?.landing_company_name === 'virtual' ? 'svg' : data?.landing_company_name,
thisyahlen-deriv marked this conversation as resolved.
Show resolved Hide resolved
},
options: { enabled: Boolean(data?.landing_company_name) },
});

const { data: added_wallets } = useWalletAccountsList();
const { getConfig } = useCurrencyConfig();

const sortedWallets = React.useMemo(() => {
thisyahlen-deriv marked this conversation as resolved.
Show resolved Hide resolved
if (!account_type_data) return undefined;
const { crypto, doughflow } = account_type_data?.get_account_types?.wallet || {};
const crypto_currencies = crypto?.currencies;
const fiat_currencies = doughflow?.currencies;

if (!crypto_currencies || !fiat_currencies) return undefined;
const available_currencies = [...fiat_currencies, ...crypto_currencies];
const non_virtual_wallets = added_wallets?.filter(wallet => !wallet.is_virtual);

const modified_wallets = non_virtual_wallets?.map(wallet => ({
currency: wallet.currency,
landing_company_name: wallet.landing_company_name,
is_added: true,
}));

const available_wallets = available_currencies
.filter(currency => !modified_wallets?.some(wallet => wallet.currency === currency))
.map(currency => ({
currency,
// This will also be changed by BE
landing_company_name: data?.landing_company_name === 'virtual' ? 'svg' : data?.landing_company_name,
is_added: false,
}));

const getConfigIsCrypto = (currency: string) => getConfig(currency)?.is_crypto;

// Sort the unadded wallets alphabetically by fiat, crypto, then virtual
available_wallets.sort((a, b) => {
const a_config = getConfigIsCrypto(a.currency);
const b_config = getConfigIsCrypto(b.currency);

if (a_config !== b_config) return a.currency ? 1 : -1;

return (a.currency || 'USD').localeCompare(b.currency || 'USD');
});

// Sort the added wallets alphabetically by fiat, crypto, then virtual (if any)
if (Array.isArray(modified_wallets)) {
modified_wallets?.sort((a, b) => {
const a_config = getConfigIsCrypto(a.currency || 'BTC');
const b_config = getConfigIsCrypto(b.currency || 'BTC');
if (a_config !== b_config) return a_config ? 1 : -1;

return (a.currency || 'USD').localeCompare(b.currency || 'USD');
});
}

return [...available_wallets];
}, [added_wallets, account_type_data, data?.landing_company_name, getConfig]);

return {
...rest,
/** Sorted available wallets */
data: sortedWallets,
};
};

export default useAvailableWallets;
79 changes: 79 additions & 0 deletions packages/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,85 @@ import type {
import type { useMutation, useQuery } from '@tanstack/react-query';

type TPrivateSocketEndpoints = {
get_account_types: {
request: {
/**
* Must be `1`
*/
get_account_types: 1;
/**
* [Optional] Set to landing company to get relevant account types. If not set, this defaults to current account landing company
*/
company?: string;
/**
* [Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.
*/
passthrough?: {
[k: string]: unknown;
};
/**
* [Optional] Used to map request to response.
*/
req_id?: number;
};
response: {
/**
* Returns accounts that are available to create or link to
* */
get_account_types: {
/**
* Trading account types that are available to create or link to
*/
trading: {
/**
* Details for trading account types
*/
[k: string]: {
/**
* Wallet currencies allowed for this trading account
*/
allowed_wallet_currencies: string[];
/**
* Can this trading account linked to another currency after opening
*/
linkable_to_different_currency: 0 | 1;
/**
* Wallet types that this trading account can be linked to.
*/
linkable_wallet_types: string[];
};
};
/**
* Wallet accounts types that are available to create or link to
* */
wallet: {
/**
* Details for wallets account types
*/
[k: string]: {
/**
* Allowed currencies for creating accounts of this type; used or disallowed currencies are not listed.
*/
currencies: string[];
};
};
/**
* Echo of the request made.
* */
echo_req: {
[k: string]: unknown;
};
/**
* Action name of the request made.
* */
msg_type: 'get_account_types';
/**
* Optional field sent in request to map to response, present only when request contains `req_id`.
* */
req_id?: number;
};
};
};
trading_platform_accounts: {
request: {
/**
Expand Down