From d566c368e09c04eb7df5f52ad2e07a8686a9c9fe Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Fri, 4 Mar 2022 19:19:54 +0800 Subject: [PATCH] cache /supply/burninfo (#827) --- src/module.api/stats.controller.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/module.api/stats.controller.ts b/src/module.api/stats.controller.ts index 2274de8a2..2f1ba2b13 100644 --- a/src/module.api/stats.controller.ts +++ b/src/module.api/stats.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get } from '@nestjs/common' +import { BadGatewayException, Controller, Get } from '@nestjs/common' import { StatsData, SupplyData } from '@whale-api-client/api/stats' import { SemaphoreCache } from '@src/module.api/cache/semaphore.cache' import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc' @@ -53,6 +53,9 @@ export class StatsController { const max = 1200000000 const total = this.blockSubsidy.getSupply(height).div(100000000) const burned = await this.getBurnedTotal() + if (burned === undefined) { + throw new BadGatewayException('rpc gateway error') + } const circulating = total.minus(burned) return { @@ -137,16 +140,18 @@ export class StatsController { } } - private async getBurnedTotal (): Promise { - // 8defichainBurnAddressXXXXXXXdRQkSm, using the hex representation as it's applicable in all network - const address = '76a914f7874e8821097615ec345f74c7e5bcf61b12e2ee88ac' - const tokens = await this.rpcClient.account.getAccount(address) - const burnInfo = await this.rpcClient.account.getBurnInfo() - - const utxo = burnInfo.amount - const account = findTokenBalance(tokens, 'DFI') - const emission = burnInfo.emissionburn - return utxo.plus(account).plus(emission) + private async getBurnedTotal (): Promise { + return await this.cache.get('stats.getBurnedTotal', async () => { + // 8defichainBurnAddressXXXXXXXdRQkSm, using the hex representation as it's applicable in all network + const address = '76a914f7874e8821097615ec345f74c7e5bcf61b12e2ee88ac' + const tokens = await this.rpcClient.account.getAccount(address) + const burnInfo = await this.rpcClient.account.getBurnInfo() + + const utxo = burnInfo.amount + const account = findTokenBalance(tokens, 'DFI') + const emission = burnInfo.emissionburn + return utxo.plus(account).plus(emission) + }) } private async getPrice (): Promise {