v2.2.0 - new major version for mempool-js (#2)

* - Refactoring code.
- Refactoring folder structure.
- Adding apiEndpoint and websocketEndpoint to Mempool config.
- Adding brownserify feature.
- Adding MIT LICENSE

* - Changing package.json information.
- Reorganizing README.md information.
- Default export for CommonJs and ES6 Modules.
- Changing default variable to mempoolJS.
- Organizing the API and WS providers.
- Splitting websocket connection types: client and server.

* Change version to 2.2.0.
Reorder keywords in alphabetical order.
This commit is contained in:
Miguel Medeiros 2021-04-08 10:15:30 -03:00 committed by GitHub
parent 3bfae54069
commit f39361263a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 5581 additions and 1386 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
/node_modules
.env
/lib

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Miguel Medeiros
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

781
README.md

File diff suppressed because it is too large Load Diff

2482
dist/mempool.js vendored Normal file

File diff suppressed because it is too large Load Diff

1
dist/mempool.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,22 +0,0 @@
import { addresses } from '../src/index';
const init = async () => {
const address =
'15e10745f15593a899cef391191bdd3d7c12412cc4696b7bcb669d0feadc8521';
const addressTest = await addresses.getAddress({ address });
console.log(addressTest);
const addressTxs = await addresses.getAddressTxs({ address });
console.log(addressTxs);
const addressTxsChain = await addresses.getAddressTxsChain({ address });
console.log(addressTxsChain);
const addressTxsMempool = await addresses.getAddressTxsMempool({ address });
console.log(addressTxsMempool);
const addressTxsUtxo = await addresses.getAddressTxsUtxo({ address });
console.log(addressTxsUtxo);
};
init();

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="./../../dist/mempool.js"></script>
<script>
const init = async () => {
const { addresses } = mempoolJS();
const address = '1wizSAYSbuyXbt9d8JV8ytm5acqq2TorC';
const addressTest = await addresses.getAddress(address);
console.log(addressTest);
const addressTxs = await addresses.getAddressTxs(address);
console.log(addressTxs);
const addressTxsChain = await addresses.getAddressTxsChain(address);
console.log(addressTxsChain);
const addressTxsMempool = await addresses.getAddressTxsMempool(address);
console.log(addressTxsMempool);
const addressTxsUtxo = await addresses.getAddressTxsUtxo(address);
console.log(addressTxsUtxo);
};
init();
</script>
</head>
<body></body>
</html>

47
examples/html/blocks.html Normal file
View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="./../../dist/mempool.js"></script>
<script>
const init = async () => {
const { blocks } = mempoolJS();
const hash =
'000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce';
const block = await blocks.getBlock(hash);
console.log(block);
const blockStatus = await blocks.getBlockStatus(hash);
console.log(blockStatus);
const blockTxs = await blocks.getBlockTxs({ hash });
console.log(blockTxs);
const blockTxids = await blocks.getBlockTxids(hash);
console.log(blockTxids);
const blockTxid = await blocks.getBlockTxid({ hash, index: 218 });
console.log(blockTxid);
const blockRaw = await blocks.getBlockRaw(hash);
console.log(blockRaw);
const blockHeight = await blocks.getBlockHeight(9999);
console.log(blockHeight);
const getBlocks = await blocks.getBlocks({ start_height: 9999 });
console.log(getBlocks);
const blocksTipHeight = await blocks.getBlocksTipHeight();
console.log(blocksTipHeight);
const blocksTipHash = await blocks.getBlocksTipHash();
console.log(blocksTipHash);
};
init();
</script>
</head>
<body></body>
</html>

20
examples/html/fees.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="./../../dist/mempool.js"></script>
<script>
const init = async () => {
const { fees } = mempoolJS();
const feesRecommended = await fees.getFeesRecommended();
console.log(feesRecommended);
const feesMempoolBlocks = await fees.getFeesMempoolBlocks();
console.log(feesMempoolBlocks);
};
init();
</script>
</head>
<body></body>
</html>

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="./../../dist/mempool.js"></script>
<script>
const init = async () => {
const { mempool } = mempoolJS();
const getMempool = await mempool.getMempool();
console.log(getMempool);
const getMempoolRecent = await mempool.getMempoolRecent();
console.log(getMempoolRecent);
const getMempoolTxids = await mempool.getMempoolTxids();
console.log(getMempoolTxids);
};
init();
</script>
</head>
<body></body>
</html>

View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="./../../dist/mempool.js"></script>
<script>
const init = async () => {
const { transactions } = mempoolJS();
const txid =
'15e10745f15593a899cef391191bdd3d7c12412cc4696b7bcb669d0feadc8521';
const tx = await transactions.getTx(txid);
console.log(tx);
const txStatus = await transactions.getTxStatus(txid);
console.log(txStatus);
const txHex = await transactions.getTxHex(txid);
console.log(txHex);
const txRaw = await transactions.getTxRaw(txid);
console.log(txRaw);
const txMerkleBlockProof = await transactions.getTxMerkleBlockProof(
txid
);
console.log(txMerkleBlockProof);
const txMerkleProof = await transactions.getTxMerkleProof(txid);
console.log(txMerkleProof);
const txOutspend = await transactions.getTxOutspend({
txid,
vout: 3,
});
console.log(txOutspend);
const txOutspends = await transactions.getTxOutspends(txid);
console.log(txOutspends);
// const postTx = await transactions.postTx(txid);
// console.log(postTx);
};
init();
</script>
</head>
<body></body>
</html>

View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="./../../dist/mempool.js"></script>
<script>
const init = async () => {
const { websocket } = mempoolJS();
const ws = await websocket.initClient({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.addEventListener('message', function incoming({ data }) {
const res = JSON.parse(data);
if (res.blocks) {
res.blocks.forEach((block) => {
console.log(block.height);
});
}
if (res.mempoolInfo) {
console.log(res.mempoolInfo);
}
if (res.transactions) {
console.log(res.transactions);
}
if (res.mempoolBlocks) {
console.log(res.mempoolBlocks);
}
});
};
init();
</script>
</head>
<body></body>
</html>

View File

@ -0,0 +1,23 @@
import mempoolJS from '../../src/index';
const init = async () => {
const { addresses } = mempoolJS();
const address = '1wizSAYSbuyXbt9d8JV8ytm5acqq2TorC';
const addressTest = await addresses.getAddress(address);
console.log(addressTest);
const addressTxs = await addresses.getAddressTxs(address);
console.log(addressTxs);
const addressTxsChain = await addresses.getAddressTxsChain(address);
console.log(addressTxsChain);
const addressTxsMempool = await addresses.getAddressTxsMempool(address);
console.log(addressTxsMempool);
const addressTxsUtxo = await addresses.getAddressTxsUtxo(address);
console.log(addressTxsUtxo);
};
init();

View File

@ -1,33 +1,33 @@
import { blocks } from '../src/index';
import mempoolJS from '../../src/index';
const init = async () => {
const { blocks } = mempoolJS();
const hash =
'000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce';
const block = await blocks.getBlock({ hash });
const block = await blocks.getBlock(hash);
console.log(block);
const blockStatus = await blocks.getBlockStatus({ hash });
const blockStatus = await blocks.getBlockStatus(hash);
console.log(blockStatus);
const blockTxs = await blocks.getBlockTxs({ hash });
console.log(blockTxs);
const blockTxids = await blocks.getBlockTxids({ hash });
const blockTxids = await blocks.getBlockTxids(hash);
console.log(blockTxids);
const blockTxid = await blocks.getBlockTxid({ hash, index: 218 });
console.log(blockTxid);
const blockRaw = await blocks.getBlockRaw({ hash });
const blockRaw = await blocks.getBlockRaw(hash);
console.log(blockRaw);
const blockHeight = await blocks.getBlockHeight({
start_height: 66666,
});
const blockHeight = await blocks.getBlockHeight(9999);
console.log(blockHeight);
const getBlocks = await blocks.getBlocks({ start_height: 66666 });
const getBlocks = await blocks.getBlocks({ start_height: 9999 });
console.log(getBlocks);
const blocksTipHeight = await blocks.getBlocksTipHeight();

View File

@ -1,6 +1,8 @@
import { fees } from './../src/index';
import mempoolJS from '../../src/index';
const init = async () => {
const { fees } = mempoolJS();
const feesRecommended = await fees.getFeesRecommended();
console.log(feesRecommended);

View File

@ -1,6 +1,8 @@
import { mempool } from '../src/index';
import mempoolJS from '../../src/index';
const init = async () => {
const { mempool } = mempoolJS();
const getMempool = await mempool.getMempool();
console.log(getMempool);

View File

@ -0,0 +1,39 @@
import mempoolJS from '../../src/index';
const init = async () => {
const { transactions } = mempoolJS();
const txid =
'15e10745f15593a899cef391191bdd3d7c12412cc4696b7bcb669d0feadc8521';
const tx = await transactions.getTx(txid);
console.log(tx);
const txStatus = await transactions.getTxStatus(txid);
console.log(txStatus);
const txHex = await transactions.getTxHex(txid);
console.log(txHex);
const txRaw = await transactions.getTxRaw(txid);
console.log(txRaw);
const txMerkleBlockProof = await transactions.getTxMerkleBlockProof(txid);
console.log(txMerkleBlockProof);
const txMerkleProof = await transactions.getTxMerkleProof(txid);
console.log(txMerkleProof);
const txOutspend = await transactions.getTxOutspend({
txid,
vout: 3,
});
console.log(txOutspend);
const txOutspends = await transactions.getTxOutspends(txid);
console.log(txOutspends);
// const postTx = await transactions.postTx(txid);
// console.log(postTx);
};
init();

View File

@ -1,14 +1,17 @@
import { websocket } from '../src/index';
/* eslint-disable @typescript-eslint/no-explicit-any */
import mempoolJS from '../../src/index';
const init = async () => {
const ws = await websocket.init({
const { websocket } = mempoolJS();
const ws = websocket.initServer({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data: any) {
const res = JSON.parse(data);
ws.on('message', function incoming(data) {
const res = JSON.parse(data.toString());
if (res.blocks) {
res.blocks.forEach((block: any) => {
res.blocks.forEach((block: { height: any }) => {
console.log(block.height);
});
}

View File

@ -1,39 +0,0 @@
import { transactions } from '../src/index';
const init = async () => {
const txid =
'15e10745f15593a899cef391191bdd3d7c12412cc4696b7bcb669d0feadc8521';
const tx = await transactions.getTx({ txid });
console.log(tx);
const txStatus = await transactions.getTxStatus({ txid });
console.log(txStatus);
const txHex = await transactions.getTxHex({ txid });
console.log(txHex);
const txRaw = await transactions.getTxRaw({ txid });
console.log(txRaw);
const txMerkleBlockProof = await transactions.getTxMerkleBlockProof({
txid,
});
console.log(txMerkleBlockProof);
const txMerkleProof = await transactions.getTxMerkleProof({ txid });
console.log(txMerkleProof);
const txOutspend = await transactions.getTxOutspend({
txid,
vout: 3,
});
console.log(txOutspend);
const txOutspends = await transactions.getTxOutspends({ txid });
console.log(txOutspends);
const postTx = await transactions.postTx({ txid });
console.log(postTx);
};
init();

View File

@ -1,43 +1,52 @@
{
"name": "mempool-space-js",
"version": "1.1.2",
"description": "NPM Package for Mempool.Space API.",
"name": "@mempool/mempool-js",
"version": "2.2.0",
"description": "NPM Package for Mempool JS API.",
"main": "lib/index.js",
"author": "MiguelMedeiros\\Miguel Medeiros <miguel@miguelmedeiros.com.br>",
"email": "miguel@miguelmedeiros.com.br",
"url": "http://miguelmedeiros.com.br",
"keywords": [
"axios",
"bitcoin",
"blockchain",
"html",
"mempool-space",
"mempool-js",
"mempool",
"nodejs",
"typescript"
],
"author": "Miguel Medeiros <contact@miguelmedeiros.com.br> (miguelmedeiros.com.br)",
"url": "https://mempool.space/",
"private": false,
"license": "MIT",
"repository": "github:MiguelMedeiros/mempool-space-js",
"repository": {
"type": "git",
"url": "git://github.com/mempool/mempool-js.git"
},
"types": "lib/index.d.ts",
"scripts": {
"start": "ts-node src/index.ts",
"dev": "nodemon src/index.ts",
"build": "tsc",
"build": "tsc | browserify lib/index.js --standalone mempoolJS > dist/mempool.js | browserify -p tinyify lib/index.js -o dist/mempool.min.js ",
"prepare": "npm run build",
"postversion": "git push && git push --tags"
},
"files": [
"lib/**/*"
],
"keywords": [
"bitcoin",
"mempool-space",
"mempool",
"blockchain",
"typescript",
"axios"
],
"dependencies": {
"axios": "^0.21.1",
"typescript": "^4.1.3",
"ws": "^7.4.3"
},
"devDependencies": {
"@types/node": "^14.14.25",
"@types/websocket": "^1.0.2",
"@types/ws": "^7.4.1",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"browserify": "^17.0.0",
"eslint": "^7.19.0",
"nodemon": "^2.0.7"
}
"nodemon": "^2.0.7",
"tinyify": "^3.0.0",
"typescript": "^4.1.3"
},
"license": "MIT"
}

View File

@ -1,166 +0,0 @@
import api from './api';
const getAddress = async (params: { address: string }) => {
return api
.get(`/address/${params.address}`)
.then(
(res: {
data: {
address: string;
chain_stats: {
funded_txo_count: number;
funded_txo_sum: number;
spent_txo_count: number;
spent_txo_sum: number;
tx_count: number;
};
mempool_stats: {
funded_txo_count: number;
funded_txo_sum: number;
spent_txo_count: number;
spent_txo_sum: number;
tx_count: number;
};
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getAddressTxs = async (params: { address: string }) => {
return api
.get(`/address/${params.address}/txs`)
.then(
(res: {
data: {
txid: string;
version: number;
locktime: number;
vin: Record<string, unknown>[];
vout: Record<string, unknown>[];
size: number;
weight: number;
fee: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getAddressTxsChain = async (params: { address: string }) => {
return api
.get(`/address/${params.address}/txs/chain`)
.then(
(res: {
data: {
txid: string;
version: number;
locktime: number;
vin: Record<string, unknown>[];
vout: Record<string, unknown>[];
size: number;
weight: number;
fee: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getAddressTxsMempool = async (params: { address: string }) => {
return api
.get(`/address/${params.address}/txs/mempool`)
.then((res: { data: any }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getAddressTxsUtxo = async (params: { address: string }) => {
return api
.get(`/address/${params.address}/utxo`)
.then(
(res: {
data: {
txid: string;
vout: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
value: number;
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
export default {
getAddress,
getAddressTxs,
getAddressTxsChain,
getAddressTxsMempool,
getAddressTxsUtxo,
};

View File

@ -1,4 +0,0 @@
import axios from 'axios';
export default axios.create({
baseURL: 'https://mempool.space/api/',
});

39
src/app/addresses.ts Normal file
View File

@ -0,0 +1,39 @@
import { AxiosInstance } from 'axios';
import { Address, AddressTxsUtxo, Tx, AddressInstance } from '../interfaces';
export const useAddresses = (api: AxiosInstance): AddressInstance => {
const getAddress = async (address: string) => {
const { data } = await api.get<Address>(`/address/${address}`);
return data;
};
const getAddressTxs = async (address: string) => {
const { data } = await api.get<Tx[]>(`/address/${address}/txs`);
return data;
};
const getAddressTxsChain = async (address: string) => {
const { data } = await api.get<Tx[]>(`/address/${address}/txs/chain`);
return data;
};
const getAddressTxsMempool = async (address: string) => {
const { data } = await api.get<Tx[]>(`/address/${address}/txs/mempool`);
return data;
};
const getAddressTxsUtxo = async (address: string) => {
const { data } = await api.get<AddressTxsUtxo[]>(
`/address/${address}/utxo`
);
return data;
};
return {
getAddress,
getAddressTxs,
getAddressTxsChain,
getAddressTxsMempool,
getAddressTxsUtxo,
};
};

74
src/app/blocks.ts Normal file
View File

@ -0,0 +1,74 @@
import { AxiosInstance } from 'axios';
import { Block, BlockStatus, BlockInstance, Tx } from '../interfaces';
export const useBlocks = (api: AxiosInstance): BlockInstance => {
const getBlock = async (hash: string) => {
const { data } = await api.get<Block>(`/block/${hash}`);
return data;
};
const getBlockStatus = async (hash: string) => {
const { data } = await api.get<BlockStatus>(`/block/${hash}/status`);
return data;
};
const getBlockTxs = async (params: {
hash: string;
start_index?: number;
}) => {
const { data } = await api.get<Tx>(
`/block/${params.hash}/txs/${params.start_index}`
);
return data;
};
const getBlockTxids = async (hash: string) => {
const { data } = await api.get<string[]>(`/block/${hash}/txids`);
return data;
};
const getBlockTxid = async (params: { hash: string; index: number }) => {
const { data } = await api.get<string>(
`/block/${params.hash}/txid/${params.index}`
);
return data;
};
const getBlockRaw = async (hash: string) => {
const { data } = await api.get<string>(`/block/${hash}/raw`);
return data;
};
const getBlockHeight = async (height: number) => {
const { data } = await api.get<string>(`/block-height/${height}`);
return data;
};
const getBlocks = async (params: { start_height?: number }) => {
const { data } = await api.get<Block>(`/blocks/${params.start_height}`);
return data;
};
const getBlocksTipHeight = async () => {
const { data } = await api.get<number>(`/blocks/tip/height`);
return data;
};
const getBlocksTipHash = async () => {
const { data } = await api.get<string>(`/blocks/tip/hash`);
return data;
};
return {
getBlock,
getBlocks,
getBlockStatus,
getBlockTxs,
getBlockTxid,
getBlockTxids,
getBlockRaw,
getBlockHeight,
getBlocksTipHash,
getBlocksTipHeight,
};
};

21
src/app/fees.ts Normal file
View File

@ -0,0 +1,21 @@
import { AxiosInstance } from 'axios';
import { FeesRecommended, FeesMempoolBlocks, FeeInstance } from '../interfaces';
export const useFees = (api: AxiosInstance): FeeInstance => {
const getFeesRecommended = async () => {
const { data } = await api.get<FeesRecommended>(`/v1/fees/recommended`);
return data;
};
const getFeesMempoolBlocks = async () => {
const { data } = await api.get<FeesMempoolBlocks[]>(
`/v1/fees/mempool-blocks`
);
return data;
};
return {
getFeesRecommended,
getFeesMempoolBlocks,
};
};

25
src/app/mempool.ts Normal file
View File

@ -0,0 +1,25 @@
import { AxiosInstance } from 'axios';
import { Mempool, MempoolRecent, MempoolInstance } from '../interfaces';
export const useMempool = (api: AxiosInstance): MempoolInstance => {
const getMempool = async () => {
const { data } = await api.get<Mempool[]>(`/mempool`);
return data;
};
const getMempoolTxids = async () => {
const { data } = await api.get<string[]>(`/mempool/txids`);
return data;
};
const getMempoolRecent = async () => {
const { data } = await api.get<MempoolRecent[]>(`/mempool/recent`);
return data;
};
return {
getMempool,
getMempoolTxids,
getMempoolRecent,
};
};

71
src/app/transactions.ts Normal file
View File

@ -0,0 +1,71 @@
import { AxiosInstance } from 'axios';
import {
Tx,
TxStatus,
TxMerkleProof,
TxOutspend,
TxInstance,
} from '../interfaces';
export const useTransactions = (api: AxiosInstance): TxInstance => {
const getTx = async (txid: string) => {
const { data } = await api.get<Tx>(`/tx/${txid}`);
return data;
};
const getTxStatus = async (txid: string) => {
const { data } = await api.get<TxStatus>(`/tx/${txid}/status`);
return data;
};
const getTxHex = async (txid: string) => {
const { data } = await api.get<string>(`/tx/${txid}/hex`);
return data;
};
const getTxRaw = async (txid: string) => {
const { data } = await api.get<string>(`/tx/${txid}/raw`);
return data;
};
const getTxMerkleBlockProof = async (txid: string) => {
const { data } = await api.get<string>(`/tx/${txid}/merkleblock-proof`);
return data;
};
const getTxMerkleProof = async (txid: string) => {
const { data } = await api.get<Array<TxMerkleProof>>(
`/tx/${txid}/merkle-proof`
);
return data;
};
const getTxOutspend = async (params: { txid: string; vout: number }) => {
const { data } = await api.get<TxOutspend>(
`/tx/${params.txid}/outspend/${params.vout}`
);
return data;
};
const getTxOutspends = async (txid: string) => {
const { data } = await api.get<Array<TxOutspend>>(`/tx/${txid}/outspends`);
return data;
};
const postTx = async (txid: string) => {
const { data } = await api.post<string>(`/tx`, { txid: txid });
return data;
};
return {
getTx,
getTxStatus,
getTxHex,
getTxRaw,
getTxMerkleBlockProof,
getTxMerkleProof,
getTxOutspend,
getTxOutspends,
postTx,
};
};

14
src/app/websocket.ts Normal file
View File

@ -0,0 +1,14 @@
import { WsInterface, WsInstance } from '../interfaces';
import wsClient from '../services/wsClient';
import wsServer from '../services/wsServer';
const defaultWs = 'wss://mempool.space/api/v1/ws';
export const useWebsocket = (websocketEndpoint?: string): WsInstance => {
return {
initClient: ({ options }: WsInterface) =>
wsClient(options, defaultWs, websocketEndpoint),
initServer: ({ options }: WsInterface) =>
wsServer(options, defaultWs, websocketEndpoint),
};
};

View File

@ -1,247 +0,0 @@
import api from './api';
const getBlock = async (params: { hash: string }) => {
return api
.get(`/block/${params.hash}`)
.then(
(res: {
data: {
id: string;
height: number;
version: number;
timestamp: number;
tx_count: number;
size: number;
weight: number;
merkle_root: string;
previousblockhash: string;
mediantime: number;
nonce: number;
bits: number;
difficulty: number;
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlockStatus = async (params: { hash: string }) => {
return api
.get(`/block/${params.hash}/status`)
.then(
(res: {
data: {
in_best_chain: boolean;
height: number;
next_best: string;
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlockTxs = async (params: { hash: string; start_index?: number }) => {
return api
.get(`/block/${params.hash}/txs/${params.start_index}`)
.then(
(res: {
data: {
txid: string;
version: number;
locktime: number;
vin: Record<string, unknown>[];
vout: Record<string, unknown>[][];
size: number;
weight: number;
fee: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlockTxids = async (params: { hash: string }) => {
return api
.get(`/block/${params.hash}/txids`)
.then((res: { data: string[] }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlockTxid = async (params: { hash: string; index: number }) => {
return api
.get(`/block/${params.hash}/txid/${params.index}`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlockRaw = async (params: { hash: string }) => {
return api
.get(`/block/${params.hash}/raw`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlockHeight = async (params: { height: number }) => {
return api
.get(`/block-height/${params.height}`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlocks = async (params: { start_height?: number }) => {
return api
.get(`/blocks/${params.start_height}`)
.then(
(res: {
data: {
id: string;
height: number;
version: number;
timestamp: number;
tx_count: number;
size: number;
weight: number;
merkle_root: string;
previousblockhash: string;
mediantime: number;
nonce: number;
bits: number;
difficulty: number;
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlocksTipHeight = async () => {
return api
.get(`/blocks/tip/height`)
.then((res: { data: number }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getBlocksTipHash = async () => {
return api
.get(`/blocks/tip/hash`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
export default {
getBlock,
getBlocks,
getBlockStatus,
getBlockTxs,
getBlockTxid,
getBlockTxids,
getBlockRaw,
getBlockHeight,
getBlocksTipHash,
getBlocksTipHeight,
};

View File

@ -1,59 +0,0 @@
import api from './api';
const getFeesRecommended = async () => {
return await api
.get(`/v1/fees/recommended`)
.then(
(res: {
data: {
fastestFee: number;
halfHourFee: number;
hourFee: number;
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getFeesMempoolBlocks = async () => {
return await api
.get(`/v1/fees/mempool-blocks`)
.then(
(res: {
data: {
blockSize: number;
blockVSize: number;
nTx: number;
totalFees: number;
medianFee: number;
feeRange: number[];
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
export default {
getFeesRecommended,
getFeesMempoolBlocks,
};

View File

@ -1,22 +1,33 @@
import fees from './fees';
import mempool from './mempool';
import blocks from './blocks';
import transactions from './transactions';
import addresses from './addresses';
import websocket from './websocket';
import { MempoolConfig, MempoolReturn } from './interfaces';
import { makeAPI } from './services/api';
export { default as fees } from './fees';
export { default as mempool } from './mempool';
export { default as blocks } from './blocks';
export { default as transactions } from './transactions';
export { default as addresses } from './addresses';
export { default as websocket } from './websocket';
import { useAddresses } from './app/addresses';
import { useBlocks } from './app/blocks';
import { useFees } from './app/fees';
import { useMempool } from './app/mempool';
import { useTransactions } from './app/transactions';
import { useWebsocket } from './app/websocket';
export default {
fees,
mempool,
blocks,
transactions,
addresses,
websocket,
const apiEndpointDefault = 'https://mempool.space/api/';
const websocketEndpointDefault = 'wss://mempool.space/api/v1/ws';
const mempool = (
{ apiEndpoint, websocketEndpoint }: MempoolConfig = {
apiEndpoint: apiEndpointDefault,
websocketEndpoint: websocketEndpointDefault,
}
): MempoolReturn => {
const { api } = makeAPI(apiEndpoint);
return {
addresses: useAddresses(api),
blocks: useBlocks(api),
fees: useFees(api),
mempool: useMempool(api),
transactions: useTransactions(api),
websocket: useWebsocket(websocketEndpoint),
};
};
mempool.default = mempool;
export = mempool;

213
src/interfaces.ts Normal file
View File

@ -0,0 +1,213 @@
import WebSocketServer from 'ws';
export interface Address {
address: string;
chain_stats: {
funded_txo_count: number;
funded_txo_sum: number;
spent_txo_count: number;
spent_txo_sum: number;
tx_count: number;
};
mempool_stats: {
funded_txo_count: number;
funded_txo_sum: number;
spent_txo_count: number;
spent_txo_sum: number;
tx_count: number;
};
}
export interface AddressInstance {
getAddress: (address: string) => Promise<Address>;
getAddressTxs: (address: string) => Promise<Tx[]>;
getAddressTxsChain: (address: string) => Promise<Tx[]>;
getAddressTxsMempool: (address: string) => Promise<Tx[]>;
getAddressTxsUtxo: (address: string) => Promise<AddressTxsUtxo[]>;
}
export interface AddressTxsUtxo {
txid: string;
vout: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
value: number;
}
export interface Block {
id: string;
height: number;
version: number;
timestamp: number;
tx_count: number;
size: number;
weight: number;
merkle_root: string;
previousblockhash: string;
mediantime: number;
nonce: number;
bits: number;
difficulty: number;
}
export interface BlockInstance {
getBlock: (hash: string) => Promise<Block>;
getBlocks: (params: { start_height?: number }) => Promise<Block>;
getBlockStatus: (hash: string) => Promise<BlockStatus>;
getBlockTxs: (params: { hash: string; start_index?: number }) => Promise<Tx>;
getBlockTxids: (hash: string) => Promise<string[]>;
getBlockTxid: (params: { hash: string; index: number }) => Promise<string>;
getBlockRaw: (hash: string) => Promise<string>;
getBlockHeight: (height: number) => Promise<string>;
getBlocksTipHeight: () => Promise<number>;
getBlocksTipHash: () => Promise<string>;
}
export interface BlockStatus {
in_best_chain: boolean;
height: number;
next_best: string;
}
export interface FeeInstance {
getFeesRecommended: () => Promise<FeesRecommended>;
getFeesMempoolBlocks: () => Promise<FeesMempoolBlocks[]>;
}
export interface FeesMempoolBlocks {
blockSize: number;
blockVSize: number;
nTx: number;
totalFees: number;
medianFee: number;
feeRange: number[];
}
export interface FeesRecommended {
fastestFee: number;
halfHourFee: number;
hourFee: number;
minimumFee: number;
}
export interface Mempool {
count: number;
vsize: number;
total_fee: number;
fee_histogram: number[];
}
export interface MempoolConfig {
apiEndpoint?: string;
websocketEndpoint?: string;
}
export interface MempoolInstance {
getMempool: () => Promise<Mempool[]>;
getMempoolTxids: () => Promise<string[]>;
getMempoolRecent: () => Promise<MempoolRecent[]>;
}
export interface MempoolReturn {
addresses: AddressInstance;
blocks: BlockInstance;
fees: FeeInstance;
mempool: MempoolInstance;
transactions: TxInstance;
websocket: WsInstance;
}
export interface MempoolRecent {
txid: string;
fee: number;
vsize: number;
value: number;
}
export interface Tx {
txid: string;
version: number;
locktime: number;
vin: {
txid: string;
vout: number;
prevout: {
scriptpubkey: string;
scriptpubkey_asm: string;
scriptpubkey_type: string;
scriptpubkey_address: string;
value: number;
};
scriptsig: string;
scriptsig_asm: string;
is_coinbase: boolean;
sequence: string;
}[];
vout: {
scriptpubkey: string;
scriptpubkey_asm: string;
scriptpubkey_type: string;
scriptpubkey_address: string;
value: number;
}[];
size: number;
weight: number;
fee: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
}
export interface TxStatus {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
}
export interface TxMerkleProof {
block_height: number;
merkle: string[];
pos: number;
}
export interface TxOutspend {
spent: boolean;
txid: string;
vin: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
}
export interface TxInstance {
getTx: (txid: string) => Promise<Tx>;
getTxStatus: (txid: string) => Promise<TxStatus>;
getTxHex: (txid: string) => Promise<string>;
getTxRaw: (txid: string) => Promise<string>;
getTxMerkleBlockProof: (txid: string) => Promise<string>;
getTxMerkleProof: (txid: string) => Promise<Array<TxMerkleProof>>;
getTxOutspend: (params: {
txid: string;
vout: number;
}) => Promise<TxOutspend>;
getTxOutspends: (txid: string) => Promise<Array<TxOutspend>>;
postTx: (txid: string) => Promise<unknown>;
}
export interface WsInterface {
options: string[];
}
export interface WsInstance {
initClient: ({ options }: WsInterface) => WebSocket;
initServer: ({ options }: WsInterface) => WebSocketServer;
}

View File

@ -1,76 +0,0 @@
import api from './api';
const getMempool = async () => {
return api
.get(`/mempool`)
.then(
(res: {
data: {
count: number;
vsize: number;
total_fee: number;
fee_histogram: number[];
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getMempoolTxids = async () => {
return api
.get(`/mempool/txids`)
.then((res: { data: string[] }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getMempoolRecent = async () => {
return api
.get(`/mempool/recent`)
.then(
(res: {
data: {
txid: string;
fee: number;
vsize: number;
value: number;
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
export default {
getMempool,
getMempoolTxids,
getMempoolRecent,
};

10
src/services/api.ts Normal file
View File

@ -0,0 +1,10 @@
import axios, { AxiosInstance } from 'axios';
export const makeAPI = (apiEndpoint?: string): { api: AxiosInstance } => {
const api = axios.create({
baseURL: apiEndpoint,
});
return {
api,
};
};

25
src/services/wsClient.ts Normal file
View File

@ -0,0 +1,25 @@
const browserWS = (
options: string[],
defaultWs: string,
websocketEndpoint?: string
): WebSocket => {
const ws = new WebSocket(websocketEndpoint || defaultWs);
ws.addEventListener('open', function open() {
handleMessage(ws, options);
});
return ws;
};
const handleMessage = (ws: WebSocket, options: string[]) => {
ws.send(JSON.stringify({ action: 'init' }));
setInterval(function timeout() {
ws.send(
JSON.stringify({
action: 'want',
data: options,
})
);
}, 500);
};
export default browserWS;

26
src/services/wsServer.ts Normal file
View File

@ -0,0 +1,26 @@
import WebSocket from 'ws';
const serverWS = (
options: string[],
defaultWs: string,
websocketEndpoint?: string
): WebSocket => {
const ws = new WebSocket(websocketEndpoint || defaultWs);
ws.on('open', function open() {
handleMessage(ws, options);
});
return ws;
};
const handleMessage = (ws: WebSocket, options: string[]) => {
ws.send(JSON.stringify({ action: 'init' }));
setInterval(function timeout() {
ws.send(
JSON.stringify({
action: 'want',
data: options,
})
);
}, 500);
};
export default serverWS;

View File

@ -1,250 +0,0 @@
import api from './api';
const getTx = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}`)
.then(
(res: {
data: {
txid: string;
version: number;
locktime: number;
vin: {
txid: string;
vout: number;
prevout: {
scriptpubkey: string;
scriptpubkey_asm: string;
scriptpubkey_type: string;
scriptpubkey_address: string;
value: number;
};
scriptsig: string;
scriptsig_asm: string;
is_coinbase: boolean;
sequence: string;
}[];
vout: {
scriptpubkey: string;
scriptpubkey_asm: string;
scriptpubkey_type: string;
scriptpubkey_address: string;
value: number;
}[];
size: number;
weight: number;
fee: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxStatus = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}/status`)
.then(
(res: {
data: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxHex = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}/hex`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxRaw = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}/raw`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxMerkleBlockProof = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}/merkleblock-proof`)
.then((res: { data: string }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxMerkleProof = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}/merkle-proof`)
.then(
(res: {
data: {
block_height: number;
merkle: string[];
pos: number;
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxOutspend = async (params: { txid: string; vout: number }) => {
return api
.get(`/tx/${params.txid}/outspend/${params.vout}`)
.then(
(res: {
data: {
spent: boolean;
txid: string;
vin: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
};
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const getTxOutspends = async (params: { txid: string }) => {
return api
.get(`/tx/${params.txid}/outspends`)
.then(
(res: {
data: {
spent: boolean;
txid: string;
vin: number;
status: {
confirmed: boolean;
block_height: number;
block_hash: string;
block_time: number;
};
}[];
}) => {
return res.data;
}
)
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
const postTx = async (params: { txid: string }) => {
return api
.post(`/tx`, { txid: params.txid })
.then((res: { data: any }) => {
return res.data;
})
.catch(
(err: {
response: {
data: string;
};
}) => {
throw err.response.data;
}
);
};
export default {
getTx,
getTxStatus,
getTxHex,
getTxRaw,
getTxMerkleBlockProof,
getTxMerkleProof,
getTxOutspend,
getTxOutspends,
postTx,
};

View File

@ -1,22 +0,0 @@
const WebSocket = require('ws');
const ws = new WebSocket('wss://mempool.space/api/v1/ws');
const init = (params: { options: string[] }) => {
ws.on('open', function open() {
ws.send(JSON.stringify({ action: 'init' }));
setInterval(function timeout() {
ws.send(
JSON.stringify({
action: 'want',
data: params.options,
})
);
}, 500);
});
return ws;
};
export default {
init,
};

View File

@ -2,9 +2,12 @@
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["ES6", "DOM"],
"declaration": true,
"outDir": "./lib",
"strict": true
"rootDir": "./src",
"strict": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]

1868
yarn.lock

File diff suppressed because it is too large Load Diff