Skip to content

Commit 6432082

Browse files
authored
feat: modify tx-indexer response data (#541)
1 parent 18da7ea commit 6432082

File tree

24 files changed

+1013
-93
lines changed

24 files changed

+1013
-93
lines changed

packages/adena-extension/src/common/provider/adena/adena-provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const AdenaProvider: React.FC<React.PropsWithChildren<unknown>> = ({ chil
5151
return new GnoProvider(currentNetwork.rpcUrl, currentNetwork.chainId);
5252
}, [currentNetwork]);
5353

54-
const axiosInstance = axios.create({ timeout: 5000 });
54+
const axiosInstance = axios.create({ timeout: 20_000 });
5555

5656
const localStorage = AdenaStorage.local();
5757

packages/adena-extension/src/common/provider/wallet/wallet-provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const WalletProvider: React.FC<React.PropsWithChildren<unknown>> = ({ chi
9595
if (currentAccount) {
9696
setCurrentAccount(currentAccount);
9797
await accountService.changeCurrentAccount(currentAccount);
98-
initTokenMetainfos(currentAccount.id);
98+
await initTokenMetainfos(currentAccount.id);
9999
}
100100
return true;
101101
}

packages/adena-extension/src/common/utils/fetch-utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import axios from 'axios';
22
import fetchAdapter from '@vespaiach/axios-fetch-adapter';
3+
import { v1 } from 'uuid';
4+
5+
export interface RPCRequest {
6+
id: string;
7+
jsonrpc: string;
8+
method: string;
9+
params: any[];
10+
}
311

412
export async function fetchHealth(url: string): Promise<{ url: string; healthy: boolean }> {
513
const healthy = await axios
@@ -11,3 +19,20 @@ export async function fetchHealth(url: string): Promise<{ url: string; healthy:
1119
healthy,
1220
};
1321
}
22+
23+
export function makeRPCRequest({
24+
id,
25+
method,
26+
params,
27+
}: {
28+
id?: string;
29+
method: string;
30+
params?: any[];
31+
}): RPCRequest {
32+
return {
33+
id: id || v1().toString(),
34+
jsonrpc: '2.0',
35+
method: method,
36+
params: params || [],
37+
};
38+
}

packages/adena-extension/src/common/utils/string-utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { fromBech32 } from 'adena-module';
2+
import { formatAddress } from './client-utils';
3+
14
export const convertTextToAmount = (text: string): { value: string; denom: string } | null => {
25
try {
36
const balance = text
@@ -17,8 +20,23 @@ export const convertTextToAmount = (text: string): { value: string; denom: strin
1720
return null;
1821
}
1922
};
23+
2024
export const makeQueryString = (parameters: { [key in string]: string }): string => {
2125
return Object.entries(parameters)
2226
.map((entry) => `${entry[0]}=${entry[1]}`)
2327
.join('&');
2428
};
29+
30+
export const makeDisplayPackagePath = (packagePath: string): string => {
31+
const items = packagePath.split('/');
32+
return items.map((item) => (isBech32Address(item) ? formatAddress(item, 4) : item)).join('/');
33+
};
34+
35+
const isBech32Address = (str: string): boolean => {
36+
try {
37+
const { prefix } = fromBech32(str);
38+
return !!prefix;
39+
} catch {
40+
return false;
41+
}
42+
};

packages/adena-extension/src/components/pages/additional-token/additional-token-search-list/additional-token-search-list.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AdditionalTokenSearchListWrapper,
55
} from './additional-token-search-list.styles';
66
import { TokenInfo } from '@types';
7+
import { makeDisplayPackagePath } from '@common/utils/string-utils';
78

89
export interface AdditionalTokenSearchListProps {
910
tokenInfos: TokenInfo[];
@@ -33,13 +34,17 @@ const AdditionalTokenSearchListItem: React.FC<AdditionalTokenSearchListItem> = (
3334
return symbol;
3435
}, [symbol]);
3536

37+
const formattedPath = useMemo(() => {
38+
return makeDisplayPackagePath(path || '');
39+
}, [path]);
40+
3641
return (
3742
<AdditionalTokenSearchListItemWrapper onClick={(): void => onClickListItem(tokenId)}>
3843
<span className='title'>
3944
<span className='name'>{name}</span>
4045
<span className='symbol'>{`(${formattedSymbol})`}</span>
4146
</span>
42-
<span className='path'>{path}</span>
47+
<span className='path'>{formattedPath}</span>
4348
</AdditionalTokenSearchListItemWrapper>
4449
);
4550
};

packages/adena-extension/src/components/pages/additional-token/additional-token/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AdditionalTokenInfo from '@components/pages/additional-token/additional-t
55
import { SubHeader } from '@components/atoms';
66
import LeftArrowIcon from '@assets/arrowL-left.svg';
77
import { AdditionalTokenProps } from '@types';
8+
import { makeDisplayPackagePath } from '@common/utils/string-utils';
89

910
const AdditionalToken: React.FC<AdditionalTokenProps> = ({
1011
opened,
@@ -46,7 +47,7 @@ const AdditionalToken: React.FC<AdditionalTokenProps> = ({
4647
<div className='info-wrapper'>
4748
<AdditionalTokenInfo
4849
symbol={selectedTokenInfo?.symbol || ''}
49-
path={selectedTokenInfo?.pathInfo || ''}
50+
path={makeDisplayPackagePath(selectedTokenInfo?.pathInfo || '')}
5051
decimals={selectedTokenInfo ? `${selectedTokenInfo.decimals}` : ''}
5152
/>
5253
</div>

packages/adena-extension/src/hooks/use-token-metainfo.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ export const useTokenMetainfo = (): UseTokenMetainfoReturn => {
199199
};
200200

201201
const tokenMetainfos = await tokenService.getTokenMetainfosByAccountId(currentAccount.id);
202-
if (tokenMetainfos.find((item) => item.tokenId === changedTokenMetainfo.tokenId)) {
202+
if (
203+
tokenMetainfos.find(
204+
(item) =>
205+
item.tokenId === changedTokenMetainfo.tokenId &&
206+
item.networkId === changedTokenMetainfo.networkId,
207+
)
208+
) {
203209
return false;
204210
}
205211

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { useMemo } from 'react';
2+
3+
import { useAdenaContext } from '@hooks/use-context';
4+
import { useCurrentAccount } from '@hooks/use-current-account';
5+
import { useMakeTransactionsWithTime } from '@hooks/use-make-transactions-with-time';
6+
import { useNetwork } from '@hooks/use-network';
7+
import { useTokenMetainfo } from '@hooks/use-token-metainfo';
8+
import { RefetchOptions, useInfiniteQuery } from '@tanstack/react-query';
9+
import { TransactionInfo } from '@types';
10+
11+
export const useTokenTransactionsPage = (
12+
isNative: boolean | undefined,
13+
tokenPath: string,
14+
{ enabled }: { enabled: boolean },
15+
): {
16+
data: TransactionInfo[] | null;
17+
isFetched: boolean;
18+
status: 'loading' | 'error' | 'success';
19+
isLoading: boolean;
20+
isFetching: boolean;
21+
isSupported: boolean;
22+
hasNextPage: boolean;
23+
fetchNextPage: () => Promise<boolean>;
24+
refetch: (options?: RefetchOptions) => void;
25+
} => {
26+
const { currentNetwork } = useNetwork();
27+
const { currentAddress } = useCurrentAccount();
28+
const { transactionHistoryService } = useAdenaContext();
29+
const { tokenMetainfos } = useTokenMetainfo();
30+
31+
const {
32+
data: allTransactions,
33+
refetch,
34+
hasNextPage,
35+
fetchNextPage,
36+
} = useInfiniteQuery(
37+
[
38+
'token-details/page/history',
39+
currentNetwork.networkId,
40+
`${isNative}`,
41+
currentAddress,
42+
tokenPath,
43+
],
44+
(context: any) => {
45+
if (isNative === undefined) {
46+
return null;
47+
}
48+
49+
const cursor = context.pageParam || null;
50+
51+
return isNative
52+
? transactionHistoryService.fetchNativeTransactionHistoryPage(currentAddress || '', cursor)
53+
: transactionHistoryService.fetchGRC20TransactionHistoryPage(
54+
currentAddress || '',
55+
tokenPath,
56+
cursor,
57+
);
58+
},
59+
{
60+
enabled:
61+
!!currentAddress &&
62+
transactionHistoryService.supported &&
63+
tokenMetainfos.length > 0 &&
64+
enabled,
65+
},
66+
);
67+
68+
const transactions = useMemo(() => {
69+
if (!allTransactions) {
70+
return null;
71+
}
72+
73+
return allTransactions.pages.flatMap((page) => page?.transactions || []);
74+
}, [allTransactions]);
75+
76+
const { data, isFetched, status, isLoading, isFetching } = useMakeTransactionsWithTime(
77+
`token-details/page/history/${currentNetwork.chainId}/${transactions?.length}/${tokenPath}`,
78+
transactions,
79+
);
80+
81+
const refetchTransactions = (options?: RefetchOptions): void => {
82+
refetch(options);
83+
};
84+
85+
return {
86+
data: data || null,
87+
isSupported: transactionHistoryService.supported && enabled,
88+
isFetched: isFetched,
89+
status,
90+
isLoading,
91+
isFetching,
92+
fetchNextPage: () =>
93+
fetchNextPage()
94+
.then(() => true)
95+
.catch(() => false),
96+
hasNextPage: hasNextPage !== false,
97+
refetch: refetchTransactions,
98+
};
99+
};

packages/adena-extension/src/hooks/wallet/token-details/use-token-transactions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import { TransactionInfo } from '@types';
1111
export const useTokenTransactions = (
1212
isNative: boolean | undefined,
1313
tokenPath: string,
14+
{ enabled }: { enabled: boolean },
1415
): {
1516
data: TransactionInfo[] | null;
1617
isFetched: boolean;
1718
status: 'loading' | 'error' | 'success';
1819
isLoading: boolean;
1920
isFetching: boolean;
2021
isSupported: boolean;
22+
hasNextPage: boolean;
2123
fetchNextPage: () => Promise<boolean>;
2224
refetch: (options?: RefetchOptions) => void;
2325
} => {
@@ -39,7 +41,11 @@ export const useTokenTransactions = (
3941
: transactionHistoryService.fetchGRC20TransactionHistory(currentAddress || '', tokenPath);
4042
},
4143
{
42-
enabled: !!currentAddress && transactionHistoryService.supported && tokenMetainfos.length > 0,
44+
enabled:
45+
!!currentAddress &&
46+
transactionHistoryService.supported &&
47+
tokenMetainfos.length > 0 &&
48+
enabled,
4349
},
4450
);
4551

@@ -85,11 +91,12 @@ export const useTokenTransactions = (
8591

8692
return {
8793
data: data || null,
88-
isSupported: transactionHistoryService.supported,
94+
isSupported: transactionHistoryService.supported && enabled,
8995
isFetched: isFetched,
9096
status,
9197
isLoading,
9298
isFetching,
99+
hasNextPage: allTransactions?.length !== blockIndex,
93100
fetchNextPage,
94101
refetch: refetchTransactions,
95102
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { useMemo } from 'react';
2+
import { RefetchOptions, useInfiniteQuery } from '@tanstack/react-query';
3+
4+
import { useAdenaContext } from '@hooks/use-context';
5+
import { useCurrentAccount } from '@hooks/use-current-account';
6+
import { useMakeTransactionsWithTime } from '@hooks/use-make-transactions-with-time';
7+
import { useNetwork } from '@hooks/use-network';
8+
import { useTokenMetainfo } from '@hooks/use-token-metainfo';
9+
import { TransactionInfo, TransactionWithPageInfo } from '@types';
10+
11+
export const useTransactionHistoryPage = ({
12+
enabled,
13+
}: {
14+
enabled: boolean;
15+
}): {
16+
data: TransactionInfo[] | null;
17+
isFetched: boolean;
18+
status: 'loading' | 'error' | 'success';
19+
isLoading: boolean;
20+
isFetching: boolean;
21+
isSupported: boolean;
22+
hasNextPage: boolean;
23+
fetchNextPage: () => Promise<boolean>;
24+
refetch: (options?: RefetchOptions) => void;
25+
} => {
26+
const { currentNetwork } = useNetwork();
27+
const { currentAddress } = useCurrentAccount();
28+
const { transactionHistoryService } = useAdenaContext();
29+
const { tokenMetainfos } = useTokenMetainfo();
30+
31+
const {
32+
data: allTransactions,
33+
hasNextPage,
34+
refetch,
35+
fetchNextPage,
36+
} = useInfiniteQuery<TransactionWithPageInfo, Error, unknown, any>(
37+
{
38+
queryKey: ['history/page/all', currentNetwork.networkId, currentAddress || ''],
39+
getNextPageParam: (lastPage?: TransactionWithPageInfo): string | boolean | null => {
40+
return lastPage?.cursor || null;
41+
},
42+
queryFn: (context: any) => {
43+
if (context?.pageParam === false) {
44+
return {
45+
hasNext: false,
46+
cursor: null,
47+
transactions: [],
48+
};
49+
}
50+
51+
const cursor = context?.pageParam || null;
52+
return transactionHistoryService.fetchAllTransactionHistoryPage(
53+
currentAddress || '',
54+
cursor,
55+
);
56+
},
57+
},
58+
{
59+
enabled:
60+
!!currentAddress &&
61+
tokenMetainfos.length > 0 &&
62+
transactionHistoryService.supported &&
63+
enabled,
64+
},
65+
);
66+
67+
const transactions = useMemo(() => {
68+
if (!allTransactions) {
69+
return null;
70+
}
71+
72+
return allTransactions.pages.flatMap(
73+
(page: unknown) => (page as TransactionWithPageInfo).transactions,
74+
);
75+
}, [allTransactions]);
76+
77+
const { data, isFetched, status, isLoading, isFetching } = useMakeTransactionsWithTime(
78+
`history/page/all/${currentNetwork.chainId}/${transactions?.length}`,
79+
transactions,
80+
);
81+
82+
const refetchTransactions = (options?: RefetchOptions): void => {
83+
refetch(options);
84+
};
85+
86+
return {
87+
data: data || null,
88+
isSupported: transactionHistoryService.supported && enabled,
89+
isFetched: isFetched,
90+
status,
91+
isLoading,
92+
isFetching,
93+
hasNextPage: hasNextPage !== false,
94+
fetchNextPage: (): Promise<boolean> => {
95+
return fetchNextPage()
96+
.then((result) => !result.error)
97+
.catch(() => false);
98+
},
99+
refetch: refetchTransactions,
100+
};
101+
};

0 commit comments

Comments
 (0)