@@ -12,6 +12,7 @@ import { BlockSupplyProps } from "../../types/models/BlockSupply";
1212import { fetchPaginatedRecords } from "../utils/db" ;
1313import { getBlockId } from "../utils/ids" ;
1414import { stringify } from "../utils/json" ;
15+ import getQueryClient from "../utils/query_client" ;
1516
1617export const getSupplyId = function ( denom : string , height : number ) : string {
1718 return `${ denom } @${ height } ` ;
@@ -25,7 +26,7 @@ export const getSupplyRecord = function(supply: Coin, block: CosmosBlock): Suppl
2526 } ) ;
2627} ;
2728
28- export async function queryTotalSupply ( ) : Promise < Coin [ ] > {
29+ export async function queryTotalSupply ( block : CosmosBlock ) : Promise < Coin [ ] > {
2930 logger . debug ( `[handleSupply] querying total supply` ) ;
3031 const finalSupply : Coin [ ] = [ ] ;
3132 let paginationKey : Uint8Array | undefined ;
@@ -35,7 +36,7 @@ export async function queryTotalSupply(): Promise<Coin[]> {
3536 // To avoid this, we need to move to implement our own rpc client and also use `unsafe` parameter which I prefer to avoid.
3637 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3738 // @ts -ignore
38- const queryClient = api . forceGetQueryClient ( ) ;
39+ const queryClient = getQueryClient ( block . header . height ) ;
3940
4041 // Initial call to get the first set of results
4142 const initialResponse : QueryTotalSupplyResponse = await queryClient . bank . totalSupply ( ) as unknown as QueryTotalSupplyResponse ;
@@ -69,57 +70,48 @@ export async function fetchAllSupplyDenom(): Promise<SupplyDenom[]> {
6970
7071// handleSupply, referenced in project.ts, handles supply information from block
7172export async function handleSupply ( block : CosmosBlock ) : Promise < void > {
72- const supplyDenoms = await fetchAllSupplyDenom ( ) ;
73+ const [ supplyDenoms , totalSupply ] = await Promise . all ( [
74+ fetchAllSupplyDenom ( ) ,
75+ // TODO: (@jorgecuesta) we should update supply handling with proper msg/event once it is implemented on pocket
76+ queryTotalSupply ( block )
77+ ] ) ;
78+
79+ const denominationsSaved = supplyDenoms . map ( supplyDenom => supplyDenom . id )
80+
81+ // here we need to create the denoms that are not saved in the database
82+ if ( totalSupply . some ( supply => ! denominationsSaved . includes ( supply . denom ) ) ) {
83+ const supplyDenomsToSave = totalSupply . filter ( supply => ! denominationsSaved . includes ( supply . denom ) ) . map ( supply => SupplyDenom . create ( { id : supply . denom } ) ) ;
84+
85+ await store . bulkCreate (
86+ "SupplyDenom" ,
87+ supplyDenomsToSave
88+ ) ;
89+
90+ supplyDenoms . push ( ...supplyDenomsToSave ) ;
91+ }
92+
7393 const supplyIdHeight = block . header . height === 1 ? block . header . height : block . header . height - 1 ;
7494
7595 const blockSuppliesMap : Map < string , BlockSupplyProps > = new Map ( ) ;
7696
77- if ( supplyDenoms . length === 0 ) {
78- // if this happens is because we may start in a higher block than genesis.
79- // note: we may need to evaluate genesis in more than just block == genesis.block
80- // like, for example, check if we already have a genesis parsed.
81- const supplyDenom = SupplyDenom . create ( { id : "upokt" } ) ;
82- await supplyDenom . save ( ) ;
83- supplyDenoms . push ( supplyDenom ) ;
84- }
97+ for ( const supplyDenom of supplyDenoms ) {
98+ const blockSupply = await BlockSupply . get (
99+ getSupplyId ( supplyDenom . id , supplyIdHeight ) ,
100+ ) ;
85101
86- if ( block . header . height > 1 ) {
87- // on any block after genesis, we need to look up for the previous BlockSupply to copy the supply id of the
88- // right one; then the claim/proof settlement or ibc txs will update to the right supply id if a new one
89- // is created for this denom@block
90- for ( const supplyDenom of supplyDenoms ) {
91- const blockSupply = await BlockSupply . get (
92- getSupplyId ( supplyDenom . id , supplyIdHeight ) ,
93- ) ;
94-
95- const blockSupplyId = getSupplyId ( supplyDenom . id , block . header . height ) ;
96-
97- // normally this should not be null,
98- // but if you start syncing from a greater height than genesis, probably will not exist
99- const supplyId = blockSupply ? blockSupply . supplyId : blockSupplyId ;
100-
101- const blockSupplyProps = {
102- id : blockSupplyId ,
103- blockId : getBlockId ( block ) ,
104- supplyId,
105- } ;
106- blockSuppliesMap . set ( blockSupplyId , blockSupplyProps ) ;
107- }
108- } else {
109- // create a base record for each supply denomination because is the first block.
110- supplyDenoms . forEach ( ( supplyDenom ) => {
111- const blockSupplyId = getSupplyId ( supplyDenom . id , block . header . height ) ;
112- const blockSupplyProps = {
113- id : blockSupplyId ,
114- blockId : getBlockId ( block ) ,
115- supplyId : getSupplyId ( supplyDenom . id , supplyIdHeight ) ,
116- } ;
117- blockSuppliesMap . set ( blockSupplyId , blockSupplyProps ) ;
118- } ) ;
119- }
102+ const blockSupplyId = getSupplyId ( supplyDenom . id , block . header . height ) ;
120103
121- // TODO: (@jorgecuesta) we should update supply handling with proper msg/event once it is implemented on pocket
122- const totalSupply = await queryTotalSupply ( ) ;
104+ // normally this should not be null,
105+ // but if you start syncing from a greater height than genesis, probably will not exist
106+ const supplyId = blockSupply ? blockSupply . supplyId : blockSupplyId ;
107+
108+ const blockSupplyProps = {
109+ id : blockSupplyId ,
110+ blockId : getBlockId ( block ) ,
111+ supplyId,
112+ } ;
113+ blockSuppliesMap . set ( blockSupplyId , blockSupplyProps ) ;
114+ }
123115
124116 if ( totalSupply . length === 0 ) {
125117 throw new Error ( `[handleSupply]: query.totalSupply returned 0 records, block.header.height=${ block . header . height } ` ) ;
0 commit comments