From 3afb29a1407b63db5a27a94d856e527cc09958dd Mon Sep 17 00:00:00 2001 From: Cafe137 Date: Sun, 19 Feb 2023 22:06:59 +0100 Subject: [PATCH] feat: add redistribution state endpoint --- src/bee-debug.ts | 82 ++++++++++++++++++++++---------------- src/modules/debug/stake.ts | 18 ++++++++- src/types/debug.ts | 13 +++++- 3 files changed, 76 insertions(+), 37 deletions(-) diff --git a/src/bee-debug.ts b/src/bee-debug.ts index 9ae7979b..409eb651 100644 --- a/src/bee-debug.ts +++ b/src/bee-debug.ts @@ -1,43 +1,56 @@ -import * as connectivity from './modules/debug/connectivity' import * as balance from './modules/debug/balance' import * as chequebook from './modules/debug/chequebook' +import * as connectivity from './modules/debug/connectivity' import * as settlements from './modules/debug/settlements' +import * as stake from './modules/debug/stake' +import * as states from './modules/debug/states' import * as status from './modules/debug/status' import * as transactions from './modules/debug/transactions' -import * as states from './modules/debug/states' -import * as stake from './modules/debug/stake' +import * as stamps from './modules/debug/stamps' +import * as tag from './modules/debug/tag' import type { Address, - Peer, + AllSettlements, BalanceResponse, - PeerBalance, + BeeVersions, + ChainState, ChequebookAddressResponse, ChequebookBalanceResponse, - LastChequesResponse, - LastChequesForPeerResponse, - LastCashoutActionResponse, - Settlements, - AllSettlements, - RemovePeerResponse, - Topology, - PingResponse, + ExtendedTag, Health, + LastCashoutActionResponse, + LastChequesForPeerResponse, + LastChequesResponse, NodeAddresses, - ReserveState, - ChainState, + NodeInfo, NumberString, - ExtendedTag, - PostageBatchBuckets, + Peer, + PeerBalance, + PingResponse, PostageBatch, - TransactionInfo, + PostageBatchBuckets, + RedistributionState, + RemovePeerResponse, + ReserveState, + Settlements, + Topology, TransactionHash, - NodeInfo, - BeeVersions, + TransactionInfo, WalletBalance, } from './types' +import { + BatchId, + BeeOptions, + CashoutOptions, + PostageBatchOptions, + RequestOptions, + STAMPS_DEPTH_MAX, + STAMPS_DEPTH_MIN, + Tag, + TransactionOptions, +} from './types' import { BeeArgumentError, BeeError } from './utils/error' -import { assertBeeUrl, stripLastSlash } from './utils/url' import { assertAddress, assertBatchId, @@ -50,26 +63,14 @@ import { assertTransactionOptions, isTag, } from './utils/type' -import { - BatchId, - BeeOptions, - CashoutOptions, - PostageBatchOptions, - RequestOptions, - STAMPS_DEPTH_MAX, - STAMPS_DEPTH_MIN, - Tag, - TransactionOptions, -} from './types' -import * as tag from './modules/debug/tag' -import * as stamps from './modules/debug/stamps' +import { assertBeeUrl, stripLastSlash } from './utils/url' // @ts-ignore: Needed TS otherwise complains about importing ESM package in CJS even though they are just typings import type { Options as KyOptions } from 'ky-universal' import { DEFAULT_KY_CONFIG, wrapRequestClosure, wrapResponseClosure } from './utils/http' -import { sleep } from './utils/sleep' import { deepMerge } from './utils/merge' +import { sleep } from './utils/sleep' export class BeeDebug { /** @@ -727,6 +728,17 @@ export class BeeDebug { await stake.stake(this.getKyOptionsForCall(options), amount, options) } + /** + * Get current status of node in redistribution game + * + * @param options + */ + async getRedistributionState(options?: RequestOptions): Promise { + assertRequestOptions(options) + + return stake.getRedistributionState(this.getKyOptionsForCall(options)) + } + private async waitForUsablePostageStamp(id: BatchId, timeout = 120_000): Promise { const TIME_STEP = 1500 for (let time = 0; time < timeout; time += TIME_STEP) { diff --git a/src/modules/debug/stake.ts b/src/modules/debug/stake.ts index 1d82577a..9a671d48 100644 --- a/src/modules/debug/stake.ts +++ b/src/modules/debug/stake.ts @@ -1,10 +1,11 @@ +import { BeeGenericResponse, NumberString, RedistributionState, TransactionOptions } from '../../types' import { http } from '../../utils/http' -import { BeeGenericResponse, NumberString, TransactionOptions } from '../../types' // @ts-ignore: Needed TS otherwise complains about importing ESM package in CJS even though they are just typings import type { Options as KyOptions } from 'ky' const STAKE_ENDPOINT = 'stake' +const REDISTRIBUTION_ENDPOINT = 'redistributionstate' interface GetStake { stakedAmount: NumberString @@ -50,3 +51,18 @@ export async function stake(kyOptions: KyOptions, amount: NumberString, options? headers, }) } + +/** + * Get current status of node in redistribution game + * + * @param kyOptions Ky Options for making requests + */ +export async function getRedistributionState(kyOptions: KyOptions): Promise { + const response = await http(kyOptions, { + method: 'get', + responseType: 'json', + path: REDISTRIBUTION_ENDPOINT, + }) + + return response.parsedData +} diff --git a/src/types/debug.ts b/src/types/debug.ts index 19e11489..dbfe3c12 100644 --- a/src/types/debug.ts +++ b/src/types/debug.ts @@ -1,5 +1,5 @@ -import { PublicKey, NumberString, Reference, TransactionHash, RequestOptions } from './index' import { HexEthAddress } from '../utils/eth' +import { NumberString, PublicKey, Reference, RequestOptions, TransactionHash } from './index' /** * Object that contains information about progress of upload of data to network. @@ -166,6 +166,17 @@ export enum BeeModes { DEV = 'dev', } +export interface RedistributionState { + isFrozen: boolean + round: number + lastWonRound: number + lastPlayedRound: number + lastFrozenRound: number + block: number + reward: NumberString + fees: NumberString +} + /** * Information about Bee node and its configuration */