Skip to content

Commit 4c8d514

Browse files
authored
fix: do not duplicate miner rewards in v2 balance endpoint (#2302)
* fix: do not duplicate miner rewards in v2 balance endpoint * test: rewards * test: exclusive
1 parent 8fccc78 commit 4c8d514

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/api/routes/v2/addresses.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export const AddressRoutesV2: FastifyPluginAsync<
167167
sql,
168168
stxAddress,
169169
});
170-
let stxBalance = stxBalancesResult.found ? stxBalancesResult.result.balance : 0n;
170+
const stxBalance = stxBalancesResult.found ? stxBalancesResult.result.balance : 0n;
171171

172172
// Get pox-locked info for STX token
173173
const stxPoxLockedResult = await fastify.db.v2.getStxPoxLockedAtBlock({
@@ -183,7 +183,6 @@ export const AddressRoutesV2: FastifyPluginAsync<
183183
stxAddress,
184184
blockHeight: chainTip.block_height,
185185
});
186-
stxBalance += totalMinerRewardsReceived;
187186

188187
const result: StxBalance = {
189188
balance: stxBalance.toString(),

tests/api/address.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,4 +3177,63 @@ describe('address tests', () => {
31773177
expect(result.type).toBe('application/json');
31783178
expect(JSON.parse(result.text).balance).toBe(v1balance);
31793179
});
3180+
3181+
test('balance calculation after miner rewards', async () => {
3182+
const addr1 = 'SP3D6PV2ACBPEKYJTCMH7HEN02KP87QSP8KTEH335';
3183+
3184+
// Send some initial balance for addr1
3185+
await db.update(
3186+
new TestBlockBuilder({
3187+
block_height: 1,
3188+
index_block_hash: '0x0001',
3189+
parent_index_block_hash: '',
3190+
})
3191+
.addTx({
3192+
tx_id: '0x1101',
3193+
token_transfer_recipient_address: addr1,
3194+
type_id: DbTxTypeId.TokenTransfer,
3195+
token_transfer_amount: 20_000n,
3196+
fee_rate: 50n,
3197+
})
3198+
.addTxStxEvent({
3199+
amount: 20_000n,
3200+
block_height: 1,
3201+
recipient: addr1,
3202+
tx_id: '0x1101',
3203+
})
3204+
.build()
3205+
);
3206+
// Add some miner rewards
3207+
await db.update(
3208+
new TestBlockBuilder({
3209+
block_height: 2,
3210+
index_block_hash: '0x0002',
3211+
parent_index_block_hash: '0x0001',
3212+
})
3213+
.addMinerReward({
3214+
index_block_hash: '0x0002',
3215+
recipient: addr1,
3216+
coinbase_amount: 2000n,
3217+
tx_fees_anchored: 0n,
3218+
tx_fees_streamed_confirmed: 0n,
3219+
tx_fees_streamed_produced: 0n,
3220+
})
3221+
.build()
3222+
);
3223+
3224+
// Check that v1 balance matches v2 balance.
3225+
let result = await supertest(api.server).get(`/extended/v1/address/${addr1}/stx`);
3226+
expect(result.status).toBe(200);
3227+
expect(result.type).toBe('application/json');
3228+
let json = JSON.parse(result.text);
3229+
const v1balance = json.balance;
3230+
const v1Rewards = json.total_miner_rewards_received;
3231+
expect(v1balance).toBe('22000');
3232+
result = await supertest(api.server).get(`/extended/v2/addresses/${addr1}/balances/stx`);
3233+
expect(result.status).toBe(200);
3234+
expect(result.type).toBe('application/json');
3235+
json = JSON.parse(result.text);
3236+
expect(json.balance).toBe(v1balance);
3237+
expect(json.total_miner_rewards_received).toBe(v1Rewards);
3238+
});
31803239
});

0 commit comments

Comments
 (0)