Skip to content

Commit 01c79b7

Browse files
Fix query cache for block explorers for raw chains
1 parent 47354d5 commit 01c79b7

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

.changeset/eight-zoos-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix query cache for block explorers for raw chains

packages/thirdweb/src/react/core/hooks/others/useChainQuery.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ export function useChainName(chain?: Chain) {
1515
// only if we have a chain and no chain name!
1616
const isEnabled = !!chain && !chain.name;
1717
const chainQuery = useQuery({
18-
enabled: isEnabled,
1918
queryFn: async () => {
2019
if (!chain) {
2120
throw new Error("chain is required");
2221
}
2322
return convertApiChainToChain(await getChainMetadata(chain));
2423
},
25-
queryKey: ["chain", chain?.id],
24+
...getQueryOptions(chain),
25+
enabled: isEnabled,
2626
retry: false,
27-
// 1 hour
28-
staleTime: 60 * 60 * 1000,
2927
});
3028

3129
return {
@@ -39,17 +37,15 @@ export function useChainIconUrl(chain?: Chain) {
3937
const isEnabled = !!chain && !chain.icon?.url;
4038
const chainQuery = useQuery({
4139
// only if we have a chain and no chain icon url!
42-
enabled: isEnabled,
4340
queryFn: async () => {
4441
if (!chain) {
4542
throw new Error("chain is required");
4643
}
4744
return convertApiChainToChain(await getChainMetadata(chain));
4845
},
49-
queryKey: ["chain", chain?.id],
46+
...getQueryOptions(chain),
47+
enabled: isEnabled,
5048
retry: false,
51-
// 1 hour
52-
staleTime: 60 * 60 * 1000,
5349
});
5450

5551
return {
@@ -67,17 +63,14 @@ export function useChainFaucets(chain?: Chain) {
6763
chain.id !== 1337;
6864

6965
const chainQuery = useQuery({
70-
enabled: isEnabled,
7166
queryFn: async () => {
7267
if (!chain) {
7368
throw new Error("chain is required");
7469
}
75-
return convertApiChainToChain(await getChainMetadata(chain));
70+
return getChainMetadata(chain);
7671
},
77-
queryKey: ["chain", chain?.id],
78-
retry: false,
79-
// 1 hour
80-
staleTime: 60 * 60 * 1000,
72+
...getQueryOptions(chain),
73+
enabled: isEnabled,
8174
});
8275

8376
return {
@@ -90,18 +83,14 @@ export function useChainSymbol(chain?: Chain) {
9083
// only if we have a chain and no chain icon url!
9184
const isEnabled = !!chain && !chain.nativeCurrency?.symbol;
9285
const chainQuery = useQuery({
93-
// only if we have a chain and no chain icon url!
94-
enabled: isEnabled,
9586
queryFn: async () => {
9687
if (!chain) {
9788
throw new Error("chain is required");
9889
}
99-
return convertApiChainToChain(await getChainMetadata(chain));
90+
return getChainMetadata(chain);
10091
},
101-
queryKey: ["chain", chain?.id],
102-
retry: false,
103-
// 1 hour
104-
staleTime: 60 * 60 * 1000,
92+
...getQueryOptions(chain),
93+
enabled: isEnabled,
10594
});
10695

10796
return {
@@ -116,21 +105,19 @@ export function useChainExplorers(chain?: Chain) {
116105
const isEnabled = !!chain && !chain.blockExplorers?.length;
117106

118107
const chainQuery = useQuery({
119-
enabled: isEnabled,
120108
queryFn: async () => {
121109
if (!chain) {
122110
throw new Error("chain is required");
123111
}
124-
return convertApiChainToChain(await getChainMetadata(chain));
112+
return getChainMetadata(chain);
125113
},
126-
queryKey: ["chain", chain?.id],
127-
retry: false,
128-
// 1 hour
129-
staleTime: 60 * 60 * 1000,
114+
...getQueryOptions(chain),
115+
enabled: isEnabled,
130116
});
131117

118+
const toChain = chainQuery.data ?convertApiChainToChain(chainQuery.data) : undefined;
132119
return {
133-
explorers: chain?.blockExplorers ?? chainQuery.data?.blockExplorers ?? [],
120+
explorers: chain?.blockExplorers && chain?.blockExplorers?.length > 0 ? chain?.blockExplorers : toChain?.blockExplorers ?? [],
134121
isLoading: isEnabled && chainQuery.isLoading,
135122
};
136123
}

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/WalletTransactionHistory.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { useActiveWalletChain } from "../../../../core/hooks/wallets/useActiveWa
2424
import { Container } from "../../components/basic.js";
2525
import { Button } from "../../components/buttons.js";
2626
import { ChainIcon } from "../../components/ChainIcon.js";
27-
import { ChainName } from "../../components/ChainName.js";
2827
import { Spacer } from "../../components/Spacer.js";
2928
import { Spinner } from "../../components/Spinner.js";
3029
import { Text } from "../../components/text.js";
@@ -141,6 +140,7 @@ function TransactionButton(props: {
141140
});
142141
const chainIconQuery = useChainIconUrl(getCachedChain(props.tx.chainId));
143142
const receipt = props.tx.receipt ?? fetchedReceipt;
143+
const decoded = props.tx.decoded;
144144

145145
const content = (
146146
<TxButton
@@ -181,9 +181,7 @@ function TransactionButton(props: {
181181
}}
182182
>
183183
<Text color="primaryText" size="sm">
184-
{receipt?.to
185-
? `Interacted with ${shortenHex(receipt?.to, 4)}`
186-
: `Hash: ${shortenHex(props.tx.transactionHash, 4)}`}
184+
{decoded ? decoded.name : `Transaction Sent`}
187185
</Text>
188186
</Container>
189187

@@ -198,11 +196,9 @@ function TransactionButton(props: {
198196
justifyContent: "space-between",
199197
}}
200198
>
201-
<ChainName
202-
chain={getCachedChain(props.tx.chainId)}
203-
client={props.client}
204-
size="xs"
205-
/>
199+
<Text color="secondaryText" size="xs">
200+
{receipt?.to ? shortenHex(receipt?.to, 4) : shortenHex(props.tx.transactionHash, 4)}
201+
</Text>
206202
</Container>
207203
</div>
208204
</Container>

packages/thirdweb/src/transaction/transaction-store.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export type StoredTransaction = {
1111
status: "success" | "failed";
1212
to: string;
1313
};
14+
decoded?: {
15+
name: string;
16+
signature: string;
17+
inputs?: {
18+
[key: string]: unknown;
19+
};
20+
};
1421
};
1522

1623
const transactionsByAddress = new Map<string, Store<StoredTransaction[]>>();
@@ -81,6 +88,7 @@ export async function getPastTransactions(options: {
8188
queryOptions: {
8289
filter_block_timestamp_gte: oneMonthsAgoInSeconds,
8390
limit: 20,
91+
decode: true,
8492
},
8593
walletAddress,
8694
});
@@ -90,9 +98,10 @@ export async function getPastTransactions(options: {
9098
? Number(tx.chain_id)
9199
: (tx.chain_id as number),
92100
receipt: {
93-
status: tx.status === 1 ? "success" : "failed",
101+
status: tx.status === 0 ? "failed" : "success",
94102
to: tx.to_address,
95103
},
96104
transactionHash: tx.hash as Hex,
105+
decoded: tx.decoded,
97106
}));
98107
}

0 commit comments

Comments
 (0)