From 0ca12a07e345eb8fb120edd36f423ef1720e1b2b Mon Sep 17 00:00:00 2001 From: Peiman <25097709+Rickk137@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:23:55 +0330 Subject: [PATCH] fix: account issues (#354) * fix: account issues * fix: use wallet address instead of signer --- liquidity/lib/useAccountProxy/useAccountProxy.ts | 10 ++++++++-- liquidity/lib/useApr/useApr.ts | 7 +++---- liquidity/lib/useCoreProxy/useCoreProxy.ts | 9 ++++++++- liquidity/lib/useMulticall3/useMulticall3.ts | 9 ++++++++- .../useOracleManagerProxy/useOracleManagerProxy.ts | 3 +++ .../lib/useSpotMarketProxy/useSpotMarketProxy.ts | 9 ++++++++- liquidity/lib/useUSDProxy/useUSDProxy.ts | 10 ++++++++-- .../ui/src/layouts/Default/NetworkController.tsx | 14 +++++++++----- 8 files changed, 55 insertions(+), 16 deletions(-) diff --git a/liquidity/lib/useAccountProxy/useAccountProxy.ts b/liquidity/lib/useAccountProxy/useAccountProxy.ts index 1f9d4a2d7..cf2fd2cfd 100644 --- a/liquidity/lib/useAccountProxy/useAccountProxy.ts +++ b/liquidity/lib/useAccountProxy/useAccountProxy.ts @@ -1,17 +1,23 @@ import { Contract } from '@ethersproject/contracts'; import { useQuery } from '@tanstack/react-query'; -import { useNetwork, useProvider, useSigner } from '@snx-v3/useBlockchain'; +import { useNetwork, useProvider, useSigner, useWallet } from '@snx-v3/useBlockchain'; import { importAccountProxy } from '@snx-v3/contracts'; export function useAccountProxy() { const { network } = useNetwork(); + const { activeWallet } = useWallet(); const provider = useProvider(); const signer = useSigner(); const signerOrProvider = signer || provider; const withSigner = Boolean(signer); return useQuery({ - queryKey: [`${network?.id}-${network?.preset}`, 'AccountProxy', { withSigner }], + queryKey: [ + `${network?.id}-${network?.preset}`, + 'AccountProxy', + { withSigner }, + activeWallet?.address, + ], queryFn: async function () { if (!signerOrProvider || !network) throw new Error('Should be disabled'); const { address, abi } = await importAccountProxy(network.id, network?.preset); diff --git a/liquidity/lib/useApr/useApr.ts b/liquidity/lib/useApr/useApr.ts index 8d9ab0457..1daea1a6d 100644 --- a/liquidity/lib/useApr/useApr.ts +++ b/liquidity/lib/useApr/useApr.ts @@ -32,10 +32,9 @@ export async function fetchApr(networkId?: number) { const data = await response.json(); // Arbitrum has multiple collateral types - const highestAprCollateral = - networkId === BASE_ANDROMEDA.id - ? data - : data?.sort((a: { apr28d: number }, b: { apr28d: number }) => b.apr28d - a.apr28d)[0]; + const highestAprCollateral = Array.isArray(data) + ? data?.sort((a: { apr28d: number }, b: { apr28d: number }) => b.apr28d - a.apr28d)[0] + : data; return { combinedApr: highestAprCollateral.apr28d * 100, diff --git a/liquidity/lib/useCoreProxy/useCoreProxy.ts b/liquidity/lib/useCoreProxy/useCoreProxy.ts index 3e11e8c7b..a9249bef1 100644 --- a/liquidity/lib/useCoreProxy/useCoreProxy.ts +++ b/liquidity/lib/useCoreProxy/useCoreProxy.ts @@ -6,6 +6,7 @@ import { useProvider, useProviderForChain, useSigner, + useWallet, } from '@snx-v3/useBlockchain'; import { importCoreProxy } from '@snx-v3/contracts'; @@ -14,12 +15,18 @@ export function useCoreProxy(customNetwork?: Network | null) { const { network } = useNetwork(); const provider = useProvider(); const signer = useSigner(); + const { activeWallet } = useWallet(); const targetNetwork = customNetwork || network; const withSigner = Boolean(signer); return useQuery({ - queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'CoreProxy', { withSigner }], + queryKey: [ + `${targetNetwork?.id}-${targetNetwork?.preset}`, + 'CoreProxy', + { withSigner }, + activeWallet?.address, + ], queryFn: async function () { if (providerForChain && customNetwork) { const { address, abi } = await importCoreProxy(customNetwork.id, customNetwork.preset); diff --git a/liquidity/lib/useMulticall3/useMulticall3.ts b/liquidity/lib/useMulticall3/useMulticall3.ts index 874183543..af9031027 100644 --- a/liquidity/lib/useMulticall3/useMulticall3.ts +++ b/liquidity/lib/useMulticall3/useMulticall3.ts @@ -6,6 +6,7 @@ import { useProvider, useProviderForChain, useSigner, + useWallet, } from '@snx-v3/useBlockchain'; import { importMulticall3 } from '@snx-v3/contracts'; @@ -16,11 +17,17 @@ export function useMulticall3(customNetwork?: Network) { const signer = useSigner(); const signerOrProvider = signer || provider || providerForChain; const withSigner = Boolean(signer); + const { activeWallet } = useWallet(); const targetNetwork = customNetwork || network; return useQuery({ - queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'Multicall3', { withSigner }], + queryKey: [ + `${targetNetwork?.id}-${targetNetwork?.preset}`, + 'Multicall3', + { withSigner }, + activeWallet?.address, + ], queryFn: async function () { if (providerForChain && customNetwork) { const { address, abi } = await importMulticall3(customNetwork.id, customNetwork.preset); diff --git a/liquidity/lib/useOracleManagerProxy/useOracleManagerProxy.ts b/liquidity/lib/useOracleManagerProxy/useOracleManagerProxy.ts index f0a3446c2..3980f77fa 100644 --- a/liquidity/lib/useOracleManagerProxy/useOracleManagerProxy.ts +++ b/liquidity/lib/useOracleManagerProxy/useOracleManagerProxy.ts @@ -6,6 +6,7 @@ import { useProvider, useProviderForChain, useSigner, + useWallet, } from '@snx-v3/useBlockchain'; import { importOracleManagerProxy } from '@snx-v3/contracts'; @@ -16,6 +17,7 @@ export function useOracleManagerProxy(customNetwork?: Network) { const signer = useSigner(); const signerOrProvider = providerForChain || signer || provider; const withSigner = Boolean(signer); + const { activeWallet } = useWallet(); const targetNetwork = customNetwork || network; @@ -24,6 +26,7 @@ export function useOracleManagerProxy(customNetwork?: Network) { `${targetNetwork?.id}-${targetNetwork?.preset}`, 'OracleManagerProxy', { withSigner }, + activeWallet?.address, ], queryFn: async function () { if (providerForChain && customNetwork) { diff --git a/liquidity/lib/useSpotMarketProxy/useSpotMarketProxy.ts b/liquidity/lib/useSpotMarketProxy/useSpotMarketProxy.ts index cd4da22ee..023724139 100644 --- a/liquidity/lib/useSpotMarketProxy/useSpotMarketProxy.ts +++ b/liquidity/lib/useSpotMarketProxy/useSpotMarketProxy.ts @@ -6,6 +6,7 @@ import { useProvider, useProviderForChain, useSigner, + useWallet, } from '@snx-v3/useBlockchain'; import { importSpotMarketProxy } from '@snx-v3/contracts'; @@ -15,13 +16,19 @@ export function useSpotMarketProxy(customNetwork?: Network) { const signer = useSigner(); const providerForChain = useProviderForChain(customNetwork); const signerOrProvider = signer || provider; + const { activeWallet } = useWallet(); const targetNetwork = customNetwork || network; const withSigner = Boolean(signer); return useQuery({ - queryKey: [`${targetNetwork?.id}-${targetNetwork?.preset}`, 'SpotMarketProxy', { withSigner }], + queryKey: [ + `${targetNetwork?.id}-${targetNetwork?.preset}`, + 'SpotMarketProxy', + { withSigner }, + activeWallet?.address, + ], queryFn: async function () { if (!signerOrProvider || !targetNetwork) throw new Error('Should be disabled'); diff --git a/liquidity/lib/useUSDProxy/useUSDProxy.ts b/liquidity/lib/useUSDProxy/useUSDProxy.ts index c0f86d4b9..0b380f9dd 100644 --- a/liquidity/lib/useUSDProxy/useUSDProxy.ts +++ b/liquidity/lib/useUSDProxy/useUSDProxy.ts @@ -1,6 +1,6 @@ import { Contract } from '@ethersproject/contracts'; import { useQuery } from '@tanstack/react-query'; -import { useNetwork, useProvider, useSigner } from '@snx-v3/useBlockchain'; +import { useNetwork, useProvider, useSigner, useWallet } from '@snx-v3/useBlockchain'; import { importUSDProxy } from '@snx-v3/contracts'; export function useUSDProxy() { @@ -9,9 +9,15 @@ export function useUSDProxy() { const signer = useSigner(); const signerOrProvider = signer || provider; const withSigner = Boolean(signer); + const { activeWallet } = useWallet(); return useQuery({ - queryKey: [`${network?.id}-${network?.preset}`, 'USDProxy', { withSigner }], + queryKey: [ + `${network?.id}-${network?.preset}`, + 'USDProxy', + { withSigner }, + activeWallet?.address, + ], queryFn: async function () { if (!signerOrProvider || !network) throw new Error('Should be disabled'); const { address, abi } = await importUSDProxy(network.id, network.preset); diff --git a/liquidity/ui/src/layouts/Default/NetworkController.tsx b/liquidity/ui/src/layouts/Default/NetworkController.tsx index 0134d1183..e1f2cc667 100644 --- a/liquidity/ui/src/layouts/Default/NetworkController.tsx +++ b/liquidity/ui/src/layouts/Default/NetworkController.tsx @@ -43,12 +43,16 @@ export function NetworkController() { const { pathname } = useLocation(); useEffect(() => { - if (!isAccountsLoading && !isAccountsFetching && !!accounts?.length) { - const accountId = queryParams.get('accountId'); + if (!isAccountsLoading && !isAccountsFetching) { + if (!!accounts?.length) { + const accountId = queryParams.get('accountId'); - if (accountId && !accounts?.includes(accountId)) { - queryParams.set('accountId', accounts[0]); - navigate({ pathname, search: accounts[0] ? queryParams.toString() : '' }); + if (accountId && !accounts?.includes(accountId)) { + queryParams.set('accountId', accounts[0]); + navigate({ pathname, search: accounts[0] ? queryParams.toString() : '' }); + } + } else { + navigate({ pathname, search: '' }); } } }, [accounts, isAccountsLoading, isAccountsFetching, queryParams, navigate, pathname]);