@@ -52,13 +52,31 @@ function formatLabel(timestamp: number, period: string): string {
5252 }
5353}
5454
55- type BalanceHistoryEntry = { timestamp : number ; date : string ; balance : string } ;
55+ type BalanceHistoryEntry = {
56+ timestamp : number ;
57+ date : string ;
58+ balance : string ;
59+ } ;
60+
5661type AllTokenBalanceHistoryCache = {
5762 get : ( key : string ) => any ;
5863 set : ( key : string , value : any , ttl ?: number ) => void ;
5964 del : ( key : string ) => void ;
6065} ;
6166
67+ const groupByPeriod = ( history : BalanceHistoryEntry [ ] ) => {
68+ const grouped = new Map < string , BalanceHistoryEntry > ( ) ;
69+
70+ for ( const entry of history ) {
71+ const key = entry . date ;
72+ if ( ! grouped . has ( key ) || entry . timestamp > grouped . get ( key ) ! . timestamp ) {
73+ grouped . set ( key , entry ) ;
74+ }
75+ }
76+
77+ return Object . fromEntries ( grouped ) ;
78+ } ;
79+
6280export async function getAllTokenBalanceHistory (
6381 cache : AllTokenBalanceHistoryCache ,
6482 cacheKey : string ,
@@ -142,20 +160,14 @@ export async function getAllTokenBalanceHistory(
142160 } ;
143161 }
144162
145- // Clamp the number of steps to the intended interval count
146- const totalSteps = Math . min (
163+ let totalSteps = Math . min (
147164 interval ,
148165 Math . floor ( ( currentBlock - lastStoredBlock ) / blocksPerStep )
149166 ) ;
150167
151168 if ( totalSteps <= 0 ) {
152169 console . log ( `[${ period } ] [${ account_id } ] No new steps to fetch.` ) ;
153- return {
154- period,
155- data : Array . isArray ( prev ?. balance_history )
156- ? prev . balance_history
157- : [ ] ,
158- } ;
170+ totalSteps = 1 ;
159171 }
160172
161173 const blockHeights = Array . from (
@@ -234,7 +246,6 @@ export async function getAllTokenBalanceHistory(
234246 let stakeBalances : any [ ] = [ ] ;
235247 if ( token_id === "near" ) {
236248 // fetch all pools where user has staked near and get the balance for each at each blockheights
237-
238249 stakeBalances = await getUserStakeBalances (
239250 account_id ,
240251 blockHeights ,
@@ -258,17 +269,25 @@ export async function getAllTokenBalanceHistory(
258269 balance = raw ? raw . replace ( / " / g, "" ) : "0" ;
259270 }
260271
272+ const ts = timestamps [ index ] ;
273+
261274 return {
262- timestamp : timestamps [ index ] ,
263- date : formatLabel ( timestamps [ index ] , period ) ,
275+ timestamp : ts ,
276+ date : formatLabel ( ts , period ) ,
264277 balance : convertFTBalance ( balance , decimals ) ,
265278 } ;
266279 } ) ;
267280
268- const fullHistory = [
281+ const groupedHistory = groupByPeriod ( newHistory ) ;
282+
283+ const mergedHistory = [
269284 ...( ( prev ?. balance_history as BalanceHistoryEntry [ ] ) || [ ] ) ,
270- ...newHistory ,
271- ] . slice ( - interval ) ;
285+ ...Object . values ( groupedHistory ) ,
286+ ] ;
287+
288+ const finalHistory = Object . values ( groupByPeriod ( mergedHistory ) ) . slice (
289+ - interval
290+ ) ;
272291
273292 if ( prev ) {
274293 await prisma . tokenBalanceHistory . update ( {
@@ -280,7 +299,7 @@ export async function getAllTokenBalanceHistory(
280299 } ,
281300 } ,
282301 data : {
283- balance_history : fullHistory ,
302+ balance_history : finalHistory ,
284303 toBlock : currentBlock ,
285304 } ,
286305 } ) ;
@@ -290,14 +309,14 @@ export async function getAllTokenBalanceHistory(
290309 account_id,
291310 token_id,
292311 period,
293- balance_history : fullHistory ,
312+ balance_history : finalHistory ,
294313 fromBlock : blockHeights [ 0 ] ,
295314 toBlock : currentBlock ,
296315 } ,
297316 } ) ;
298317 }
299318
300- return { period, data : fullHistory } ;
319+ return { period, data : finalHistory } ;
301320 } )
302321 ) ;
303322
0 commit comments