From 3d7953a47bc42d52730794fd53212328ebb35c1b Mon Sep 17 00:00:00 2001 From: Keith Date: Tue, 12 Nov 2019 17:46:32 +0800 Subject: [PATCH] refactor(neuron-ui): move withdraw outside the dao row component. --- .../components/CustomRows/DAORecordRow.tsx | 54 +++++++++---------- .../src/components/NervosDAO/index.tsx | 33 ++++++++++-- 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/packages/neuron-ui/src/components/CustomRows/DAORecordRow.tsx b/packages/neuron-ui/src/components/CustomRows/DAORecordRow.tsx index 2412f8b2ea..75c796bc76 100644 --- a/packages/neuron-ui/src/components/CustomRows/DAORecordRow.tsx +++ b/packages/neuron-ui/src/components/CustomRows/DAORecordRow.tsx @@ -12,51 +12,49 @@ import * as styles from './daoRecordRow.module.scss' const DAORecord = ({ daoData, blockNumber, - blockHash, outPoint: { txHash, index }, tipBlockNumber, - tipBlockHash, capacity, actionLabel, onClick, timestamp, depositOutPoint, epoch, + withdraw, }: State.NervosDAORecord & { actionLabel: string onClick: any tipBlockNumber: string - tipBlockHash: string epoch: string + withdraw: string | null }) => { const [t] = useTranslation() - const [withdrawValue, setWithdrawValue] = useState('') const [withdrawingEpoch, setWithdrawingEpoch] = useState('') const [depositEpoch, setDepositEpoch] = useState('') - useEffect(() => { - const withdrawBlockHash = depositOutPoint ? blockHash : tipBlockHash - if (!withdrawBlockHash) { - return - } - const formattedDepositOutPoint = depositOutPoint - ? { - txHash: depositOutPoint.txHash, - index: BigInt(depositOutPoint.index), - } - : { - txHash, - index: BigInt(index), - } - ;(ckbCore.rpc as any) - .calculateDaoMaximumWithdraw(formattedDepositOutPoint, withdrawBlockHash) - .then((res: string) => { - setWithdrawValue(BigInt(res).toString()) - }) - .catch((err: Error) => { - console.error(err) - }) - }, [txHash, index, tipBlockHash, depositOutPoint, blockHash]) + // useEffect(() => { + // const withdrawBlockHash = depositOutPoint ? blockHash : tipBlockHash + // if (!withdrawBlockHash) { + // return + // } + // const formattedDepositOutPoint = depositOutPoint + // ? { + // txHash: depositOutPoint.txHash, + // index: BigInt(depositOutPoint.index), + // } + // : { + // txHash, + // index: BigInt(index), + // } + // ;(ckbCore.rpc as any) + // .calculateDaoMaximumWithdraw(formattedDepositOutPoint, withdrawBlockHash) + // .then((res: string) => { + // setWithdrawValue(BigInt(res).toString()) + // }) + // .catch((err: Error) => { + // console.error(err) + // }) + // }, [txHash, index, tipBlockHash, depositOutPoint, blockHash]) useEffect(() => { if (!depositOutPoint) { @@ -80,7 +78,7 @@ const DAORecord = ({ }) }, [daoData, depositOutPoint, blockNumber]) - const interest = BigInt(withdrawValue) - BigInt(capacity) + const interest = BigInt(withdraw || capacity) - BigInt(capacity) let ready = false let metaInfo = 'Ready' diff --git a/packages/neuron-ui/src/components/NervosDAO/index.tsx b/packages/neuron-ui/src/components/NervosDAO/index.tsx index 738fcec07a..21effbf8ad 100644 --- a/packages/neuron-ui/src/components/NervosDAO/index.tsx +++ b/packages/neuron-ui/src/components/NervosDAO/index.tsx @@ -12,6 +12,7 @@ import { shannonToCKBFormatter } from 'utils/formatters' import { MIN_DEPOSIT_AMOUNT, MEDIUM_FEE_RATE, SHANNON_CKB_RATIO } from 'utils/const' import { generateDepositTx, generateWithdrawTx, generateClaimTx } from 'services/remote' +import { ckbCore } from 'services/chain' import { epochParser } from 'utils/parsers' import DAORecord from 'components/CustomRows/DAORecordRow' @@ -38,6 +39,7 @@ const NervosDAO = ({ const [showDepositDialog, setShowDepositDialog] = useState(false) const [activeRecord, setActiveRecord] = useState(null) const [errorMessage, setErrorMessage] = useState('') + const [withdrawList, setWithdrawList] = useState<(string | null)[]>([]) const clearGeneratedTx = useCallback(() => { dispatch({ @@ -178,6 +180,31 @@ const NervosDAO = ({ send.generatedTx ? send.generatedTx.fee || calculateFee(send.generatedTx) : '0' )} CKB` + useEffect(() => { + Promise.all( + records.map(async ({ outPoint, depositOutPoint, blockHash }) => { + if (!tipBlockHash) { + return null + } + const withdrawBlockHash = depositOutPoint ? blockHash : tipBlockHash + const formattedDepositOutPoint = depositOutPoint + ? { + txHash: depositOutPoint.txHash, + index: BigInt(depositOutPoint.index), + } + : { + txHash: outPoint.txHash, + index: BigInt(outPoint.index), + } + return (ckbCore.rpc as any).calculateDaoMaximumWithdraw(formattedDepositOutPoint, withdrawBlockHash) as string + }) + ) + .then(res => { + setWithdrawList(res) + }) + .catch(console.error) + }, [records, tipBlockHash]) + const Records = useMemo(() => { return ( <> @@ -185,7 +212,7 @@ const NervosDAO = ({ {t('nervos-dao.deposit-records')} - {records.map(record => { + {records.map((record, i) => { let stage = 'deposited' if (record.depositOutPoint) { stage = 'withdrawing' @@ -193,10 +220,10 @@ const NervosDAO = ({ return ( @@ -205,7 +232,7 @@ const NervosDAO = ({ ) - }, [records, t, tipBlockHash, onActionClick, tipBlockNumber, epoch]) + }, [records, withdrawList, t, onActionClick, tipBlockNumber, epoch]) let free = BigInt(0) let locked = BigInt(0)