Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
3262817
Switch upstream GraphQL API endpoint
ankurdotb Sep 13, 2022
78071a0
ci: Update upstream API endpoints
ankurdotb Sep 14, 2022
45ddb06
Simplified get accounts query so that it doesn't break
drgomesp Sep 16, 2022
b627416
Fixed bad query
drgomesp Sep 19, 2022
27f81af
Improved error handling for graphql client
drgomesp Sep 21, 2022
158e10b
wip
drgomesp Sep 22, 2022
59e1bf5
Fixed circulating supply endpoint (still missing rewards and unbonding)
drgomesp Sep 22, 2022
b0b8081
Switch upstream GraphQL API endpoint
ankurdotb Sep 13, 2022
5dbe3e2
ci: Update upstream API endpoints
ankurdotb Sep 14, 2022
078d623
Fixed bad query
drgomesp Sep 19, 2022
6832756
Created new endpoint to update single account balance on KV for later…
drgomesp Sep 23, 2022
b191575
Added total balance as field on account balance cache
drgomesp Sep 23, 2022
88da7d3
Update bigDipperApi.ts
ankurdotb Sep 23, 2022
a50df0d
Revert "Update bigDipperApi.ts"
ankurdotb Sep 23, 2022
ed2491f
Refactoring for balance fetching from different sources
drgomesp Sep 26, 2022
935a679
Fixed account balance helper
drgomesp Sep 26, 2022
033132b
Started with new graphql query
drgomesp Sep 27, 2022
18720bb
Fixed update balance per account endpoint (temporary)
drgomesp Sep 27, 2022
39583e4
Fixed balance query checks
drgomesp Sep 27, 2022
2ec827f
Fixed circulating supply endpoint to work with new query
drgomesp Sep 27, 2022
9556b61
Fixed update all balances endpoint to feed KV (temporary endpoint)
drgomesp Sep 27, 2022
8cd17ca
Better error handling so that API doesn't crash
drgomesp Sep 27, 2022
ac8391e
Fixed all balances updater
drgomesp Sep 27, 2022
58ed4ea
Only updating missing balances (temp)
drgomesp Sep 27, 2022
871602e
Reactivating cron
drgomesp Sep 27, 2022
f5a5bf3
Removed single account balance updater temp endpoint
drgomesp Sep 28, 2022
9027ce6
Improved error logging for graphgl (workers requires a Map for loggin…
drgomesp Sep 28, 2022
97d6523
Removing duplicates from KV while procecssing
drgomesp Sep 28, 2022
c415557
Removed temporary account removal from KV
drgomesp Sep 28, 2022
d36bc4f
Better logging
drgomesp Sep 28, 2022
272d2cd
Fixed endpoint
drgomesp Sep 28, 2022
7fd96ca
Fixed endpoint
drgomesp Sep 28, 2022
77958ad
Added proper list of addresses to import (temp)
drgomesp Sep 30, 2022
4598741
Implementing requested changes from PR review
drgomesp Sep 30, 2022
1aa7d8b
Made group be random on cron
drgomesp Sep 30, 2022
2374092
Simplified record on KV
drgomesp Sep 30, 2022
a2d2395
Better naming and place for cron helper
drgomesp Sep 30, 2022
9403d2f
Better error handling
drgomesp Sep 30, 2022
a48af48
Removed unused code
drgomesp Sep 30, 2022
4b79e2f
Better yet error handling
drgomesp Sep 30, 2022
30d1978
Fixed error issue returning bad response
drgomesp Sep 30, 2022
a1b5229
Clean-up of addresses
drgomesp Oct 2, 2022
0e821e7
Reprocessing addresses
drgomesp Oct 2, 2022
84fde0b
Removed static account addresses
drgomesp Oct 2, 2022
ec730d9
Removed unecessary endpoint for processing balance groups
drgomesp Oct 3, 2022
f2d8a1c
Added check for null account on balance endpoint
drgomesp Oct 3, 2022
b83bd32
Added updated at to KV record
drgomesp Oct 3, 2022
ed201d1
Squashed commit of the following:
ankurdotb Oct 12, 2022
519f9d2
Update wrangler.toml
ankurdotb Oct 12, 2022
64c3ca7
Fixed broken trigger
ankurdotb Oct 12, 2022
a13332c
fix total delegations count per validatior
benyam7 Oct 13, 2022
3113a4e
fix totat delegators count
benyam7 Oct 13, 2022
ff56494
Merge branch 'fix-broken-api' of github.com:cheqd/data-api into fix-b…
benyam7 Oct 13, 2022
3c8b55a
fix total staked coins
benyam7 Oct 13, 2022
7a67d4d
Merge branch 'main' into fix-broken-api
benyam7 Oct 14, 2022
b66692c
Update wrangler.toml
ankurdotb Oct 14, 2022
61766fb
fix: total balance
benyam7 Oct 14, 2022
0640c16
Merge branch 'fix-broken-api' of github.com:cheqd/data-api into fix-b…
benyam7 Oct 14, 2022
86fb48f
Fix endpoint
ankurdotb Oct 14, 2022
489a845
Remove group cron
ankurdotb Oct 14, 2022
11b424b
Delete balanceGroup.ts
ankurdotb Oct 14, 2022
4fb2869
Switch to a better example for vesting
ankurdotb Oct 14, 2022
b9773c0
Update arbitrage endpoints
ankurdotb Oct 14, 2022
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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The only way to derive this figure from the Cosmos SDK APIs is by iterating over

#### Endpoint

[`data-api.cheqd.io/staking/balances/<validator-address>`](https://data-api.cheqd.io/staking/balances/cheqdvaloper1lg0vwuu888hu4arnt9egtqrm2662kcrtf2unrs)
[`data-api.cheqd.io/staking/delegators/<validator-address>`](https://data-api.cheqd.io/staking/delegators/cheqdvaloper1lg0vwuu888hu4arnt9egtqrm2662kcrtf2unrs)

#### Response

Expand All @@ -97,7 +97,7 @@ There is no simple Cosmos SDK API to fetch the number of delegators for a given

#### Endpoint

[`data-api.cheqd.io/balances/vesting/<address>`](https://data-api.cheqd.io/balances/vesting/cheqd1qs0nhyk868c246defezhz5eymlt0dmajna2csg)
[`data-api.cheqd.io/balances/vesting/<address>`](https://data-api.cheqd.io/balances/vesting/cheqd17wrwqxsfwy4rqlltjhj6jxtz68tvxm0ykge5dr)

#### Response

Expand All @@ -111,7 +111,7 @@ There is no Cosmos SDK API that returns balances that are yet to be vested for [

#### Endpoint

[`data-api.cheqd.io/balances/vested/<address>`](https://data-api.cheqd.io/balances/vesting/cheqd1qs0nhyk868c246defezhz5eymlt0dmajna2csg)
[`data-api.cheqd.io/balances/vested/<address>`](https://data-api.cheqd.io/balances/vesting/cheqd17wrwqxsfwy4rqlltjhj6jxtz68tvxm0ykge5dr)

#### Response

Expand All @@ -125,7 +125,7 @@ There is no Cosmos SDK API that returns balances that are already vested for [co

#### Endpoint

[`data-api.cheqd.io/balances/liquid/<address>`](https://data-api.cheqd.io/balances/vesting/cheqd1qs0nhyk868c246defezhz5eymlt0dmajna2csg)
[`data-api.cheqd.io/balances/liquid/<address>`](https://data-api.cheqd.io/balances/vesting/cheqd17wrwqxsfwy4rqlltjhj6jxtz68tvxm0ykge5dr)

#### Response

Expand All @@ -143,7 +143,7 @@ Tokens in [continuous or delayed vesting accounts](https://docs.cosmos.network/v

#### Endpoint

[`data-api.cheqd.io/balances/total/<address>`](https://data-api.cheqd.io/balances/total/cheqd1qs0nhyk868c246defezhz5eymlt0dmajna2csg)
[`data-api.cheqd.io/balances/total/<address>`](https://data-api.cheqd.io/balances/total/cheqd17wrwqxsfwy4rqlltjhj6jxtz68tvxm0ykge5dr)

#### Response

Expand All @@ -157,7 +157,8 @@ The standard Cosmos SDK REST API for account balances returns JSON with the acco

#### Endpoint

[`data-api.cheqd.io/arbitrage/`](https://data-api.cheqd.io/arbitrage)
- Results filtered by threshold value: [`data-api.cheqd.io/arbitrage`](https://data-api.cheqd.io/arbitrage)
- Unfiltered results: [`data-api.cheqd.io/arbitrage/all`](https://data-api.cheqd.io/arbitrage/all)

#### Response

Expand Down
258 changes: 144 additions & 114 deletions src/api/bigDipperApi.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,147 @@
import { GraphQLClient } from "../helpers/graphql";
import { Account } from "../types/bigDipper";
import { Coin, Delegation, ValidatorAggregateCountResponse, ValidatorDetailResponse } from "../types/node";
import { GraphQLClient } from '../helpers/graphql';
import {
ActiveValidatorsResponse,
Coin,
TotalStakedCoinsResponse,
ValidatorDelegationsCountResponse,
} from '../types/node';
import { Account } from '../types/bigDipper';
import { NodeApi } from './nodeApi';

export class BigDipperApi {
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;
}

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;
}
constructor(public readonly graphql_client: GraphQLClient) {}

async get_account(address: string): Promise<Account | null> {
let query = `query Account($address: String!, $where: vesting_account_bool_exp) {
accountBalance: action_account_balance(address: $address) {
coins
}
delegationBalance: action_delegation_total(address: $address) {
coins
}
unbondingBalance: action_unbonding_delegation_total(address: $address) {
coins
}
redelegationBalance: action_redelegation(address: $address) {
redelegations
}
rewardBalance: action_delegation_reward(address: $address) {
coins
}
vesting_account(where: $where) {
id
type
original_vesting
start_time
end_time
}
}`;

let params = {
address: address,
where: {
address: {
_eq: address,
},
},
};

try {
let resp = await this.graphql_client.query<{
data: any;
errors: any;
}>(query, params);

return resp.data as Account;
} catch (e: any) {
console.error(new Map(e));
return null;
}
}

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<{
data: { supply: { coins: Coin[] }[] };
}>(query);

return resp.data.supply[0].coins;
}

get_delegator_count_for_validator = async (
address: string
): Promise<Number> => {
let query = `query ValidatorDelegations($address: String!, $pagination: Boolean! = true) {
delegations: action_validator_delegations(address: $address, count_total: $pagination) {
pagination
}
}
`;

const params = {
address: address,
};

const resp = await this.graphql_client.query<{
data: ValidatorDelegationsCountResponse;
}>(query, params);

return resp.data.delegations.pagination.total;
};

get_total_delegator_count = async (): Promise<Number> => {
const queryActiveValidators = `query ActiveValidators {
validator_info(distinct_on: operator_address, where: {validator: {validator_statuses: {jailed: {_eq: false}}}}) {
operator_address
}
}`;

const data = [];
const uniques = new Set();

const activeValidator = await this.graphql_client.query<{
data: ActiveValidatorsResponse;
}>(queryActiveValidators);

for (let i = 0; i < activeValidator.data.validator_info.length; i++) {
const operator_address =
activeValidator.data.validator_info[i].operator_address;
const resp = await new NodeApi(
REST_API
).staking_get_delegators_per_validator(operator_address);
data.push({
validator: operator_address,
delegators: resp.delegation_responses,
});
}

for (let i = 0; i < data.length; i++) {
const delegators = data[i].delegators;
for (let j = 0; j < delegators.length; j++) {
uniques.add(
`${delegators[j].delegation.delegator_address}${delegators[j].delegation.validator_address}`
);
}
}
return uniques.size;
};

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

const resp = await this.graphql_client.query<{
data: TotalStakedCoinsResponse;
}>(query);
return resp.data.staking_pool[0].bonded_tokens;
};
}
Loading