2021-04-08 10:15:30 -03:00
|
|
|
import { AxiosInstance } from 'axios';
|
2021-04-14 17:27:28 -03:00
|
|
|
import {
|
|
|
|
|
Block,
|
|
|
|
|
BlockStatus,
|
|
|
|
|
BlockInstance,
|
|
|
|
|
} from '../../interfaces/bitcoin/blocks';
|
|
|
|
|
import { Tx } from '../../interfaces/bitcoin/transactions';
|
2021-04-08 10:15:30 -03:00
|
|
|
|
|
|
|
|
export const useBlocks = (api: AxiosInstance): BlockInstance => {
|
2021-04-14 17:27:28 -03:00
|
|
|
const getBlock = async (params: { hash: string }) => {
|
|
|
|
|
const { data } = await api.get<Block>(`/block/${params.hash}`);
|
2021-04-08 10:15:30 -03:00
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-14 17:27:28 -03:00
|
|
|
const getBlockStatus = async (params: { hash: string }) => {
|
|
|
|
|
const { data } = await api.get<BlockStatus>(`/block/${params.hash}/status`);
|
2021-04-08 10:15:30 -03:00
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBlockTxs = async (params: {
|
|
|
|
|
hash: string;
|
|
|
|
|
start_index?: number;
|
|
|
|
|
}) => {
|
|
|
|
|
const { data } = await api.get<Tx>(
|
|
|
|
|
`/block/${params.hash}/txs/${params.start_index}`
|
|
|
|
|
);
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-14 17:27:28 -03:00
|
|
|
const getBlockTxids = async (params: { hash: string }) => {
|
|
|
|
|
const { data } = await api.get<string[]>(`/block/${params.hash}/txids`);
|
2021-04-08 10:15:30 -03:00
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBlockTxid = async (params: { hash: string; index: number }) => {
|
|
|
|
|
const { data } = await api.get<string>(
|
|
|
|
|
`/block/${params.hash}/txid/${params.index}`
|
|
|
|
|
);
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-14 17:27:28 -03:00
|
|
|
const getBlockRaw = async (params: { hash: string }) => {
|
|
|
|
|
const { data } = await api.get<string>(`/block/${params.hash}/raw`);
|
2021-04-08 10:15:30 -03:00
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-14 17:27:28 -03:00
|
|
|
const getBlockHeight = async (params: { height: number }) => {
|
|
|
|
|
const { data } = await api.get<string>(`/block-height/${params.height}`);
|
2021-04-08 10:15:30 -03:00
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBlocks = async (params: { start_height?: number }) => {
|
|
|
|
|
const { data } = await api.get<Block>(`/blocks/${params.start_height}`);
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBlocksTipHeight = async () => {
|
|
|
|
|
const { data } = await api.get<number>(`/blocks/tip/height`);
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getBlocksTipHash = async () => {
|
|
|
|
|
const { data } = await api.get<string>(`/blocks/tip/hash`);
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
getBlock,
|
|
|
|
|
getBlocks,
|
|
|
|
|
getBlockStatus,
|
|
|
|
|
getBlockTxs,
|
|
|
|
|
getBlockTxid,
|
|
|
|
|
getBlockTxids,
|
|
|
|
|
getBlockRaw,
|
|
|
|
|
getBlockHeight,
|
|
|
|
|
getBlocksTipHash,
|
|
|
|
|
getBlocksTipHeight,
|
|
|
|
|
};
|
|
|
|
|
};
|