@atomicfinance/bitcoin-esplora-api-provider 3.5.3 → 3.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -7
- package/lib/BitcoinEsploraApiProvider.js +0 -150
- package/lib/index.js +0 -34
- package/lib/types.js +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/bitcoin-esplora-api-provider",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"node": ">=14"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@atomicfinance/bitcoin-utils": "^3.
|
|
28
|
-
"@atomicfinance/crypto": "^3.
|
|
29
|
-
"@atomicfinance/errors": "^3.
|
|
30
|
-
"@atomicfinance/node-provider": "^3.
|
|
31
|
-
"@atomicfinance/types": "^3.
|
|
32
|
-
"@atomicfinance/utils": "^3.
|
|
27
|
+
"@atomicfinance/bitcoin-utils": "^3.6.1",
|
|
28
|
+
"@atomicfinance/crypto": "^3.6.1",
|
|
29
|
+
"@atomicfinance/errors": "^3.6.1",
|
|
30
|
+
"@atomicfinance/node-provider": "^3.6.1",
|
|
31
|
+
"@atomicfinance/types": "^3.6.1",
|
|
32
|
+
"@atomicfinance/utils": "^3.6.1",
|
|
33
33
|
"@babel/runtime": "^7.12.1",
|
|
34
34
|
"bignumber.js": "^9.0.0",
|
|
35
35
|
"bitcoin-networks": "^1.0.0",
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const bitcoin_utils_1 = require("@atomicfinance/bitcoin-utils");
|
|
4
|
-
const errors_1 = require("@atomicfinance/errors");
|
|
5
|
-
const node_provider_1 = require("@atomicfinance/node-provider");
|
|
6
|
-
const types_1 = require("@atomicfinance/types");
|
|
7
|
-
const utils_1 = require("@atomicfinance/utils");
|
|
8
|
-
const lodash_1 = require("lodash");
|
|
9
|
-
class BitcoinEsploraApiProvider extends node_provider_1.NodeProvider {
|
|
10
|
-
constructor(options) {
|
|
11
|
-
const { url, network, numberOfBlockConfirmation = 1, defaultFeePerByte = 3, } = options;
|
|
12
|
-
super({
|
|
13
|
-
baseURL: url,
|
|
14
|
-
responseType: 'text',
|
|
15
|
-
transformResponse: undefined, // https://github.com/axios/axios/issues/907,
|
|
16
|
-
});
|
|
17
|
-
this._network = network;
|
|
18
|
-
this._numberOfBlockConfirmation = numberOfBlockConfirmation;
|
|
19
|
-
this._defaultFeePerByte = defaultFeePerByte;
|
|
20
|
-
this._usedAddressCache = {};
|
|
21
|
-
}
|
|
22
|
-
async getFeePerByte(numberOfBlocks = this._numberOfBlockConfirmation) {
|
|
23
|
-
try {
|
|
24
|
-
const feeEstimates = await this.nodeGet('/fee-estimates');
|
|
25
|
-
const blockOptions = Object.keys(feeEstimates).map((block) => parseInt(block));
|
|
26
|
-
const closestBlockOption = blockOptions.reduce((prev, curr) => {
|
|
27
|
-
return Math.abs(prev - numberOfBlocks) < Math.abs(curr - numberOfBlocks)
|
|
28
|
-
? prev
|
|
29
|
-
: curr;
|
|
30
|
-
});
|
|
31
|
-
const rate = Math.round(feeEstimates[closestBlockOption]);
|
|
32
|
-
return rate;
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
return this._defaultFeePerByte;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
async getMinRelayFee() {
|
|
39
|
-
return 1;
|
|
40
|
-
}
|
|
41
|
-
async getBalance(_addresses) {
|
|
42
|
-
const addresses = _addresses.map(utils_1.addressToString);
|
|
43
|
-
const _utxos = await this.getUnspentTransactions(addresses);
|
|
44
|
-
const utxos = (0, lodash_1.flatten)(_utxos);
|
|
45
|
-
return utxos.reduce((acc, utxo) => acc.plus(utxo.value), new types_1.BigNumber(0));
|
|
46
|
-
}
|
|
47
|
-
async _getUnspentTransactions(address) {
|
|
48
|
-
const data = await this.nodeGet(`/address/${address}/utxo`);
|
|
49
|
-
return data.map((utxo) => ({
|
|
50
|
-
...utxo,
|
|
51
|
-
address,
|
|
52
|
-
value: utxo.value,
|
|
53
|
-
blockHeight: utxo.status.block_height,
|
|
54
|
-
}));
|
|
55
|
-
}
|
|
56
|
-
async getUnspentTransactions(_addresses) {
|
|
57
|
-
const addresses = _addresses.map(utils_1.addressToString);
|
|
58
|
-
const utxoSets = await Promise.all(addresses.map((addr) => this._getUnspentTransactions(addr)));
|
|
59
|
-
const utxos = (0, lodash_1.flatten)(utxoSets);
|
|
60
|
-
return utxos;
|
|
61
|
-
}
|
|
62
|
-
async _getAddressTransactionCount(address) {
|
|
63
|
-
const data = await this.nodeGet(`/address/${address}`);
|
|
64
|
-
return data.chain_stats.tx_count + data.mempool_stats.tx_count;
|
|
65
|
-
}
|
|
66
|
-
async getAddressTransactionCounts(_addresses) {
|
|
67
|
-
const addresses = _addresses.map(utils_1.addressToString);
|
|
68
|
-
const transactionCountsArray = await Promise.all(addresses.map(async (addr) => {
|
|
69
|
-
const txCount = await this._getAddressTransactionCount(addr);
|
|
70
|
-
return { [addr]: txCount };
|
|
71
|
-
}));
|
|
72
|
-
const transactionCounts = Object.assign({}, ...transactionCountsArray);
|
|
73
|
-
return transactionCounts;
|
|
74
|
-
}
|
|
75
|
-
async getTransactionHex(transactionHash) {
|
|
76
|
-
return this.nodeGet(`/tx/${transactionHash}/hex`);
|
|
77
|
-
}
|
|
78
|
-
async getTransaction(transactionHash) {
|
|
79
|
-
let data;
|
|
80
|
-
try {
|
|
81
|
-
data = await this.nodeGet(`/tx/${transactionHash}`);
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
if (e.name === 'NodeError' &&
|
|
85
|
-
e.message.includes('Transaction not found')) {
|
|
86
|
-
const { name, message, ...attrs } = e;
|
|
87
|
-
throw new errors_1.TxNotFoundError(`Transaction not found: ${transactionHash}`, attrs);
|
|
88
|
-
}
|
|
89
|
-
throw e;
|
|
90
|
-
}
|
|
91
|
-
const currentHeight = await this.getBlockHeight();
|
|
92
|
-
return this.formatTransaction(data, currentHeight);
|
|
93
|
-
}
|
|
94
|
-
async formatTransaction(tx, currentHeight) {
|
|
95
|
-
const hex = await this.getTransactionHex(tx.txid);
|
|
96
|
-
const confirmations = tx.status.confirmed
|
|
97
|
-
? currentHeight - tx.status.block_height + 1
|
|
98
|
-
: 0;
|
|
99
|
-
const decodedTx = (0, bitcoin_utils_1.decodeRawTransaction)(hex, this._network);
|
|
100
|
-
decodedTx.confirmations = confirmations;
|
|
101
|
-
return (0, bitcoin_utils_1.normalizeTransactionObject)(decodedTx, tx.fee, {
|
|
102
|
-
hash: tx.status.block_hash,
|
|
103
|
-
number: tx.status.block_height,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
async getBlockByHash(blockHash) {
|
|
107
|
-
let data;
|
|
108
|
-
try {
|
|
109
|
-
data = await this.nodeGet(`/block/${blockHash}`);
|
|
110
|
-
}
|
|
111
|
-
catch (e) {
|
|
112
|
-
if (e.name === 'NodeError' && e.message.includes('Block not found')) {
|
|
113
|
-
const { name, message, ...attrs } = e;
|
|
114
|
-
throw new errors_1.BlockNotFoundError(`Block not found: ${blockHash}`, attrs);
|
|
115
|
-
}
|
|
116
|
-
throw e;
|
|
117
|
-
}
|
|
118
|
-
const { id: hash, height: number, timestamp, mediantime, size, previousblockhash: parentHash, difficulty, nonce, } = data;
|
|
119
|
-
return {
|
|
120
|
-
hash,
|
|
121
|
-
number,
|
|
122
|
-
timestamp: mediantime || timestamp,
|
|
123
|
-
size,
|
|
124
|
-
parentHash,
|
|
125
|
-
difficulty,
|
|
126
|
-
nonce,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
async getBlockHash(blockNumber) {
|
|
130
|
-
return this.nodeGet(`/block-height/${blockNumber}`);
|
|
131
|
-
}
|
|
132
|
-
async getBlockByNumber(blockNumber) {
|
|
133
|
-
return this.getBlockByHash(await this.getBlockHash(blockNumber));
|
|
134
|
-
}
|
|
135
|
-
async getBlockHeight() {
|
|
136
|
-
const data = await this.nodeGet('/blocks/tip/height');
|
|
137
|
-
return parseInt(data);
|
|
138
|
-
}
|
|
139
|
-
async getTransactionByHash(transactionHash) {
|
|
140
|
-
return this.getTransaction(transactionHash);
|
|
141
|
-
}
|
|
142
|
-
async getRawTransactionByHash(transactionHash) {
|
|
143
|
-
return this.getTransactionHex(transactionHash);
|
|
144
|
-
}
|
|
145
|
-
async sendRawTransaction(rawTransaction) {
|
|
146
|
-
return this.nodePost('/tx', rawTransaction);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.default = BitcoinEsploraApiProvider;
|
|
150
|
-
//# sourceMappingURL=BitcoinEsploraApiProvider.js.map
|
package/lib/index.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.esplora = exports.BitcoinEsploraApiProvider = void 0;
|
|
30
|
-
const BitcoinEsploraApiProvider_1 = __importDefault(require("./BitcoinEsploraApiProvider"));
|
|
31
|
-
exports.BitcoinEsploraApiProvider = BitcoinEsploraApiProvider_1.default;
|
|
32
|
-
const esplora = __importStar(require("./types"));
|
|
33
|
-
exports.esplora = esplora;
|
|
34
|
-
//# sourceMappingURL=index.js.map
|
package/lib/types.js
DELETED