Gilt app (#4893)
* Gilt app * Basic queue display * Gilt bidding * disabled status * defaultValue toString
This commit is contained in:
parent
f49a613ec7
commit
5a834648e6
@ -10,7 +10,6 @@ export default function create (t: TFunction): Route {
|
||||
return {
|
||||
Component,
|
||||
display: {
|
||||
isHidden: true,
|
||||
needsApi: [
|
||||
'tx.gilt.placeBid'
|
||||
]
|
||||
|
||||
109
packages/page-gilt/src/Overview/BidAdd.tsx
Normal file
109
packages/page-gilt/src/Overview/BidAdd.tsx
Normal file
@ -0,0 +1,109 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type BN from 'bn.js';
|
||||
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Button, InputAddress, InputBalance, InputNumber, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { Available } from '@polkadot/react-query';
|
||||
import { BN_ONE } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
isDisabled: boolean;
|
||||
}
|
||||
|
||||
function Bid ({ className, isDisabled }: 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 [amount, setAmount] = useState<BN | undefined>();
|
||||
const [duration, setDuration] = useState<BN | undefined>();
|
||||
|
||||
const isAmountError = !amount || amount.isZero() || amount.lt(api.consts.gilt.minFreeze);
|
||||
const isDurationError = !duration || !duration.gte(BN_ONE) || duration.gt(api.consts.gilt.queueCount);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button
|
||||
icon='plus'
|
||||
isDisabled={!hasAccounts || isDisabled}
|
||||
label={t<string>('Submit Bid')}
|
||||
onClick={toggleOpen}
|
||||
/>
|
||||
{isOpen && (
|
||||
<Modal
|
||||
className={className}
|
||||
header={t<string>('submit gilt bid')}
|
||||
size='large'
|
||||
>
|
||||
<Modal.Content>
|
||||
<Modal.Columns hint={t<string>('This account will make the bid for the gilt and pay all associated fees.')}>
|
||||
<InputAddress
|
||||
help={t<string>('The account you want to register the preimage from')}
|
||||
label={t<string>('send from account')}
|
||||
labelExtra={
|
||||
<Available
|
||||
label={<span className='label'>{t<string>('transferrable')}</span>}
|
||||
params={accountId}
|
||||
/>
|
||||
}
|
||||
onChange={setAccountId}
|
||||
type='account'
|
||||
/>
|
||||
</Modal.Columns>
|
||||
<Modal.Columns hint={t<string>('The amount you wish to lock for the duration. It needs to be more than the gilt minimum.')}>
|
||||
<InputBalance
|
||||
autoFocus
|
||||
defaultValue={api.consts.gilt.minFreeze}
|
||||
isError={isAmountError}
|
||||
isZeroable={false}
|
||||
label={t<string>('bid amount')}
|
||||
onChange={setAmount}
|
||||
/>
|
||||
<InputBalance
|
||||
defaultValue={api.consts.gilt.minFreeze}
|
||||
help={t<string>('The minimum amount that is allowed as a bid')}
|
||||
isDisabled
|
||||
label={t<string>('minimum freeze amount')}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
<Modal.Columns hint={t<string>('The number of periods this bid is to be freezed for, less than the maximum period')}>
|
||||
<InputNumber
|
||||
defaultValue={BN_ONE}
|
||||
isError={isDurationError}
|
||||
isZeroable={false}
|
||||
label={t<string>('lock periods')}
|
||||
onChange={setDuration}
|
||||
/>
|
||||
<InputNumber
|
||||
defaultValue={api.consts.gilt.queueCount}
|
||||
isDisabled
|
||||
label={t<string>('maximum lock periods')}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
</Modal.Content>
|
||||
<Modal.Actions onCancel={toggleOpen}>
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
icon='check'
|
||||
isDisabled={isAmountError || isDurationError}
|
||||
label={t<string>('Bid')}
|
||||
onStart={toggleOpen}
|
||||
params={[amount, duration]}
|
||||
tx={api.tx.gilt.placeBid}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Bid);
|
||||
32
packages/page-gilt/src/Overview/Queue.tsx
Normal file
32
packages/page-gilt/src/Overview/Queue.tsx
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { QueueTotal } from './types';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
value: QueueTotal;
|
||||
}
|
||||
|
||||
function Queue ({ className, value: { balance, index, numItems } }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<tr className={className}>
|
||||
<td className='number'>
|
||||
<h1>{formatNumber(index)}</h1>
|
||||
</td>
|
||||
<td className='number all'>
|
||||
{formatNumber(numItems)}
|
||||
</td>
|
||||
<td className='all'>
|
||||
<FormatBalance value={balance} />
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Queue);
|
||||
43
packages/page-gilt/src/Overview/Queues.tsx
Normal file
43
packages/page-gilt/src/Overview/Queues.tsx
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { QueueTotal } from './types';
|
||||
|
||||
import React, { useRef } from 'react';
|
||||
|
||||
import { Table } from '@polkadot/react-components';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import Queue from './Queue';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
queueTotals?: QueueTotal[];
|
||||
}
|
||||
|
||||
function Queues ({ className, queueTotals }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const headerRef = useRef([
|
||||
[t('queues'), 'start'],
|
||||
[t('participants'), 'number'],
|
||||
[t('balance'), 'number']
|
||||
]);
|
||||
|
||||
return (
|
||||
<Table
|
||||
className={className}
|
||||
empty={queueTotals && t<string>('No active gilt queues found.')}
|
||||
header={headerRef.current}
|
||||
>
|
||||
{queueTotals?.map((value) => (
|
||||
<Queue
|
||||
key={value.index}
|
||||
value={value}
|
||||
/>
|
||||
))}
|
||||
</Table>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Queues);
|
||||
67
packages/page-gilt/src/Overview/Summary.tsx
Normal file
67
packages/page-gilt/src/Overview/Summary.tsx
Normal file
@ -0,0 +1,67 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ActiveGiltsTotal } from '@polkadot/types/interfaces';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
|
||||
import { CardSummary, SummaryBox } from '@polkadot/react-components';
|
||||
import { useApi, useBestNumber } from '@polkadot/react-hooks';
|
||||
import { BN_QUINTILL, formatNumber } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
interface Props {
|
||||
activeTotal?: ActiveGiltsTotal;
|
||||
className?: string;
|
||||
isDisabled: boolean;
|
||||
}
|
||||
|
||||
const DIVIDOR_NU = 10_000;
|
||||
const DIVISOR_BN = new BN(10_000);
|
||||
|
||||
function Summary ({ activeTotal, className, isDisabled }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const bestNumber = useBestNumber();
|
||||
|
||||
return (
|
||||
<SummaryBox className={className}>
|
||||
<section>
|
||||
<CardSummary label={t<string>('active')}>
|
||||
{isDisabled ? t('no') : t('yes')}
|
||||
</CardSummary>
|
||||
{activeTotal && (
|
||||
<CardSummary label={t<string>('index')}>
|
||||
{formatNumber(activeTotal.index)}
|
||||
</CardSummary>
|
||||
)}
|
||||
</section>
|
||||
{activeTotal && (
|
||||
<section>
|
||||
<CardSummary label={t<string>('proportion')}>
|
||||
{(activeTotal.proportion.mul(DIVISOR_BN).div(BN_QUINTILL).toNumber() / DIVIDOR_NU).toFixed(2)}%
|
||||
</CardSummary>
|
||||
<CardSummary label={t<string>('target')}>
|
||||
{(activeTotal.target.mul(DIVISOR_BN).div(BN_QUINTILL).toNumber() / DIVIDOR_NU).toFixed(2)}%
|
||||
</CardSummary>
|
||||
</section>
|
||||
)}
|
||||
<section>
|
||||
{bestNumber && (
|
||||
<CardSummary
|
||||
label={t<string>('intake')}
|
||||
progress={{
|
||||
total: api.consts.gilt.intakePeriod,
|
||||
value: bestNumber.mod(api.consts.gilt.intakePeriod),
|
||||
withTime: true
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</section>
|
||||
</SummaryBox>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Summary);
|
||||
39
packages/page-gilt/src/Overview/index.tsx
Normal file
39
packages/page-gilt/src/Overview/index.tsx
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import React, { useMemo } from 'react';
|
||||
|
||||
import { Button } from '@polkadot/react-components';
|
||||
|
||||
import BidAdd from './BidAdd';
|
||||
import Queues from './Queues';
|
||||
import Summary from './Summary';
|
||||
import useInfo from './useInfo';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
function Overview ({ className }: Props): React.ReactElement<Props> {
|
||||
const { info } = useInfo();
|
||||
|
||||
const isDisabled = useMemo(
|
||||
() => !info || !info.activeTotal || info.activeTotal.target.isZero(),
|
||||
[info]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<Summary
|
||||
activeTotal={info?.activeTotal}
|
||||
isDisabled={isDisabled}
|
||||
/>
|
||||
<Button.Group>
|
||||
<BidAdd isDisabled={isDisabled} />
|
||||
</Button.Group>
|
||||
<Queues queueTotals={info?.queueTotals} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Overview);
|
||||
17
packages/page-gilt/src/Overview/types.ts
Normal file
17
packages/page-gilt/src/Overview/types.ts
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type BN from 'bn.js';
|
||||
import type { ActiveGiltsTotal, BalanceOf } from '@polkadot/types/interfaces';
|
||||
|
||||
export interface QueueTotal {
|
||||
balance: BalanceOf;
|
||||
index: number;
|
||||
numItems: BN;
|
||||
}
|
||||
|
||||
export interface GiltInfo {
|
||||
activeIndex?: BN | null;
|
||||
activeTotal?: ActiveGiltsTotal;
|
||||
queueTotals?: QueueTotal[];
|
||||
}
|
||||
49
packages/page-gilt/src/Overview/useInfo.ts
Normal file
49
packages/page-gilt/src/Overview/useInfo.ts
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { u32 } from '@polkadot/types';
|
||||
import type { ActiveGiltsTotal, BalanceOf } from '@polkadot/types/interfaces';
|
||||
import type { GiltInfo, QueueTotal } from './types';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useApi, useCallMulti } from '@polkadot/react-hooks';
|
||||
import { BN_ONE } from '@polkadot/util';
|
||||
|
||||
interface State {
|
||||
info?: GiltInfo;
|
||||
}
|
||||
|
||||
const optGiltInfo = {
|
||||
defaultValue: {} as GiltInfo,
|
||||
transform: ([activeTotal, queueTotals]: [ActiveGiltsTotal, [u32, BalanceOf][]]): GiltInfo => ({
|
||||
activeIndex: activeTotal.index.isZero()
|
||||
? null
|
||||
: activeTotal.index.sub(BN_ONE),
|
||||
activeTotal,
|
||||
queueTotals: queueTotals
|
||||
.map(([numItems, balance], index): QueueTotal => ({ balance, index: index + 1, numItems }))
|
||||
.filter(({ balance }) => !balance.isZero())
|
||||
})
|
||||
};
|
||||
|
||||
export default function useInfo (): State {
|
||||
const { api } = useApi();
|
||||
const info = useCallMulti<GiltInfo>([
|
||||
api.query.gilt.activeTotal,
|
||||
api.query.gilt.queueTotals
|
||||
], optGiltInfo);
|
||||
|
||||
// useEffect((): void => {
|
||||
// info.activeIndex &&
|
||||
// api.query.gilt.active
|
||||
// .entries()
|
||||
// .then((value) => console.log(JSON.stringify(value)))
|
||||
// .catch(console.error);
|
||||
// }, [api, info?.activeIndex]);
|
||||
|
||||
return useMemo(
|
||||
() => ({ info }),
|
||||
[info]
|
||||
);
|
||||
}
|
||||
@ -1,8 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import React from 'react';
|
||||
import React, { useRef } from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
|
||||
import { Tabs } from '@polkadot/react-components';
|
||||
|
||||
import Overview from './Overview';
|
||||
import { useTranslation } from './translate';
|
||||
|
||||
interface Props {
|
||||
@ -10,10 +14,30 @@ interface Props {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
function GiltApp ({ className }: Props): React.ReactElement<Props> {
|
||||
function GiltApp ({ basePath, className }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return <div className={className}>{t<string>('gilts')}</div>;
|
||||
const tabsRef = useRef([
|
||||
{
|
||||
isRoot: true,
|
||||
name: 'overview',
|
||||
text: t<string>('Gilt overview')
|
||||
}
|
||||
]);
|
||||
|
||||
return (
|
||||
<main className={className}>
|
||||
<Tabs
|
||||
basePath={basePath}
|
||||
items={tabsRef.current}
|
||||
/>
|
||||
<Switch>
|
||||
<Route>
|
||||
<Overview />
|
||||
</Route>
|
||||
</Switch>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(GiltApp);
|
||||
|
||||
@ -5,6 +5,6 @@ import type { UseTranslationResponse } from 'react-i18next';
|
||||
|
||||
import { useTranslation as useTranslationBase } from 'react-i18next';
|
||||
|
||||
export function useTranslation (): UseTranslationResponse<'app-poll'> {
|
||||
return useTranslationBase('app-poll');
|
||||
export function useTranslation (): UseTranslationResponse<'app-gilt'> {
|
||||
return useTranslationBase('app-gilt');
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
import type BN from 'bn.js';
|
||||
import type { DeriveSociety } from '@polkadot/api-derive/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import React, { useMemo } from 'react';
|
||||
import styled from 'styled-components';
|
||||
@ -48,8 +47,8 @@ function Summary ({ className = '', info, payoutTotal }: Props): React.ReactElem
|
||||
<CardSummary
|
||||
label={t<string>('rotation')}
|
||||
progress={{
|
||||
total: api.consts.society.rotationPeriod as BlockNumber,
|
||||
value: bestNumber.mod(api.consts.society.rotationPeriod as BlockNumber),
|
||||
total: api.consts.society.rotationPeriod,
|
||||
value: bestNumber.mod(api.consts.society.rotationPeriod),
|
||||
withTime: true
|
||||
}}
|
||||
/>
|
||||
@ -58,8 +57,8 @@ function Summary ({ className = '', info, payoutTotal }: Props): React.ReactElem
|
||||
<CardSummary
|
||||
label={t<string>('challenge')}
|
||||
progress={{
|
||||
total: api.consts.society.challengePeriod as BlockNumber,
|
||||
value: bestNumber.mod(api.consts.society.challengePeriod as BlockNumber),
|
||||
total: api.consts.society.challengePeriod,
|
||||
value: bestNumber.mod(api.consts.society.challengePeriod),
|
||||
withTime: true
|
||||
}}
|
||||
/>
|
||||
|
||||
@ -22,7 +22,7 @@ interface Props {
|
||||
bitLength?: BitLength;
|
||||
children?: React.ReactNode;
|
||||
className?: string;
|
||||
defaultValue?: string;
|
||||
defaultValue?: string | BN;
|
||||
help?: React.ReactNode;
|
||||
isDisabled?: boolean;
|
||||
isError?: boolean;
|
||||
@ -192,7 +192,7 @@ function InputNumber ({ autoFocus, bitLength = DEFAULT_BITLENGTH, children, clas
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
defaultValue && _onChange(defaultValue);
|
||||
defaultValue && _onChange(defaultValue.toString());
|
||||
}, [_onChange, defaultValue]);
|
||||
|
||||
const _onKeyDown = useCallback(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user