Skip to content

Commit

Permalink
feat: chain and reserve state endpoints (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau authored May 26, 2021
1 parent 64ecec0 commit 0ec57e9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/bee-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as balance from './modules/debug/balance'
import * as chequebook from './modules/debug/chequebook'
import * as settlements from './modules/debug/settlements'
import * as status from './modules/debug/status'
import * as states from './modules/debug/states'
import type {
Address,
Peer,
Expand All @@ -23,6 +24,8 @@ import type {
PingResponse,
Health,
NodeAddresses,
ReserveState,
ChainState,
} from './types'
import { assertBeeUrl, stripLastSlash } from './utils/url'
import { assertAddress, assertInteger } from './utils/type'
Expand Down Expand Up @@ -233,4 +236,18 @@ export class BeeDebug {
isSupportedVersion(): Promise<boolean> | never {
return status.isSupportedVersion(this.url)
}

/**
* Get reserve state
*/
getReserveState(): Promise<ReserveState> {
return states.getReserveState(this.url)
}

/**
* Get chain state
*/
getChainState(): Promise<ChainState> {
return states.getChainState(this.url)
}
}
36 changes: 36 additions & 0 deletions src/modules/debug/states.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { safeAxios } from '../../utils/safeAxios'
import { ChainState, ReserveState } from '../../types'

const RESERVE_STATE_ENDPOINT = '/reservestate'
const CHAIN_STATE_ENDPOINT = '/chainstate'

/**
* Get state of reserve
*
* @param url Bee debug URL
*/
export async function getReserveState(url: string): Promise<ReserveState> {
const response = await safeAxios<ReserveState>({
method: 'get',
url: `${url}${RESERVE_STATE_ENDPOINT}`,
responseType: 'json',
})

return response.data
}

/**
* Get state of reserve
*
* @param url Bee debug URL
*/
export async function getChainState(url: string): Promise<ChainState> {
const response = await safeAxios<ChainState>({
method: 'get',
url: `${url}${CHAIN_STATE_ENDPOINT}`,
responseType: 'json',
forceBigInt: true,
})

return response.data
}
13 changes: 13 additions & 0 deletions src/types/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,16 @@ export interface Topology {
export interface PingResponse {
rtt: string
}

export interface ReserveState {
radius: number
available: number
outer: number
inner: number
}

export interface ChainState {
block: bigint
totalAmount: bigint
currentPrice: bigint
}
34 changes: 34 additions & 0 deletions test/integration/modules/debug/states.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { beeDebugUrl, commonMatchers } from '../../../utils'
import * as states from '../../../../src/modules/debug/states'

const BEE_DEBUG_URL = beeDebugUrl()
commonMatchers()

describe('modules/states', () => {
describe('chainstate', () => {
it('should fetch the chainstate', async () => {
const state = await states.getChainState(BEE_DEBUG_URL)

expect(state).toHaveProperty('block')
expect(state).toHaveProperty('totalAmount')
expect(state).toHaveProperty('currentPrice')
expect(state.block).toBeType('bigint')
expect(state.totalAmount).toBeType('bigint')
expect(state.currentPrice).toBeType('bigint')
})
})
describe('ReserveState', () => {
it('should fetch the reserve state', async () => {
const state = await states.getReserveState(BEE_DEBUG_URL)

expect(state).toHaveProperty('radius')
expect(state).toHaveProperty('available')
expect(state).toHaveProperty('outer')
expect(state).toHaveProperty('inner')
expect(state.radius).toBeType('number')
expect(state.available).toBeType('number')
expect(state.outer).toBeType('number')
expect(state.inner).toBeType('number')
})
})
})

0 comments on commit 0ec57e9

Please sign in to comment.