-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Delegator Count for Validators #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,81 @@ | ||
| import { GraphQLClient } from "../helpers/graphql"; | ||
| import { Account } from "../types/bigDipper"; | ||
| import { Coin } from "../types/node"; | ||
| import { Coin, ValidatorAggregateCountResponse } from "../types/node"; | ||
|
|
||
| export class BigDipperApi { | ||
| constructor(public readonly graphql_client: GraphQLClient) { | ||
| } | ||
|
|
||
| async get_accounts(addresses: string[]): Promise<Account[]> { | ||
| let query = "query Account($addresses: [String], $utc: timestamp) {\n" + | ||
| " account(where: { address: { _in: $addresses } }) {\n" + | ||
| " address\n" + | ||
| " accountBalances: account_balances(limit: 1, order_by: { height: desc }) {\n" + | ||
| " coins\n" + | ||
| " }\n" + | ||
| " delegations {\n" + | ||
| " amount\n" + | ||
| " }\n" + | ||
| " unbonding: unbonding_delegations(\n" + | ||
| " where: { completion_timestamp: { _gt: $utc } }\n" + | ||
| " ) {\n" + | ||
| " amount\n" + | ||
| " }\n" + | ||
| " redelegations(where: { completion_time: { _gt: $utc } }) {\n" + | ||
| " amount\n" + | ||
| " }\n" + | ||
| " delegationRewards: delegation_rewards {\n" + | ||
| " amount\n" + | ||
| " }\n" + | ||
| " }\n" + | ||
| "}\n"; | ||
|
|
||
| let params = { | ||
| utc: new Date(), | ||
| addresses | ||
| } | ||
|
|
||
| let resp = await this.graphql_client.query<{ account: Account[] }>(query, params); | ||
| return resp.account; | ||
| } | ||
|
|
||
| async get_account(address: string): Promise<Account> { | ||
| let accounts = await this.get_accounts([address]); | ||
| return accounts[0]; | ||
| } | ||
|
|
||
| async get_total_supply(): Promise<Coin[]> { | ||
| let query = "query Supply {\n" + | ||
| " supply(order_by: {height:desc} limit: 1) {\n" + | ||
| " coins\n" + | ||
| " height\n" + | ||
| " }\n" + | ||
| "}\n"; | ||
|
|
||
| let resp = await this.graphql_client.query<{ supply: { coins: Coin[] }[] }>(query); | ||
| return resp.supply[0].coins; | ||
| } | ||
| constructor(public readonly graphql_client: GraphQLClient) { | ||
| } | ||
|
|
||
| async get_accounts(addresses: string[]): Promise<Account[]> { | ||
| let query = `query Account($addresses: [String], $utc: timestamp) { | ||
| account(where: { address: { _in: $addresses } }) { | ||
| address | ||
| accountBalances: account_balances(limit: 1, order_by: { height: desc }) { | ||
| coins | ||
| } | ||
| delegations { | ||
| amount | ||
| } | ||
| unbonding: unbonding_delegations( | ||
| where: { completion_timestamp: { _gt: $utc } } | ||
| ) { | ||
| amount | ||
| } | ||
| redelegations(where: { completion_time: { _gt: $utc } }) { | ||
| amount | ||
| } | ||
| delegationRewards: delegation_rewards { | ||
| amount | ||
| } | ||
| } | ||
| }` | ||
|
|
||
| let params = { | ||
| utc: new Date(), | ||
| addresses | ||
| } | ||
|
|
||
| let resp = await this.graphql_client.query<{ account: Account[] }>(query, params); | ||
| return resp.account; | ||
| } | ||
|
|
||
| async get_account(address: string): Promise<Account> { | ||
| let accounts = await this.get_accounts([address]); | ||
| return accounts[0]; | ||
| } | ||
|
|
||
| async get_total_supply(): Promise<Coin[]> { | ||
| let query = `query Supply { | ||
| supply(order_by: {height:desc} limit: 1) { | ||
| coins | ||
| height | ||
| } | ||
| }`; | ||
|
|
||
| let resp = await this.graphql_client.query<{ supply: { coins: Coin[] }[] }>(query); | ||
| return resp.supply[0].coins; | ||
| } | ||
|
|
||
| get_delegator_count_for_validator = async (address: string): Promise<Number> => { | ||
| let query = `query ValidatorDetails($address: String) { | ||
| validator(where: {validator_info: {operator_address: {_eq: $address}}}) { | ||
| delegations_aggregate { | ||
| aggregate { | ||
| count | ||
| } | ||
| } | ||
| } | ||
| }` | ||
|
|
||
| const params = { | ||
| address: address, | ||
| } | ||
|
|
||
| const resp = await this.graphql_client.query<ValidatorAggregateCountResponse>(query, params); | ||
| if (!resp.validator || !resp.validator.length) { | ||
| return 0; | ||
| } | ||
|
|
||
| return resp.validator[0].delegations_aggregate.aggregate.count; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Request } from "itty-router"; | ||
| import { BigDipperApi } from "../api/bigDipperApi"; | ||
| import { GraphQLClient } from "../helpers/graphql"; | ||
|
|
||
| export async function handler(request: Request): Promise<Response> { | ||
| const address = request.params?.['validator_address']; | ||
|
|
||
| if (!address) { | ||
| throw new Error("No address specified or wrong address format."); | ||
| } | ||
|
|
||
| let gql_client = new GraphQLClient(GRAPHQL_API); | ||
| let bd_api = new BigDipperApi(gql_client); | ||
|
|
||
| let delegators = await bd_api.get_delegator_count_for_validator(address); | ||
| return new Response(delegators.toString()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,49 @@ | ||
| export type Account ={ | ||
| '@type': string; | ||
| start_time: number; | ||
| base_vesting_account: { base_account: BaseAccount, original_vesting: Coin[], delegated_free?: Coin[], delegated_vesting?: Coin[], end_time: number }; | ||
| export type Account = { | ||
| '@type': string; | ||
| start_time: number; | ||
| base_vesting_account: { base_account: BaseAccount, original_vesting: Coin[], delegated_free?: Coin[], delegated_vesting?: Coin[], end_time: number }; | ||
| } | ||
|
|
||
| export type BaseAccount = { | ||
| address: string; | ||
| pub_key: PublicKey; | ||
| account_number: string; | ||
| sequence: string; | ||
| address: string; | ||
| pub_key: PublicKey; | ||
| account_number: string; | ||
| sequence: string; | ||
| } | ||
|
|
||
| export type PublicKey = { | ||
| '@type': string; | ||
| key: string; | ||
| '@type': string; | ||
| key: string; | ||
| } | ||
|
|
||
| export class Coin { | ||
| public denom: string; | ||
| public amount: string; | ||
| public denom: string; | ||
| public amount: string; | ||
|
|
||
| constructor(denom: string, amount: string) { | ||
| this.denom = denom; | ||
| this.amount = amount; | ||
| } | ||
| constructor(denom: string, amount: string) { | ||
| this.denom = denom; | ||
| this.amount = amount; | ||
| } | ||
| } | ||
|
|
||
| export class Delegation { | ||
| public amount: Coin; | ||
| public delegatorAddress: string; | ||
|
|
||
| constructor(amount: Coin, delegatorAddress: string) { | ||
| this.delegatorAddress = delegatorAddress; | ||
| this.amount = amount; | ||
| } | ||
| } | ||
|
|
||
| export interface ValidatorAggregateCountResponse { | ||
| validator: [ | ||
| { | ||
| delegations_aggregate: { | ||
| aggregate: { | ||
| count: number | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.