Skip to content

Commit bb1fd86

Browse files
committed
chore: test PoX rewards against bitcoin json-rpc results
1 parent e8e650e commit bb1fd86

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/tests-2.1/pox-2.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AnchorMode, bufferCV, makeContractCall, tupleCV, uintCV } from '@stacks
88
import { CoreRpcPoxInfo, StacksCoreRpcClient } from '../core-rpc/client';
99
import { testnetKeys } from '../api/routes/debug';
1010
import * as poxHelpers from '../pox-helpers';
11-
import { parsePort } from '../helpers';
11+
import { hexToBuffer, parsePort } from '../helpers';
1212
import { PgWriteStore } from '../datastore/pg-write-store';
1313
import { StacksNetwork } from '@stacks/network';
1414
import * as btcLib from 'bitcoinjs-lib';
@@ -20,6 +20,7 @@ import {
2020
BurnchainRewardsTotal,
2121
} from '@stacks/stacks-blockchain-api-types';
2222
import { RPCClient } from 'rpc-bitcoin';
23+
import bignumber from 'bignumber.js';
2324

2425
describe('PoX-2 tests', () => {
2526
let db: PgWriteStore;
@@ -406,6 +407,31 @@ describe('PoX-2 tests', () => {
406407
expect(Number(rewardsTotal.reward_amount)).toBeGreaterThan(0);
407408
});
408409

410+
test('stacking rewards - BTC JSON-RPC', async () => {
411+
const rewards = await fetchGet<BurnchainRewardListResponse>(
412+
`/extended/v1/burnchain/rewards/${btcAddr}`
413+
);
414+
const firstReward = rewards.results.sort(
415+
(a, b) => a.burn_block_height - b.burn_block_height
416+
)[0];
417+
const blockResult: {
418+
tx: { vout?: { scriptPubKey: { addresses?: string[] }; value?: number }[] }[];
419+
} = await rpcClient.getblock({
420+
blockhash: hexToBuffer(firstReward.burn_block_hash).toString('hex'),
421+
verbosity: 2,
422+
});
423+
const vout = blockResult.tx
424+
.flatMap(t => t.vout)
425+
.find(t => t?.scriptPubKey.addresses?.includes(btcRegtestAddr) && t.value);
426+
if (!vout || !vout.value) {
427+
throw new Error(
428+
`Could not find bitcoin vout for ${btcRegtestAddr} in block ${firstReward.burn_block_hash}`
429+
);
430+
}
431+
const sats = new bignumber(vout.value).shiftedBy(8).toString();
432+
expect(sats).toBe(firstReward.reward_amount);
433+
});
434+
409435
test('stx unlocked - RPC balance endpoint', async () => {
410436
// Wait until account has unlocked (finished Stacking cycles)
411437
const rpcAccountInfo1 = await client.getAccount(account.stacksAddress);

0 commit comments

Comments
 (0)