Skip to content

Commit af4de6b

Browse files
committed
* Added the following fields to block entity:
* `stakedValidators` * `stakedValidatorsTokens` * `unstakingValidators` * `unstakingValidatorsTokens`
1 parent ca42208 commit af4de6b

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
StakeStatus,
88
Supplier,
99
SupplierServiceConfig,
10-
Transaction,
10+
Transaction, Validator,
1111
} from "../../types";
1212
import { fetchPaginatedRecords } from "../utils/db";
1313

@@ -105,6 +105,19 @@ export async function fetchAllSupplierByUnstakingEndBlockId(blockId: bigint): Pr
105105
});
106106
}
107107

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

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

src/mappings/poktroll/reports.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
fetchAllSupplierByStatus,
2121
fetchAllSupplierByUnstakingEndBlockId,
2222
fetchAllSupplierServiceConfigBySupplier,
23-
fetchAllTransactions,
23+
fetchAllTransactions, fetchAllValidatorByStatus,
2424
} from "./pagination";
2525

2626
export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
@@ -44,6 +44,8 @@ export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
4444
{ unstakedApps, unstakedTokensByApp },
4545
{ stakedGateways, stakedTokensByGateway },
4646
{ unstakedGateways, unstakedTokensByGateway },
47+
{ stakedTokensByValidators, stakedValidators },
48+
{ unstakingTokensByValidators, unstakingValidators }
4749
] = await Promise.all([
4850
getRelaysData(blockHeight),
4951
getTransactionsData(blockHeight),
@@ -56,6 +58,8 @@ export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
5658
getUnstakedAppsData(blockHeight),
5759
getStakedGatewaysData(),
5860
getUnstakedGatewaysData(blockHeight),
61+
getStakedValidatorsData(),
62+
getUnstakingValidatorsData(),
5963
]);
6064

6165
blockEntity.totalComputedUnits = computedUnits;
@@ -80,6 +84,10 @@ export async function handleAddBlockReports(block: CosmosBlock): Promise<void> {
8084
blockEntity.stakedGatewaysTokens = stakedTokensByGateway;
8185
blockEntity.unstakedGateways = unstakedGateways;
8286
blockEntity.unstakedGatewaysTokens = unstakedTokensByGateway;
87+
blockEntity.stakedValidators = stakedValidators;
88+
blockEntity.stakedValidatorsTokens = stakedTokensByValidators;
89+
blockEntity.unstakingValidators = unstakingValidators;
90+
blockEntity.unstakingValidatorsTokens = unstakingTokensByValidators;
8391

8492
await Promise.all([
8593
blockEntity.save(),
@@ -230,6 +238,26 @@ async function getUnstakingSuppliersData() {
230238
};
231239
}
232240

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

0 commit comments

Comments
 (0)