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: wallet endpoint support #683

Merged
merged 1 commit into from
May 20, 2022
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
12 changes: 12 additions & 0 deletions src/bee-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
TransactionHash,
NodeInfo,
BeeVersions,
WalletBalance,
} from './types'
import { BeeArgumentError } from './utils/error'
import { assertBeeUrl, stripLastSlash } from './utils/url'
Expand Down Expand Up @@ -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<WalletBalance> {
assertRequestOptions(options)

return states.getWalletBalance(this.getKy(options))
}

/**
* Creates new postage batch from the funds that the node has available in its Ethereum account.
*
Expand Down
18 changes: 17 additions & 1 deletion src/modules/debug/states.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -33,3 +34,18 @@ export async function getChainState(ky: Ky): Promise<ChainState> {

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<WalletBalance> {
const response = await http<WalletBalance>(ky, {
method: 'get',
path: `${WALLET_ENDPOINT}`,
responseType: 'json',
})

return response.data
}
22 changes: 22 additions & 0 deletions src/types/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions test/integration/bee-debug-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}$/),
}),
)
})
})
})