-
Notifications
You must be signed in to change notification settings - Fork 2
fix: Fix circulating supply API endpoint [DEV-1821] #35
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
Show all changes
104 commits
Select commit
Hold shift + click to select a range
3262817
Switch upstream GraphQL API endpoint
ankurdotb 78071a0
ci: Update upstream API endpoints
ankurdotb 45ddb06
Simplified get accounts query so that it doesn't break
drgomesp b627416
Fixed bad query
drgomesp 27f81af
Improved error handling for graphql client
drgomesp 158e10b
wip
drgomesp 59e1bf5
Fixed circulating supply endpoint (still missing rewards and unbonding)
drgomesp b0b8081
Switch upstream GraphQL API endpoint
ankurdotb 5dbe3e2
ci: Update upstream API endpoints
ankurdotb 078d623
Fixed bad query
drgomesp 6832756
Created new endpoint to update single account balance on KV for later…
drgomesp b191575
Added total balance as field on account balance cache
drgomesp 88da7d3
Update bigDipperApi.ts
ankurdotb a50df0d
Revert "Update bigDipperApi.ts"
ankurdotb ed2491f
Refactoring for balance fetching from different sources
drgomesp 935a679
Fixed account balance helper
drgomesp 033132b
Started with new graphql query
drgomesp 18720bb
Fixed update balance per account endpoint (temporary)
drgomesp 39583e4
Fixed balance query checks
drgomesp 2ec827f
Fixed circulating supply endpoint to work with new query
drgomesp 9556b61
Fixed update all balances endpoint to feed KV (temporary endpoint)
drgomesp 8cd17ca
Better error handling so that API doesn't crash
drgomesp ac8391e
Fixed all balances updater
drgomesp 58ed4ea
Only updating missing balances (temp)
drgomesp 871602e
Reactivating cron
drgomesp f5a5bf3
Removed single account balance updater temp endpoint
drgomesp 9027ce6
Improved error logging for graphgl (workers requires a Map for loggin…
drgomesp 97d6523
Removing duplicates from KV while procecssing
drgomesp c415557
Removed temporary account removal from KV
drgomesp d36bc4f
Better logging
drgomesp 272d2cd
Fixed endpoint
drgomesp 7fd96ca
Fixed endpoint
drgomesp 77958ad
Added proper list of addresses to import (temp)
drgomesp 4598741
Implementing requested changes from PR review
drgomesp 1aa7d8b
Made group be random on cron
drgomesp 2374092
Simplified record on KV
drgomesp a2d2395
Better naming and place for cron helper
drgomesp 9403d2f
Better error handling
drgomesp a48af48
Removed unused code
drgomesp 4b79e2f
Better yet error handling
drgomesp 30d1978
Fixed error issue returning bad response
drgomesp a1b5229
Clean-up of addresses
drgomesp 0e821e7
Reprocessing addresses
drgomesp 84fde0b
Removed static account addresses
drgomesp ec730d9
Removed unecessary endpoint for processing balance groups
drgomesp f2d8a1c
Added check for null account on balance endpoint
drgomesp b83bd32
Added updated at to KV record
drgomesp ed201d1
Squashed commit of the following:
ankurdotb 519f9d2
Update wrangler.toml
ankurdotb 64c3ca7
Fixed broken trigger
ankurdotb a13332c
fix total delegations count per validatior
benyam7 3113a4e
fix totat delegators count
benyam7 ff56494
Merge branch 'fix-broken-api' of github.com:cheqd/data-api into fix-b…
benyam7 3c8b55a
fix total staked coins
benyam7 7a67d4d
Merge branch 'main' into fix-broken-api
benyam7 b66692c
Update wrangler.toml
ankurdotb 61766fb
fix: total balance
benyam7 0640c16
Merge branch 'fix-broken-api' of github.com:cheqd/data-api into fix-b…
benyam7 86fb48f
Fix endpoint
ankurdotb 2dc9fdb
Merge branch 'main' into fix-circulating-supply
ankurdotb 5047784
fix total balance
benyam7 d7dfca0
fix circulating supply
benyam7 d07d0ee
chore refactor a bit
benyam7 74a67fb
fix node api (include unbounding/delegated free balance for totalBalance
benyam7 4528425
make total balance endpoint to consider unbounding balance
benyam7 921a886
Squashed commit of the following:
ankurdotb 0a294b1
Merge branch 'main' into fix-circulating-supply
ankurdotb 5f955e1
chore store account balance infos in KV
benyam7 168c67b
Merge branch 'fix-circulating-supply' of github.com:cheqd/data-api in…
benyam7 2e34644
fix total balance
benyam7 2573fa3
trigger cron based on current
benyam7 0c77937
remove CSW KV and change trigger interval for staging
benyam7 2597392
remove :delayed
benyam7 4a884e9
fix wrangler.toml
benyam7 61750fd
fix total balance
benyam7 0c07fde
decrease height by one
benyam7 014f3f3
lowwer chances of getting total balance to be 0
benyam7 11e3ffb
Fix typo in available balance
ankurdotb d8efedf
Fix typo in unbonding balance
ankurdotb cd310b6
Convert available balance to CHEQ
ankurdotb 9b024c4
Update staging trigger to every hour
ankurdotb d4e36ec
get total delegations balance from node api
benyam7 a591b35
total unbonding balance for delegator from node api
benyam7 a835e11
Merge branch 'fix-circulating-supply' of github.com:cheqd/data-api in…
benyam7 2798e3e
fix calculate unbonding balance
benyam7 2ed09b8
get account balance infos from node for totalBalance
benyam7 89430c2
update account balance infos from node api
benyam7 e124c26
add a logic to calculate vesting and vested(taken form wallet app)
benyam7 160f271
update vesting and vested endpoints to get result from newly added logic
benyam7 284b746
review changes
benyam7 b5abbf9
fix a bug when calculating avaliable balance
benyam7 0439669
remove getting account details from gql
benyam7 a0a93ee
Squashed commit of the following:
ankurdotb 44d7017
Remove total supply from
ankurdotb 3b2023e
Update wrangler.toml
ankurdotb df99708
Add wrangler variable for watchlist groups
ankurdotb 4e55ad0
Updated logic for dynamically selecting a random group
ankurdotb 1ce38ee
Merge branch 'main' into fix-circulating-supply
ankurdotb 8db90aa
revert back to node api for total supply
benyam7 b3ed9af
Updated KV pair
ankurdotb a30258b
Update KV again
ankurdotb 2475c53
Update logging
ankurdotb ea9380e
Reset cron to hourly
ankurdotb 91a2840
Remove unnecessary logging
ankurdotb 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
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
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,57 +1,46 @@ | ||
| import { GraphQLClient } from "../helpers/graphql"; | ||
| import { BigDipperApi } from "../api/bigDipperApi"; | ||
| import { Request } from "itty-router"; | ||
| import { ncheq_to_cheq_fixed } from "../helpers/currency"; | ||
| import { total_balance_ncheq } from "../helpers/node"; | ||
| import { Account } from "../types/bigDipper"; | ||
| import { Request } from 'itty-router'; | ||
| import { ncheq_to_cheq_fixed } from '../helpers/currency'; | ||
| import { NodeApi } from '../api/nodeApi'; | ||
| import { AccountBalanceInfos } from '../types/node'; | ||
|
|
||
| async function get_total_supply(): Promise<number> { | ||
| let node_api = new NodeApi(REST_API); | ||
| let total_supply_ncheq = await node_api.bank_get_total_supply_ncheq(); | ||
| const total_supply = Number(ncheq_to_cheq_fixed(total_supply_ncheq)); | ||
|
|
||
| return total_supply; | ||
| } | ||
|
|
||
| async function get_circulating_supply(): Promise<number> { | ||
| let gql_client = new GraphQLClient(GRAPHQL_API); | ||
| let bd_api = new BigDipperApi(gql_client); | ||
|
|
||
| let total_supply = await bd_api.get_total_supply(); | ||
| let total_supply_ncheq = Number(total_supply.find(c => c.denom === "ncheq")?.amount || '0'); | ||
|
|
||
| try { | ||
| const cached = await CIRCULATING_SUPPLY_WATCHLIST.list() | ||
| console.log(`found ${cached.keys.length} cached items`) | ||
|
|
||
| let non_circulating_supply_ncheq = Number(0); | ||
| for (const r of cached.keys) { | ||
| console.log(`looking for account: ${r.name} in cache`) | ||
| let data: any = await CIRCULATING_SUPPLY_WATCHLIST.get(r.name, { type: "json" }); | ||
|
|
||
| if (data !== null) { | ||
| if (data.totalBalance === undefined) { | ||
| const balance = total_balance_ncheq(JSON.parse(data) as Account) | ||
| data = JSON.stringify({ totalBalance: balance, updatedAt: Date.now() }) | ||
| console.log(`updating bad cache entry: ${JSON.stringify(data)} totalBalance=${data.totalBalance} data=${JSON.stringify(data)}`) | ||
| await CIRCULATING_SUPPLY_WATCHLIST.put(r.name, data) | ||
| } | ||
|
|
||
| console.log(`found cache entry: ${JSON.stringify(data)} totalBalance=${data.totalBalance}`) | ||
|
|
||
| if (data.totalBalance !== null) { | ||
| non_circulating_supply_ncheq += data.totalBalance; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(`Non-circulating supply: ${non_circulating_supply_ncheq}`); | ||
| // Get total supply | ||
| let total_supply_ncheq = Number(total_supply.find(c => c.denom === "ncheq")?.amount || '0'); | ||
| console.log(`Total supply: ${total_supply_ncheq}`); | ||
|
|
||
| // Calculate circulating supply | ||
| return total_supply_ncheq - non_circulating_supply_ncheq; | ||
| } catch (e: any) { | ||
| console.error(new Map(e)) | ||
| return total_supply_ncheq | ||
| const total_supply = await get_total_supply(); | ||
|
|
||
| try { | ||
| const cached = await CIRCULATING_SUPPLY_WATCHLIST.list(); | ||
| console.log(`Total cached entries: ${cached.keys.length}`); | ||
|
|
||
| let shareholders_total_balance = Number(0); | ||
| for (const key of cached.keys) { | ||
| let data: AccountBalanceInfos | null = | ||
| await CIRCULATING_SUPPLY_WATCHLIST.get(key.name, { | ||
| type: 'json', | ||
| }); | ||
|
|
||
| if (data !== null && data.totalBalance !== null) { | ||
| shareholders_total_balance += Number(data.totalBalance); | ||
| } | ||
| } | ||
|
|
||
| console.log('Total supply', total_supply); | ||
| console.log(`Watchlist total balance: ${shareholders_total_balance}`); | ||
|
|
||
| return total_supply - shareholders_total_balance; | ||
| } catch (e: any) { | ||
| throw new Error(e.toString); | ||
| } | ||
| } | ||
|
|
||
| export async function handler(request: Request): Promise<Response> { | ||
| let circulating_supply = await get_circulating_supply(); | ||
| let circulating_supply = await get_circulating_supply(); | ||
|
|
||
| return new Response(ncheq_to_cheq_fixed(circulating_supply)); | ||
| return new Response(circulating_supply.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,10 @@ | ||
| import { Request } from 'itty-router'; | ||
| import { | ||
| is_delayed_vesting_account_type, | ||
| validate_cheqd_address, | ||
| } from '../helpers/validate'; | ||
| import { ncheq_to_cheq_fixed } from '../helpers/currency'; | ||
| import { BigDipperApi } from '../api/bigDipperApi'; | ||
| import { GraphQLClient } from '../helpers/graphql'; | ||
| import { total_balance_ncheq } from '../helpers/node'; | ||
| import { NodeApi } from '../api/nodeApi'; | ||
| import { get_account_balance_infos_from_node_api } from '../helpers/balance'; | ||
|
|
||
| export async function handler(request: Request): Promise<Response> { | ||
| const address = request.params?.['address']; | ||
|
|
||
| if (!address || !validate_cheqd_address(address)) { | ||
| throw new Error('No address specified or wrong address format.'); | ||
| } | ||
|
|
||
| let node_api = new NodeApi(REST_API); | ||
| let auth_account = await node_api.auth_get_account(address); | ||
|
|
||
| if (is_delayed_vesting_account_type(auth_account?.['@type'])) { | ||
| let balance = Number( | ||
| (await ( | ||
| await node_api.bank_get_account_balances(address) | ||
| ).find((b) => b.denom === 'ncheq')?.amount) ?? '0' | ||
| ); | ||
| let rewards = Number( | ||
| (await await node_api.distribution_get_total_rewards(address)) ?? '0' | ||
| ); | ||
| let delegated = Number( | ||
| auth_account?.base_vesting_account?.delegated_vesting?.find( | ||
| (d) => d.denom === 'ncheq' | ||
| )?.amount ?? '0' | ||
| ); | ||
|
|
||
| return new Response(ncheq_to_cheq_fixed(balance + rewards + delegated)); | ||
| } | ||
|
|
||
| let gql_client = new GraphQLClient(GRAPHQL_API); | ||
| let bd_api = new BigDipperApi(gql_client); | ||
|
|
||
| let account = await bd_api.get_account(address); | ||
|
|
||
| if (!account) { | ||
| throw new Error('Account not found'); | ||
| } | ||
|
|
||
| return new Response(ncheq_to_cheq_fixed(total_balance_ncheq(account))); | ||
| let account_balance_infos = await get_account_balance_infos_from_node_api( | ||
| address!! | ||
| ); | ||
| return new Response(account_balance_infos?.totalBalance.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import { NodeApi } from "../api/nodeApi"; | ||
| import { Request } from "itty-router"; | ||
| import { ncheq_to_cheq_fixed } from "../helpers/currency"; | ||
| import { Request } from 'itty-router'; | ||
| import { NodeApi } from '../api/nodeApi'; | ||
| import { ncheq_to_cheq_fixed } from '../helpers/currency'; | ||
|
|
||
| export async function handler(request: Request): Promise<Response> { | ||
| let nodeApi = new NodeApi(REST_API); | ||
| let totalSupply = await nodeApi.bank_get_total_supply_ncheq(); | ||
| let nodeApi = new NodeApi(REST_API); | ||
| let totalSupply = await nodeApi.bank_get_total_supply_ncheq(); | ||
|
|
||
| return new Response(ncheq_to_cheq_fixed(totalSupply)); | ||
| return new Response(ncheq_to_cheq_fixed(totalSupply)); | ||
| } |
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,24 +1,29 @@ | ||
| import { Request } from "itty-router"; | ||
| import { is_vesting_account_type, validate_cheqd_address } from "../helpers/validate"; | ||
| import { NodeApi } from "../api/nodeApi"; | ||
| import { calculate_vested_coins } from "../helpers/vesting"; | ||
| import { ncheq_to_cheq_fixed } from "../helpers/currency"; | ||
| import { Request } from 'itty-router'; | ||
| import { | ||
| is_vesting_account_type, | ||
| validate_cheqd_address, | ||
| } from '../helpers/validate'; | ||
| import { NodeApi } from '../api/nodeApi'; | ||
| import { calculate_vested_coins, estimatedVesting } from '../helpers/vesting'; | ||
| import { ncheq_to_cheq_fixed } from '../helpers/currency'; | ||
|
|
||
| export async function handler(request: Request): Promise<Response> { | ||
| const address = request.params?.['address']; | ||
| const address = request.params?.['address']; | ||
|
|
||
| if (!address || !validate_cheqd_address(address)) { | ||
| throw new Error("No address specified or wrong address format."); | ||
| } | ||
| if (!address || !validate_cheqd_address(address)) { | ||
| throw new Error('No address specified or wrong address format.'); | ||
| } | ||
|
|
||
| let api = new NodeApi(REST_API); | ||
| const account = await api.auth_get_account(address); | ||
| let api = new NodeApi(REST_API); | ||
| const account = await api.auth_get_account(address); | ||
|
|
||
| if (!is_vesting_account_type(account["@type"])) { | ||
| throw new Error(`Only vesting accounts are supported. Accounts type '${account["@type"]}'.`) | ||
| } | ||
| if (!is_vesting_account_type(account['@type'])) { | ||
| throw new Error( | ||
| `Only vesting accounts are supported. Accounts type '${account['@type']}'.` | ||
| ); | ||
| } | ||
|
|
||
| let vested_coins = calculate_vested_coins(account); | ||
| let vested_coins = estimatedVesting(account)?.vested; | ||
|
|
||
| return new Response(ncheq_to_cheq_fixed(vested_coins)); | ||
| return new Response(ncheq_to_cheq_fixed(vested_coins!!)); | ||
| } |
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,24 +1,29 @@ | ||
| import { Request } from "itty-router"; | ||
| import { is_vesting_account_type, validate_cheqd_address } from "../helpers/validate"; | ||
| import { NodeApi } from "../api/nodeApi"; | ||
| import { calculate_vesting_coins } from "../helpers/vesting"; | ||
| import { ncheq_to_cheq_fixed } from "../helpers/currency"; | ||
| import { Request } from 'itty-router'; | ||
| import { | ||
| is_vesting_account_type, | ||
| validate_cheqd_address, | ||
| } from '../helpers/validate'; | ||
| import { NodeApi } from '../api/nodeApi'; | ||
| import { calculate_vesting_coins, estimatedVesting } from '../helpers/vesting'; | ||
| import { ncheq_to_cheq_fixed } from '../helpers/currency'; | ||
|
|
||
| export async function handler(request: Request): Promise<Response> { | ||
| const address = request.params?.['address']; | ||
| const address = request.params?.['address']; | ||
|
|
||
| if (!address || !validate_cheqd_address(address)) { | ||
| throw new Error("No address specified or wrong address format."); | ||
| } | ||
| if (!address || !validate_cheqd_address(address)) { | ||
| throw new Error('No address specified or wrong address format.'); | ||
| } | ||
|
|
||
| let api = new NodeApi(REST_API); | ||
| const account = await api.auth_get_account(address) | ||
| let api = new NodeApi(REST_API); | ||
| const account = await api.auth_get_account(address); | ||
|
|
||
| if (!is_vesting_account_type(account["@type"])) { | ||
| throw new Error(`Only vesting accounts are supported. Accounts type '${account["@type"]}'.`) | ||
| } | ||
| if (!is_vesting_account_type(account['@type'])) { | ||
| throw new Error( | ||
| `Only vesting accounts are supported. Accounts type '${account['@type']}'.` | ||
| ); | ||
| } | ||
|
|
||
| let vestingCoins = calculate_vesting_coins(account); | ||
| let vestingCoins = estimatedVesting(account)?.vesting; | ||
|
|
||
| return new Response(ncheq_to_cheq_fixed(vestingCoins)); | ||
| return new Response(ncheq_to_cheq_fixed(vestingCoins!!)); | ||
| } |
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
Oops, something went wrong.
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.