Skip to content

Commit 5274edd

Browse files
committed
fix(neuron-ui): cache the genesis block timestamp in the nervos dao component instead of in global
1 parent df3eb0e commit 5274edd

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

packages/neuron-ui/src/components/CustomRows/DAORecordRow.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DAORecord = ({
1919
actionLabel,
2020
onClick,
2121
timestamp,
22+
genesisBlockTimestamp,
2223
depositTimestamp,
2324
depositOutPoint,
2425
epoch,
@@ -30,6 +31,7 @@ const DAORecord = ({
3031
tipBlockNumber: string
3132
epoch: string
3233
withdraw: string | null
34+
genesisBlockTimestamp: number | undefined
3335
connectionStatus: 'online' | 'offline'
3436
}) => {
3537
const [t] = useTranslation()
@@ -38,10 +40,10 @@ const DAORecord = ({
3840
const [apc, setApc] = useState(0)
3941

4042
useEffect(() => {
41-
calculateGlobalAPC(+(depositTimestamp || timestamp)).then(res => {
43+
calculateGlobalAPC(+(depositTimestamp || timestamp), genesisBlockTimestamp).then(res => {
4244
setApc(res)
4345
})
44-
})
46+
}, [depositTimestamp, timestamp, genesisBlockTimestamp])
4547

4648
useEffect(() => {
4749
if (!depositOutPoint) {

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { MIN_DEPOSIT_AMOUNT, MEDIUM_FEE_RATE, SHANNON_CKB_RATIO, MAX_DECIMAL_DIG
1515
import { verifyAmount } from 'utils/validators'
1616

1717
import { generateDepositTx, generateWithdrawTx, generateClaimTx } from 'services/remote'
18-
import { ckbCore } from 'services/chain'
18+
import { ckbCore, getBlockByNumber } from 'services/chain'
1919
import { epochParser } from 'utils/parsers'
2020

2121
import DAORecord from 'components/CustomRows/DAORecordRow'
@@ -46,6 +46,7 @@ const NervosDAO = ({
4646
const [errorMessage, setErrorMessage] = useState('')
4747
const [withdrawList, setWithdrawList] = useState<(string | null)[]>([])
4848
const [globalAPC, setGlobalAPC] = useState(0)
49+
const [genesisBlockTimestamp, setGenesisBlockTimestamp] = useState<number | undefined>(undefined)
4950

5051
const clearGeneratedTx = useCallback(() => {
5152
dispatch({
@@ -98,6 +99,9 @@ const NervosDAO = ({
9899
useEffect(() => {
99100
updateNervosDaoData({ walletID: wallet.id })(dispatch)
100101
updateDepositValue(`${MIN_DEPOSIT_AMOUNT}`)
102+
getBlockByNumber('0x0')
103+
.then(b => setGenesisBlockTimestamp(+b.header.timestamp))
104+
.catch(err => console.error(err))
101105
return () => {
102106
clearNervosDaoData()(dispatch)
103107
clearGeneratedTx()
@@ -106,11 +110,13 @@ const NervosDAO = ({
106110

107111
useEffect(() => {
108112
if (tipBlockTimestamp) {
109-
calculateGlobalAPC(tipBlockTimestamp).then(apc => {
110-
setGlobalAPC(apc)
111-
})
113+
calculateGlobalAPC(tipBlockTimestamp, genesisBlockTimestamp)
114+
.then(apc => {
115+
setGlobalAPC(apc)
116+
})
117+
.catch(err => console.error(err))
112118
}
113-
}, [tipBlockTimestamp])
119+
}, [tipBlockTimestamp, genesisBlockTimestamp])
114120

115121
const onDepositDialogDismiss = () => {
116122
setShowDepositDialog(false)
@@ -276,14 +282,15 @@ const NervosDAO = ({
276282
onClick={onActionClick}
277283
tipBlockNumber={tipBlockNumber}
278284
epoch={epoch}
285+
genesisBlockTimestamp={genesisBlockTimestamp}
279286
connectionStatus={connectionStatus}
280287
/>
281288
)
282289
})}
283290
</Stack>
284291
</>
285292
)
286-
}, [records, withdrawList, t, onActionClick, tipBlockNumber, epoch, connectionStatus])
293+
}, [records, withdrawList, t, onActionClick, tipBlockNumber, epoch, connectionStatus, genesisBlockTimestamp])
287294

288295
const free = BigInt(wallet.balance)
289296
const locked = withdrawList.reduce((acc, w) => acc + BigInt(w || 0), BigInt(0))

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ const DAYS_PER_PERIOD = 365 * 4 * 1
66
const MILLI_SECONDS_PER_DAY = 24 * 3600 * 1000
77
const PERIOD_LENGTH = DAYS_PER_PERIOD * MILLI_SECONDS_PER_DAY
88

9-
let cachedGenesisTimestamp: number | undefined
10-
11-
export default async (checkPointTimestamp: number, initialTimestamp: number | undefined = cachedGenesisTimestamp) => {
9+
export default async (checkPointTimestamp: number, initialTimestamp?: number | undefined) => {
1210
let genesisTimestamp = initialTimestamp
1311
if (genesisTimestamp === undefined) {
1412
genesisTimestamp = await getBlockByNumber('0x0')
15-
.then(b => {
16-
cachedGenesisTimestamp = +b.header.timestamp
17-
return cachedGenesisTimestamp
18-
})
13+
.then(b => +b.header.timestamp)
1914
.catch(() => undefined)
2015
}
2116
if (genesisTimestamp === undefined || checkPointTimestamp <= genesisTimestamp) {

0 commit comments

Comments
 (0)