From caf8d956b5985e9d3dd882efd36635871fc69bef Mon Sep 17 00:00:00 2001 From: Miguel Medeiros Date: Fri, 23 Jul 2021 17:37:28 -0300 Subject: [PATCH] Add difficulty adjustment endpoints methods. --- examples/nodejs/index.ts | 6 ++++-- src/app/bitcoin/difficulty.ts | 16 ++++++++++++++++ src/index.ts | 2 ++ src/interfaces/bitcoin/difficulty.ts | 13 +++++++++++++ src/interfaces/index.ts | 2 ++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/app/bitcoin/difficulty.ts create mode 100644 src/interfaces/bitcoin/difficulty.ts diff --git a/examples/nodejs/index.ts b/examples/nodejs/index.ts index 099250c9c..2731e163d 100644 --- a/examples/nodejs/index.ts +++ b/examples/nodejs/index.ts @@ -2,7 +2,7 @@ import mempoolJS from "@mempool/mempool.js"; const init = async () => { const { bitcoin } = mempoolJS({ - hostname:'mempool.ninja' + hostname:'localhost:4200' }); const feesRecommended = await bitcoin.fees.getFeesRecommended(); @@ -10,8 +10,10 @@ const init = async () => { const hash = "0000000000000000000065bda8f8a88f2e1e00d9a6887a43d640e52a4c7660f2"; const block = await bitcoin.blocks.getBlockHeader({hash}); - console.log(block); + + const difficultyAdjustment = await bitcoin.difficulty.getDifficultyAdjustment(); + console.log(difficultyAdjustment); }; init(); \ No newline at end of file diff --git a/src/app/bitcoin/difficulty.ts b/src/app/bitcoin/difficulty.ts new file mode 100644 index 000000000..b01fbff23 --- /dev/null +++ b/src/app/bitcoin/difficulty.ts @@ -0,0 +1,16 @@ +import { AxiosInstance } from 'axios'; +import { + Adjustment, + DifficultyInstance, +} from '../../interfaces/bitcoin/difficulty'; + +export const useDifficulty = (api: AxiosInstance): DifficultyInstance => { + const getDifficultyAdjustment = async () => { + const { data } = await api.get(`/difficulty-adjustment`); + return data; + }; + + return { + getDifficultyAdjustment, + }; +}; diff --git a/src/index.ts b/src/index.ts index 646740e68..90863a066 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import { import { useAddresses } from './app/bitcoin/addresses'; import { useBlocks } from './app/bitcoin/blocks'; +import { useDifficulty } from './app/bitcoin/difficulty'; import { useFees } from './app/bitcoin/fees'; import { useMempool } from './app/bitcoin/mempool'; import { useTransactions } from './app/bitcoin/transactions'; @@ -45,6 +46,7 @@ const mempool = ( bitcoin: { addresses: useAddresses(apiBitcoin), blocks: useBlocks(apiBitcoin), + difficulty: useDifficulty(apiBitcoin), fees: useFees(apiBitcoin), mempool: useMempool(apiBitcoin), transactions: useTransactions(apiBitcoin), diff --git a/src/interfaces/bitcoin/difficulty.ts b/src/interfaces/bitcoin/difficulty.ts new file mode 100644 index 000000000..d248ce817 --- /dev/null +++ b/src/interfaces/bitcoin/difficulty.ts @@ -0,0 +1,13 @@ + +export interface Adjustment { + progressPercent: number, + difficultyChange: number, + estimatedRetargetDate: number, + remainingBlocks: number, + remainingTime: number, + previousRetarget: number, +} + +export interface DifficultyInstance { + getDifficultyAdjustment: () => Promise; +} diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index 9fe6fad6d..ca2d83a8d 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -1,5 +1,6 @@ import { AddressInstance } from './bitcoin/addresses'; import { BlockInstance } from './bitcoin/blocks'; +import { DifficultyInstance } from './bitcoin/difficulty'; import { FeeInstance } from './bitcoin/fees'; import { MempoolInstance } from './bitcoin/mempool'; import { TxInstance } from './bitcoin/transactions'; @@ -21,6 +22,7 @@ export interface MempoolReturn { bitcoin: { addresses: AddressInstance; blocks: BlockInstance; + difficulty: DifficultyInstance; fees: FeeInstance; mempool: MempoolInstance; transactions: TxInstance;