Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/api/bigDipperApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLClient } from "../helpers/graphql";
import { Account } from "../types/bigDipper";
import { Coin, ValidatorAggregateCountResponse } from "../types/node";
import { Coin, Delegation, ValidatorAggregateCountResponse, ValidatorDetailResponse } from "../types/node";

export class BigDipperApi {
constructor(public readonly graphql_client: GraphQLClient) {
Expand Down Expand Up @@ -78,4 +78,40 @@ export class BigDipperApi {

return resp.validator[0].delegations_aggregate.aggregate.count;
}

get_total_delegator_count = async (): Promise<Number> => {
const query = `query ValidatorDetails {
validator {
validatorStatuses: validator_statuses(order_by: {height: desc}, limit: 1) {
jailed
}
delegations {
delegatorAddress: delegator_address
}
}
}`

const resp = await this.graphql_client.query<ValidatorDetailResponse>(query);
const set = new Set();
resp.validator.forEach((obj, i) => {
if (!obj.validatorStatuses[0]?.jailed) {
obj.delegations.forEach(delegation => {
set.add(delegation.delegatorAddress)
})
}
})

return set.size
}

get_total_staked_coins = async (): Promise<string> => {
let query = `query StakingInfo{
staking_pool {
bonded_tokens
}
}`

const resp = await this.graphql_client.query<{ staking_pool: [{ "bonded_tokens": string }] }>(query);
return resp.staking_pool[0].bonded_tokens;
}
}
11 changes: 11 additions & 0 deletions src/handlers/totalDelegators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Request } from "itty-router";
import { BigDipperApi } from "../api/bigDipperApi";
import { GraphQLClient } from "../helpers/graphql";

export async function handler(request: Request): Promise<Response> {
let gql_client = new GraphQLClient(GRAPHQL_API);
let bd_api = new BigDipperApi(gql_client);

const delegators = await bd_api.get_total_delegator_count();
return new Response(JSON.stringify(delegators));
}
12 changes: 12 additions & 0 deletions src/handlers/totalStakedCoins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request } from "itty-router";
import { BigDipperApi } from "../api/bigDipperApi";
import { ncheq_to_cheq_fixed } from "../helpers/currency";
import { GraphQLClient } from "../helpers/graphql";

export async function handler(request: Request): Promise<Response> {
let gql_client = new GraphQLClient(GRAPHQL_API);
let bd_api = new BigDipperApi(gql_client);

let total_staked_coins = await bd_api.get_total_staked_coins();
return new Response(ncheq_to_cheq_fixed(Number(total_staked_coins)));
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { handler as liquidBalanceHandler } from "./handlers/liquidBalance";
import { handler as vestingBalanceHandler } from "./handlers/vestingBalance";
import { handler as vestedBalanceHandler } from "./handlers/vestedBalance";
import { handler as delegatorCount } from './handlers/delegatorCount';
import { handler as totalDelegators } from './handlers/totalDelegators';
import { handler as totalStakedCoins } from "./handlers/totalStakedCoins";

addEventListener('fetch', (event: FetchEvent) => {
const router = Router<Request, IHTTPMethods>()
Expand All @@ -18,12 +20,13 @@ function registerRoutes(router: Router) {
router.get('/', totalSupplyHandler);
router.get('/supply/total', totalSupplyHandler);
router.get('/supply/circulating', circulatingSupplyHandler);
router.get('/staking/delegators/total', totalDelegators);
router.get('/balances/total/:address', totalBalanceHandler);
router.get('/balances/liquid/:address', liquidBalanceHandler);
router.get('/balances/vesting/:address', vestingBalanceHandler);
router.get('/balances/vested/:address', vestedBalanceHandler);
router.get('/staking/delegators/:validator_address', delegatorCount);

router.get('/supply/staked', totalStakedCoins);

// 404 for all other requests
router.all('*', () => new Response('Not Found.', { status: 404 }))
Expand Down
9 changes: 9 additions & 0 deletions src/types/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ export interface ValidatorAggregateCountResponse {
}
]
}

export interface ValidatorDetailResponse {
validator: [
{
validatorStatuses: [{ jailed: boolean }],
delegations: [{ delegatorAddress: string }]
}
]
}