Skip to content

Commit fd07961

Browse files
authored
Merge pull request #1449 from nervosnetwork/refine-nervos-dao-record-component
[ᚬrc/v0.29.0] feat: refine the nervos dao record component
2 parents e6d5717 + 43d6755 commit fd07961

File tree

15 files changed

+309
-201
lines changed

15 files changed

+309
-201
lines changed

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import React, { useRef } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import Button from 'widgets/Button'
44
import { useDialog } from 'utils/hooks'
5+
import { WITHDRAW_EPOCHS } from 'utils/const'
56

67
import styles from './compensationPeriodDialog.module.scss'
78

89
interface CompensationPeriodDialogProps {
910
onDismiss: () => void
1011
compensationPeriod: {
11-
currentEpochNumber: bigint
12-
currentEpochIndex: bigint
13-
currentEpochLength: bigint
14-
targetEpochNumber: bigint
12+
currentEpochValue: number
13+
targetEpochValue: number
1514
} | null
1615
}
1716

@@ -20,18 +19,11 @@ const CompensationPeriodDialog = ({ onDismiss, compensationPeriod }: Compensatio
2019
const dialogRef = useRef<HTMLDialogElement | null>(null)
2120
useDialog({ show: compensationPeriod, dialogRef, onClose: onDismiss })
2221

23-
let pastEpochs = 0
24-
if (compensationPeriod) {
25-
pastEpochs =
26-
Number(compensationPeriod.currentEpochNumber) -
27-
Number(compensationPeriod.targetEpochNumber) +
28-
180 +
29-
(compensationPeriod.currentEpochLength === BigInt(0)
30-
? 0
31-
: +(Number(compensationPeriod.currentEpochIndex) / Number(compensationPeriod.currentEpochLength)).toFixed(1))
32-
}
22+
const pastEpochs = compensationPeriod
23+
? +(compensationPeriod.currentEpochValue - compensationPeriod.targetEpochValue + WITHDRAW_EPOCHS).toFixed(1)
24+
: 0
3325

34-
const totalHours = Math.ceil((180 - pastEpochs) * 4)
26+
const totalHours = Math.ceil((WITHDRAW_EPOCHS - pastEpochs) * 4)
3527
const leftDays = Math.floor(totalHours / 24)
3628
const leftHours = totalHours % 24
3729

packages/neuron-ui/src/components/NervosDAO/hooks.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TFunction } from 'i18next'
33
import { AppActions, StateAction } from 'states/stateProvider/reducer'
44
import { verifyAmount } from 'utils/validators'
55
import { epochParser } from 'utils/parsers'
6-
import calculateClaimEpochNumber from 'utils/calculateClaimEpochNumber'
6+
import calculateClaimEpochValue from 'utils/calculateClaimEpochValue'
77
import calculateAPC from 'utils/calculateAPC'
88
import { updateNervosDaoData, clearNervosDaoData } from 'states/stateProvider/actionCreators'
99

@@ -25,7 +25,7 @@ import {
2525
generateDaoDepositTx,
2626
generateDaoClaimTx,
2727
} from 'services/remote'
28-
import { getHeader, getHeaderByNumber, calculateDaoMaximumWithdraw } from 'services/chain'
28+
import { ckbCore, getHeaderByNumber, calculateDaoMaximumWithdraw } from 'services/chain'
2929

3030
let timer: NodeJS.Timeout
3131

@@ -93,7 +93,8 @@ export const useInitData = ({
9393
clearNervosDaoData()(dispatch)
9494
clearGeneratedTx()
9595
}
96-
}, [clearGeneratedTx, dispatch, updateDepositValue, wallet.id, wallet.balance, setGenesisBlockTimestamp])
96+
// eslint-disable-next-line
97+
}, [])
9798

