-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseTronResources.ts
More file actions
104 lines (93 loc) · 2.98 KB
/
useTronResources.ts
File metadata and controls
104 lines (93 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import BigNumber from 'bignumber.js';
import { selectTronResourcesBySelectedAccountGroup } from '../../../../selectors/assets/assets-list';
import { TRON_RESOURCE } from '../../../../core/Multichain/constants';
export interface TronResource {
type: 'energy' | 'bandwidth';
current: number;
max: number;
/**
* Percentage of the resource that is currently available, in the range 0–100.
*/
percentage: number;
}
function createResource(
type: TronResource['type'],
current: number,
max: number,
): TronResource {
const currentBN = new BigNumber(current);
// Use max of 1 only for percentage calculation to avoid division by zero
const divisor = new BigNumber(Math.max(1, max));
const percentageBN = currentBN.dividedBy(divisor).multipliedBy(100);
const percentage = BigNumber.min(
100,
BigNumber.max(0, percentageBN),
).toNumber();
return {
type,
current,
max, // Keep actual max for display (can be 0)
percentage,
};
}
/**
* Hook to build Tron daily resource data (energy and bandwidth) for the
* currently selected account group.
*
* It normalizes raw Tron resource assets into a simple model consumable by
* UI components. The max capacity is derived only from the base max values,
* matching the behavior used in the extension codebase.
*/
export const useTronResources = (): {
energy: TronResource;
bandwidth: TronResource;
} => {
const tronResources = useSelector(selectTronResourcesBySelectedAccountGroup);
return useMemo(() => {
let energy;
let bandwidth;
let maxEnergy;
let maxBandwidth;
// Extract the different Tron resource entries from the flat list.
for (const asset of tronResources) {
switch (asset.symbol?.toLowerCase()) {
case TRON_RESOURCE.ENERGY:
energy = asset;
break;
case TRON_RESOURCE.BANDWIDTH:
bandwidth = asset;
break;
case TRON_RESOURCE.MAX_ENERGY:
maxEnergy = asset;
break;
case TRON_RESOURCE.MAX_BANDWIDTH:
maxBandwidth = asset;
break;
default:
break;
}
}
const parseValue = (value?: string | number): number => {
if (value === undefined || value === null) return 0;
// Remove commas from string values before parsing
const cleanValue =
typeof value === 'string' ? value.replace(/,/g, '') : value;
const num = Number(cleanValue);
return Number.isNaN(num) ? 0 : num;
};
const energyCurrent = parseValue(energy?.balance);
const bandwidthCurrent = parseValue(bandwidth?.balance);
const maxEnergyValue = parseValue(maxEnergy?.balance);
const maxBandwidthValue = parseValue(maxBandwidth?.balance);
return {
energy: createResource('energy', energyCurrent, maxEnergyValue),
bandwidth: createResource(
'bandwidth',
bandwidthCurrent,
maxBandwidthValue,
),
};
}, [tronResources]);
};