Skip to content

Commit d6c72fa

Browse files
committed
Add Supply entity and update supply handling logic
- Introduced `supplies` field in `Block` type to reference `Supply` entities. - Added new fields `denom` and `block` to `Supply` type. - Replaced `getSupply` with `queryTotalSupply` and implement `getSupplyRecord`. - Modified `_handleSupply` to create supply records with `getSupplyRecord` and avoid duplicates.
1 parent 80f41c6 commit d6c72fa

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

schema.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Block @entity {
2323
messages: [Message] @derivedFrom(field: "block")
2424
events: [Event] @derivedFrom(field: "block")
2525
balancesOfAccountByDenom: [Balance] @derivedFrom(field: "lastUpdatedBlock")
26+
supplies: [Supply] @derivedFrom(field: "block")
2627
}
2728

2829
type Transaction @entity {
@@ -124,5 +125,7 @@ type GenesisFile @entity {
124125
type Supply @entity {
125126
# coin denom
126127
id: ID!
128+
denom: String! @index
127129
amount: BigInt!
130+
block: Block!
128131
}

src/mappings/bank/supply.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import type {
2+
Coin,
3+
CosmosBlock,
4+
} from "@subql/types-cosmos";
15
import type { QueryTotalSupplyResponse } from "cosmjs-types/cosmos/bank/v1beta1/query";
2-
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
6+
import { Supply } from "../../types";
37
import { stringify } from "../utils";
48

5-
export async function getSupply(): Promise<Coin[]> {
9+
export const getSupplyRecord = (supply: Coin, block: CosmosBlock): Supply => Supply.create({
10+
id: `${supply.denom}@${block.block.id}`,
11+
denom: supply.denom,
12+
amount: BigInt(supply.amount),
13+
blockId: block.block.id,
14+
});
15+
16+
export async function queryTotalSupply(): Promise<Coin[]> {
617
let finalSupply: Coin[] = [];
718
let paginationKey: Uint8Array | undefined;
819

src/mappings/primitives.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import {
2020
GenesisFile as GenesisEntity,
2121
Message,
2222
NativeBalanceChange,
23-
Supply,
2423
Transaction,
2524
TxStatus,
2625
} from "../types";
27-
import { getSupply } from "./bank/supply";
26+
import {
27+
getSupplyRecord,
28+
queryTotalSupply,
29+
} from "./bank/supply";
2830
import { PREFIX } from "./constants";
2931
import type { Genesis } from "./types/genesis";
3032
import {
@@ -57,12 +59,6 @@ export async function handleGenesis(block: CosmosBlock): Promise<void> {
5759
chainId: block.block.header.chainId,
5860
};
5961
})),
60-
store.bulkCreate("Supply", genesis.app_state.bank.supply.map(supply => {
61-
return {
62-
id: supply.denom,
63-
amount: BigInt(supply.amount),
64-
};
65-
})),
6662
Event.create({
6763
id: "genesis",
6864
type: "genesis",
@@ -173,7 +169,7 @@ async function _handleBlock(block: CosmosBlock): Promise<void> {
173169

174170
await blockEntity.save();
175171

176-
await _handleSupply();
172+
await _handleSupply(block);
177173
}
178174

179175
async function _handleTransaction(tx: CosmosTransaction): Promise<void> {
@@ -285,29 +281,15 @@ async function _handleEvent(event: CosmosEvent): Promise<void> {
285281
}
286282
}
287283

288-
async function _handleSupply(): Promise<void> {
289-
// we need to check if the amount is different to avoid duplicate records because subquery does not check that
290-
const totalSupply = await getSupply();
291-
if (totalSupply.length > 0) {
292-
logger.info(`[handleSupply]: indexing total supply for ${totalSupply.length} coins`);
293-
for (const supply of totalSupply) {
294-
logger.debug(`[handleSupply] (totalSupply.coin: total supply of ${supply.denom}`);
295-
// we need to check if the amount is different to avoid duplicate records because subquery does not check that
296-
const s = await Supply.get(supply.denom);
297-
if (!s) {
298-
await Supply.create({
299-
id: supply.denom,
300-
amount: BigInt(supply.amount),
301-
}).save();
302-
continue;
303-
} else if (s.amount.toString() === supply.amount) {
304-
continue;
305-
}
306-
307-
s.amount = BigInt(supply.amount);
308-
await s.save();
309-
}
284+
async function _handleSupply(block: CosmosBlock): Promise<void> {
285+
const totalSupply = await queryTotalSupply();
286+
if (totalSupply.length === 0) {
287+
logger.warn(`[handleSupply]: no total supply found`);
288+
return;
310289
}
290+
291+
logger.info(`[handleSupply]: indexing total supply for ${totalSupply.length} coins`);
292+
await store.bulkCreate("Supply", totalSupply.map(supply => getSupplyRecord(supply, block)));
311293
}
312294

313295
// eslint-disable-next-line @typescript-eslint/no-unused-vars

0 commit comments

Comments
 (0)