Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/api/routes/v3/staking-rewards.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { FastifyPluginAsync } from 'fastify';
import { Server } from 'node:http';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { STACKS_MAINNET } from '@stacks/network';
import { handleBurnchainChainTipCache } from '../../controllers/cache-controller.js';
import { StakingRewardsSchema } from '../../schemas/v3/entities/staking-rewards.js';

/**
* BTC burned on mainnet by the 2.0-era PoX sunset-ramp surcharge (bitcoin blocks ~766050-781551),
* in satoshis. The node never reported these burns in its burn block events, so they are added as a
* fixed historical total.
*/
const MAINNET_SUNSET_BURN_AMOUNT = 489352064n;

export const StakingRewardsRoutes: FastifyPluginAsync<
Record<never, never>,
Server,
Expand All @@ -28,11 +36,15 @@ export const StakingRewardsRoutes: FastifyPluginAsync<
},
async (_req, reply) => {
const totals = await fastify.db.v3.getStakingRewards();
const sunsetBurnAmount =
fastify.chainId === STACKS_MAINNET.chainId ? MAINNET_SUNSET_BURN_AMOUNT : 0n;
Comment thread
rafa-stacks marked this conversation as resolved.
Resolved
const rewardAmount = BigInt(totals.reward_amount);
const burnAmount = BigInt(totals.burn_amount) + sunsetBurnAmount;
await reply.send({
btc: {
reward_amount: totals.reward_amount,
burn_amount: totals.burn_amount,
total_amount: (BigInt(totals.reward_amount) + BigInt(totals.burn_amount)).toString(),
reward_amount: rewardAmount.toString(),
burn_amount: burnAmount.toString(),
total_amount: (rewardAmount + burnAmount).toString(),
},
});
}
Expand Down
15 changes: 14 additions & 1 deletion tests/api/burnchain/staking-rewards.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import supertest from 'supertest';
import { afterEach, beforeEach, describe, test } from 'node:test';
import assert from 'node:assert/strict';
import { STACKS_TESTNET } from '@stacks/network';
import { STACKS_MAINNET, STACKS_TESTNET } from '@stacks/network';
import { ApiServer, startApiServer } from '../../../src/api/init.ts';
import { EventStreamServer, startEventServer } from '../../../src/event-stream/event-server.ts';
import { httpPostRequest } from '../../../src/helpers.ts';
Expand Down Expand Up @@ -182,6 +182,19 @@ describe('staking rewards totals', () => {
});
});

test('mainnet totals include the fixed PoX sunset burn', async () => {
await api.terminate();
api = await startApiServer({ datastore: db, chainId: STACKS_MAINNET.chainId });
assert.deepEqual(await getTotals(), {
btc: { reward_amount: '0', burn_amount: '489352064', total_amount: '489352064' },
});

await deliverBurnBlock({ hash: '0xaa01', height: 100, burnAmount: 500n, rewardAmount: 1000n });
assert.deepEqual(await getTotals(), {
btc: { reward_amount: '1000', burn_amount: '489352564', total_amount: '489353564' },
});
});

test('migration backfill reproduces the live counters', async () => {
const migrationName = '1779800000016_chain-tip-staking-rewards';
await deliverBurnBlock({ hash: '0xaa01', height: 100, burnAmount: 500n, rewardAmount: 1000n });
Expand Down
Loading