Skip to content

Commit 3eeb36c

Browse files
authored
Add validators data to block entity (#41)
Added the following fields to block entity: * `stakedValidators` * `stakedValidatorsTokens` * `unstakingValidators` * `unstakingValidatorsTokens`
1 parent ca42208 commit 3eeb36c

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ type Block @entity {
254254
stakedGatewaysTokens: BigInt
255255
unstakedGateways: Int
256256
unstakedGatewaysTokens: BigInt
257+
stakedValidators: Int
258+
stakedValidatorsTokens: BigInt
259+
unstakingValidators: Int
260+
unstakingValidatorsTokens: BigInt
257261
}
258262

259263
# This handle all the information that is coming with the block but is not often used

src/mappings/poktroll/pagination.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Supplier,
99
SupplierServiceConfig,
1010
Transaction,
11+
Validator,
1112
} from "../../types";
1213
import { fetchPaginatedRecords } from "../utils/db";
1314

@@ -105,6 +106,19 @@ export async function fetchAllSupplierByUnstakingEndBlockId(blockId: bigint): Pr
105106
});
106107
}
107108

109+
// validator
110+
111+
export async function fetchAllValidatorByStatus(status: StakeStatus): Promise<Validator[]> {
112+
return fetchPaginatedRecords({
113+
fetchFn: (options) => Validator.getByStakeStatus(status, options),
114+
initialOptions: {
115+
// add order and direction to speedup if there is a way
116+
// orderBy: 'id', // Order results by ID
117+
// orderDirection: 'ASC', // Ascending order
118+
},
119+
});
120+
}
121+
108122
// gateway
109123

110124
export async function fetchAllGatewayByStatus(status: StakeStatus): Promise<Gateway[]> {

src/mappings/poktroll/reports.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
fetchAllSupplierByUnstakingEndBlockId,
2222
fetchAllSupplierServiceConfigBySupplier,
2323
fetchAllTransactions,
24+
fetchAllValidatorByStatus,
2425
} from "./pagination";
2526

2627
export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
@@ -44,6 +45,8 @@ export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
4445
{ unstakedApps, unstakedTokensByApp },
4546
{ stakedGateways, stakedTokensByGateway },
4647
{ unstakedGateways, unstakedTokensByGateway },
48+
{ stakedTokensByValidators, stakedValidators },
49+
{ unstakingTokensByValidators, unstakingValidators }
4750
] = await Promise.all([
4851
getRelaysData(blockHeight),
4952
getTransactionsData(blockHeight),
@@ -56,6 +59,8 @@ export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
5659
getUnstakedAppsData(blockHeight),
5760
getStakedGatewaysData(),
5861
getUnstakedGatewaysData(blockHeight),
62+
getStakedValidatorsData(),
63+
getUnstakingValidatorsData(),
5964
]);
6065

6166
blockEntity.totalComputedUnits = computedUnits;
@@ -80,6 +85,10 @@ export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
8085
blockEntity.stakedGatewaysTokens = stakedTokensByGateway;
8186
blockEntity.unstakedGateways = unstakedGateways;
8287
blockEntity.unstakedGatewaysTokens = unstakedTokensByGateway;
88+
blockEntity.stakedValidators = stakedValidators;
89+
blockEntity.stakedValidatorsTokens = stakedTokensByValidators;
90+
blockEntity.unstakingValidators = unstakingValidators;
91+
blockEntity.unstakingValidatorsTokens = unstakingTokensByValidators;
8392

8493
await Promise.all([
8594
blockEntity.save(),
@@ -230,6 +239,26 @@ async function getUnstakingSuppliersData() {
230239
};
231240
}
232241

242+
async function getStakedValidatorsData() {
243+
const stakedValidators = await fetchAllValidatorByStatus(StakeStatus.Staked);
244+
const stakedTokensByValidators = stakedValidators.reduce((acc, validator) => acc + BigInt(validator.stakeAmount), BigInt(0));
245+
246+
return {
247+
stakedValidators: stakedValidators.length,
248+
stakedTokensByValidators: stakedTokensByValidators,
249+
};
250+
}
251+
252+
async function getUnstakingValidatorsData() {
253+
const unstakingValidators = await fetchAllValidatorByStatus(StakeStatus.Unstaking);
254+
const unstakingTokensByValidators = unstakingValidators.reduce((acc, validator) => acc + BigInt(validator.stakeAmount), BigInt(0));
255+
256+
return {
257+
unstakingValidators: unstakingValidators.length,
258+
unstakingTokensByValidators,
259+
};
260+
}
261+
233262
async function getTook(block: CosmosBlock) {
234263
if (block.header.height === 1) {
235264
return 0;

0 commit comments

Comments
 (0)