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' ;
33import type { NFTInfo , Transaction , Wallet , WalletBalance } from '@chia-network/api' ;
44import 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
0 commit comments