Adding websocket.

This commit is contained in:
MiguelMedeiros\Miguel Medeiros 2021-02-09 16:22:48 -03:00
parent b39fb0bd60
commit 90be85f6f0
6 changed files with 121 additions and 30 deletions

View File

@ -49,6 +49,7 @@ Easy way to add Mempool API to your JS application.
- [Get Address Txs Chain](#get-address-txs-chain) - [Get Address Txs Chain](#get-address-txs-chain)
- [Get Address Txs Mempool](#get-address-txs-mempool) - [Get Address Txs Mempool](#get-address-txs-mempool)
- [Get Address Txs Utxo](#get-address-txs-utxo) - [Get Address Txs Utxo](#get-address-txs-utxo)
- [Websocket](#websocket)
- :pushpin: [References](#references) - :pushpin: [References](#references)
- :pushpin: [Donate](#donate) - :pushpin: [Donate](#donate)
@ -80,7 +81,7 @@ Returns our currently suggested fees for new transactions.
[Code Example](examples/fees.ts) [Code Example](examples/fees.ts)
```js ```js
import { fees } from './../src/index'; import { fees } from 'mempool-space-js';
... ...
const feesRecommended = await fees.getFeesRecommended(); const feesRecommended = await fees.getFeesRecommended();
console.log(feesRecommended); console.log(feesRecommended);
@ -95,7 +96,7 @@ Returns current mempool as projected blocks.
[Code Example](examples/fees.ts) [Code Example](examples/fees.ts)
```js ```js
import { fees } from './../src/index'; import { fees } from 'mempool-space-js';
... ...
const feesMempoolBlocks = await fees.getFeesMempoolBlocks(); const feesMempoolBlocks = await fees.getFeesMempoolBlocks();
console.log(feesMempoolBlocks); console.log(feesMempoolBlocks);
@ -110,7 +111,7 @@ Returns current mempool backlog statistics.
[Code Example](examples/mempool.ts) [Code Example](examples/mempool.ts)
```js ```js
import { mempool } from './../src/index'; import { mempool } from 'mempool-space-js';
... ...
const getMempool = await mempool.getMempool(); const getMempool = await mempool.getMempool();
console.log(getMempool); console.log(getMempool);
@ -140,7 +141,7 @@ Get a list of the last 10 transactions to enter the mempool. Each transaction ob
[Code Example](examples/mempool.ts) [Code Example](examples/mempool.ts)
```js ```js
import { mempool } from './../src/index'; import { mempool } from 'mempool-space-js';
... ...
const getMempoolRecent = await mempool.getMempoolRecent(); const getMempoolRecent = await mempool.getMempoolRecent();
console.log(getMempoolRecent); console.log(getMempoolRecent);
@ -160,7 +161,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const block = await blocks.getBlock({ const block = await blocks.getBlock({
hash: '000000000000000015dc...' hash: '000000000000000015dc...'
@ -182,7 +183,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blockStatus = await blocks.getBlockStatus({ const blockStatus = await blocks.getBlockStatus({
hash: '000000000000000015dc...' hash: '000000000000000015dc...'
@ -205,7 +206,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blockTxs = await blocks.getBlockTxs({ const blockTxs = await blocks.getBlockTxs({
hash: '000000000000000015dc...' hash: '000000000000000015dc...'
@ -227,7 +228,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blockTxids = await blocks.getBlockTxids({ const blockTxids = await blocks.getBlockTxids({
hash: '000000000000000015dc...' hash: '000000000000000015dc...'
@ -250,7 +251,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blockTxid = await blocks.getBlockTxid({ const blockTxid = await blocks.getBlockTxid({
hash: '000000000000000015dc...', hash: '000000000000000015dc...',
@ -273,7 +274,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blockRaw = await blocks.getBlockRaw({ const blockRaw = await blocks.getBlockRaw({
hash: '000000000000000015dc...' hash: '000000000000000015dc...'
@ -295,7 +296,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blockHeight = await blocks.getBlockHeight({ const blockHeight = await blocks.getBlockHeight({
height: 66666, height: 66666,
@ -317,7 +318,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const getBlocks = await blocks.getBlocks({ const getBlocks = await blocks.getBlocks({
start_height: 66666 start_height: 66666
@ -339,7 +340,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blocksTipHeight = await blocks.getBlocksTipHeight(); const blocksTipHeight = await blocks.getBlocksTipHeight();
console.log(blocksTipHeight); console.log(blocksTipHeight);
@ -359,7 +360,7 @@ Parameters:
[Code Example](examples/blocks.ts) [Code Example](examples/blocks.ts)
```js ```js
import { blocks } from './../src/index'; import { blocks } from 'mempool-space-js';
... ...
const blocksTipHash = await blocks.getBlocksTipHash(); const blocksTipHash = await blocks.getBlocksTipHash();
console.log(blocksTipHash); console.log(blocksTipHash);
@ -379,7 +380,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const tx = await transactions.getTx({ const tx = await transactions.getTx({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -401,7 +402,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txStatus = await transactions.getTxStatus({ const txStatus = await transactions.getTxStatus({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -423,7 +424,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txHex = await transactions.getTxHex({ const txHex = await transactions.getTxHex({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -445,7 +446,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txRaw = await transactions.getTxRaw({ const txRaw = await transactions.getTxRaw({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -467,7 +468,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txMerkleBlockProof = await transactions.getTxMerkleBlockProof({ const txMerkleBlockProof = await transactions.getTxMerkleBlockProof({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -489,7 +490,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txMerkleProof = await transactions.getTxMerkleProof({ const txMerkleProof = await transactions.getTxMerkleProof({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -511,7 +512,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txOutspend = await transactions.getTxOutspend({ const txOutspend = await transactions.getTxOutspend({
txid: '15e10745f15593...', txid: '15e10745f15593...',
@ -534,7 +535,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const txOutspends = await transactions.getTxOutspends({ const txOutspends = await transactions.getTxOutspends({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -556,7 +557,7 @@ Parameters:
[Code Example](examples/transactions.ts) [Code Example](examples/transactions.ts)
```js ```js
import { transactions } from './../src/index'; import { transactions } from 'mempool-space-js';
... ...
const postTx = await transactions.postTx({ const postTx = await transactions.postTx({
txid: '15e10745f15593...' txid: '15e10745f15593...'
@ -580,7 +581,7 @@ Parameters:
[Code Example](examples/addresses.ts) [Code Example](examples/addresses.ts)
```js ```js
import { addresses } from './../src/index'; import { addresses } from 'mempool-space-js';
... ...
const addressTest = await addresses.getAddress({ const addressTest = await addresses.getAddress({
address: '15e10745f15593a...' address: '15e10745f15593a...'
@ -602,7 +603,7 @@ Parameters:
[Code Example](examples/addresses.ts) [Code Example](examples/addresses.ts)
```js ```js
import { addresses } from './../src/index'; import { addresses } from 'mempool-space-js';
... ...
const addressTxs = await addresses.getAddressTxs({ const addressTxs = await addresses.getAddressTxs({
address: '15e10745f15593a...' address: '15e10745f15593a...'
@ -624,7 +625,7 @@ Parameters:
[Code Example](examples/addresses.ts) [Code Example](examples/addresses.ts)
```js ```js
import { addresses } from './../src/index'; import { addresses } from 'mempool-space-js';
... ...
const addressTxsChain = await addresses.getAddressTxsChain({ const addressTxsChain = await addresses.getAddressTxsChain({
address: '15e10745f15593a...' address: '15e10745f15593a...'
@ -646,7 +647,7 @@ Parameters:
[Code Example](examples/addresses.ts) [Code Example](examples/addresses.ts)
```js ```js
import { addresses } from './../src/index'; import { addresses } from 'mempool-space-js';
... ...
const addressTxsMempool = await addresses.getAddressTxsMempool({ const addressTxsMempool = await addresses.getAddressTxsMempool({
address: '15e10745f15593a...' address: '15e10745f15593a...'
@ -668,7 +669,7 @@ Parameters:
[Code Example](examples/addresses.ts) [Code Example](examples/addresses.ts)
```js ```js
import { addresses } from './../src/index'; import { addresses } from 'mempool-space-js';
... ...
const addressTxsUtxo = await addresses.getAddressTxsUtxo({ const addressTxsUtxo = await addresses.getAddressTxsUtxo({
address: '15e10745f15593a...' address: '15e10745f15593a...'
@ -678,10 +679,43 @@ console.log(addressTxsUtxo);
<br/> <br/>
### Websocket
```js
import { websocket } from 'mempool-space-js';
...
const ws = await websocket.init({
options: [
'blocks',
'stats',
'mempool-blocks',
'live-2h-chart'
],
});
ws.on('message', function incoming(data: any) {
const res = JSON.parse(data);
if (res.blocks) {
res.blocks.forEach((block: any) => {
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);
}
});
```
## References ## References
- Mempool.Space Website: [https://mempool.space](https://mempool.space) - Mempool.Space Website: [https://mempool.space](https://mempool.space)
- Mempool.Sapce API Documentation: [https://mempool.space/api](https://mempool.space/api) - Mempool.Space API Documentation: [https://mempool.space/api](https://mempool.space/api)
- My Website: [https://miguelmedeiros.com](https://miguelmedeiros.com.br) - My Website: [https://miguelmedeiros.com](https://miguelmedeiros.com.br)
--- ---

26
examples/websocket.ts Normal file
View File

@ -0,0 +1,26 @@
import { websocket } from '../src/index';
const init = async () => {
const ws = await websocket.init({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data: any) {
const res = JSON.parse(data);
if (res.blocks) {
res.blocks.forEach((block: any) => {
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();

View File

@ -30,7 +30,8 @@
], ],
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"typescript": "^4.1.3" "typescript": "^4.1.3",
"ws": "^7.4.3"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.25", "@types/node": "^14.14.25",

View File

@ -3,12 +3,14 @@ import mempool from './mempool';
import blocks from './blocks'; import blocks from './blocks';
import transactions from './transactions'; import transactions from './transactions';
import addresses from './addresses'; import addresses from './addresses';
import websocket from './websocket';
export { default as fees } from './fees'; export { default as fees } from './fees';
export { default as mempool } from './mempool'; export { default as mempool } from './mempool';
export { default as blocks } from './blocks'; export { default as blocks } from './blocks';
export { default as transactions } from './transactions'; export { default as transactions } from './transactions';
export { default as addresses } from './addresses'; export { default as addresses } from './addresses';
export { default as websocket } from './websocket';
export default { export default {
fees, fees,
@ -16,4 +18,5 @@ export default {
blocks, blocks,
transactions, transactions,
addresses, addresses,
websocket,
}; };

22
src/websocket.ts Normal file
View File

@ -0,0 +1,22 @@
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

@ -1576,6 +1576,11 @@ write-file-atomic@^3.0.0:
signal-exit "^3.0.2" signal-exit "^3.0.2"
typedarray-to-buffer "^3.1.5" typedarray-to-buffer "^3.1.5"
ws@^7.4.3:
version "7.4.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.3.tgz#1f9643de34a543b8edb124bdcbc457ae55a6e5cd"
integrity sha512-hr6vCR76GsossIRsr8OLR9acVVm1jyfEWvhbNjtgPOrfvAlKzvyeg/P6r8RuDjRyrcQoPQT7K0DGEPc7Ae6jzA==
xdg-basedir@^4.0.0: xdg-basedir@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13" resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"