Skip to content

Commit 98ba68d

Browse files
authored
feat(neuron-ui): add difficulty formatter (#1105)
* feat(neuron-ui): add difficulty formatter * test(neuron-ui): fix the tests of difficulty formatter
1 parent df7f4db commit 98ba68d

9 files changed

Lines changed: 51 additions & 8 deletions

File tree

packages/neuron-ui/src/components/Overview/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import PropertyList, { Property } from 'widgets/PropertyList'
77
import { StateWithDispatch } from 'states/stateProvider/reducer'
88
import { updateTransactionList } from 'states/stateProvider/actionCreators'
99

10-
import { localNumberFormatter, shannonToCKBFormatter } from 'utils/formatters'
10+
import { localNumberFormatter, shannonToCKBFormatter, difficultyFormatter } from 'utils/formatters'
1111
import { epochParser } from 'utils/parsers'
1212
import { PAGE_SIZE, Routes, CONFIRMATION_THRESHOLD } from 'utils/const'
1313
import { backToTop } from 'utils/animations'
@@ -104,7 +104,7 @@ const Overview = ({
104104
},
105105
{
106106
label: t('overview.difficulty'),
107-
value: localNumberFormatter(+difficulty),
107+
value: difficultyFormatter(difficulty),
108108
},
109109
],
110110
[t, chain, epoch, difficulty, tipBlockNumber]

packages/neuron-ui/src/containers/Main/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const useSyncChainData = ({ chainURL, dispatch }: { chainURL: string; dis
4242
tipBlockHash: header.hash,
4343
tipBlockTimestamp: +header.timestamp,
4444
chain: chainInfo.chain,
45-
difficulty: `${BigInt(chainInfo.difficulty)}`,
45+
difficulty: BigInt(chainInfo.difficulty),
4646
epoch: chainInfo.epoch,
4747
},
4848
})

packages/neuron-ui/src/states/initStates/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const appState: State.App = {
55
tipBlockHash: '',
66
tipBlockTimestamp: 0,
77
chain: '',
8-
difficulty: '',
8+
difficulty: BigInt(0),
99
epoch: '',
1010
send: {
1111
txID: '',

packages/neuron-ui/src/states/stateProvider/reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export const reducer = (
197197
...app,
198198
tipBlockNumber: '0',
199199
chain: '',
200-
difficulty: '',
200+
difficulty: BigInt(0),
201201
epoch: '',
202202
},
203203
chain: {

packages/neuron-ui/src/stories/Overview.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const stateTemplate = {
1616
app: {
1717
...initStates.app,
1818
epoch: '1',
19-
difficulty: '0x111111',
19+
difficulty: BigInt('0x111111'),
2020
chain: 'chain_dev',
2121
},
2222
wallet: {
@@ -77,7 +77,7 @@ stories.addDecorator(withKnobs).add('With knobs', () => {
7777
app: {
7878
...initStates.app,
7979
epoch: text('Epoch', '1'),
80-
difficulty: text('Difficulty', '0x111'),
80+
difficulty: BigInt(100000),
8181
chain: text('Chain', 'chain_dev'),
8282
},
8383
wallet: {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{ "difficulty": 0, "expected": "0 H/s" },
3+
{ "difficulty": 123, "expected": "123 H/s" },
4+
{ "difficulty": 12345, "expected": "12,345 H/s" },
5+
{ "difficulty": 123454669, "expected": "123,454.67 KH/s" },
6+
{ "difficulty": 1234546698945, "expected": "1,234.55 GH/s" },
7+
{ "difficulty": 100003439, "expected": "100,003.44 KH/s" }
8+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { difficultyFormatter } from 'utils/formatters'
2+
import fixtures from './fixtures.json'
3+
4+
describe('test difficulty formatter', () => {
5+
test.each(fixtures)(`%s => %s`, ({ difficulty, expected }) => {
6+
const str = difficultyFormatter(BigInt(difficulty))
7+
expect(str).toBe(expected)
8+
})
9+
})

packages/neuron-ui/src/types/App/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ declare namespace State {
7777
tipBlockHash: string
7878
tipBlockTimestamp: number
7979
chain: string
80-
difficulty: string
80+
difficulty: bigint
8181
epoch: string
8282
send: Send
8383
passwordRequest: {

packages/neuron-ui/src/utils/formatters.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,39 @@ export const failureResToNotification = (res: any): State.Message => {
177177
}
178178
}
179179

180+
export const difficultyFormatter = (value: bigint) => {
181+
const units = new Map([
182+
['YH/s', 1e24],
183+
['ZH/s', 1e21],
184+
['EH/s', 1e18],
185+
['PH/s', 1e15],
186+
['TH/s', 1e12],
187+
['GH/s', 1e9],
188+
['MH/s', 1e6],
189+
['KH/s', 1e3],
190+
])
191+
192+
/* eslint-disable no-restricted-syntax */
193+
for (const [unit, range] of units) {
194+
if (value >= range * 1e3) {
195+
const integer = value / BigInt(range)
196+
const decimal = (Number(value) / range).toFixed(2).split('.')[1]
197+
return `${localNumberFormatter(integer)}.${decimal} ${unit}`
198+
}
199+
}
200+
/* eslint-enable no-restricted-syntax */
201+
202+
return `${localNumberFormatter(value)} H/s`
203+
}
204+
180205
export default {
181206
queryFormatter,
182207
currencyFormatter,
183208
CKBToShannonFormatter,
184209
shannonToCKBFormatter,
185210
localNumberFormatter,
186211
uniformTimeFormatter,
212+
difficultyFormatter,
187213
addressesToBalance,
188214
outputsToTotalAmount,
189215
failureResToNotification,

0 commit comments

Comments
 (0)