Skip to content

Commit

Permalink
fix: rosetta account endpoint should assume chain tip if block not sp…
Browse files Browse the repository at this point in the history
…ecified (#1956)

* fix: rosetta account endpoint should assume chain tip if block not specified

* chore: remove invalid rosetta test
  • Loading branch information
zone117x committed Apr 19, 2024
1 parent 63b417e commit 4bba526
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
5 changes: 4 additions & 1 deletion src/api/routes/rosetta/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function createRosettaAccountRouter(db: PgStore, chainId: ChainID): expre
let blockHash: string = '0x';
// we need to return the block height/hash in the response, so we
// need to fetch the block first.
if (blockIdentifier === undefined) {
if (
(!blockIdentifier?.hash && !blockIdentifier?.index) ||
(blockIdentifier && blockIdentifier.index <= 0)
) {
blockQuery = await db.getCurrentBlock();
} else if (blockIdentifier.index > 0) {
blockQuery = await db.getBlock({ height: blockIdentifier.index });
Expand Down
37 changes: 25 additions & 12 deletions src/tests-rosetta/account-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,7 @@ describe('/account tests', () => {
.build();
await db.update(block2);

const query1 = await supertest(api.server)
.post(`/rosetta/v1/account/balance`)
.send({
network_identifier: { blockchain: 'stacks', network: 'testnet' },
block_identifier: { index: 2, hash: '0xf1f1' },
account_identifier: { address: addr2 },
});
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
const result1 = JSON.parse(query1.text);
expect(result1.balances).toEqual([
const expectedBalance = [
{
currency: {
decimals: 6,
Expand All @@ -99,7 +89,30 @@ describe('/account tests', () => {
},
value: '7500'
}
]);
];
const query1 = await supertest(api.server)
.post(`/rosetta/v1/account/balance`)
.send({
network_identifier: { blockchain: 'stacks', network: 'testnet' },
block_identifier: { index: 2, hash: '0xf1f1' },
account_identifier: { address: addr2 },
});
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
expect(JSON.parse(query1.text).balances).toEqual(expectedBalance);

// ensure query works with block identifier omitted
const query2 = await supertest(api.server)
.post(`/rosetta/v1/account/balance`)
.send({
network_identifier: { blockchain: 'stacks', network: 'testnet' },
account_identifier: { address: addr2 },
});
expect(query2.status).toBe(200);
expect(query2.type).toBe('application/json');
expect(JSON.parse(query2.text).balances).toEqual(expectedBalance);

nock.cleanAll();
});

});
26 changes: 0 additions & 26 deletions src/tests-rosetta/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,32 +1239,6 @@ describe('Rosetta API', () => {
expect(JSON.parse(result.text)).toEqual(expectResponse);
});

test('account/balance - empty block identifier', async () => {
const request: RosettaAccountBalanceRequest = {
network_identifier: {
blockchain: 'stacks',
network: 'testnet',
},
account_identifier: {
address: 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ',
metadata: {},
},
block_identifier: {},
};

const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
expect(result.status).toBe(400);
expect(result.type).toBe('application/json');

const expectResponse = {
code: 615,
message: 'Block identifier is null.',
retriable: false,
};

expect(JSON.parse(result.text)).toEqual(expectResponse);
});

test('account/balance - invalid block hash', async () => {
const request: RosettaAccountBalanceRequest = {
network_identifier: {
Expand Down

0 comments on commit 4bba526

Please sign in to comment.