diff --git a/README.md b/README.md index dbe3b554e..515dc5d56 100644 --- a/README.md +++ b/README.md @@ -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 Mempool](#get-address-txs-mempool) - [Get Address Txs Utxo](#get-address-txs-utxo) + - [Websocket](#websocket) - :pushpin: [References](#references) - :pushpin: [Donate](#donate) @@ -80,7 +81,7 @@ Returns our currently suggested fees for new transactions. [Code Example](examples/fees.ts) ```js -import { fees } from './../src/index'; +import { fees } from 'mempool-space-js'; ... const feesRecommended = await fees.getFeesRecommended(); console.log(feesRecommended); @@ -95,7 +96,7 @@ Returns current mempool as projected blocks. [Code Example](examples/fees.ts) ```js -import { fees } from './../src/index'; +import { fees } from 'mempool-space-js'; ... const feesMempoolBlocks = await fees.getFeesMempoolBlocks(); console.log(feesMempoolBlocks); @@ -110,7 +111,7 @@ Returns current mempool backlog statistics. [Code Example](examples/mempool.ts) ```js -import { mempool } from './../src/index'; +import { mempool } from 'mempool-space-js'; ... const getMempool = await mempool.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) ```js -import { mempool } from './../src/index'; +import { mempool } from 'mempool-space-js'; ... const getMempoolRecent = await mempool.getMempoolRecent(); console.log(getMempoolRecent); @@ -160,7 +161,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const block = await blocks.getBlock({ hash: '000000000000000015dc...' @@ -182,7 +183,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blockStatus = await blocks.getBlockStatus({ hash: '000000000000000015dc...' @@ -205,7 +206,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blockTxs = await blocks.getBlockTxs({ hash: '000000000000000015dc...' @@ -227,7 +228,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blockTxids = await blocks.getBlockTxids({ hash: '000000000000000015dc...' @@ -250,7 +251,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blockTxid = await blocks.getBlockTxid({ hash: '000000000000000015dc...', @@ -273,7 +274,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blockRaw = await blocks.getBlockRaw({ hash: '000000000000000015dc...' @@ -295,7 +296,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blockHeight = await blocks.getBlockHeight({ height: 66666, @@ -317,7 +318,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const getBlocks = await blocks.getBlocks({ start_height: 66666 @@ -339,7 +340,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blocksTipHeight = await blocks.getBlocksTipHeight(); console.log(blocksTipHeight); @@ -359,7 +360,7 @@ Parameters: [Code Example](examples/blocks.ts) ```js -import { blocks } from './../src/index'; +import { blocks } from 'mempool-space-js'; ... const blocksTipHash = await blocks.getBlocksTipHash(); console.log(blocksTipHash); @@ -379,7 +380,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const tx = await transactions.getTx({ txid: '15e10745f15593...' @@ -401,7 +402,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txStatus = await transactions.getTxStatus({ txid: '15e10745f15593...' @@ -423,7 +424,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txHex = await transactions.getTxHex({ txid: '15e10745f15593...' @@ -445,7 +446,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txRaw = await transactions.getTxRaw({ txid: '15e10745f15593...' @@ -467,7 +468,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txMerkleBlockProof = await transactions.getTxMerkleBlockProof({ txid: '15e10745f15593...' @@ -489,7 +490,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txMerkleProof = await transactions.getTxMerkleProof({ txid: '15e10745f15593...' @@ -511,7 +512,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txOutspend = await transactions.getTxOutspend({ txid: '15e10745f15593...', @@ -534,7 +535,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const txOutspends = await transactions.getTxOutspends({ txid: '15e10745f15593...' @@ -556,7 +557,7 @@ Parameters: [Code Example](examples/transactions.ts) ```js -import { transactions } from './../src/index'; +import { transactions } from 'mempool-space-js'; ... const postTx = await transactions.postTx({ txid: '15e10745f15593...' @@ -580,7 +581,7 @@ Parameters: [Code Example](examples/addresses.ts) ```js -import { addresses } from './../src/index'; +import { addresses } from 'mempool-space-js'; ... const addressTest = await addresses.getAddress({ address: '15e10745f15593a...' @@ -602,7 +603,7 @@ Parameters: [Code Example](examples/addresses.ts) ```js -import { addresses } from './../src/index'; +import { addresses } from 'mempool-space-js'; ... const addressTxs = await addresses.getAddressTxs({ address: '15e10745f15593a...' @@ -624,7 +625,7 @@ Parameters: [Code Example](examples/addresses.ts) ```js -import { addresses } from './../src/index'; +import { addresses } from 'mempool-space-js'; ... const addressTxsChain = await addresses.getAddressTxsChain({ address: '15e10745f15593a...' @@ -646,7 +647,7 @@ Parameters: [Code Example](examples/addresses.ts) ```js -import { addresses } from './../src/index'; +import { addresses } from 'mempool-space-js'; ... const addressTxsMempool = await addresses.getAddressTxsMempool({ address: '15e10745f15593a...' @@ -668,7 +669,7 @@ Parameters: [Code Example](examples/addresses.ts) ```js -import { addresses } from './../src/index'; +import { addresses } from 'mempool-space-js'; ... const addressTxsUtxo = await addresses.getAddressTxsUtxo({ address: '15e10745f15593a...' @@ -678,10 +679,43 @@ console.log(addressTxsUtxo);
+### 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 - 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) --- diff --git a/examples/websocket.ts b/examples/websocket.ts new file mode 100644 index 000000000..239a7c5b8 --- /dev/null +++ b/examples/websocket.ts @@ -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(); diff --git a/package.json b/package.json index f735d99dd..d202b721c 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ ], "dependencies": { "axios": "^0.21.1", - "typescript": "^4.1.3" + "typescript": "^4.1.3", + "ws": "^7.4.3" }, "devDependencies": { "@types/node": "^14.14.25", diff --git a/src/index.ts b/src/index.ts index 73cfb7504..367c3c995 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,12 +3,14 @@ import mempool from './mempool'; import blocks from './blocks'; import transactions from './transactions'; import addresses from './addresses'; +import websocket from './websocket'; 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'; export default { fees, @@ -16,4 +18,5 @@ export default { blocks, transactions, addresses, + websocket, }; diff --git a/src/websocket.ts b/src/websocket.ts new file mode 100644 index 000000000..833ac67a1 --- /dev/null +++ b/src/websocket.ts @@ -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, +}; diff --git a/yarn.lock b/yarn.lock index eebdda0f8..01a380249 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1576,6 +1576,11 @@ write-file-atomic@^3.0.0: signal-exit "^3.0.2" 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: version "4.0.0" resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"