chore: update bee-js to 0.8.1 version (#78)
* chore: upgrade to bee-js 0.8.1 * refactor: removed toString on number types, Token now accepts numbers * test: removed fromBZZbaseUnit test, makeBigNumber accepts number now * chore: fix logic error when displaying Cashout button
This commit is contained in:
+41
-19
@@ -1,17 +1,27 @@
|
||||
import { LastCashoutActionResponse, PeerBalance, Settlements } from '@ethersphere/bee-js'
|
||||
import { LastCashoutActionResponse } from '@ethersphere/bee-js'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Token } from '../models/Token'
|
||||
import { beeDebugApi } from '../services/bee'
|
||||
import { useApiPeerBalances, useApiSettlements } from './apiHooks'
|
||||
import { Balance, Settlement, useApiPeerBalances, useApiSettlements } from './apiHooks'
|
||||
|
||||
interface UseAccountingHook {
|
||||
isLoading: boolean
|
||||
isLoadingUncashed: boolean
|
||||
error: Error | null
|
||||
totalsent: number
|
||||
totalreceived: number
|
||||
totalsent: Token
|
||||
totalreceived: Token
|
||||
accounting: Accounting[] | null
|
||||
}
|
||||
|
||||
export interface Accounting {
|
||||
peer: string
|
||||
uncashedAmount: Token
|
||||
balance: Token
|
||||
received: Token
|
||||
sent: Token
|
||||
total: Token
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the balances, settlements and uncashedAmounts arrays into single array which is sorted by uncashed amounts (if any)
|
||||
*
|
||||
@@ -22,8 +32,8 @@ interface UseAccountingHook {
|
||||
* @returns
|
||||
*/
|
||||
function mergeAccounting(
|
||||
balances?: PeerBalance[],
|
||||
settlements?: Settlements[],
|
||||
balances: Balance[] | null,
|
||||
settlements?: Settlement[],
|
||||
uncashedAmounts?: LastCashoutActionResponse[],
|
||||
): Accounting[] | null {
|
||||
// Settlements or balances are still loading or there is an error -> return null
|
||||
@@ -34,22 +44,38 @@ function mergeAccounting(
|
||||
balances.forEach(
|
||||
// Some peers may not have settlement but all have balance (therefore initialize sent, received and uncashed to 0)
|
||||
({ peer, balance }) =>
|
||||
(accounting[peer] = { peer, balance, sent: 0, received: 0, uncashedAmount: 0, total: balance }),
|
||||
(accounting[peer] = {
|
||||
peer,
|
||||
balance,
|
||||
sent: new Token('0'),
|
||||
received: new Token('0'),
|
||||
uncashedAmount: new Token('0'),
|
||||
total: balance,
|
||||
}),
|
||||
)
|
||||
|
||||
settlements.forEach(
|
||||
({ peer, sent, received }) =>
|
||||
(accounting[peer] = { ...accounting[peer], sent, received, total: accounting[peer].balance + received - sent }),
|
||||
(accounting[peer] = {
|
||||
...accounting[peer],
|
||||
sent,
|
||||
received,
|
||||
total: new Token(accounting[peer].balance.toBigNumber.plus(received.toBigNumber).minus(sent.toBigNumber)),
|
||||
}),
|
||||
)
|
||||
|
||||
// If there are no cheques (and hence last cashout actions), we don't need to sort and can return values right away
|
||||
if (!uncashedAmounts) return Object.values(accounting)
|
||||
|
||||
uncashedAmounts?.forEach(
|
||||
({ peer, cumulativePayout }) => (accounting[peer].uncashedAmount = accounting[peer].received - cumulativePayout),
|
||||
)
|
||||
uncashedAmounts?.forEach(({ peer, cumulativePayout }) => {
|
||||
accounting[peer].uncashedAmount = new Token(
|
||||
accounting[peer].received.toBigNumber.minus(cumulativePayout.toString()),
|
||||
)
|
||||
})
|
||||
|
||||
return Object.values(accounting).sort((a, b) => b.uncashedAmount - a.uncashedAmount)
|
||||
return Object.values(accounting).sort((a, b) =>
|
||||
b.uncashedAmount.toBigNumber.minus(a.uncashedAmount.toBigNumber).toNumber(),
|
||||
)
|
||||
}
|
||||
|
||||
export const useAccounting = (): UseAccountingHook => {
|
||||
@@ -76,18 +102,14 @@ export const useAccounting = (): UseAccountingHook => {
|
||||
.finally(() => setIsloadingUncashed(false))
|
||||
}, [settlements, isLoadingUncashed, uncashedAmounts, error])
|
||||
|
||||
const accounting = mergeAccounting(
|
||||
balances.peerBalances?.balances,
|
||||
settlements.settlements?.settlements,
|
||||
uncashedAmounts,
|
||||
)
|
||||
const accounting = mergeAccounting(balances.peerBalances, settlements.settlements?.settlements, uncashedAmounts)
|
||||
|
||||
return {
|
||||
isLoading: settlements.isLoadingSettlements || balances.isLoadingPeerBalances,
|
||||
isLoadingUncashed,
|
||||
error,
|
||||
accounting,
|
||||
totalsent: settlements.settlements?.totalsent || 0,
|
||||
totalreceived: settlements.settlements?.totalreceived || 0,
|
||||
totalsent: settlements.settlements?.totalsent || new Token('0'),
|
||||
totalreceived: settlements.settlements?.totalreceived || new Token('0'),
|
||||
}
|
||||
}
|
||||
|
||||
+58
-19
@@ -3,11 +3,7 @@ import { useState, useEffect } from 'react'
|
||||
import {
|
||||
NodeAddresses,
|
||||
ChequebookAddressResponse,
|
||||
ChequebookBalanceResponse,
|
||||
BalanceResponse,
|
||||
LastChequesResponse,
|
||||
AllSettlements,
|
||||
LastCashoutActionResponse,
|
||||
Health,
|
||||
Peer,
|
||||
Topology,
|
||||
@@ -16,6 +12,7 @@ import {
|
||||
|
||||
import { beeDebugApi, beeApi } from '../services/bee'
|
||||
import axios from 'axios'
|
||||
import { Token } from '../models/Token'
|
||||
|
||||
export interface HealthHook {
|
||||
health: boolean
|
||||
@@ -189,14 +186,19 @@ export const useApiNodePeers = (): NodePeersHook => {
|
||||
return { peers, isLoading, error }
|
||||
}
|
||||
|
||||
export interface ChequebookBalance {
|
||||
totalBalance: Token
|
||||
availableBalance: Token
|
||||
}
|
||||
|
||||
export interface ChequebookBalanceHook {
|
||||
chequebookBalance: ChequebookBalanceResponse | null
|
||||
chequebookBalance: ChequebookBalance | null
|
||||
isLoadingChequebookBalance: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export const useApiChequebookBalance = (): ChequebookBalanceHook => {
|
||||
const [chequebookBalance, setChequebookBalance] = useState<ChequebookBalanceResponse | null>(null)
|
||||
const [chequebookBalance, setChequebookBalance] = useState<ChequebookBalance | null>(null)
|
||||
const [isLoadingChequebookBalance, setLoading] = useState<boolean>(true)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
@@ -204,8 +206,12 @@ export const useApiChequebookBalance = (): ChequebookBalanceHook => {
|
||||
setLoading(true)
|
||||
beeDebugApi.chequebook
|
||||
.balance()
|
||||
.then(res => {
|
||||
setChequebookBalance(res)
|
||||
.then(({ totalBalance, availableBalance }) => {
|
||||
const balance = {
|
||||
totalBalance: new Token(totalBalance),
|
||||
availableBalance: new Token(availableBalance),
|
||||
}
|
||||
setChequebookBalance(balance)
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
@@ -218,14 +224,19 @@ export const useApiChequebookBalance = (): ChequebookBalanceHook => {
|
||||
return { chequebookBalance, isLoadingChequebookBalance, error }
|
||||
}
|
||||
|
||||
export interface Balance {
|
||||
peer: string
|
||||
balance: Token
|
||||
}
|
||||
|
||||
export interface PeerBalanceHook {
|
||||
peerBalances: BalanceResponse | null
|
||||
peerBalances: Balance[] | null
|
||||
isLoadingPeerBalances: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export const useApiPeerBalances = (): PeerBalanceHook => {
|
||||
const [peerBalances, setPeerBalances] = useState<BalanceResponse | null>(null)
|
||||
const [peerBalances, setPeerBalances] = useState<Balance[] | null>(null)
|
||||
const [isLoadingPeerBalances, setLoading] = useState<boolean>(true)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
@@ -234,7 +245,9 @@ export const useApiPeerBalances = (): PeerBalanceHook => {
|
||||
beeDebugApi.balance
|
||||
.balances()
|
||||
.then(res => {
|
||||
setPeerBalances(res)
|
||||
// for some reason sometimes these are numbers and not BigInts
|
||||
const balances = res.balances.map(({ peer, balance }) => ({ peer, balance: new Token(balance) }))
|
||||
setPeerBalances(balances)
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
@@ -305,14 +318,26 @@ export const useApiPeerLastCheque = (peerId: string): PeerLastChequesHook => {
|
||||
return { peerCheque, isLoadingPeerCheque, error }
|
||||
}
|
||||
|
||||
export interface Settlement {
|
||||
peer: string
|
||||
received: Token
|
||||
sent: Token
|
||||
}
|
||||
|
||||
export interface Settlements {
|
||||
totalreceived: Token
|
||||
totalsent: Token
|
||||
settlements: Settlement[]
|
||||
}
|
||||
|
||||
export interface SettlementsHook {
|
||||
settlements: AllSettlements | null
|
||||
settlements: Settlements | null
|
||||
isLoadingSettlements: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export const useApiSettlements = (): SettlementsHook => {
|
||||
const [settlements, setSettlements] = useState<AllSettlements | null>(null)
|
||||
const [settlements, setSettlements] = useState<Settlements | null>(null)
|
||||
const [isLoadingSettlements, setLoading] = useState<boolean>(true)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
@@ -320,8 +345,17 @@ export const useApiSettlements = (): SettlementsHook => {
|
||||
setLoading(true)
|
||||
beeDebugApi.settlements
|
||||
.getSettlements()
|
||||
.then(res => {
|
||||
setSettlements(res)
|
||||
.then(({ totalreceived, settlements, totalsent }) => {
|
||||
const set = {
|
||||
totalreceived: new Token(totalreceived),
|
||||
totalsent: new Token(totalsent),
|
||||
settlements: settlements.map(({ peer, received, sent }) => ({
|
||||
peer,
|
||||
received: new Token(received),
|
||||
sent: new Token(sent),
|
||||
})),
|
||||
}
|
||||
setSettlements(set)
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
@@ -334,14 +368,19 @@ export const useApiSettlements = (): SettlementsHook => {
|
||||
return { settlements, isLoadingSettlements, error }
|
||||
}
|
||||
|
||||
export interface LastCashout {
|
||||
peer: string
|
||||
cumulativePayout: Token
|
||||
}
|
||||
|
||||
export interface PeerLastCashoutHook {
|
||||
peerCashout: LastCashoutActionResponse | null
|
||||
peerCashout: LastCashout | null
|
||||
isLoadingPeerCashout: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export const useApiPeerLastCashout = (peerId: string): PeerLastCashoutHook => {
|
||||
const [peerCashout, setPeerCashout] = useState<LastCashoutActionResponse | null>(null)
|
||||
const [peerCashout, setPeerCashout] = useState<LastCashout | null>(null)
|
||||
const [isLoadingPeerCashout, setLoading] = useState<boolean>(true)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
@@ -349,8 +388,8 @@ export const useApiPeerLastCashout = (peerId: string): PeerLastCashoutHook => {
|
||||
setLoading(true)
|
||||
beeDebugApi.chequebook
|
||||
.getPeerLastCashout(peerId)
|
||||
.then(res => {
|
||||
setPeerCashout(res)
|
||||
.then(({ peer, cumulativePayout }) => {
|
||||
setPeerCashout({ peer, cumulativePayout: new Token(cumulativePayout) })
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
|
||||
+10
-1
@@ -1,4 +1,6 @@
|
||||
import { ChequebookAddressResponse } from '@ethersphere/bee-js'
|
||||
import {
|
||||
ChequebookBalance,
|
||||
useApiChequebookAddress,
|
||||
useApiChequebookBalance,
|
||||
useApiHealth,
|
||||
@@ -8,6 +10,11 @@ import {
|
||||
useLatestBeeRelease,
|
||||
} from './apiHooks'
|
||||
|
||||
export interface StatusChequebookHook extends StatusHookCommon {
|
||||
chequebookBalance: ChequebookBalance | null
|
||||
chequebookAddress: ChequebookAddressResponse | null
|
||||
}
|
||||
|
||||
export const useStatusNodeVersion = (): StatusNodeVersionHook => {
|
||||
const { latestBeeRelease, isLoadingLatestBeeRelease } = useLatestBeeRelease()
|
||||
const { nodeHealth, isLoadingNodeHealth } = useDebugApiHealth()
|
||||
@@ -66,7 +73,9 @@ export const useStatusChequebook = (): StatusChequebookHook => {
|
||||
return {
|
||||
isLoading: isLoadingChequebookAddress || isLoadingChequebookBalance,
|
||||
isOk:
|
||||
Boolean(chequebookAddress?.chequebookaddress) && chequebookBalance !== null && chequebookBalance.totalBalance > 0,
|
||||
Boolean(chequebookAddress?.chequebookaddress) &&
|
||||
chequebookBalance !== null &&
|
||||
chequebookBalance?.totalBalance.toBigNumber.isGreaterThan(0),
|
||||
chequebookBalance,
|
||||
chequebookAddress,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user