9899
export const useClearGeneratedTx = (dispatch: React.Dispatch<StateAction>) =>
99100
useCallback(() => {
@@ -238,12 +239,13 @@ export const useCompensationPeriods = ({
238239
try {
239240
const depositEpochInfo = epochParser(depositEpoch)
240241
const currentEpochInfo = epochParser(currentEpoch)
241-
const targetEpochNumber = calculateClaimEpochNumber(depositEpochInfo, currentEpochInfo)
242+
const targetEpochValue = calculateClaimEpochValue(depositEpochInfo, currentEpochInfo)
242243
return {
243-
targetEpochNumber,
244-
currentEpochNumber: currentEpochInfo.number,
245-
currentEpochIndex: currentEpochInfo.index,
246-
currentEpochLength: currentEpochInfo.length,
244+
depositEpochValue:
245+
Number(depositEpochInfo.number) + Number(depositEpochInfo.index) / Number(depositEpochInfo.length),
246+
targetEpochValue,
247+
currentEpochValue:
248+
Number(currentEpochInfo.number) + Number(currentEpochInfo.index) / Number(currentEpochInfo.length),
247249
}
248250
} catch {
249251
return null
@@ -491,11 +493,12 @@ export const useUpdateDepositEpochList = ({
491493
useEffect(() => {
492494
if (connectionStatus === 'online') {
493495
Promise.all(
494-
records.map(({ blockHash }) =>
495-
getHeader(blockHash)
496+
records.map(({ daoData, depositOutPoint, blockNumber }) => {
497+
const depositBlockNumber = depositOutPoint ? ckbCore.utils.toHexInLittleEndian(daoData) : blockNumber
498+
return getHeaderByNumber(BigInt(depositBlockNumber))
496499
.then(header => header.epoch)
497500
.catch(() => null)
498-
)
501+
})
499502
).then(epochsList => setDepositEpochList(epochsList))
500503
}
501504
}, [records, setDepositEpochList, connectionStatus])

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,21 @@ const NervosDAO = () => {
132132
<h2 className={styles.recordsTitle}>{t('nervos-dao.deposit-records')}</h2>
133133
<Stack className={styles.recordsContainer}>
134134
{records.map((record, i) => {
135-
let stage = 'deposited'
136-
if (record.depositOutPoint) {
137-
stage = 'withdrawing'
135+
const props = {
136+
...record,
137+
tipBlockTimestamp,
138+
compensationPeriod: compensationPeriods[i],
139+
withdrawCapacity: withdrawList[i],
140+
key: `${record.outPoint.txHash}-${record.outPoint.index}`,
141+
onClick: onActionClick,
142+
onCompensationPeriodExplanationClick,
143+
tipBlockNumber,
144+
depositEpoch: depositEpochList[i] || '',
145+
currentEpoch: epoch,
146+
genesisBlockTimestamp,
147+
connectionStatus,
138148
}
139-
return (
140-
<DAORecord
141-
{...record}
142-
compensationPeriod={compensationPeriods[i]}
143-
withdraw={withdrawList[i]}
144-
actionLabel={t(`nervos-dao.${stage}-action-label`)}
145-
key={JSON.stringify(record.outPoint)}
146-
onClick={onActionClick}
147-
onCompensationPeriodExplanationClick={onCompensationPeriodExplanationClick}
148-
tipBlockNumber={tipBlockNumber}
149-
tipBlockTimestamp={tipBlockTimestamp}
150-
epoch={epoch}
151-
genesisBlockTimestamp={genesisBlockTimestamp}
152-
connectionStatus={connectionStatus}
153-
dispatch={dispatch}
154-
/>
155-
)
149+
return <DAORecord {...props} />
156150
})}
157151
</Stack>
158152
</>
@@ -169,7 +163,7 @@ const NervosDAO = () => {
169163
connectionStatus,
170164
genesisBlockTimestamp,
171165
tipBlockTimestamp,
172-
dispatch,
166+
depositEpochList,
173167
])
174168

175169
const MemoizedDepositDialog = useMemo(() => {
@@ -188,6 +182,7 @@ const NervosDAO = () => {
188182
isTxGenerated={!!send.generatedTx}
189183
/>
190184
)
185+
// eslint-disable-next-line
191186
}, [
192187
showDepositDialog,
193188
depositValue,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { useEffect } from 'react'
2+
import { getHeaderByNumber } from 'services/chain'
3+
import { MILLISECONDS_IN_YEAR } from 'utils/const'
4+
import calculateAPC from 'utils/calculateAPC'
5+
6+
export const useUpdateEpochs = ({
7+
depositOutPoint,
8+
blockNumber,
9+
setWithdrawingEpoch,
10+
}: {
11+
depositOutPoint: CKBComponents.OutPoint | undefined
12+
blockNumber: CKBComponents.BlockNumber
13+
setWithdrawingEpoch: React.Dispatch<string>
14+
}) => {
15+
useEffect(() => {
16+
if (depositOutPoint) {
17+
// It's under withdrawing, the block number is the one that withdrawing starts at
18+
getHeaderByNumber(BigInt(blockNumber))
19+
.then(header => {
20+
setWithdrawingEpoch(header.epoch)
21+
})
22+
.catch((err: Error) => {
23+
console.error(err)
24+
})
25+
}
26+
}, [depositOutPoint, blockNumber, setWithdrawingEpoch])
27+
}
28+
29+
export const useUpdateApc = ({
30+
depositTimestamp,
31+
genesisBlockTimestamp = 0,
32+
timestamp,
33+
tipBlockTimestamp,
34+
setApc,
35+
}: {
36+
depositTimestamp: number
37+
genesisBlockTimestamp: number
38+
timestamp: number
39+
tipBlockTimestamp: number
40+
setApc: React.Dispatch<number>
41+
}) => {
42+
useEffect(() => {
43+
if (depositTimestamp) {
44+
const startYearNumber = (depositTimestamp - genesisBlockTimestamp) / MILLISECONDS_IN_YEAR
45+
const endYearNumber = (timestamp - genesisBlockTimestamp) / MILLISECONDS_IN_YEAR
46+
try {
47+
const calculatedAPC = calculateAPC({
48+
startYearNumber,
49+
endYearNumber,
50+
})
51+
setApc(calculatedAPC)
52+
} catch (err) {
53+
console.error(err)
54+
}
55+
} else {
56+
const startYearNumber = (timestamp - genesisBlockTimestamp) / MILLISECONDS_IN_YEAR
57+
const endYearNumber = (tipBlockTimestamp - genesisBlockTimestamp) / MILLISECONDS_IN_YEAR
58+
try {
59+
const calculatedAPC = calculateAPC({
60+
startYearNumber,
61+
endYearNumber,
62+
})
63+
setApc(calculatedAPC)
64+
} catch (err) {
65+
console.error(err)
66+
}
67+
}
68+
}, [depositTimestamp, tipBlockTimestamp, timestamp, genesisBlockTimestamp, setApc])
69+
}
70+
71+
export default { useUpdateEpochs, useUpdateApc }

0 commit comments

Comments
 (0)