Skip to content

Commit

Permalink
feat: add estimated inbound and outbound stx balance
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jan 27, 2025
1 parent 396e2ea commit 63c692c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
20 changes: 16 additions & 4 deletions src/api/routes/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ export const AddressRoutes: FastifyPluginAsync<
blockHeight
);
let mempoolBalance: bigint | undefined = undefined;
let mempoolInbound: bigint | undefined = undefined;
let mempoolOutbound: bigint | undefined = undefined;
if (req.query.until_block === undefined) {
const delta = await fastify.db.getPrincipalMempoolStxBalanceDelta(sql, stxAddress);
mempoolBalance = stxBalanceResult.balance + delta;
const pending = await fastify.db.getPrincipalMempoolStxBalanceDelta(sql, stxAddress);
mempoolInbound = pending.inbound;
mempoolOutbound = pending.outbound;
mempoolBalance = stxBalanceResult.balance + pending.delta;
}
const result: AddressStxBalance = {
balance: stxBalanceResult.balance.toString(),
estimated_balance: mempoolBalance?.toString(),
estimated_balance_inbound: mempoolInbound?.toString(),
estimated_balance_outbound: mempoolOutbound?.toString(),
total_sent: stxBalanceResult.totalSent.toString(),
total_received: stxBalanceResult.totalReceived.toString(),
total_fees_sent: stxBalanceResult.totalFeesSent.toString(),
Expand Down Expand Up @@ -211,15 +217,21 @@ export const AddressRoutes: FastifyPluginAsync<
});

let mempoolBalance: bigint | undefined = undefined;
let mempoolInbound: bigint | undefined = undefined;
let mempoolOutbound: bigint | undefined = undefined;
if (req.query.until_block === undefined) {
const delta = await fastify.db.getPrincipalMempoolStxBalanceDelta(sql, stxAddress);
mempoolBalance = stxBalanceResult.balance + delta;
const pending = await fastify.db.getPrincipalMempoolStxBalanceDelta(sql, stxAddress);
mempoolInbound = pending.inbound;
mempoolOutbound = pending.outbound;
mempoolBalance = stxBalanceResult.balance + pending.delta;
}

const result: AddressBalance = {
stx: {
balance: stxBalanceResult.balance.toString(),
estimated_balance: mempoolBalance?.toString(),
estimated_balance_inbound: mempoolInbound?.toString(),
estimated_balance_outbound: mempoolOutbound?.toString(),
total_sent: stxBalanceResult.totalSent.toString(),
total_received: stxBalanceResult.totalReceived.toString(),
total_fees_sent: stxBalanceResult.totalFeesSent.toString(),
Expand Down
10 changes: 10 additions & 0 deletions src/api/schemas/entities/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export const StxBalanceSchema = Type.Object(
description: 'Total STX balance considering pending mempool transactions',
})
),
estimated_balance_inbound: Type.Optional(
Type.String({
description: 'Inbound STX balance from pending mempool transactions',
})
),
estimated_balance_outbound: Type.Optional(
Type.String({
description: 'Outbound STX balance from pending mempool transactions',
})
),
total_sent: Type.String(),
total_received: Type.String(),
total_fees_sent: Type.String(),
Expand Down
20 changes: 12 additions & 8 deletions src/datastore/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2480,8 +2480,8 @@ export class PgStore extends BasePgStore {
* Returns the total STX balance delta affecting a principal from transactions currently in the
* mempool.
*/
async getPrincipalMempoolStxBalanceDelta(sql: PgSqlClient, principal: string): Promise<bigint> {
const results = await sql<{ delta: string }[]>`
async getPrincipalMempoolStxBalanceDelta(sql: PgSqlClient, principal: string) {
const results = await sql<{ inbound: string; outbound: string; delta: string }[]>`
WITH sent AS (
SELECT SUM(COALESCE(token_transfer_amount, 0) + fee_rate) AS total
FROM mempool_txs
Expand All @@ -2496,14 +2496,18 @@ export class PgStore extends BasePgStore {
SELECT SUM(COALESCE(token_transfer_amount, 0)) AS total
FROM mempool_txs
WHERE pruned = false AND token_transfer_recipient_address = ${principal}
),
values AS (
COALESCE((SELECT total FROM received), 0) AS inbound,
COALESCE((SELECT total FROM sent), 0) + COALESCE((SELECT total FROM sponsored), 0) AS outbound
)
SELECT
COALESCE((SELECT total FROM received), 0)
- COALESCE((SELECT total FROM sent), 0)
- COALESCE((SELECT total FROM sponsored), 0)
AS delta
SELECT inbound, outbound, (inbound - outbound) AS delta
`;
return BigInt(results[0]?.delta ?? '0');
return {
inbound: BigInt(results[0]?.inbound ?? '0'),
outbound: BigInt(results[0]?.outbound ?? '0'),
delta: BigInt(results[0]?.delta ?? '0'),
};
}

async getUnlockedStxSupply(
Expand Down

0 comments on commit 63c692c

Please sign in to comment.