|
| 1 | +import type { |
| 2 | + Coin, |
| 3 | + CosmosBlock, |
| 4 | +} from "@subql/types-cosmos"; |
| 5 | +import type { QueryTotalSupplyResponse } from "cosmjs-types/cosmos/bank/v1beta1/query"; |
| 6 | +import { |
| 7 | + BlockSupply, |
| 8 | + Supply, |
| 9 | +} from "../../types"; |
| 10 | +import { stringify } from "../utils"; |
| 11 | + |
| 12 | +export const getSupplyId = function(denom: string, height: number): string { |
| 13 | + return `${denom}@${height}`; |
| 14 | +}; |
| 15 | + |
| 16 | +export const getSupplyRecord = function(supply: Coin, block: CosmosBlock): Supply { |
| 17 | + return Supply.create({ |
| 18 | + id: getSupplyId(supply.denom, block.header.height), |
| 19 | + denom: supply.denom, |
| 20 | + amount: BigInt(supply.amount), |
| 21 | + }); |
| 22 | +}; |
| 23 | + |
| 24 | +export async function queryTotalSupply(): Promise<Coin[]> { |
| 25 | + const finalSupply: Coin[] = []; |
| 26 | + let paginationKey: Uint8Array | undefined; |
| 27 | + |
| 28 | + try { |
| 29 | + // Here we force the use of a private property, breaking typescript limitation, due to the need of call a total supply |
| 30 | + // rpc query of cosmosjs that is not exposed on the implemented client by SubQuery team. |
| 31 | + // To avoid this, we need to move to implement our own rpc client and also use `unsafe` parameter which I prefer to avoid. |
| 32 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 33 | + // @ts-ignore |
| 34 | + const queryClient = api.forceGetQueryClient(); |
| 35 | + |
| 36 | + // Initial call to get the first set of results |
| 37 | + const initialResponse: QueryTotalSupplyResponse = await queryClient.bank.totalSupply() as unknown as QueryTotalSupplyResponse; |
| 38 | + logger.debug(`[handleTotalSupply]: initialResponse=${stringify(initialResponse, undefined, 2)}`); |
| 39 | + finalSupply.push(...initialResponse.supply); |
| 40 | + paginationKey = initialResponse.pagination?.nextKey; |
| 41 | + |
| 42 | + // Continue fetching if there is a nextKey |
| 43 | + while (paginationKey && paginationKey.length > 0) { |
| 44 | + logger.debug(`[handleTotalSupply]: loading more supply pages pagination.nextKey=${JSON.stringify(paginationKey, undefined, 2)}`); |
| 45 | + const response = await queryClient.bank.totalSupply(paginationKey); |
| 46 | + finalSupply.push(...response.supply); |
| 47 | + paginationKey = response.pagination?.nextKey; |
| 48 | + } |
| 49 | + logger.debug(`[handleTotalSupply]: all_total_supply=${JSON.stringify(finalSupply, undefined, 2)}`); |
| 50 | + } catch (error) { |
| 51 | + logger.error(`[handleTotalSupply] errored: ${error}`); |
| 52 | + } |
| 53 | + |
| 54 | + return finalSupply; |
| 55 | +} |
| 56 | + |
| 57 | +export async function _handleSupply(block: CosmosBlock): Promise<void> { |
| 58 | + const totalSupply = await queryTotalSupply(); |
| 59 | + if (totalSupply.length === 0) { |
| 60 | + logger.warn(`[_handleSupply]: no total supply found`); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + for (const supply of totalSupply) { |
| 65 | + // get the current blockSupply create on block handler to been able to access the assigned previous supply id |
| 66 | + // that will allow us to compare the amount and create a new on if needed. |
| 67 | + const blockSupplyId = getSupplyId(supply.denom, block.header.height); |
| 68 | + const latestBlockSupply = await BlockSupply.get(blockSupplyId); |
| 69 | + if (!latestBlockSupply) { |
| 70 | + logger.warn(`[_handleSupply]: no BlockSupply found id=${blockSupplyId}`); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + const latestDenomSupply = await Supply.get(latestBlockSupply.supplyId); |
| 75 | + if (!latestDenomSupply) { |
| 76 | + logger.warn(`[_handleSupply]: no total supply found id=${latestBlockSupply.supplyId}`); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + if (latestDenomSupply.amount.toString() !== supply.amount) { |
| 81 | + const newSupply = getSupplyRecord(supply, block); |
| 82 | + await newSupply.save(); |
| 83 | + latestBlockSupply.supplyId = newSupply.id; |
| 84 | + await latestBlockSupply.save(); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments