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

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,
};