Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redistribution state endpoint #834

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 47 additions & 35 deletions src/bee-debug.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
/**
Expand Down Expand Up @@ -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<RedistributionState> {
assertRequestOptions(options)

return stake.getRedistributionState(this.getKyOptionsForCall(options))
}

private async waitForUsablePostageStamp(id: BatchId, timeout = 120_000): Promise<void> {
const TIME_STEP = 1500
for (let time = 0; time < timeout; time += TIME_STEP) {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/debug/stake.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<RedistributionState> {
const response = await http<RedistributionState>(kyOptions, {
method: 'get',
responseType: 'json',
path: REDISTRIBUTION_ENDPOINT,
})

return response.parsedData
}
13 changes: 12 additions & 1 deletion src/types/debug.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
*/
Expand Down