Skip to content

Commit 07464bd

Browse files
authored
Fixed regression - CAT names no longer show on offer overview or details screens (#2802)
* CHIA-3829: Fixed unknown CAT issue * Fixed APIs affected by 2.5.7 breaking changes * Fixed a missing prop
1 parent a60333c commit 07464bd

5 files changed

Lines changed: 67 additions & 17 deletions

File tree

packages/api-react/src/services/wallet.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-param-reassign -- This file use Immer */
2-
import { CAT, DID, Farmer, NFT, Pool, WalletService, WalletType, toBech32m, VC } from '@chia-network/api';
2+
import { CAT, DID, Farmer, NFT, Pool, WalletService, WalletType, toBech32m, VC, normalizeHex } from '@chia-network/api';
33
import type { NFTInfo, Transaction, Wallet, WalletBalance } from '@chia-network/api';
44
import BigNumber from 'bignumber.js';
55

@@ -91,7 +91,7 @@ export const walletApi = apiWithTag.injectEndpoints({
9191
throw assetError as Error;
9292
}
9393

94-
meta.assetId = assetData.assetId;
94+
meta.assetId = normalizeHex(assetData.assetId);
9595

9696
// get CAT name
9797
const { data: nameData, error: nameError } = await fetchWithBQ({
@@ -120,8 +120,12 @@ export const walletApi = apiWithTag.injectEndpoints({
120120
meta.did = didData.didId;
121121
}
122122

123+
// Normalize authorizedProviders for CRCAT wallets (bytes32 now has 0x prefix in 2.5.7+)
124+
const normalizedAuthorizedProviders = wallet.authorizedProviders?.map(normalizeHex);
125+
123126
return {
124127
...wallet,
128+
...(normalizedAuthorizedProviders && { authorizedProviders: normalizedAuthorizedProviders }),
125129
meta,
126130
};
127131
}),
@@ -228,14 +232,15 @@ export const walletApi = apiWithTag.injectEndpoints({
228232
transformResponse: (response) => {
229233
const {
230234
walletBalance,
231-
walletBalance: { confirmedWalletBalance, unconfirmedWalletBalance },
235+
walletBalance: { confirmedWalletBalance, unconfirmedWalletBalance, assetId },
232236
} = response;
233237

234238
const pendingBalance = new BigNumber(unconfirmedWalletBalance).minus(confirmedWalletBalance);
235239
const pendingTotalBalance = new BigNumber(confirmedWalletBalance).plus(pendingBalance);
236240

237241
return {
238242
...walletBalance,
243+
...(assetId && { assetId: normalizeHex(assetId) }),
239244
pendingBalance,
240245
pendingTotalBalance,
241246
};
@@ -269,7 +274,21 @@ export const walletApi = apiWithTag.injectEndpoints({
269274
]),
270275
}),
271276

272-
getWalletBalances: query(build, WalletService, 'getWalletBalances', {}),
277+
getWalletBalances: query(build, WalletService, 'getWalletBalances', {
278+
transformResponse: (response) => {
279+
const { walletBalances } = response;
280+
// Normalize assetId in each wallet balance
281+
const normalizedBalances: Record<string, WalletBalance> = {};
282+
Object.entries(walletBalances).forEach(([walletId, balance]) => {
283+
const walletBalance = balance as WalletBalance;
284+
normalizedBalances[walletId] = {
285+
...walletBalance,
286+
...(walletBalance.assetId && { assetId: normalizeHex(walletBalance.assetId) }),
287+
};
288+
});
289+
return normalizedBalances;
290+
},
291+
}),
273292

274293
getFarmedAmount: query(build, WalletService, 'getFarmedAmount', {
275294
onCacheEntryAdded: onCacheEntryAddedInvalidate(baseQuery, api, [
@@ -654,7 +673,12 @@ export const walletApi = apiWithTag.injectEndpoints({
654673
invalidatesTags: (_result, _error, { tradeId }) => [{ type: 'OfferTradeRecord', id: tradeId }],
655674
}),
656675

657-
checkOfferValidity: mutation(build, WalletService, 'checkOfferValidity'),
676+
checkOfferValidity: mutation(build, WalletService, 'checkOfferValidity', {
677+
transformResponse: (response) => ({
678+
...response,
679+
id: normalizeHex(response.id),
680+
}),
681+
}),
658682

659683
takeOffer: mutation(build, WalletService, 'takeOffer', {
660684
invalidatesTags: [{ type: 'OfferTradeRecord', id: 'LIST' }, 'OfferCounts'],
@@ -694,11 +718,15 @@ export const walletApi = apiWithTag.injectEndpoints({
694718
}),
695719

696720
getCATAssetId: query(build, CAT, 'getAssetId', {
697-
transformResponse: (response) => response.assetId,
721+
transformResponse: (response) => normalizeHex(response.assetId),
698722
}),
699723

700724
getCatList: query(build, CAT, 'getCatList', {
701-
transformResponse: (response) => response.catList,
725+
transformResponse: (response) =>
726+
response.catList.map((cat: { assetId: string; name: string; symbol: string }) => ({
727+
...cat,
728+
assetId: normalizeHex(cat.assetId),
729+
})),
702730
providesTags(result) {
703731
return result
704732
? [...result.map(({ assetId }) => ({ type: 'CATs', id: assetId }) as const), { type: 'CATs', id: 'LIST' }]
@@ -718,7 +746,14 @@ export const walletApi = apiWithTag.injectEndpoints({
718746
}),
719747

720748
getStrayCats: query(build, CAT, 'getStrayCats', {
721-
transformResponse: (response) => response.strayCats,
749+
transformResponse: (response) =>
750+
response.strayCats.map(
751+
(cat: { assetId: string; name: string; firstSeenHeight: number; senderPuzzleHash: string }) => ({
752+
...cat,
753+
assetId: normalizeHex(cat.assetId),
754+
senderPuzzleHash: normalizeHex(cat.senderPuzzleHash),
755+
}),
756+
),
722757
}),
723758

724759
// TODO refactor

packages/api/src/@types/WalletBalance.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type WalletBalance = {
2+
assetId?: string;
23
confirmedWalletBalance: number;
34
fingerprint: number;
45
maxSendAmount: number;

packages/api/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export { default as optionsForPlotter } from './optionsForPlotter';
99
export { default as toCamelCase } from './toCamelCase';
1010
export { default as toSnakeCase } from './toSnakeCase';
1111
export { default as toBech32m, fromBech32m, decodeBech32m } from './toBech32m';
12+
export { default as normalizeHex } from './normalizeHex';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Normalize a hex string by removing the '0x' prefix and converting to lowercase.
3+
*
4+
* This is needed because chia-blockchain 2.5.7+ returns bytes32 fields with '0x' prefix
5+
* via the @marshal decorator, while offer summaries and other parts of the codebase
6+
* use hex strings without the prefix.
7+
*
8+
* @param hex - The hex string to normalize (with or without '0x' prefix)
9+
* @returns The normalized hex string (lowercase, no '0x' prefix)
10+
*/
11+
export default function normalizeHex(hex: string): string {
12+
return hex.toLowerCase().replace(/^0x/, '');
13+
}

packages/gui/src/hooks/useAssetIdName.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WalletType } from '@chia-network/api';
1+
import { WalletType, normalizeHex } from '@chia-network/api';
22
import { useGetCatListQuery, useGetWalletsQuery } from '@chia-network/api-react';
33
import type { CATToken, Wallet } from '@chia-network/core';
44
import { useCurrencyCode } from '@chia-network/core';
@@ -44,10 +44,10 @@ export default function useAssetIdName() {
4444
symbol = currencyCode;
4545
isVerified = true;
4646
} else if ([WalletType.CAT, WalletType.RCAT, WalletType.CRCAT].includes(walletType)) {
47-
const lowercaseTail = wallet.meta.assetId.toLowerCase();
48-
const cat = catList.find((catItem: CATToken) => catItem.assetId.toLowerCase() === lowercaseTail);
47+
const normalizedTail = normalizeHex(wallet.meta.assetId);
48+
const cat = catList.find((catItem: CATToken) => normalizeHex(catItem.assetId) === normalizedTail);
4949

50-
assetId = lowercaseTail;
50+
assetId = normalizedTail;
5151
name = wallet.name;
5252

5353
if (cat) {
@@ -73,11 +73,11 @@ export default function useAssetIdName() {
7373
});
7474

7575
catList.forEach((cat: CATToken) => {
76-
if (assetIdNameMapping.has(cat.assetId)) {
76+
const normalizedCatAssetId = normalizeHex(cat.assetId);
77+
if (assetIdNameMapping.has(normalizedCatAssetId)) {
7778
return;
7879
}
7980

80-
const { assetId } = cat;
8181
const { name } = cat;
8282
const { symbol } = cat;
8383
const displayName = symbol || name;
@@ -88,9 +88,9 @@ export default function useAssetIdName() {
8888
symbol,
8989
displayName,
9090
isVerified: true,
91-
assetId,
91+
assetId: normalizedCatAssetId,
9292
};
93-
assetIdNameMapping.set(assetId, entry);
93+
assetIdNameMapping.set(normalizedCatAssetId, entry);
9494
});
9595

9696
// If using testnet, add a TXCH assetId entry
@@ -117,7 +117,7 @@ export default function useAssetIdName() {
117117
ref.current = memoized;
118118

119119
const lookupByAssetId = useCallback(
120-
(assetId: string) => ref.current.assetIdNameMapping.get(assetId.toLowerCase()),
120+
(assetId: string) => ref.current.assetIdNameMapping.get(normalizeHex(assetId)),
121121
[ref],
122122
);
123123

0 commit comments

Comments
 (0)