Skip to content

Commit 345ef57

Browse files
authored
Migrate portfolio decomposition request to ZPI (#853)
1 parent b5a6296 commit 345ef57

File tree

5 files changed

+45
-67
lines changed

5 files changed

+45
-67
lines changed

src/modules/zerion-api/hooks/useWalletPortfolio.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import { useQuery } from '@tanstack/react-query';
22
import { persistentQuery } from 'src/ui/shared/requests/queryClientPersistence';
3+
import { queryClient } from 'src/ui/shared/requests/queryClient';
34
import { ZerionAPI } from '../zerion-api.client';
45
import type { Params } from '../requests/wallet-get-portfolio';
56
import type { BackendSourceParams } from '../shared';
67

8+
const STALE_TIME = 20000;
9+
const QUERY_KEY = 'walletGetPortfolio';
10+
11+
export function queryWalletPortfolio(
12+
params: Params,
13+
clientParams: BackendSourceParams
14+
) {
15+
return queryClient.fetchQuery({
16+
queryKey: persistentQuery([QUERY_KEY, params, clientParams]),
17+
queryFn: () => ZerionAPI.walletGetPortfolio(params, clientParams),
18+
staleTime: STALE_TIME,
19+
});
20+
}
21+
722
export function useWalletPortfolio(
823
params: Params,
924
{ source }: BackendSourceParams,
@@ -22,13 +37,13 @@ export function useWalletPortfolio(
2237
} = {}
2338
) {
2439
return useQuery({
25-
queryKey: persistentQuery(['walletGetPortfolio', params, source]),
40+
queryKey: persistentQuery([QUERY_KEY, params, source]),
2641
queryFn: () => ZerionAPI.walletGetPortfolio(params, { source }),
2742
retry: 0, // if not 0, there are too many rerenders if the queryFn throws synchronously
2843
suspense,
2944
enabled,
3045
keepPreviousData,
31-
staleTime: 20000,
46+
staleTime: STALE_TIME,
3247
refetchInterval,
3348
refetchOnWindowFocus,
3449
});

src/shared/analytics/shared/getTotalWalletsBalance.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/shared/analytics/shared/getUserProperties.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { PortfolioDecomposition } from 'defi-sdk';
21
import type { Account } from 'src/background/account/Account';
32
import { getAddressActivity } from 'src/ui/shared/requests/useAddressActivity';
43
import { INTERNAL_SYMBOL_CONTEXT } from 'src/background/Wallet/Wallet';
@@ -13,7 +12,10 @@ import type {
1312
WalletMeta,
1413
} from 'src/modules/zerion-api/requests/wallet-get-meta';
1514
import { PREMIUM_PRIORITY } from 'src/modules/zerion-api/requests/wallet-get-meta';
16-
import { getAddressesPortfolio } from './getTotalWalletsBalance';
15+
import type {
16+
Params,
17+
WalletPortfolio,
18+
} from 'src/modules/zerion-api/requests/wallet-get-portfolio';
1719
import {
1820
getProviderForMixpanel,
1921
getProviderNameFromGroup,
@@ -45,9 +47,17 @@ function getFundedStatsByEcosystem(
4547
};
4648
}
4749

50+
async function queryWalletPortfolio(params: Params) {
51+
return backgroundQueryClient.fetchQuery({
52+
queryKey: ['walletGetPortfolio', params],
53+
queryFn: () => ZerionAPI.walletGetPortfolio(params),
54+
staleTime: 20000,
55+
});
56+
}
57+
4858
async function getPortfolioStats(addresses: string[]) {
4959
return Promise.allSettled([
50-
getAddressesPortfolio(addresses),
60+
queryWalletPortfolio({ addresses, currency: 'usd' }),
5161
getAddressActivity({ addresses }, { cachePolicy: 'cache-first' }),
5262
]).then(([result1, result2]) => {
5363
return {
@@ -111,12 +121,12 @@ async function fetchWalletsMeta({ addresses }: { addresses: string[] }) {
111121
});
112122
}
113123

114-
function getChainBreakdown(portfolio: PortfolioDecomposition | null) {
124+
function getChainBreakdown(portfolio: WalletPortfolio | null) {
115125
const chainBreakdown: Record<string, number> = {};
116126
if (portfolio) {
117-
for (const internalId in portfolio.positions_chains_distribution) {
127+
for (const internalId in portfolio.positionsChainsDistribution) {
118128
const key = `${internalId}_balance`;
119-
chainBreakdown[key] = portfolio.positions_chains_distribution[internalId];
129+
chainBreakdown[key] = portfolio.positionsChainsDistribution[internalId];
120130
}
121131
}
122132
return chainBreakdown;
@@ -209,8 +219,8 @@ export async function getUserProperties(account: Account) {
209219
num_funded_evm_wallets: fundedStatsByEcosystem.evmFundedCount,
210220
num_watchlist_solana_wallets: readonlySolanaAddressesCount ?? 0,
211221
num_watchlist_evm_wallets: readonlyEvmAddressesCount ?? 0,
212-
total_balance: portfolioStats?.portfolio?.total_value ?? 0,
213-
...getChainBreakdown(portfolioStats?.portfolio ?? null),
222+
total_balance: portfolioStats?.portfolio?.data.totalValue ?? 0,
223+
...getChainBreakdown(portfolioStats?.portfolio?.data ?? null),
214224
currency: 'usd',
215225
language: 'en',
216226
zerion_premium_holder: zerionStats?.zerion_premium_holder ?? false,
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { lookupAddressName } from 'src/modules/name-service';
2-
import { getAddressPortfolio } from 'src/ui/shared/requests/PortfolioValue/getAddressPortfolio';
2+
import { getHttpClientSource } from 'src/modules/zerion-api/getHttpClientSource';
3+
import { queryWalletPortfolio } from 'src/modules/zerion-api/hooks/useWalletPortfolio';
34

45
export interface WalletInfo {
56
portfolio: number;
@@ -10,7 +11,12 @@ export async function getWalletInfo(
1011
address: string,
1112
currency: string
1213
): Promise<WalletInfo> {
13-
const name = await lookupAddressName(address);
14-
const portfolio = await getAddressPortfolio({ address, currency });
15-
return { portfolio: portfolio.total_value, name };
14+
const [name, portfolioData] = await Promise.all([
15+
lookupAddressName(address),
16+
queryWalletPortfolio(
17+
{ addresses: [address], currency },
18+
{ source: await getHttpClientSource() }
19+
),
20+
]);
21+
return { portfolio: portfolioData.data.totalValue, name };
1622
}

src/ui/shared/requests/PortfolioValue/getAddressPortfolio.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)