@@ -25,6 +25,7 @@ import { useMemo } from 'react'
25
25
import { IconCoin , IconError } from 'app/components/icons'
26
26
import { useCoins } from 'app/provider/coins'
27
27
import { investmentCoins as investmentCoinsList } from 'app/data/coins'
28
+ import { formatUnits } from 'viem'
28
29
import { HomeBodyCard } from './screen'
29
30
30
31
export const InvestmentsBalanceCard = ( props : CardProps ) => {
@@ -141,22 +142,41 @@ function OverlappingCoinIcons({
141
142
}
142
143
143
144
function InvestmentsAggregate ( ) {
144
- const tokenIds = useCoins ( )
145
- . investmentCoins . filter ( ( c ) => c ?. balance && c . balance > 0n )
146
- . map ( ( c ) => c . coingeckoTokenId )
145
+ const coins = useCoins ( ) . investmentCoins . filter ( ( c ) => c ?. balance && c . balance > 0n )
147
146
147
+ const tokenIds = coins . map ( ( c ) => c . coingeckoTokenId )
148
148
const { data : marketData , isLoading, isError } = useMultipleTokensMarketData ( tokenIds )
149
+
150
+ const { totalValue, assetValues } = useMemo ( ( ) => {
151
+ if ( ! marketData ?. length ) return { totalValue : 0 , assetValues : [ ] }
152
+
153
+ // Calculate values for each asset and total
154
+ const assetValues = coins . map ( ( coin ) => {
155
+ const marketInfo = marketData . find ( ( m ) => m . id === coin . coingeckoTokenId )
156
+ if ( ! marketInfo || ! coin . balance ) return { value : 0 , percentChange : 0 }
157
+
158
+ const parsedBalance = formatUnits ( coin . balance , coin . decimals )
159
+ return {
160
+ value : Number ( parsedBalance ) * ( marketInfo . current_price ?? 0 ) ,
161
+ percentChange : marketInfo . price_change_percentage_24h ?? 0 ,
162
+ }
163
+ } )
164
+
165
+ const totalValue = assetValues . reduce ( ( sum , asset ) => sum + asset . value , 0 )
166
+
167
+ return { totalValue, assetValues }
168
+ } , [ marketData , coins ] )
169
+
149
170
const aggregatePercentage = useMemo ( ( ) => {
150
- if ( ! marketData ?. length ) return 0
171
+ if ( totalValue <= 0 ) return 0
151
172
152
- // Simple average of percentage changes
153
- const aggregatePercentage =
154
- marketData . reduce ( ( total , coin ) => {
155
- return total + ( coin ?. price_change_percentage_24h ?? 0 )
156
- } , 0 ) / marketData . length
173
+ const weightedPercentage = assetValues . reduce ( ( sum , asset ) => {
174
+ const weight = asset . value / totalValue
175
+ return sum + asset . percentChange * weight
176
+ } , 0 )
157
177
158
- return Number ( aggregatePercentage . toFixed ( 2 ) )
159
- } , [ marketData ] )
178
+ return Math . round ( weightedPercentage * 100 ) / 100
179
+ } , [ totalValue , assetValues ] )
160
180
161
181
if ( tokenIds . length === 0 )
162
182
return (
0 commit comments