@@ -17,11 +17,10 @@ export async function getAssets() {
1717 price : asset . price . toString ( ) ,
1818 } ) ) ;
1919 await db . insert ( assetsSchema ) . values ( assetsToInsert ) ;
20- const seededAssets = await db . query . assets . findMany ( ) ;
21- return seededAssets . map ( a => ( { ...a , price : parseFloat ( a . price ) } ) ) ;
20+ // After seeding, we continue to fetch all assets including the newly seeded ones.
2221 }
2322
24- const baseAssets = assetsInDb . map ( a => ( { ...a , price : parseFloat ( a . price ) } ) ) ;
23+ const baseAssets = ( await db . query . assets . findMany ( ) ) . map ( a => ( { ...a , price : parseFloat ( a . price ) } ) ) ;
2524
2625 const listedCompanies = await db . query . companies . findMany ( {
2726 where : eq ( companiesSchema . isListed , true ) ,
@@ -50,7 +49,7 @@ export async function getAssets() {
5049 price : sharePrice ,
5150 change24h : '+0.00%' , // Placeholder, as company value change is not tracked over 24h yet
5251 marketCap : marketCapString ,
53- }
52+ } ;
5453 } ) ;
5554
5655 return [ ...baseAssets , ...companyAssets ] ;
@@ -82,13 +81,21 @@ export async function updatePriceFromTrade(ticker: string, tradeValue: number) {
8281 if ( ! asset || asset . type === 'Company Share' ) return ;
8382
8483 const marketCap = parseMarketCap ( asset . marketCap ) ;
85- if ( marketCap === 0 ) return ;
84+ const currentPrice = parseFloat ( asset . price ) ;
85+
86+ // Guards to prevent invalid calculations or division by zero.
87+ if ( marketCap <= 0 || currentPrice <= 0 ) return ;
8688
8789 const IMPACT_CONSTANT = 0.05 ;
8890 const impactPercentage = ( tradeValue / marketCap ) * IMPACT_CONSTANT ;
8991
90- const currentPrice = parseFloat ( asset . price ) ;
9192 const newPrice = currentPrice * ( 1 + impactPercentage ) ;
93+
94+ // Final sanity check to prevent writing bad data (NaN, Infinity, negative) to the DB
95+ if ( isNaN ( newPrice ) || ! isFinite ( newPrice ) || newPrice <= 0 ) {
96+ console . warn ( `Prevented invalid price update for ${ ticker } . New price would be: ${ newPrice } ` ) ;
97+ return ;
98+ }
9299
93100 await db . update ( assetsSchema )
94101 . set ( { price : newPrice . toString ( ) } )
0 commit comments