Skip to content

Commit

Permalink
Merge pull request #141 from ckb-cell/feat/get-bitcoin-balance-from-c…
Browse files Browse the repository at this point in the history
…ache

feat: add data cache for /bitcoin/v1/address/:address/balance
  • Loading branch information
ahonn authored May 17, 2024
2 parents c4995a2 + 0e6fc7c commit 956c80a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/routes/bitcoin/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
}),
querystring: z.object({
min_satoshi: z.coerce.number().optional().describe('The minimum value of the UTXO in satoshi'),
no_cache: z
.enum(['true', 'false'])
.default('false')
.describe('Whether to disable cache to get utxos, default is false'),
}),
response: {
200: Balance,
Expand All @@ -36,8 +40,15 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
},
async (request) => {
const { address } = request.params;
const { min_satoshi } = request.query;
const utxos = await fastify.bitcoin.getAddressTxsUtxo({ address });
const { min_satoshi, no_cache } = request.query;

let utxosCache = null;
if (env.UTXO_SYNC_DATA_CACHE_ENABLE && no_cache !== 'true') {
utxosCache = await fastify.utxoSyncer.getUTXOsFromCache(address);
await fastify.utxoSyncer.enqueueSyncJob(address);
}
const utxos = utxosCache ? utxosCache : await fastify.bitcoin.getAddressTxsUtxo({ address });

return utxos.reduce(
(acc: Balance, utxo: UTXO) => {
if (utxo.status.confirmed) {
Expand Down

0 comments on commit 956c80a

Please sign in to comment.