diff --git a/src/api/bigDipperApi.ts b/src/api/bigDipperApi.ts index 6c67c2ec..7b980476 100644 --- a/src/api/bigDipperApi.ts +++ b/src/api/bigDipperApi.ts @@ -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) { @@ -78,4 +78,40 @@ export class BigDipperApi { return resp.validator[0].delegations_aggregate.aggregate.count; } + + get_total_delegator_count = async (): Promise => { + 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(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 => { + 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; + } } diff --git a/src/handlers/totalDelegators.ts b/src/handlers/totalDelegators.ts new file mode 100644 index 00000000..eb14f5e7 --- /dev/null +++ b/src/handlers/totalDelegators.ts @@ -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 { + 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)); +} diff --git a/src/handlers/totalStakedCoins.ts b/src/handlers/totalStakedCoins.ts new file mode 100644 index 00000000..ccdcd109 --- /dev/null +++ b/src/handlers/totalStakedCoins.ts @@ -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 { + 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))); +} diff --git a/src/index.ts b/src/index.ts index 851f9098..4d265e18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() @@ -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 })) diff --git a/src/types/node.ts b/src/types/node.ts index e07193ec..385f4f3e 100644 --- a/src/types/node.ts +++ b/src/types/node.ts @@ -47,3 +47,12 @@ export interface ValidatorAggregateCountResponse { } ] } + +export interface ValidatorDetailResponse { + validator: [ + { + validatorStatuses: [{ jailed: boolean }], + delegations: [{ delegatorAddress: string }] + } + ] +}