working switch network deposits

This commit is contained in:
Bill He
2023-08-28 11:15:46 -07:00
parent ad9e144d40
commit 0d8f3b4922
3 changed files with 20 additions and 6 deletions
+10 -4
View File
@@ -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
@@ -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();
};
}, []);
@@ -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();
};
}, []);