@@ -20,10 +20,12 @@ import { maspIndexerUrlAtom, rpcUrlAtom } from "atoms/settings";
2020import { queryDependentFn } from "atoms/utils" ;
2121import { isAxiosError } from "axios" ;
2222import BigNumber from "bignumber.js" ;
23+ import * as E from "fp-ts/Either" ;
2324import { sequenceT } from "fp-ts/lib/Apply" ;
2425import { pipe } from "fp-ts/lib/function" ;
2526import * as O from "fp-ts/Option" ;
2627import invariant from "invariant" ;
28+ import * as t from "io-ts" ;
2729import { atom , getDefaultStore } from "jotai" ;
2830import { atomWithQuery } from "jotai-tanstack-query" ;
2931import { atomWithStorage } from "jotai/utils" ;
@@ -109,12 +111,43 @@ export const viewingKeysAtom = atomWithQuery<
109111 } ;
110112} ) ;
111113
114+ const NamadilloShieldedBalanceCodec = t . record (
115+ t . string , // Address
116+ t . array (
117+ t . type ( {
118+ tokenAddress : t . string ,
119+ amount : t . string ,
120+ } )
121+ )
122+ ) ;
123+
112124export const storageShieldedBalanceAtom = atomWithStorage <
113- Record < Address , { address : Address ; minDenomAmount : string } [ ] >
114- > ( "namadillo:shieldedBalance" , { } ) ;
125+ Record < Address , { tokenAddress : Address ; amount : string } [ ] >
126+ > (
127+ "namadillo:shieldedBalance" ,
128+ { } ,
129+ {
130+ // Invalidation function to ensure the stored data matches the codec
131+ getItem ( key , initialValue ) {
132+ const storedValue = localStorage . getItem ( key ) ;
133+ const maybeValue = NamadilloShieldedBalanceCodec . decode (
134+ JSON . parse ( storedValue ?? "{}" )
135+ ) ;
136+
137+ return E . isRight ( maybeValue ) ? maybeValue . right : initialValue ;
138+ } ,
139+ setItem ( key , value ) {
140+ localStorage . setItem ( key , JSON . stringify ( value ) ) ;
141+ } ,
142+ removeItem ( key ) {
143+ localStorage . removeItem ( key ) ;
144+ } ,
145+ }
146+ ) ;
115147
116148export const shieldedSyncProgress = atom ( 0 ) ;
117149
150+ // After changing the stored data type, change the version in the key
118151export const lastCompletedShieldedSyncAtom = atomWithStorage <
119152 Record < Address , Date | undefined >
120153> ( "namadillo:last-shielded-sync" , { } , undefined , { getOnInit : true } ) ;
@@ -132,11 +165,13 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
132165 const rpcUrl = get ( rpcUrlAtom ) ;
133166 const maspIndexerUrl = get ( maspIndexerUrlAtom ) ;
134167 const defaultAccount = get ( defaultAccountAtom ) ;
168+ const assetsMapAtom = get ( namadaRegistryChainAssetsMapAtom ) ;
135169
136170 const [ viewingKey , allViewingKeys ] = viewingKeysQuery . data ?? [ ] ;
137171 const chainTokens = chainTokensQuery . data ?. map ( ( t ) => t . address ) ;
138172 const chainId = chainParametersQuery . data ?. chainId ;
139173 const namTokenAddress = namTokenAddressQuery . data ;
174+ const assetsMap = assetsMapAtom . data ;
140175
141176 return {
142177 refetchInterval : enablePolling ? 1000 : false ,
@@ -148,7 +183,8 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
148183 ! chainTokens ||
149184 ! chainId ||
150185 ! namTokenAddress ||
151- ! rpcUrl
186+ ! rpcUrl ||
187+ ! assetsMap
152188 ) {
153189 return [ ] ;
154190 }
@@ -168,16 +204,24 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
168204 chainTokens ,
169205 chainId
170206 ) ;
171-
172- const shieldedBalance = response . map ( ( [ address , amount ] ) => ( {
173- address,
174- minDenomAmount : amount ,
175- } ) ) ;
207+ const shieldedBalance = response
208+ // Filter out unknown assets
209+ . filter ( ( [ address ] ) => address in assetsMap )
210+ . map ( ( [ address , amount ] ) => {
211+ const asset = assetsMap [ address ] ;
212+ return {
213+ tokenAddress : address ,
214+ amount : toDisplayAmount ( asset , BigNumber ( amount ) ) ,
215+ } ;
216+ } ) ;
176217
177218 const storage = get ( storageShieldedBalanceAtom ) ;
178219 set ( storageShieldedBalanceAtom , {
179220 ...storage ,
180- [ viewingKey . key ] : shieldedBalance ,
221+ [ viewingKey . key ] : shieldedBalance . map ( ( balance ) => ( {
222+ ...balance ,
223+ amount : balance . amount . toString ( ) ,
224+ } ) ) ,
181225 } ) ;
182226
183227 if ( defaultAccount . data ) {
@@ -198,6 +242,7 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
198242 chainTokensQuery ,
199243 chainParametersQuery ,
200244 namTokenAddressQuery ,
245+ assetsMapAtom ,
201246 ] ) ,
202247 } ;
203248} ) ;
@@ -218,8 +263,10 @@ export const namadaShieldedAssetsAtom = atomWithQuery((get) => {
218263
219264 return mapNamadaAddressesToAssets ( {
220265 balances :
221- shieldedBalance ?. map ( ( i ) => ( { ...i , tokenAddress : i . address } ) ) ??
222- [ ] ,
266+ shieldedBalance ?. map ( ( i ) => ( {
267+ ...i ,
268+ amount : BigNumber ( i . amount ) ,
269+ } ) ) ?? [ ] ,
223270 assets : Object . values ( chainAssetsMap . data ) ,
224271 } ) ;
225272 } , [ viewingKeysQuery , chainTokensQuery , chainAssetsMap ] ) ,
0 commit comments