Remove extra crowdloan contributions encoding (#5172)
This commit is contained in:
parent
2ec436031d
commit
da115195ea
@ -27,19 +27,18 @@ interface Props {
|
||||
}
|
||||
|
||||
interface Contributions {
|
||||
uniqueKeys: string[];
|
||||
contributors: string[];
|
||||
myAccounts: string[];
|
||||
}
|
||||
|
||||
const NO_CONTRIB: Contributions = { myAccounts: [], uniqueKeys: [] };
|
||||
const NO_CONTRIB: Contributions = { contributors: [], myAccounts: [] };
|
||||
|
||||
function extractContributors (allAccounts: string[], keys: StorageKey[]): Contributions {
|
||||
const uniqueKeys = keys.map((k) => k.toHex());
|
||||
const contributors = keys.map((k) => encodeAddress(k));
|
||||
|
||||
return {
|
||||
myAccounts: contributors.filter((c) => allAccounts.includes(c)),
|
||||
uniqueKeys
|
||||
contributors,
|
||||
myAccounts: contributors.filter((c) => allAccounts.includes(c))
|
||||
};
|
||||
}
|
||||
|
||||
@ -47,7 +46,7 @@ function Fund ({ bestNumber, className, isOngoing, leasePeriod, value: { childKe
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { allAccounts, isAccount } = useAccounts();
|
||||
const [{ myAccounts, uniqueKeys }, setContributors] = useState<Contributions>(NO_CONTRIB);
|
||||
const [{ contributors, myAccounts }, setContributors] = useState<Contributions>(NO_CONTRIB);
|
||||
const trigger = useEventTrigger([api.events.crowdloan.Contributed, api.events.crowdloan.Withdrew, api.events.crowdloan.AllRefunded, api.events.crowdloan.PartiallyRefunded], useCallback(
|
||||
({ event: { data } }: EventRecord) =>
|
||||
((data.length === 1
|
||||
@ -134,8 +133,8 @@ function Fund ({ bestNumber, className, isOngoing, leasePeriod, value: { childKe
|
||||
<div>{percentage}</div>
|
||||
</td>
|
||||
<td className='number'>
|
||||
{uniqueKeys.length !== 0 && (
|
||||
formatNumber(uniqueKeys.length)
|
||||
{contributors.length !== 0 && (
|
||||
formatNumber(contributors.length)
|
||||
)}
|
||||
</td>
|
||||
<td className='badge'>
|
||||
@ -145,12 +144,8 @@ function Fund ({ bestNumber, className, isOngoing, leasePeriod, value: { childKe
|
||||
/>
|
||||
</td>
|
||||
<td className='button media--1300'>
|
||||
{canWithdraw && uniqueKeys.length !== 0 && (
|
||||
<Refund
|
||||
allAccounts={uniqueKeys}
|
||||
myAccounts={myAccounts}
|
||||
paraId={paraId}
|
||||
/>
|
||||
{canWithdraw && contributors.length !== 0 && (
|
||||
<Refund paraId={paraId} />
|
||||
)}
|
||||
{canDissolve && (
|
||||
<TxButton
|
||||
|
||||
@ -67,7 +67,7 @@ function Funds ({ bestNumber, className, leasePeriod, value }: Props): React.Rea
|
||||
[t('ending'), 'media--1000'],
|
||||
[t('leases')],
|
||||
[t('raised')],
|
||||
[t('unique')],
|
||||
[t('count')],
|
||||
[undefined, 'badge'],
|
||||
[undefined, 'media--1300']
|
||||
]);
|
||||
@ -79,7 +79,7 @@ function Funds ({ bestNumber, className, leasePeriod, value }: Props): React.Rea
|
||||
[t('ending'), 'media--1000'],
|
||||
[t('leases')],
|
||||
[t('raised')],
|
||||
[t('unique')],
|
||||
[t('count')],
|
||||
[undefined, 'badge'],
|
||||
[undefined, 'media--1300']
|
||||
]);
|
||||
|
||||
@ -1,60 +1,26 @@
|
||||
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import type { ParaId } from '@polkadot/types/interfaces';
|
||||
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Button, Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle, useTxBatch } from '@polkadot/react-hooks';
|
||||
import { Button, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
interface Props {
|
||||
allAccounts: string[];
|
||||
className?: string;
|
||||
myAccounts: string[];
|
||||
paraId: ParaId;
|
||||
}
|
||||
|
||||
const optTxs = { batchSize: 128 };
|
||||
|
||||
function Refund ({ allAccounts, className, myAccounts, paraId }: Props): React.ReactElement<Props> {
|
||||
function Refund ({ className, paraId }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { hasAccounts } = useAccounts();
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [txs, setTxs] = useState<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const [withdrawType, setWithdrawType] = useState(0);
|
||||
const extrinsics = useTxBatch(txs, optTxs);
|
||||
|
||||
const typeOptions = useMemo(
|
||||
() => [
|
||||
{
|
||||
text: t<string>('Withdraw for all contributed accounts'),
|
||||
value: 0
|
||||
},
|
||||
myAccounts.length && {
|
||||
text: t<string>('Withdraw only for my accounts'),
|
||||
value: 1
|
||||
}
|
||||
].filter((o) => !!o),
|
||||
[myAccounts, t]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
const addresses = withdrawType === 0
|
||||
? allAccounts
|
||||
: myAccounts;
|
||||
|
||||
setTxs(() =>
|
||||
addresses.length
|
||||
? addresses.map((a) => api.tx.crowdloan.withdraw(a, paraId))
|
||||
: null
|
||||
);
|
||||
}, [api, allAccounts, myAccounts, paraId, withdrawType]);
|
||||
|
||||
return (
|
||||
<>
|
||||
@ -79,38 +45,16 @@ function Refund ({ allAccounts, className, myAccounts, paraId }: Props): React.R
|
||||
value={accountId}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
{!api.tx.crowdloan.refund && (
|
||||
<Modal.Columns hint={t<string>('The type of withdrawal, all accounts or only own accounts (if available)')}>
|
||||
<Dropdown
|
||||
label={t<string>('withdrawal type')}
|
||||
onChange={setWithdrawType}
|
||||
options={typeOptions}
|
||||
value={withdrawType}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
)}
|
||||
</Modal.Content>
|
||||
<Modal.Actions onCancel={toggleOpen}>
|
||||
{api.tx.crowdloan.refund
|
||||
? (
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
icon='credit-card'
|
||||
label={t<string>('Refund')}
|
||||
onStart={toggleOpen}
|
||||
tx={api.tx.crowdloan.refund}
|
||||
/>
|
||||
)
|
||||
: (
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
extrinsic={extrinsics}
|
||||
icon='credit-card'
|
||||
isDisabled={!extrinsics || !extrinsics.length || !accountId}
|
||||
label={t<string>('Refund')}
|
||||
onStart={toggleOpen}
|
||||
/>
|
||||
)}
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
icon='credit-card'
|
||||
label={t<string>('Refund')}
|
||||
onStart={toggleOpen}
|
||||
params={[paraId]}
|
||||
tx={api.tx.crowdloan.refund}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
)}
|
||||
|
||||
@ -66,6 +66,22 @@ function isFundUpdated (bestNumber: BlockNumber, minContribution: BN, { info: {
|
||||
(!prev.isWinner && hasLease(paraId, leased));
|
||||
}
|
||||
|
||||
function sortCampaigns (a: Campaign, b: Campaign): number {
|
||||
return a.isWinner !== b.isWinner
|
||||
? a.isWinner
|
||||
? -1
|
||||
: 1
|
||||
: a.isCapped !== b.isCapped
|
||||
? a.isCapped
|
||||
? -1
|
||||
: 1
|
||||
: a.isEnded !== b.isEnded
|
||||
? a.isEnded
|
||||
? 1
|
||||
: -1
|
||||
: 0;
|
||||
}
|
||||
|
||||
// compare the current campaigns against the previous, manually adding ending and calculating the new totals
|
||||
function createResult (bestNumber: BlockNumber, minContribution: BN, funds: Campaign[], leased: ParaId[], prev: Campaigns): Campaigns {
|
||||
const [activeRaised, activeCap, totalRaised, totalCap] = funds.reduce(([ar, ac, tr, tc], { info: { cap, end, raised } }) => [
|
||||
@ -96,21 +112,7 @@ function createResult (bestNumber: BlockNumber, minContribution: BN, funds: Camp
|
||||
: prev.activeRaised,
|
||||
funds: funds
|
||||
.map((c) => updateFund(bestNumber, minContribution, c, leased))
|
||||
.sort((a, b) =>
|
||||
a.isWinner !== b.isWinner
|
||||
? a.isWinner
|
||||
? -1
|
||||
: 1
|
||||
: a.isCapped !== b.isCapped
|
||||
? a.isCapped
|
||||
? -1
|
||||
: 1
|
||||
: a.isEnded !== b.isEnded
|
||||
? a.isEnded
|
||||
? 1
|
||||
: -1
|
||||
: 0
|
||||
),
|
||||
.sort(sortCampaigns),
|
||||
totalCap: hasNewTotalCap
|
||||
? totalCap
|
||||
: prev.totalCap,
|
||||
|
||||
@ -92,9 +92,12 @@ export default React.memo(styled(Table)`
|
||||
}
|
||||
|
||||
&.all {
|
||||
word-break: break-word;
|
||||
width: 100%;
|
||||
|
||||
&:not(.overflow) {
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
summary {
|
||||
white-space: normal;
|
||||
}
|
||||
@ -210,6 +213,7 @@ export default React.memo(styled(Table)`
|
||||
max-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
word-break: none;
|
||||
}
|
||||
|
||||
&.start {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user