|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import { DefaultButton } from 'office-ui-fabric-react' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | +import { ckbCore, getBlockByNumber } from 'services/chain' |
| 5 | +import calculateAPY from 'utils/calculateAPY' |
| 6 | +import { shannonToCKBFormatter, uniformTimeFormatter, localNumberFormatter } from 'utils/formatters' |
| 7 | +import { epochParser } from 'utils/parsers' |
| 8 | +import { WITHDRAW_EPOCHS } from 'utils/const' |
| 9 | + |
| 10 | +import * as styles from './daoRecordRow.module.scss' |
| 11 | + |
| 12 | +const DAORecord = ({ |
| 13 | + daoData, |
| 14 | + blockNumber, |
| 15 | + blockHash, |
| 16 | + outPoint: { txHash, index }, |
| 17 | + tipBlockNumber, |
| 18 | + tipBlockHash, |
| 19 | + capacity, |
| 20 | + actionLabel, |
| 21 | + onClick, |
| 22 | + timestamp, |
| 23 | + depositOutPoint, |
| 24 | + epoch, |
| 25 | +}: State.NervosDAORecord & { |
| 26 | + actionLabel: string |
| 27 | + onClick: any |
| 28 | + tipBlockNumber: string |
| 29 | + tipBlockHash: string |
| 30 | + epoch: string |
| 31 | +}) => { |
| 32 | + const [t] = useTranslation() |
| 33 | + const [withdrawValue, setWithdrawValue] = useState('') |
| 34 | + const [depositEpoch, setDepositEpoch] = useState('') |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + const withdrawBlockHash = depositOutPoint ? blockHash : tipBlockHash |
| 38 | + if (!withdrawBlockHash) { |
| 39 | + return |
| 40 | + } |
| 41 | + ;(ckbCore.rpc as any) |
| 42 | + .calculateDaoMaximumWithdraw({ txHash, index: `0x${BigInt(index).toString(16)}` }, withdrawBlockHash) |
| 43 | + .then((res: string) => { |
| 44 | + setWithdrawValue(BigInt(res).toString()) |
| 45 | + }) |
| 46 | + .catch((err: Error) => { |
| 47 | + console.error(err) |
| 48 | + }) |
| 49 | + }, [txHash, index, tipBlockHash, depositOutPoint, blockHash]) |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + if (!depositOutPoint) { |
| 53 | + return |
| 54 | + } |
| 55 | + const depositBlockNumber = ckbCore.utils.bytesToHex(ckbCore.utils.hexToBytes(daoData).reverse()) |
| 56 | + getBlockByNumber(BigInt(depositBlockNumber)) |
| 57 | + .then(b => { |
| 58 | + setDepositEpoch(b.header.epoch) |
| 59 | + }) |
| 60 | + .catch((err: Error) => { |
| 61 | + console.error(err) |
| 62 | + }) |
| 63 | + }, [daoData, depositOutPoint]) |
| 64 | + |
| 65 | + const interest = BigInt(withdrawValue) - BigInt(capacity) |
| 66 | + |
| 67 | + let ready = false |
| 68 | + let metaInfo = 'Ready' |
| 69 | + if (!depositOutPoint) { |
| 70 | + const duration = BigInt(tipBlockNumber) - BigInt(blockNumber) |
| 71 | + metaInfo = t('nervos-dao.interest-accumulated', { |
| 72 | + blockNumber: localNumberFormatter(duration >= BigInt(0) ? duration : 0), |
| 73 | + }) |
| 74 | + } else { |
| 75 | + const depositEpochInfo = epochParser(depositEpoch) |
| 76 | + const currentEpochInfo = epochParser(epoch) |
| 77 | + |
| 78 | + let depositedEpochs = currentEpochInfo.number - depositEpochInfo.number |
| 79 | + const depositEpochFraction = depositEpochInfo.index * currentEpochInfo.length |
| 80 | + const currentEpochFraction = currentEpochInfo.index * depositEpochInfo.length |
| 81 | + if (currentEpochFraction > depositEpochFraction) { |
| 82 | + depositedEpochs += BigInt(1) |
| 83 | + } |
| 84 | + const minLockEpochs = |
| 85 | + ((depositedEpochs + BigInt(WITHDRAW_EPOCHS - 1)) / BigInt(WITHDRAW_EPOCHS)) * BigInt(WITHDRAW_EPOCHS) |
| 86 | + const targetEpochNumber = depositEpochInfo.number + minLockEpochs |
| 87 | + |
| 88 | + if (targetEpochNumber < currentEpochInfo.number + BigInt(1) && targetEpochNumber >= currentEpochInfo.number) { |
| 89 | + metaInfo = 'Ready' |
| 90 | + ready = true |
| 91 | + } else { |
| 92 | + metaInfo = t('nervos-dao.blocks-left', { |
| 93 | + epochs: localNumberFormatter(targetEpochNumber - currentEpochInfo.number - BigInt(1)), |
| 94 | + blocks: currentEpochInfo.length - currentEpochInfo.index, |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return ( |
| 100 | + <div className={styles.daoRecord}> |
| 101 | + <div className={styles.primaryInfo}> |
| 102 | + <div>{interest >= BigInt(0) ? `${shannonToCKBFormatter(interest.toString()).toString()} CKB` : ''}</div> |
| 103 | + <div>{`${shannonToCKBFormatter(capacity)} CKB`}</div> |
| 104 | + <div> |
| 105 | + <DefaultButton |
| 106 | + text={actionLabel} |
| 107 | + data-tx-hash={txHash} |
| 108 | + data-index={index} |
| 109 | + onClick={onClick} |
| 110 | + disabled={depositOutPoint && !ready} |
| 111 | + styles={{ |
| 112 | + flexContainer: { |
| 113 | + pointerEvents: 'none', |
| 114 | + }, |
| 115 | + textContainer: { |
| 116 | + pointerEvents: 'none', |
| 117 | + }, |
| 118 | + label: { |
| 119 | + pointerEvents: 'none', |
| 120 | + }, |
| 121 | + }} |
| 122 | + /> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + <div className={styles.secondaryInfo}> |
| 126 | + <span> |
| 127 | + {`APY: ~${calculateAPY( |
| 128 | + interest >= BigInt(0) ? interest.toString() : '0', |
| 129 | + capacity, |
| 130 | + `${Date.now() - +timestamp}` |
| 131 | + )}%`} |
| 132 | + </span> |
| 133 | + <span>{uniformTimeFormatter(+timestamp)}</span> |
| 134 | + <span>{metaInfo}</span> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +DAORecord.displayName = 'DAORecord' |
| 141 | + |
| 142 | +export default DAORecord |
0 commit comments