From 0d8f3b49228337ffecc25b0cb56497911a1f9a1d Mon Sep 17 00:00:00 2001 From: Bill He Date: Tue, 22 Aug 2023 16:11:46 -0700 Subject: [PATCH] working switch network deposits --- src/hooks/useAccounts.tsx | 14 ++++++++++---- .../forms/AccountManagementForms/DepositForm.tsx | 6 +++++- .../forms/AccountManagementForms/WithdrawForm.tsx | 6 +++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/hooks/useAccounts.tsx b/src/hooks/useAccounts.tsx index c1ce52f..8601f05 100644 --- a/src/hooks/useAccounts.tsx +++ b/src/hooks/useAccounts.tsx @@ -1,10 +1,10 @@ import { useCallback, useContext, createContext, useEffect, useState, useMemo } from 'react'; -import { useDispatch } from 'react-redux'; +import { useSelector, useDispatch, shallowEqual } from 'react-redux'; import { AES, enc } from 'crypto-js'; import { LocalWallet, USDC_DENOM, type Subaccount } from '@dydxprotocol/v4-client'; -import { SubAccountHistoricalPNLs } from '@/constants/abacus'; +import { SubAccountHistoricalPNLs, TransferType } from '@/constants/abacus'; import { OnboardingGuard, OnboardingState, type EvmDerivedAddresses } from '@/constants/account'; import { LocalStorageKey, LOCAL_STORAGE_VERSIONS } from '@/constants/localStorage'; import { DydxAddress, EthereumAddress, PrivateInformation } from '@/constants/wallets'; @@ -15,6 +15,7 @@ import { setSubaccount, setHistoricalPnl, } from '@/state/account'; +import { getTransferInputs } from '@/state/inputsSelectors'; import abacusStateManager from '@/lib/abacus'; import { log } from '@/lib/telemetry'; @@ -36,6 +37,7 @@ export const useAccounts = () => useContext(AccountsContext)!; const useAccountsContext = () => { const dispatch = useDispatch(); + const { type: transferType } = useSelector(getTransferInputs, shallowEqual) || {}; // Wallet connection const { @@ -225,8 +227,12 @@ const useAccountsContext = () => { // abacus // TODO: useAbacus({ dydxAddress }) useEffect(() => { - if (dydxAddress) abacusStateManager.setAccount(dydxAddress); - else abacusStateManager.disconnectAccount(); + if (dydxAddress) { + abacusStateManager.setAccount(dydxAddress); + } else if (transferType?.rawValue !== TransferType.deposit.rawValue) { + // we don't want to disconnect the account if we switch network during the deposit form + abacusStateManager.disconnectAccount(); + } }, [dydxAddress]); // clear subaccounts when no dydxAddress is set diff --git a/src/views/forms/AccountManagementForms/DepositForm.tsx b/src/views/forms/AccountManagementForms/DepositForm.tsx index 9f9f94e..d49f3a4 100644 --- a/src/views/forms/AccountManagementForms/DepositForm.tsx +++ b/src/views/forms/AccountManagementForms/DepositForm.tsx @@ -97,11 +97,15 @@ export const DepositForm = ({ onDeposit, onError }: DepositFormProps) => { useEffect(() => { abacusStateManager.setTransferValue({ - value: TransferType.deposit.rawValue, field: TransferInputField.type, + value: TransferType.deposit.rawValue, }); return () => { + abacusStateManager.setTransferValue({ + field: TransferInputField.type, + value: null, + }); abacusStateManager.clearTransferInputValues(); }; }, []); diff --git a/src/views/forms/AccountManagementForms/WithdrawForm.tsx b/src/views/forms/AccountManagementForms/WithdrawForm.tsx index 6d98f3d..7716e59 100644 --- a/src/views/forms/AccountManagementForms/WithdrawForm.tsx +++ b/src/views/forms/AccountManagementForms/WithdrawForm.tsx @@ -80,11 +80,15 @@ export const WithdrawForm = () => { useEffect(() => { abacusStateManager.setTransferValue({ - value: TransferType.withdrawal.rawValue, field: TransferInputField.type, + value: TransferType.withdrawal.rawValue, }); return () => { + abacusStateManager.setTransferValue({ + field: TransferInputField.type, + value: null, + }); abacusStateManager.clearTransferInputValues(); }; }, []);