Skip to content

Commit

Permalink
fix: account issues (#354)
Browse files Browse the repository at this point in the history
* fix: account issues

* fix: use wallet address instead of signer
  • Loading branch information
Rickk137 authored Jul 10, 2024
1 parent e019451 commit 0ca12a0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 16 deletions.
10 changes: 8 additions & 2 deletions liquidity/lib/useAccountProxy/useAccountProxy.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
7 changes: 3 additions & 4 deletions liquidity/lib/useApr/useApr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion liquidity/lib/useCoreProxy/useCoreProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useProvider,
useProviderForChain,
useSigner,
useWallet,
} from '@snx-v3/useBlockchain';
import { importCoreProxy } from '@snx-v3/contracts';

Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion liquidity/lib/useMulticall3/useMulticall3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useProvider,
useProviderForChain,
useSigner,
useWallet,
} from '@snx-v3/useBlockchain';
import { importMulticall3 } from '@snx-v3/contracts';

Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions liquidity/lib/useOracleManagerProxy/useOracleManagerProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useProvider,
useProviderForChain,
useSigner,
useWallet,
} from '@snx-v3/useBlockchain';
import { importOracleManagerProxy } from '@snx-v3/contracts';

Expand All @@ -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;

Expand All @@ -24,6 +26,7 @@ export function useOracleManagerProxy(customNetwork?: Network) {
`${targetNetwork?.id}-${targetNetwork?.preset}`,
'OracleManagerProxy',
{ withSigner },
activeWallet?.address,
],
queryFn: async function () {
if (providerForChain && customNetwork) {
Expand Down
9 changes: 8 additions & 1 deletion liquidity/lib/useSpotMarketProxy/useSpotMarketProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useProvider,
useProviderForChain,
useSigner,
useWallet,
} from '@snx-v3/useBlockchain';
import { importSpotMarketProxy } from '@snx-v3/contracts';

Expand All @@ -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');

Expand Down
10 changes: 8 additions & 2 deletions liquidity/lib/useUSDProxy/useUSDProxy.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
Expand Down
14 changes: 9 additions & 5 deletions liquidity/ui/src/layouts/Default/NetworkController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 0ca12a0

Please sign in to comment.