Skip to content

Commit 3480e59

Browse files
authored
Merge pull request #2241 from hirosystems/develop
Cut release `v8.8.0-beta.2`
2 parents aa5d1b5 + 8b27809 commit 3480e59

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/api/routes/v2/addresses.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,50 @@ export const AddressRoutesV2: FastifyPluginAsync<
264264
}
265265
);
266266

267+
fastify.get(
268+
'/:principal/balances/ft/:token',
269+
{
270+
preHandler: handleChainTipCache, // TODO: use handlePrincipalCache once it's optimized
271+
schema: {
272+
operationId: 'get_principal_ft_balance',
273+
summary: 'Get principal FT balance',
274+
description: `Retrieves a specific fungible-token balance for a given principal.`,
275+
tags: ['Accounts'],
276+
params: Type.Object({
277+
principal: PrincipalSchema,
278+
token: Type.String({
279+
description: 'fungible token identifier',
280+
examples: [
281+
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token::sbtc-token',
282+
'SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc::aeUSDC',
283+
],
284+
}),
285+
}),
286+
response: {
287+
200: Type.Object({
288+
balance: Type.String(),
289+
}),
290+
},
291+
},
292+
},
293+
async (req, reply) => {
294+
const stxAddress = req.params.principal;
295+
validatePrincipal(stxAddress);
296+
const result = await fastify.db.sqlTransaction(async sql => {
297+
const ftBalanceResult = await fastify.db.v2.getFtHolderBalance({
298+
sql,
299+
stxAddress,
300+
token: req.params.token,
301+
});
302+
const balance = ftBalanceResult.found ? ftBalanceResult.result.balance : 0n;
303+
const result = {
304+
balance: balance.toString(),
305+
};
306+
return result;
307+
});
308+
await reply.send(result);
309+
}
310+
);
311+
267312
await Promise.resolve();
268313
};

src/datastore/pg-store-v2.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,26 @@ export class PgStoreV2 extends BasePgStoreModule {
10541054
};
10551055
}
10561056

1057+
async getFtHolderBalance(args: {
1058+
sql: PgSqlClient;
1059+
stxAddress: string;
1060+
token: string;
1061+
}): Promise<FoundOrNot<{ balance: bigint }>> {
1062+
const [result] = await args.sql<{ balance: string }[]>`
1063+
SELECT token, balance FROM ft_balances
1064+
WHERE address = ${args.stxAddress}
1065+
AND token = ${args.token}
1066+
LIMIT 1
1067+
`;
1068+
if (!result) {
1069+
return { found: false };
1070+
}
1071+
return {
1072+
found: true,
1073+
result: { balance: BigInt(result.balance) },
1074+
};
1075+
}
1076+
10571077
async getStxPoxLockedAtBlock({
10581078
sql,
10591079
stxAddress,

tests/api/address.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,27 @@ describe('address tests', () => {
16861686
results: [{ token: 'gox', balance: '585' }],
16871687
});
16881688

1689+
const fetchAddrV2BalanceFt1 = await supertest(api.server).get(
1690+
`/extended/v2/addresses/${testContractAddr}/balances/ft/bux`
1691+
);
1692+
expect(fetchAddrV2BalanceFt1.status).toBe(200);
1693+
expect(fetchAddrV2BalanceFt1.type).toBe('application/json');
1694+
expect(fetchAddrV2BalanceFt1.body).toEqual({ balance: '375' });
1695+
1696+
const fetchAddrV2BalanceFt2 = await supertest(api.server).get(
1697+
`/extended/v2/addresses/${testContractAddr}/balances/ft/gox`
1698+
);
1699+
expect(fetchAddrV2BalanceFt2.status).toBe(200);
1700+
expect(fetchAddrV2BalanceFt2.type).toBe('application/json');
1701+
expect(fetchAddrV2BalanceFt2.body).toEqual({ balance: '585' });
1702+
1703+
const fetchAddrV2BalanceFt3 = await supertest(api.server).get(
1704+
`/extended/v2/addresses/${testContractAddr}/balances/ft/none`
1705+
);
1706+
expect(fetchAddrV2BalanceFt3.status).toBe(200);
1707+
expect(fetchAddrV2BalanceFt3.type).toBe('application/json');
1708+
expect(fetchAddrV2BalanceFt3.body).toEqual({ balance: '0' });
1709+
16891710
const tokenLocked: DbTokenOfferingLocked = {
16901711
address: testContractAddr,
16911712
value: BigInt(4139391122),

0 commit comments

Comments
 (0)