Add create bounty modal (#4231)

* Add new bounty modal with inputs

* WIP - add bounty bond to modal

* Add bounty bond

* Remove "Add Bounty" from locales

* useCallback for input change handler

Co-authored-by: Ewa Kowalska <ewcia.kowalska@gmail.com>
Co-authored-by: Krzysztof Jelski <krzysztof@ethworks.io>
This commit is contained in:
MiZiet 2020-12-18 18:32:29 +01:00 committed by GitHub
parent 744eeffa17
commit 9557986457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 161 additions and 15 deletions

View File

@ -1,11 +1,21 @@
{
"Add Bounty": "Add Bounty",
"Bounties": "Bounties",
"Calculated bond for bounty": "Calculated bond for bounty",
"Claimable": "Claimable",
"No open bounties": "No open bounties",
"Select the account you wish to propose the bounty from.": "Select the account you wish to propose the bounty from.",
"The description of this bounty": "The description of this bounty",
"The total payment amount of this bounty, curators fee included.": "The total payment amount of this bounty, curators fee included.",
"This amount will be reserved from origin account and returned on approval or slashed upon rejection.": "This amount will be reserved from origin account and returned on approval or slashed upon rejection.",
"beneficiary": "beneficiary",
"bounties": "bounties",
"bounty bond": "bounty bond",
"bounty requested allocation": "bounty requested allocation",
"bounty title": "bounty title",
"curator": "curator",
"payout due": "payout due",
"submit with account": "submit with account",
"title": "title",
"update due": "update due",
"value": "value",

View File

@ -6,7 +6,8 @@ import type { BlockNumber } from '@polkadot/types/interfaces';
import React, { useRef } from 'react';
import { Table } from '@polkadot/react-components';
import BountyCreate from '@polkadot/app-bounties/BountyCreate';
import { Button, Table } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import Bounty from './Bounty';
@ -33,20 +34,25 @@ function Bounties (): React.ReactElement {
]);
return (
<Table
empty={deriveBounties && t<string>('No open bounties')}
header={headerRef.current}
>
{deriveBounties && bestNumber && deriveBounties.map(({ bounty, description }, index) => (
<Bounty
bestNumber={bestNumber}
bounty={bounty}
description={description}
index={index}
key={index}
/>
))}
</Table>
<>
<Button.Group>
<BountyCreate />
</Button.Group>
<Table
empty={deriveBounties && t<string>('No open bounties')}
header={headerRef.current}
>
{deriveBounties && bestNumber && deriveBounties.map(({ bounty, description }, index) => (
<Bounty
bestNumber={bestNumber}
bounty={bounty}
description={description}
index={index}
key={index}
/>
))}
</Table>
</>
);
}

View File

@ -0,0 +1,91 @@
// Copyright 2017-2020 @polkadot/app-bounties authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import React, { useCallback, useState } from 'react';
import { useTranslation } from '@polkadot/app-accounts/translate';
import { calculateBountyBond } from '@polkadot/app-bounties/helpers/calculateBountyBond';
import { Button, Input, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components';
import { useApi, useToggle } from '@polkadot/react-hooks';
import { BN_ZERO } from '@polkadot/util';
function BountyCreate () {
const { t } = useTranslation();
const { api } = useApi();
const [accountId, setAccountId] = useState<string | null>(null);
const [title, setTitle] = useState('');
const [bond, setBond] = useState(api.consts.treasury.bountyDepositBase.toBn());
const [amount, setAmount] = useState<BN | undefined>(BN_ZERO);
const [isValid] = useState(true);
const [isOpen, toggleIsOpen] = useToggle();
const onTitleChange = useCallback((value: string) => {
const bountyDepositBase = api.consts.treasury.bountyDepositBase;
const bountyDepositPerByte = api.consts.treasury.dataDepositPerByte;
setTitle(value);
setBond(calculateBountyBond(value, bountyDepositBase, bountyDepositPerByte));
}, [api]);
return (
<>
<Button
icon='plus'
isDisabled={false}
label={t<string>('Add Bounty')}
onClick={toggleIsOpen}
/>
{isOpen && (
<Modal
className='ui--AddBountyModal'
header={t<string>('Add Bounty')}
>
<Modal.Content>
<Input
autoFocus
help={t<string>('The description of this bounty')}
label={t<string>('bounty title')}
onChange={onTitleChange}
value={title}
/>
<InputBalance
help={t<string>('The total payment amount of this bounty, curators fee included.')}
isZeroable
label={t<string>('bounty requested allocation')}
onChange={setAmount}
value={ amount }
/>
<InputBalance
defaultValue={bond.toString()}
help={t<string>('This amount will be reserved from origin account and returned on approval or slashed upon rejection.')}
isDisabled
label={t<string>('bounty bond')}
/>
<InputAddress
help={t<string>('Select the account you wish to propose the bounty from.')}
label={t<string>('submit with account')}
onChange={setAccountId}
type='account'
withLabel
/>
</Modal.Content>
<Modal.Actions onCancel={toggleIsOpen}>
<TxButton
accountId={accountId}
icon='plus'
isDisabled={!accountId || !isValid}
label={t<string>('Add Bounty')}
onStart={toggleIsOpen}
params={[amount, title]}
tx='treasury.proposeBounty'
/>
</Modal.Actions>
</Modal>
)}
</>
);
}
export default React.memo(BountyCreate);

View File

@ -0,0 +1,25 @@
// Copyright 2017-2020 @polkadot/app-bounties authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { calculateBountyBond } from '@polkadot/app-bounties/helpers/calculateBountyBond';
import { TypeRegistry } from '@polkadot/types/create';
describe('Calculate bounty bond', () => {
it('sums deposit base and deposit for each byte of description', () => {
const registry = new TypeRegistry();
const depositBase = registry.createType('BalanceOf', new BN(166666666666));
const depositPerByte = registry.createType('BalanceOf', new BN(1666666666));
expect(calculateBountyBond('Kusama network UI Bounty', depositBase, depositPerByte)).toEqual(new BN(206666666650));
});
it('handles utf-8 chars', () => {
const registry = new TypeRegistry();
const depositBase = registry.createType('BalanceOf', new BN(100));
const depositPerByte = registry.createType('BalanceOf', new BN(10));
expect(calculateBountyBond('óy😅€', depositBase, depositPerByte)).toEqual(new BN(200));
});
});

View File

@ -0,0 +1,14 @@
// Copyright 2017-2020 @polkadot/app-bounties authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BalanceOf } from '@polkadot/types/interfaces';
import BN from 'bn.js';
export function calculateBountyBond (description: string, depositBase: BalanceOf, depositPerByte: BalanceOf): BN {
return depositBase.toBn().add(depositPerByte.toBn().muln(countUtf8Bytes(description)));
}
function countUtf8Bytes (str: string) {
return new Blob([str]).size;
}