Created
January 7, 2025 01:05
-
-
Save paullinator/a0642ee560b93c50281eab819951731f to your computer and use it in GitHub Desktop.
test blockbook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { asObject, asString, asNumber, asArray, asOptional } = require("cleaners"); | |
| // Base URL for the Blockbook server | |
| const BASE_URL = "https://btc-wusa1.edge.app"; | |
| // Validators using `cleaners` | |
| const asBlockbookStatus = asObject({ | |
| blockbook: asObject({ | |
| coin: asString, | |
| bestHeight: asNumber | |
| }), | |
| backend: asObject({ | |
| version: asString, | |
| }) | |
| }); | |
| const asBlockIndex = asObject({ | |
| blockHash: asString, | |
| }); | |
| const asBlockDetails = asObject({ | |
| hash: asString, | |
| height: asNumber, | |
| confirmations: asNumber, | |
| }); | |
| const asAddressDetails = asObject({ | |
| address: asString, | |
| txs: asNumber, | |
| unconfirmedBalance: asString, | |
| balance: asString, | |
| }); | |
| const asTransactionDetails = asObject({ | |
| txid: asString, | |
| blockHeight: asNumber, | |
| confirmations: asNumber, | |
| }); | |
| const asUTXOs = asArray( | |
| asObject({ | |
| txid: asString, | |
| vout: asNumber, | |
| value: asString, | |
| confirmations: asNumber, | |
| }) | |
| ); | |
| // Helper function to perform fetch and validation | |
| async function fetchAndValidate(url, validator, expectedStatus = 200) { | |
| const response = await fetch(url); | |
| if (response.status !== expectedStatus) { | |
| throw new Error(`Failed request to ${url}. Status: ${response.status}`); | |
| } | |
| const data = await response.json(); | |
| console.log(`Success: ${url}`); | |
| return validator(data); | |
| } | |
| async function runTests() { | |
| try { | |
| // Test: Get Blockbook status | |
| const statusData = await fetchAndValidate(`${BASE_URL}/api/v2`, asBlockbookStatus); | |
| if (statusData.blockbook.coin !== "Bitcoin") { | |
| throw new Error(`Unexpected coin: ${statusData.blockbook.coin}`); | |
| } | |
| // Test: Get block hash for a specific block height | |
| const blockHeight = 680000; // Example block height | |
| const blockIndexData = await fetchAndValidate( | |
| `${BASE_URL}/api/v2/block-index/${blockHeight}`, | |
| asBlockIndex | |
| ); | |
| // Test: Get block details by block hash | |
| const blockHash = blockIndexData.blockHash; | |
| const blockData = await fetchAndValidate(`${BASE_URL}/api/v2/block/${blockHash}`, asBlockDetails); | |
| if (blockData.hash !== blockHash) { | |
| throw new Error(`Block hash mismatch: Expected ${blockHash}, got ${blockData.hash}`); | |
| } | |
| // Test: Get address details | |
| const address = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"; // Example Bitcoin address | |
| const addressData = await fetchAndValidate(`${BASE_URL}/api/v2/address/${address}`, asAddressDetails); | |
| if (addressData.address !== address) { | |
| throw new Error(`Address mismatch: Expected ${address}, got ${addressData.address}`); | |
| } | |
| // Test: Get transaction details | |
| const txid = "8eed578779acc589ecd5465e585f084440ae33f8d0e0be4e12b0f853d7686070"; // Example transaction ID | |
| const txData = await fetchAndValidate(`${BASE_URL}/api/v2/tx/${txid}`, asTransactionDetails); | |
| if (txData.txid !== txid) { | |
| throw new Error(`Transaction ID mismatch: Expected ${txid}, got ${txData.txid}`); | |
| } | |
| // Test: Get UTXOs for an address | |
| const utxosData = await fetchAndValidate(`${BASE_URL}/api/v2/utxo/${address}`, asUTXOs); | |
| if (!Array.isArray(utxosData) || utxosData.length === 0) { | |
| console.warn(`No UTXOs found for address ${address}`); | |
| } | |
| console.log("All tests passed successfully!"); | |
| } catch (error) { | |
| console.error(`Test failed: ${error.message}`); | |
| process.exit(1); // Exit with non-zero status code on failure | |
| } | |
| } | |
| runTests(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment