1- import { useCallback , useMemo } from 'react'
1+ import { useCallback } from 'react'
22
3- import { useCoinBalance , useArtistCoin , useUserCoin } from '@audius/common/api'
3+ import {
4+ useCoinBalance ,
5+ useArtistCoin ,
6+ useCoinBalanceBreakdown
7+ } from '@audius/common/api'
48import {
59 useFormattedCoinBalance ,
610 useIsManagedAccount ,
@@ -12,6 +16,7 @@ import {
1216 sendTokensModalActions
1317} from '@audius/common/store'
1418import { shortenSPLAddress } from '@audius/common/utils'
19+ import { AUDIO } from '@audius/fixed-decimal'
1520import { useDispatch } from 'react-redux'
1621
1722import {
@@ -24,6 +29,7 @@ import {
2429} from '@audius/harmony-native'
2530import { TokenIcon } from 'app/components/core'
2631import { useNavigation } from 'app/hooks/useNavigation'
32+ import { env } from 'app/services/env'
2733
2834const messages = coinDetailsMessages . balance
2935
@@ -95,31 +101,25 @@ const HasBalanceState = ({
95101 onSend,
96102 onReceive,
97103 mint,
98- coinName
99- } : BalanceStateProps & { mint : string ; coinName : string } ) => {
104+ coinName,
105+ isAudio
106+ } : BalanceStateProps & {
107+ mint : string
108+ coinName : string
109+ isAudio : boolean
110+ } ) => {
100111 const isManagerMode = useIsManagedAccount ( )
101112 const { coinBalanceFormatted, formattedHeldValue } =
102113 useFormattedCoinBalance ( mint )
103114
104- // Fetch wallet accounts for balance breakdown
105- const { data : userCoins } = useUserCoin ( { mint } )
106- const { accounts : unsortedAccounts = [ ] , decimals } = userCoins ?? { }
107-
108- // Sort accounts by balance (descending)
109- const accounts = useMemo (
110- ( ) => [ ...unsortedAccounts ] . sort ( ( a , b ) => b . balance - a . balance ) ,
111- [ unsortedAccounts ]
112- )
113-
114- // Separate built-in wallet from linked wallets
115- const inAppWallet = useMemo (
116- ( ) => accounts . find ( ( account ) => account . isInAppWallet ) ,
117- [ accounts ]
118- )
119- const linkedWallets = useMemo (
120- ( ) => accounts . filter ( ( account ) => ! account . isInAppWallet ) ,
121- [ accounts ]
122- )
115+ const {
116+ decimals,
117+ inAppWallet,
118+ linkedWallets,
119+ audioBuiltInBalance,
120+ associatedAudioBalances,
121+ hasBreakdown
122+ } = useCoinBalanceBreakdown ( { mint, isAudio } )
123123
124124 return (
125125 < Flex column gap = 'l' w = '100%' >
@@ -162,15 +162,15 @@ const HasBalanceState = ({
162162 </ Text >
163163 </ Flex >
164164 </ Flex >
165- { linkedWallets . length > 0 && (
165+ { hasBreakdown && (
166166 < >
167167 < Divider />
168168 < Flex column gap = 's' w = '100%' >
169169 < Text variant = 'title' size = 'l' >
170170 { coinDetailsMessages . externalWallets . hasBalanceTitle }
171171 </ Text >
172172 < Flex column gap = 's' w = '100%' >
173- { inAppWallet && (
173+ { ( inAppWallet || ( isAudio && audioBuiltInBalance ) ) && (
174174 < Flex
175175 direction = 'row'
176176 alignItems = 'center'
@@ -182,36 +182,77 @@ const HasBalanceState = ({
182182 { coinDetailsMessages . externalWallets . builtIn }
183183 </ Text >
184184 < Text variant = 'body' size = 'l' >
185- { Math . trunc (
186- inAppWallet . balance / Math . pow ( 10 , decimals ?? 0 )
187- ) . toLocaleString ( ) }
185+ { isAudio
186+ ? audioBuiltInBalance
187+ : Math . trunc (
188+ inAppWallet ! . balance / Math . pow ( 10 , decimals ?? 0 )
189+ ) . toLocaleString ( ) }
188190 </ Text >
189191 </ Flex >
190192 ) }
191- { linkedWallets . map ( ( wallet , index ) => (
192- < Flex
193- key = { wallet . owner }
194- direction = 'row'
195- alignItems = 'center'
196- justifyContent = 'space-between'
197- w = '100%'
198- pv = '2xs'
199- >
200- < Flex gap = 'xs' alignItems = 'center' row >
201- < Text variant = 'body' size = 'l' >
202- { walletMessages . linkedWallets . wallet ( index ) }
203- </ Text >
204- < Text variant = 'body' size = 'l' color = 'subdued' >
205- ({ shortenSPLAddress ( wallet . owner ) } )
206- </ Text >
207- </ Flex >
208- < Text variant = 'body' size = 'l' >
209- { Math . trunc (
210- wallet . balance / Math . pow ( 10 , decimals ?? 0 )
211- ) . toLocaleString ( ) }
212- </ Text >
213- </ Flex >
214- ) ) }
193+ { isAudio
194+ ? // For AUDIO, show associated wallets (both ETH and SOL)
195+ associatedAudioBalances . data . map ( ( walletBalance , index ) => {
196+ if (
197+ ! walletBalance . balance ||
198+ walletBalance . balance === AUDIO ( 0 ) . value
199+ )
200+ return null
201+ // Use AUDIO constructor to format bigint balance properly
202+ const balanceFormatted = AUDIO (
203+ walletBalance . balance
204+ ) . toLocaleString ( 'en-US' , {
205+ maximumFractionDigits : 2 ,
206+ roundingMode : 'trunc'
207+ } )
208+ return (
209+ < Flex
210+ key = { `${ walletBalance . chain } -${ walletBalance . address } ` }
211+ direction = 'row'
212+ alignItems = 'center'
213+ justifyContent = 'space-between'
214+ w = '100%'
215+ pv = '2xs'
216+ >
217+ < Flex gap = 'xs' alignItems = 'center' row >
218+ < Text variant = 'body' size = 'l' >
219+ { walletMessages . linkedWallets . wallet ( index ) }
220+ </ Text >
221+ < Text variant = 'body' size = 'l' color = 'subdued' >
222+ ({ shortenSPLAddress ( walletBalance . address ) } )
223+ </ Text >
224+ </ Flex >
225+ < Text variant = 'body' size = 'l' >
226+ { balanceFormatted }
227+ </ Text >
228+ </ Flex >
229+ )
230+ } )
231+ : // For other coins, show SPL linked wallets
232+ linkedWallets . map ( ( wallet , index ) => (
233+ < Flex
234+ key = { wallet . owner }
235+ direction = 'row'
236+ alignItems = 'center'
237+ justifyContent = 'space-between'
238+ w = '100%'
239+ pv = '2xs'
240+ >
241+ < Flex gap = 'xs' alignItems = 'center' row >
242+ < Text variant = 'body' size = 'l' >
243+ { walletMessages . linkedWallets . wallet ( index ) }
244+ </ Text >
245+ < Text variant = 'body' size = 'l' color = 'subdued' >
246+ ({ shortenSPLAddress ( wallet . owner ) } )
247+ </ Text >
248+ </ Flex >
249+ < Text variant = 'body' size = 'l' >
250+ { Math . trunc (
251+ wallet . balance / Math . pow ( 10 , decimals ?? 0 )
252+ ) . toLocaleString ( ) }
253+ </ Text >
254+ </ Flex >
255+ ) ) }
215256 </ Flex >
216257 </ Flex >
217258 < Divider />
@@ -248,6 +289,7 @@ export const BalanceCard = ({ mint }: { mint: string }) => {
248289 const { data : coin , isPending : coinsLoading } = useArtistCoin ( mint )
249290 const { data : tokenBalance } = useCoinBalance ( { mint } )
250291 const initialTab = useBuySellInitialTab ( )
292+ const isAudio = mint === env . WAUDIO_MINT_ADDRESS
251293
252294 const handleBuy = useCallback ( ( ) => {
253295 navigation . navigate ( 'BuySell' , {
@@ -293,6 +335,7 @@ export const BalanceCard = ({ mint }: { mint: string }) => {
293335 onReceive = { handleReceive }
294336 mint = { mint }
295337 coinName = { coinName }
338+ isAudio = { isAudio }
296339 />
297340 ) }
298341 </ Paper >
0 commit comments