From bfd085b9c89b56c448fef182c0dbe1062a537712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Thu, 19 May 2022 15:52:09 +0200 Subject: [PATCH] feat: wallet endpoint support --- src/bee-debug.ts | 12 ++++++++++++ src/modules/debug/states.ts | 18 +++++++++++++++++- src/types/debug.ts | 22 ++++++++++++++++++++++ test/integration/bee-debug-class.spec.ts | 13 +++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/bee-debug.ts b/src/bee-debug.ts index 6ebd4c18..d7b3f151 100644 --- a/src/bee-debug.ts +++ b/src/bee-debug.ts @@ -33,6 +33,7 @@ import type { TransactionHash, NodeInfo, BeeVersions, + WalletBalance, } from './types' import { BeeArgumentError } from './utils/error' import { assertBeeUrl, stripLastSlash } from './utils/url' @@ -481,6 +482,17 @@ export class BeeDebug { return states.getChainState(this.getKy(options)) } + /** + * Get wallet balances for xDai and BZZ of the Bee node + * + * @param options + */ + async getWalletBalance(options?: RequestOptions): Promise { + assertRequestOptions(options) + + return states.getWalletBalance(this.getKy(options)) + } + /** * Creates new postage batch from the funds that the node has available in its Ethereum account. * diff --git a/src/modules/debug/states.ts b/src/modules/debug/states.ts index 9c8d1d8f..f67aee60 100644 --- a/src/modules/debug/states.ts +++ b/src/modules/debug/states.ts @@ -1,7 +1,8 @@ import { http } from '../../utils/http' -import { ChainState, Ky, ReserveState } from '../../types' +import { ChainState, Ky, ReserveState, WalletBalance } from '../../types' const RESERVE_STATE_ENDPOINT = 'reservestate' +const WALLET_ENDPOINT = 'wallet' const CHAIN_STATE_ENDPOINT = 'chainstate' /** @@ -33,3 +34,18 @@ export async function getChainState(ky: Ky): Promise { return response.data } + +/** + * Get wallet balances for xDai and BZZ of the node + * + * @param ky Ky debug instance + */ +export async function getWalletBalance(ky: Ky): Promise { + const response = await http(ky, { + method: 'get', + path: `${WALLET_ENDPOINT}`, + responseType: 'json', + }) + + return response.data +} diff --git a/src/types/debug.ts b/src/types/debug.ts index 3f27f8af..54739f3b 100644 --- a/src/types/debug.ts +++ b/src/types/debug.ts @@ -254,3 +254,25 @@ export interface ChainState { totalAmount: NumberString currentPrice: NumberString } + +export interface WalletBalance { + /** + * Balance of BZZ tokens + */ + bzz: NumberString + + /** + * Balance of xDai + */ + xDai: NumberString + + /** + * Chain network ID to which the Bee node is connected + */ + chainID: number + + /** + * Chequebook contract address + */ + contractAddress: string +} diff --git a/test/integration/bee-debug-class.spec.ts b/test/integration/bee-debug-class.spec.ts index 6cdf6454..7e636bb3 100644 --- a/test/integration/bee-debug-class.spec.ts +++ b/test/integration/bee-debug-class.spec.ts @@ -122,4 +122,17 @@ describe('Bee Debug class', () => { ) }) }) + + describe('Wallet', () => { + it('should return the nodes balances and other data', async () => { + expect(await beeDebug.getWalletBalance()).toEqual( + expect.objectContaining({ + bzz: expect.stringMatching(/^[0-9]+$/), + xDai: expect.stringMatching(/^[0-9]+$/), + chainID: expect.any(Number), + contractAddress: expect.stringMatching(/^0x[0-9a-f]{40}$/), + }), + ) + }) + }) })