Skip to content

Commit f4dbb11

Browse files
committed
fix: btc tests
1 parent 6b7ac2f commit f4dbb11

File tree

1 file changed

+79
-77
lines changed

1 file changed

+79
-77
lines changed

test/e2e/flask/btc/mocks/esplora.ts

Lines changed: 79 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
} from '../../../constants';
88

99
const ESPLORA_URL = 'https://esplora.rivet.link/esplora/api';
10-
const INFURA_BTC_MAINNET_URL =
11-
'https://bitcoin-mainnet.infura.io/v3/5b98a22672004ef1bf40a80123c5c48d';
10+
const INFURA_BTC_MAINNET_URL_PATTERN =
11+
/^https:\/\/bitcoin-mainnet\.infura\.io\/v3\/[a-f0-9]{32}$/u;
1212

1313
const FUNDING_SCRIPT_HASH =
1414
'538c172f4f5ff9c24693359c4cdc8ee4666565326a789d5e4b2df1db7acb4721';
@@ -20,74 +20,77 @@ const GENESIS_BLOCK_HASH =
2020

2121
/* eslint-disable @typescript-eslint/naming-convention */
2222

23-
const mockBlocks = (
24-
mockServer: Mockttp,
25-
network: string = INFURA_BTC_MAINNET_URL,
26-
) => {
27-
if (network === INFURA_BTC_MAINNET_URL) {
28-
return mockServer.forGet(`${network}/esplora/blocks`).thenJson(200, [
29-
{
30-
id: '000000000000000000005645962b8ea026323dd3eb24a7ee39c60f285a259bc2',
31-
height: 918176,
32-
version: 566566912,
33-
timestamp: 1759922068,
34-
tx_count: 3575,
35-
size: 1681759,
36-
weight: 3993790,
37-
merkle_root:
38-
'7672f7b0d4a6d322ddbdb5b6aa1802352329862af3d116c3aedef2efc167c9e5',
39-
previousblockhash:
40-
'000000000000000000012dca96f7e4803c607e2f34d41a2a7ae8f4afb00ae1a7',
41-
mediantime: 1759919229,
42-
nonce: 3365112101,
43-
bits: 385998260,
44-
difficulty: 150839487445890.5,
45-
},
46-
{
47-
id: '000000000000000000012dca96f7e4803c607e2f34d41a2a7ae8f4afb00ae1a7',
48-
height: 918175,
49-
version: 1040187392,
50-
timestamp: 1759921295,
51-
tx_count: 3801,
52-
size: 1642907,
53-
weight: 3993518,
54-
merkle_root:
55-
'd4e542752e01040b575db060cbb3efc40800fefc234b102e196e90a11fe7b99d',
56-
previousblockhash:
57-
'00000000000000000000197762f3b38879ed3c4ed0e96d66d203879c51f9f27c',
58-
mediantime: 1759917935,
59-
nonce: 2255807358,
60-
bits: 385998260,
61-
difficulty: 150839487445890.5,
62-
},
63-
{
64-
id: '00000000000000000000197762f3b38879ed3c4ed0e96d66d203879c51f9f27c',
65-
height: 918174,
66-
version: 579330048,
67-
timestamp: 1759920849,
68-
tx_count: 3250,
69-
size: 1756669,
70-
weight: 3993331,
71-
merkle_root:
72-
'bfa30d00c8fd4c2537029f2880a63aad0dda5a0102f7017f73531d08704a23e6',
73-
previousblockhash:
74-
'00000000000000000000bfe2dfa7db0249854da123686810fc678b2e6e596576',
75-
mediantime: 1759917929,
76-
nonce: 1749894252,
77-
bits: 385998260,
78-
difficulty: 150839487445890.5,
79-
},
80-
]);
23+
const mockBlocks = (mockServer: Mockttp, network?: string) => {
24+
if (!network || INFURA_BTC_MAINNET_URL_PATTERN.test(network)) {
25+
return mockServer
26+
.forGet(
27+
/^https:\/\/bitcoin-mainnet\.infura\.io\/v3\/[a-f0-9]{32}\/esplora\/blocks$/u,
28+
)
29+
.thenJson(200, [
30+
{
31+
id: '000000000000000000005645962b8ea026323dd3eb24a7ee39c60f285a259bc2',
32+
height: 918176,
33+
version: 566566912,
34+
timestamp: 1759922068,
35+
tx_count: 3575,
36+
size: 1681759,
37+
weight: 3993790,
38+
merkle_root:
39+
'7672f7b0d4a6d322ddbdb5b6aa1802352329862af3d116c3aedef2efc167c9e5',
40+
previousblockhash:
41+
'000000000000000000012dca96f7e4803c607e2f34d41a2a7ae8f4afb00ae1a7',
42+
mediantime: 1759919229,
43+
nonce: 3365112101,
44+
bits: 385998260,
45+
difficulty: 150839487445890.5,
46+
},
47+
{
48+
id: '000000000000000000012dca96f7e4803c607e2f34d41a2a7ae8f4afb00ae1a7',
49+
height: 918175,
50+
version: 1040187392,
51+
timestamp: 1759921295,
52+
tx_count: 3801,
53+
size: 1642907,
54+
weight: 3993518,
55+
merkle_root:
56+
'd4e542752e01040b575db060cbb3efc40800fefc234b102e196e90a11fe7b99d',
57+
previousblockhash:
58+
'00000000000000000000197762f3b38879ed3c4ed0e96d66d203879c51f9f27c',
59+
mediantime: 1759917935,
60+
nonce: 2255807358,
61+
bits: 385998260,
62+
difficulty: 150839487445890.5,
63+
},
64+
{
65+
id: '00000000000000000000197762f3b38879ed3c4ed0e96d66d203879c51f9f27c',
66+
height: 918174,
67+
version: 579330048,
68+
timestamp: 1759920849,
69+
tx_count: 3250,
70+
size: 1756669,
71+
weight: 3993331,
72+
merkle_root:
73+
'bfa30d00c8fd4c2537029f2880a63aad0dda5a0102f7017f73531d08704a23e6',
74+
previousblockhash:
75+
'00000000000000000000bfe2dfa7db0249854da123686810fc678b2e6e596576',
76+
mediantime: 1759917929,
77+
nonce: 1749894252,
78+
bits: 385998260,
79+
difficulty: 150839487445890.5,
80+
},
81+
]);
8182
}
8283
return mockServer.forGet(`${network}/esplora/blocks`).thenJson(200, []);
8384
};
8485

85-
const mockFundingTx = (
86-
mockServer: Mockttp,
87-
network: string = INFURA_BTC_MAINNET_URL,
88-
) =>
86+
const mockFundingTx = (mockServer: Mockttp) =>
8987
mockServer
90-
.forGet(`${network}/scripthash/${FUNDING_SCRIPT_HASH}/txs`)
88+
.forGet(
89+
new RegExp(
90+
`^https:\\/\\/bitcoin-mainnet\\.infura\\.io\\/v3\\/[a-f0-9]{32}\\/scripthash\\/${FUNDING_SCRIPT_HASH}\\/txs$`,
91+
'u',
92+
),
93+
)
9194
.thenJson(200, [
9295
{
9396
txid: '6e7ebcc607a83f7cf5659fb6fb70433c775017092302630f027ca728b4d06aba',
@@ -178,18 +181,22 @@ const mockFundingTx = (
178181
const mockAnyTxs = (mockServer: Mockttp) =>
179182
mockServer
180183
.forGet(
181-
/^https:\/\/bitcoin-mainnet.infura.io\/v3\/5b98a22672004ef1bf40a80123c5c48d\/esplora\/scripthash\/[0-9a-f]{64}\/txs$/u,
184+
/^https:\/\/bitcoin-mainnet\.infura\.io\/v3\/[a-f0-9]{32}\/esplora\/scripthash\/[0-9a-f]{64}\/txs$/u,
182185
)
183186
.thenJson(200, []);
184187

185188
const mockBlockHeight = (
186189
mockServer: Mockttp,
187190
blockHeight: number,
188191
blockHash: string,
189-
network: string = INFURA_BTC_MAINNET_URL,
190192
) =>
191193
mockServer
192-
.forGet(`${network}/esplora/block-height/${blockHeight}`)
194+
.forGet(
195+
new RegExp(
196+
`^https:\\/\\/bitcoin-mainnet\\.infura\\.io\\/v3\\/[a-f0-9]{32}\\/esplora\\/block-height\\/${blockHeight}$`,
197+
'u',
198+
),
199+
)
193200
.thenReply(200, blockHash);
194201

195202
const mockFeeEstimates = (mockServer: Mockttp, network: string = ESPLORA_URL) =>
@@ -232,13 +239,13 @@ const mockFeeEstimates = (mockServer: Mockttp, network: string = ESPLORA_URL) =>
232239
*/
233240
export async function mockInitialFullScan(mockServer: Mockttp) {
234241
// Mock latest blocks
235-
await mockBlocks(mockServer, INFURA_BTC_MAINNET_URL);
242+
await mockBlocks(mockServer);
236243
// await mockBlocks(mockServer, ESPLORA_TESTNET_URL);
237244
// Mock the funding transaction setting the balance to default
238-
await mockFundingTx(mockServer, INFURA_BTC_MAINNET_URL);
245+
await mockFundingTx(mockServer);
239246
// await mockFundingTx(mockServer, ESPLORA_TESTNET_URL);
240247
// Mock funding tx block hash
241-
// await mockBlockHeight(mockServer, FUNDING_BLOCK_HEIGHT, FUNDING_BLOCK_HASH, INFURA_BTC_MAINNET_URL);
248+
// await mockBlockHeight(mockServer, FUNDING_BLOCK_HEIGHT, FUNDING_BLOCK_HASH);
242249
/* await mockBlockHeight(
243250
mockServer,
244251
FUNDING_BLOCK_HEIGHT,
@@ -248,12 +255,7 @@ export async function mockInitialFullScan(mockServer: Mockttp) {
248255
// Mock other calls to fetch txs given the stop gap (returns empty)
249256
await mockAnyTxs(mockServer);
250257
// Mock genesis block hash
251-
await mockBlockHeight(
252-
mockServer,
253-
0,
254-
GENESIS_BLOCK_HASH,
255-
INFURA_BTC_MAINNET_URL,
256-
);
258+
await mockBlockHeight(mockServer, 0, GENESIS_BLOCK_HASH);
257259

258260
// Mock fee estimates
259261
await mockFeeEstimates(mockServer);

0 commit comments

Comments
 (0)