This commit is contained in:
MiguelMedeiros\Miguel Medeiros 2021-02-08 16:54:37 -03:00
commit ff53139f78
21 changed files with 3334 additions and 0 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

16
.eslintrc.js Normal file
View File

@ -0,0 +1,16 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
},
};

3
.gitignore vendored Normal file
View File

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

6
.prettierrc Normal file
View File

@ -0,0 +1,6 @@
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"printWidth": 80
}

707
README.md Normal file
View File

@ -0,0 +1,707 @@
# Mempool.Space JS API
[![npm version](https://img.shields.io/npm/v/mempool-js.svg?style=flat-square)](https://www.npmjs.org/package/mempool-js)
[![NPM](https://img.shields.io/david/MiguelMedeiros/mempool-js.svg?style=flat-square)](https://david-dm.org/MiguelMedeiros/mempool-js#info=dependencies)
[![Known Vulnerabilities](https://snyk.io/test/github/MiguelMedeiros/mempool-js/badge.svg)](https://snyk.io/test/github/MiguelMedeiros/mempool-js)
---
Easy way to add Mempool API to your JS application.
---
## Features
- :pushpin: [Instalation](#installation)
- :pushpin: [Usage](#usage)
- Fees
- Get Fees Recommended
- Get Fees Mempool Blocks
- Mempool
- Get Mempool
- Get Mempool Recent
- Get Mempool Txids
- Blocks
- Get Block
- Get Block Status
- Get Block Txs
- Get Block Txids
- Get Block Txid
- Get Block Raw
- Get Blocks Height
- Get Blocks
- Get Blocks Tip Height
- Get Blocks Tip Hash
- Transactions
- Get Tx
- Get Tx Status
- Get Tx Hex
- Get Tx Raw
- Get Tx Merkle Block Proof
- Get Tx Merkle Proof
- Get Tx Outspend
- Get Tx Outspends
- Post Tx Outspends
- Addresses
- Get Address
- Get Address Txs
- Get Address Txs Chain
- Get Address Txs Mempool
- Get Address Txs Utxo
- :pushpin: [References](#references)
- :pushpin: [Donate](#donate)
- :pushpin: [Contribute](#contribute)
- :pushpin: [License](#license)
---
## Installation
Using npm:
```bash
$ npm install mempool-space-js
```
Using yarn:
```bash
$ yarn add mempool-space-js
```
## Usage
### Get Fees Recommended
Returns our currently suggested fees for new transactions.
[Code Example](examples/fees.ts)
```js
import { fees } from './../src/index';
...
const feesRecommended = await fees.getFeesRecommended();
console.log(feesRecommended);
```
<br/>
### Get Fees Mempool Blocks
Returns current mempool as projected blocks.
[Code Example](examples/fees.ts)
```js
import { fees } from './../src/index';
...
const feesMempoolBlocks = await fees.getFeesMempoolBlocks();
console.log(feesMempoolBlocks);
```
<br/>
### Get Mempool
Returns current mempool backlog statistics.
[Code Example](examples/mempool.ts)
```js
import { mempool } from './../src/index';
...
const getMempool = await mempool.getMempool();
console.log(getMempool);
```
<br/>
### Get Mempool Txids
Get the full list of txids in the mempool as an array. The order of the txids is arbitrary and does not match bitcoind.
[Code Example](examples/mempool.ts)
```js
import { mempool } from './../src/';
...
const getMempoolTxids = await mempool.getMempoolTxids();
console.log(getMempoolTxids);
```
<br/>
### Get Mempool Recent
Get a list of the last 10 transactions to enter the mempool. Each transaction object contains simplified overview data, with the following fields: txid, fee, vsize, and value.
[Code Example](examples/mempool.ts)
```js
import { mempool } from './../src/index';
...
const getMempoolRecent = await mempool.getMempoolRecent();
console.log(getMempoolRecent);
```
<br/>
### Get Block
Returns details about a block. Available fields: id, height, version, timestamp, bits, nonce, merkle_root, tx_count, size, weight, and previousblockhash.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const block = await blocks.getBlock({
hash: '000000000000000015dc...'
});
console.log(block);
```
<br/>
### Get Block Status
Returns the confirmation status of a block. Available fields: in_best_chain (boolean, false for orphaned blocks), next_best (the hash of the next block, only available for blocks in the best chain).
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blockStatus = await blocks.getBlockStatus({
hash: '000000000000000015dc...'
});
console.log(blockStatus);
```
<br/>
### Get Block Txs
Returns a list of transactions in the block (up to 25 transactions beginning at start_index). Transactions returned here do not have the status field, since all the transactions share the same block and confirmation status.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
- {number} params.start_index - Default: 25
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blockTxs = await blocks.getBlockTxs({
hash: '000000000000000015dc...'
});
console.log(blockTxs);
```
<br/>
### Get Block Txids
Returns a list of all txids in the block.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blockTxids = await blocks.getBlockTxids({
hash: '000000000000000015dc...'
});
console.log(blockTxids);
```
<br/>
### Get Block Txid
Returns the transaction at index :index within the specified block.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
- {number} params.index - Index
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blockTxid = await blocks.getBlockTxid({
hash: '000000000000000015dc...',
index: 218
});
console.log(blockTxids);
```
<br/>
### Get Block Raw
Returns the raw block representation in binary.
Parameters:
- {Object} params - Params object.
- {string} params.hash - Hash from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blockRaw = await blocks.getBlockRaw({
hash: '000000000000000015dc...'
});
console.log(blockRaw);
```
<br/>
### Get Blocks Height
Returns the hash of the block currently at :height.
Parameters:
- {Object} params - Params object.
- {number} params.height - Height from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blockHeight = await blocks.getBlockHeight({
height: 66666,
});
console.log(blockHeight);
```
<br/>
### Get Blocks
Returns the 10 newest blocks starting at the tip or at :start_height if specified.
Parameters:
- {Object} params - Params object.
- {number} params.start_height - Height from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const getBlocks = await blocks.getBlocks({
start_height: 66666
});
console.log(getBlocks);
```
<br/>
### Get Blocks Tip Height
Returns the 10 newest blocks starting at the tip or at :start_height if specified.
Parameters:
- {Object} params - Params object.
- {number} params.start_height - Height from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blocksTipHeight = await blocks.getBlocksTipHeight();
console.log(blocksTipHeight);
```
<br/>
### Get Blocks Tip Hash
Returns the hash of the last block.
Parameters:
- {Object} params - Params object.
- {number} params.start_height - Height from a block
[Code Example](examples/blocks.ts)
```js
import { blocks } from './../src/index';
...
const blocksTipHash = await blocks.getBlocksTipHash();
console.log(blocksTipHash);
```
<br/>
### Get Tx
Returns details about a transaction. Available fields: txid, version, locktime, size, weight, fee, vin, vout, and status.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const tx = await transactions.getTx({
txid: '15e10745f15593...'
});
console.log(tx);
```
<br/>
### Get Tx Status
Returns the confirmation status of a transaction. Available fields: confirmed (boolean), block_height (optional), and block_hash (optional).
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txStatus = await transactions.getTxStatus({
txid: '15e10745f15593...'
});
console.log(txStatus);
```
<br/>
### Get Tx Hex
Returns a transaction serialized as hex.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txHex = await transactions.getTxHex({
txid: '15e10745f15593...'
});
console.log(txHex);
```
<br/>
### Get Tx Raw
Returns a transaction as binary data.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txRaw = await transactions.getTxRaw({
txid: '15e10745f15593...'
});
console.log(txRaw);
```
<br/>
### Get Tx Merkle Block Proof
Returns a merkle inclusion proof for the transaction using bitcoind's merkleblock format.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txMerkleBlockProof = await transactions.getTxMerkleBlockProof({
txid: '15e10745f15593...'
});
console.log(txMerkleBlockProof);
```
<br/>
### Get Tx Merkle Proof
Returns a merkle inclusion proof for the transaction using Electrum's blockchain.transaction.get_merkle format.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txMerkleProof = await transactions.getTxMerkleProof({
txid: '15e10745f15593...'
});
console.log(txMerkleProof);
```
<br/>
### Get Tx Outspend
Returns the spending status of a transaction output. Available fields: spent (boolean), txid (optional), vin (optional), and status (optional, the status of the spending tx).
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txOutspend = await transactions.getTxOutspend({
txid: '15e10745f15593...',
vout: 3,
});
console.log(txOutspend);
```
<br/>
### Get Tx Outspends
Returns the spending status of all transaction outputs.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const txOutspends = await transactions.getTxOutspends({
txid: '15e10745f15593...'
});
console.log(txOutspends);
```
<br/>
### Post Tx Outspends
Broadcast a raw transaction to the network. The transaction should be provided as hex in the request body. The txid will be returned on success.
Parameters:
- {Object} params - Params object.
- {string} params.txid - Transactions id.
[Code Example](examples/transactions.ts)
```js
import { transactions } from './../src/index';
...
const postTx = await transactions.postTx({
txid: '15e10745f15593...'
});
console.log(postTx);
```
<br/>
---
### Get Address
Returns details about an address. Available fields: address, chain_stats, and mempool_stats. {chain,mempool}\_stats each contain an object with tx_count, funded_txo_count, funded_txo_sum, spent_txo_count, and spent_txo_sum.
Parameters:
- {Object} params - Params object.
- {string} params.address - Address id.
[Code Example](examples/addresses.ts)
```js
import { addresses } from './../src/index';
...
const addressTest = await addresses.getAddress({
address: '15e10745f15593a...'
});
console.log(addressTest);
```
<br/>
### Get Address Txs
Get transaction history for the specified address/scripthash, sorted with newest first. Returns up to 50 mempool transactions plus the first 25 confirmed transactions. You can request more confirmed transactions using :last_seen_txid (see below).
Parameters:
- {Object} params - Params object.
- {string} params.address - Address id.
[Code Example](examples/addresses.ts)
```js
import { addresses } from './../src/index';
...
const addressTxs = await addresses.getAddressTxs({
address: '15e10745f15593a...'
});
console.log(addressTxs);
```
<br/>
### Get Address Txs Chain
Get confirmed transaction history for the specified address/scripthash, sorted with newest first. Returns 25 transactions per page. More can be requested by specifying the last txid seen by the previous query.
Parameters:
- {Object} params - Params object.
- {string} params.address - Address id.
[Code Example](examples/addresses.ts)
```js
import { addresses } from './../src/index';
...
const addressTxsChain = await addresses.getAddressTxsChain({
address: '15e10745f15593a...'
});
console.log(addressTxsChain);
```
<br/>
### Get Address Txs Mempool
Get unconfirmed transaction history for the specified address/scripthash. Returns up to 50 transactions (no paging).
Parameters:
- {Object} params - Params object.
- {string} params.address - Address id.
[Code Example](examples/addresses.ts)
```js
import { addresses } from './../src/index';
...
const addressTxsMempool = await addresses.getAddressTxsMempool({
address: '15e10745f15593a...'
});
console.log(addressTxsMempool);
```
<br/>
### Get Address Txs Utxo
Get unconfirmed transaction history for the specified address/scripthash. Returns up to 50 transactions (no paging).
Parameters:
- {Object} params - Params object.
- {string} params.address - Address id.
[Code Example](examples/addresses.ts)
```js
import { addresses } from './../src/index';
...
const addressTxsUtxo = await addresses.getAddressTxsUtxo({
address: '15e10745f15593a...'
});
console.log(addressTxsUtxo);
```
<br/>
## References
- Mempool.Space Website: [https://mempool.space](https://mempool.space)
- Mempool.Sapce API Documentation: [https://mempool.space/api](https://mempool.space/api)
- My Website: [https://miguelmedeiros.com](https://miguelmedeiros.com.br)
---
## Donate
Help me to stack sats! :blush:
[bc1q4m9hs4fv3etleyqgp4jhvak0q3w26mmkntqq02](bitcoin:bc1q4m9hs4fv3etleyqgp4jhvak0q3w26mmkntqq02)
[Or donate via Lightning Network!](https://paywall.link/to/lnpayapi)
---
## Contributing
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
---
## License
[MIT](https://choosealicense.com/licenses/mit/)

22
examples/addresses.ts Normal file
View File

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

39
examples/blocks.ts Normal file
View File

@ -0,0 +1,39 @@
import { blocks } from '../src/index';
const init = async () => {
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({
start_height: 66666,
});
console.log(blockHeight);
const getBlocks = await blocks.getBlocks({ start_height: 66666 });
console.log(getBlocks);
const blocksTipHeight = await blocks.getBlocksTipHeight();
console.log(blocksTipHeight);
const blocksTipHash = await blocks.getBlocksTipHash();
console.log(blocksTipHash);
};
init();

10
examples/fees.ts Normal file
View File

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

13
examples/mempool.ts Normal file
View File

@ -0,0 +1,13 @@
import { mempool } from '../src/index';
const init = async () => {
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();

39
examples/transactions.ts Normal file
View File

@ -0,0 +1,39 @@
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();

6
nodemon.json Normal file
View File

@ -0,0 +1,6 @@
{
"watch": ["src"],
"ext": "ts,json",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts"
}

42
package.json Normal file
View File

@ -0,0 +1,42 @@
{
"name": "mempool-js",
"version": "1.0.0",
"description": "NPM Package for Mempool.Space API.",
"main": "lib/index.js",
"author": "MiguelMedeiros\\Miguel Medeiros <miguel@miguelmedeiros.com.br>",
"email": "miguel@miguelmedeiros.com.br",
"url": "http://miguelmedeiros.com.br",
"private": false,
"license": "MIT",
"repository": "github:MiguelMedeiros/mempool-js",
"types": "lib/index.d.ts",
"scripts": {
"start": "ts-node src/index.ts",
"dev": "nodemon src/index.ts",
"build": "tsc",
"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"
},
"devDependencies": {
"@types/node": "^14.14.25",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"eslint": "^7.19.0",
"nodemon": "^2.0.7"
}
}

166
src/addresses.ts Normal file
View File

@ -0,0 +1,166 @@
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,
};

4
src/api.ts Normal file
View File

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

247
src/blocks.ts Normal file
View File

@ -0,0 +1,247 @@
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,
};

59
src/fees.ts Normal file
View File

@ -0,0 +1,59 @@
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,
};

19
src/index.ts Normal file
View File

@ -0,0 +1,19 @@
import fees from './fees';
import mempool from './mempool';
import blocks from './blocks';
import transactions from './transactions';
import addresses from './addresses';
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 {
fees,
mempool,
blocks,
transactions,
addresses,
};

76
src/mempool.ts Normal file
View File

@ -0,0 +1,76 @@
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,
};

250
src/transactions.ts Normal file
View File

@ -0,0 +1,250 @@
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,
};

11
tsconfig.json Normal file
View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}

1587
yarn.lock Normal file

File diff suppressed because it is too large Load Diff