Skip to content

Commit

Permalink
fix: include rewards in ada circulating supply
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed Mar 8, 2021
1 parent f86ef42 commit 69f23c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
36 changes: 34 additions & 2 deletions packages/api-cardano-db-hasura/src/HasuraClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ export class HasuraClient {
this.applyingSchemaAndMetadata = false
this.adaCirculatingSupplyFetcher = new DataFetcher<AssetSupply['circulating']>(
'AdaCirculatingSupply',
this.getAdaCirculatingSupply.bind(this),
() => {
try {
return this.getAdaCirculatingSupply()
} catch (error) {
if (error.message !== 'currentEpoch is only available when close to the chain tip. This is expected during the initial chain-sync.') {
throw error
}
this.logger.debug(error.message)
}
},
pollingInterval,
this.logger
)
Expand All @@ -60,16 +69,39 @@ export class HasuraClient {
private async getAdaCirculatingSupply (): Promise<AssetSupply['circulating']> {
const result = await this.client.query({
query: gql`query {
rewards_aggregate {
aggregate {
sum {
amount
}
}
}
utxos_aggregate {
aggregate {
sum {
value
}
}
}
withdrawals_aggregate {
aggregate {
sum {
amount
}
}
}
}`
})
return result.data.utxos_aggregate.aggregate.sum.value
const {
rewards_aggregate: rewardsAggregate,
utxos_aggregate: utxosAggregate,
withdrawals_aggregate: withdrawalsAggregate
} = result.data
const rewards = new BigNumber(rewardsAggregate.aggregate.sum.amount)
const utxos = new BigNumber(utxosAggregate.aggregate.sum.value)
const withdrawals = new BigNumber(withdrawalsAggregate.aggregate.sum.amount)
const withdrawableRewards = rewards.minus(withdrawals)
return utxos.plus(withdrawableRewards).toString()
}

private async hasuraCli (command: string) {
Expand Down
6 changes: 5 additions & 1 deletion packages/api-cardano-db-hasura/src/executableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ export async function buildSchema (
},
ada: async () => {
await throwIfNotInCurrentEra('ada')
const circulating = hasuraClient.adaCirculatingSupplyFetcher.value
if (circulating === undefined) {
return new ApolloError('ada query results are not ready yet. This can occur during startup.')
}
return {
supply: {
circulating: hasuraClient.adaCirculatingSupplyFetcher.value,
circulating,
max: genesis.shelley.maxLovelaceSupply
}
}
Expand Down

0 comments on commit 69f23c1

Please sign in to comment.