feat(trading): improve desposit and withraw asset selection (#5493)

This commit is contained in:
Bartłomiej Głownia 2023-12-13 09:47:22 +01:00 committed by GitHub
parent b82615a3d8
commit 8a110584dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 34 deletions

View File

@ -72,17 +72,14 @@ export const MarketPage = () => {
const { data, loading } = useMarket(marketId); const { data, loading } = useMarket(marketId);
useEffect(() => { useEffect(() => {
if (data?.id && data.id !== lastMarketId && !closed) { if (data?.id && data.id !== lastMarketId) {
update({ marketId: data.id }); update({ marketId: data.id });
} }
}, [update, lastMarketId, data?.id]); }, [update, lastMarketId, data?.id]);
useEffect(() => { useEffect(() => {
if (largeScreen && view === undefined) { if (largeScreen && view === undefined) {
setViews( setViews({ type: ViewType.Order }, currentRouteId);
{ type: closed ? ViewType.Info : ViewType.Order },
currentRouteId
);
} }
}, [setViews, view, currentRouteId, largeScreen]); }, [setViews, view, currentRouteId, largeScreen]);

View File

@ -13,6 +13,7 @@ import { ViewType, useSidebar } from '../sidebar';
import { useMarketClickHandler } from '../../lib/hooks/use-market-click-handler'; import { useMarketClickHandler } from '../../lib/hooks/use-market-click-handler';
import { useGetCurrentRouteId } from '../../lib/hooks/use-get-current-route-id'; import { useGetCurrentRouteId } from '../../lib/hooks/use-get-current-route-id';
import { useT } from '../../lib/use-t'; import { useT } from '../../lib/use-t';
import { usePersistentDepositStore } from '@vegaprotocol/deposits';
export const AccountsContainer = ({ export const AccountsContainer = ({
pinnedAsset, pinnedAsset,
@ -25,6 +26,7 @@ export const AccountsContainer = ({
const { open: openAssetDetailsDialog } = useAssetDetailsDialogStore(); const { open: openAssetDetailsDialog } = useAssetDetailsDialogStore();
const currentRouteId = useGetCurrentRouteId(); const currentRouteId = useGetCurrentRouteId();
const setViews = useSidebar((store) => store.setViews); const setViews = useSidebar((store) => store.setViews);
const setDepositAsset = usePersistentDepositStore((store) => store.saveValue);
const gridStore = useAccountStore((store) => store.gridStore); const gridStore = useAccountStore((store) => store.gridStore);
const updateGridStore = useAccountStore((store) => store.updateGridStore); const updateGridStore = useAccountStore((store) => store.updateGridStore);
@ -55,7 +57,10 @@ export const AccountsContainer = ({
setViews({ type: ViewType.Withdraw, assetId }, currentRouteId); setViews({ type: ViewType.Withdraw, assetId }, currentRouteId);
}} }}
onClickDeposit={(assetId) => { onClickDeposit={(assetId) => {
setViews({ type: ViewType.Deposit, assetId }, currentRouteId); setViews({ type: ViewType.Deposit }, currentRouteId);
if (assetId) {
setDepositAsset({ assetId });
}
}} }}
onClickTransfer={(assetId) => { onClickTransfer={(assetId) => {
setViews({ type: ViewType.Transfer, assetId }, currentRouteId); setViews({ type: ViewType.Transfer, assetId }, currentRouteId);

View File

@ -5,7 +5,7 @@ import { prepend0x } from '@vegaprotocol/smart-contracts';
import sortBy from 'lodash/sortBy'; import sortBy from 'lodash/sortBy';
import { useSubmitApproval } from './use-submit-approval'; import { useSubmitApproval } from './use-submit-approval';
import { useSubmitFaucet } from './use-submit-faucet'; import { useSubmitFaucet } from './use-submit-faucet';
import { useCallback, useState } from 'react'; import { useCallback, useEffect } from 'react';
import { useDepositBalances } from './use-deposit-balances'; import { useDepositBalances } from './use-deposit-balances';
import type { Asset } from '@vegaprotocol/assets'; import type { Asset } from '@vegaprotocol/assets';
import { import {
@ -30,7 +30,7 @@ export const DepositManager = ({
const { config } = useEthereumConfig(); const { config } = useEthereumConfig();
const [persistentDeposit, savePersistentDeposit] = const [persistentDeposit, savePersistentDeposit] =
usePersistentDeposit(initialAssetId); usePersistentDeposit(initialAssetId);
const [assetId, setAssetId] = useState(persistentDeposit?.assetId); const assetId = persistentDeposit?.assetId;
const asset = assets.find((a) => a.id === assetId); const asset = assets.find((a) => a.id === assetId);
const bridgeContract = useBridgeContract(); const bridgeContract = useBridgeContract();
@ -70,18 +70,19 @@ export const DepositManager = ({
[savePersistentDeposit, persistentDeposit] [savePersistentDeposit, persistentDeposit]
); );
useEffect(() => {
// When we change asset, also clear the tracked faucet/approve transactions so
// we dont render stale UI
approve.reset();
faucet.reset();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [assetId]);
return ( return (
<DepositForm <DepositForm
selectedAsset={asset} selectedAsset={asset}
onDisconnect={reset} onDisconnect={reset}
onSelectAsset={(id) => { onSelectAsset={(assetId) => savePersistentDeposit({ assetId })}
setAssetId(id);
savePersistentDeposit({ assetId: id });
// When we change asset, also clear the tracked faucet/approve transactions so
// we dont render stale UI
approve.reset();
faucet.reset();
}}
handleAmountChange={onAmountChange} handleAmountChange={onAmountChange}
assets={sortBy(assets, 'name')} assets={sortBy(assets, 'name')}
submitApprove={approve.perform} submitApprove={approve.perform}

View File

@ -13,3 +13,4 @@ export * from './use-get-deposit-maximum';
export * from './use-get-deposited-amount'; export * from './use-get-deposited-amount';
export * from './use-submit-approval'; export * from './use-submit-approval';
export * from './use-submit-faucet'; export * from './use-submit-faucet';
export * from './use-persistent-deposit';

View File

@ -10,7 +10,7 @@ interface PersistedDeposit {
} }
type PersistedDepositData = Record<string, PersistedDeposit>; type PersistedDepositData = Record<string, PersistedDeposit>;
const usePersistentDepositStore = create<{ export const usePersistentDepositStore = create<{
deposits: PersistedDepositData; deposits: PersistedDepositData;
saveValue: (entry: PersistedDeposit) => void; saveValue: (entry: PersistedDeposit) => void;
lastVisited?: PersistedDeposit; lastVisited?: PersistedDeposit;

View File

@ -3,7 +3,7 @@ import { addDecimal } from '@vegaprotocol/utils';
import { localLoggerFactory } from '@vegaprotocol/logger'; import { localLoggerFactory } from '@vegaprotocol/logger';
import * as Schema from '@vegaprotocol/types'; import * as Schema from '@vegaprotocol/types';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import { useCallback, useEffect, useMemo } from 'react'; import { useCallback, useEffect, useMemo, useRef } from 'react';
import type { AccountFieldsFragment } from '@vegaprotocol/accounts'; import type { AccountFieldsFragment } from '@vegaprotocol/accounts';
import { import {
useGetWithdrawDelay, useGetWithdrawDelay,
@ -18,6 +18,7 @@ export const useWithdrawAsset = (
assetId?: string assetId?: string
) => { ) => {
const { asset, balance, min, threshold, delay, update } = useWithdrawStore(); const { asset, balance, min, threshold, delay, update } = useWithdrawStore();
const currentAssetId = useRef(asset?.id);
const getThreshold = useGetWithdrawThreshold(); const getThreshold = useGetWithdrawThreshold();
const getDelay = useGetWithdrawDelay(); const getDelay = useGetWithdrawDelay();
const { param } = useNetworkParam( const { param } = useNetworkParam(
@ -54,6 +55,8 @@ export const useWithdrawAsset = (
) )
) )
: new BigNumber(0); : new BigNumber(0);
currentAssetId.current = asset?.id;
update({ asset, balance, min, threshold: undefined, delay: undefined });
// Query collateral bridge for threshold for selected asset // Query collateral bridge for threshold for selected asset
// and subsequent delay if withdrawal amount is larger than it // and subsequent delay if withdrawal amount is larger than it
let threshold = new BigNumber(0); let threshold = new BigNumber(0);
@ -66,9 +69,14 @@ export const useWithdrawAsset = (
if (result[1] != null) delay = result[1]; if (result[1] != null) delay = result[1];
} catch (err) { } catch (err) {
logger.error('get withdraw asset data', err); logger.error('get withdraw asset data', err);
} finally {
if (currentAssetId.current === asset?.id) {
update({
threshold,
delay,
});
}
} }
update({ asset, balance, min, threshold, delay });
}, },
[ [
assets, assets,

View File

@ -239,7 +239,7 @@ export const WithdrawForm = ({
</TradingInputError> </TradingInputError>
)} )}
</TradingFormGroup> </TradingFormGroup>
{selectedAsset && threshold && ( {selectedAsset && (
<div className="mb-4"> <div className="mb-4">
<WithdrawLimits <WithdrawLimits
amount={amount} amount={amount}

View File

@ -12,7 +12,7 @@ import { useT } from './use-t';
interface WithdrawLimitsProps { interface WithdrawLimitsProps {
amount: string; amount: string;
threshold: BigNumber; threshold: BigNumber | undefined;
balance: BigNumber; balance: BigNumber;
delay: number | undefined; delay: number | undefined;
asset: Asset; asset: Asset;
@ -27,7 +27,7 @@ export const WithdrawLimits = ({
}: WithdrawLimitsProps) => { }: WithdrawLimitsProps) => {
const t = useT(); const t = useT();
const delayTime = const delayTime =
new BigNumber(amount).isGreaterThan(threshold) && delay threshold && delay && new BigNumber(amount).isGreaterThan(threshold)
? formatDistanceToNow(Date.now() + delay * 1000) ? formatDistanceToNow(Date.now() + delay * 1000)
: t('None'); : t('None');
@ -48,21 +48,23 @@ export const WithdrawLimits = ({
'-' '-'
), ),
}, },
]; {
if (threshold.isFinite()) {
limits.push({
key: 'WITHDRAWAL_THRESHOLD', key: 'WITHDRAWAL_THRESHOLD',
label: t('Delayed withdrawal threshold'), label: t('Delayed withdrawal threshold'),
tooltip: WITHDRAW_THRESHOLD_TOOLTIP_TEXT, tooltip: WITHDRAW_THRESHOLD_TOOLTIP_TEXT,
rawValue: threshold, rawValue: threshold,
value: <CompactNumber number={threshold} decimals={asset.decimals} />, value: threshold ? (
}); <CompactNumber number={threshold} decimals={asset.decimals} />
} ) : (
limits.push({ '-'
key: 'DELAY_TIME', ),
label: t('Delay time'), },
value: delayTime, {
}); key: 'DELAY_TIME',
label: t('Delay time'),
value: threshold && delay ? delayTime : '-',
},
];
return ( return (
<KeyValueTable> <KeyValueTable>