Only allow Gilt bids via proxy (#6103)

* Only allow Gilt bids via proxy

* Proxy actual bid

* Fix summary
This commit is contained in:
Jaco 2021-09-13 08:23:28 +03:00 committed by GitHub
parent f53a6e2593
commit 162470d80e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 18 deletions

View File

@ -11,7 +11,8 @@ export default function create (t: TFunction): Route {
Component,
display: {
needsApi: [
'tx.gilt.placeBid'
'tx.gilt.placeBid',
'query.proxy.proxies'
]
},
group: 'network',

View File

@ -3,10 +3,10 @@
import type BN from 'bn.js';
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import { Button, InputAddress, InputBalance, InputNumber, Modal, TxButton } from '@polkadot/react-components';
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
import { useApi, useToggle } from '@polkadot/react-hooks';
import { Available } from '@polkadot/react-query';
import { BN_ONE } from '@polkadot/util';
@ -15,17 +15,26 @@ import { useTranslation } from '../translate';
interface Props {
className?: string;
isDisabled: boolean;
proxies: Record<string, string[]>;
}
function Bid ({ className, isDisabled }: Props): React.ReactElement<Props> {
function Bid ({ className, isDisabled, proxies }: 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 [proxyId, setProxyId] = useState<string | null>(null);
const [amount, setAmount] = useState<BN | undefined>();
const [duration, setDuration] = useState<BN | undefined>();
const tx = useMemo(
() => accountId && amount && duration
? api.tx.proxy.proxy(accountId, null, api.tx.gilt.placeBid(amount, duration))
: null,
[api, accountId, amount, duration]
);
const proxiedAccounts = Object.keys(proxies);
const isAmountError = !amount || amount.isZero() || amount.lt(api.consts.gilt.minFreeze);
const isDurationError = !duration || !duration.gte(BN_ONE) || duration.gt(api.consts.gilt.queueCount);
@ -33,7 +42,7 @@ function Bid ({ className, isDisabled }: Props): React.ReactElement<Props> {
<>
<Button
icon='plus'
isDisabled={!hasAccounts || isDisabled}
isDisabled={!proxiedAccounts.length || isDisabled}
label={t<string>('Submit Bid')}
onClick={toggleOpen}
/>
@ -47,8 +56,9 @@ function Bid ({ className, isDisabled }: Props): React.ReactElement<Props> {
<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')}
filter={proxiedAccounts}
help={t<string>('The account you want to register the bid from')}
label={t<string>('use proxied account')}
labelExtra={
<Available
label={<span className='label'>{t<string>('transferrable')}</span>}
@ -58,6 +68,15 @@ function Bid ({ className, isDisabled }: Props): React.ReactElement<Props> {
onChange={setAccountId}
type='account'
/>
{accountId && (
<InputAddress
filter={proxies[accountId]}
help={t<string>('The associated proxy to use for this account')}
label={t<string>('send via proxy')}
onChange={setProxyId}
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
@ -92,13 +111,12 @@ function Bid ({ className, isDisabled }: Props): React.ReactElement<Props> {
</Modal.Content>
<Modal.Actions>
<TxButton
accountId={accountId}
accountId={proxyId}
extrinsic={tx}
icon='check'
isDisabled={isAmountError || isDurationError}
isDisabled={isAmountError || isDurationError || !accountId}
label={t<string>('Bid')}
onStart={toggleOpen}
params={[amount, duration]}
tx={api.tx.gilt.placeBid}
/>
</Modal.Actions>
</Modal>

View File

@ -8,7 +8,7 @@ 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 { BN_HUNDRED, BN_QUINTILL, formatNumber } from '@polkadot/util';
import { useTranslation } from '../translate';
@ -41,10 +41,10 @@ function Summary ({ activeTotal, className, isDisabled }: Props): React.ReactEle
{activeTotal && (
<section>
<CardSummary label={t<string>('proportion')}>
{(activeTotal.proportion.mul(DIVISOR_BN).div(BN_QUINTILL).toNumber() / DIVIDOR_NU).toFixed(2)}%
{(activeTotal.proportion.mul(DIVISOR_BN).imul(BN_HUNDRED).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)}%
{(activeTotal.target.mul(DIVISOR_BN).imul(BN_HUNDRED).div(BN_QUINTILL).toNumber() / DIVIDOR_NU).toFixed(2)}%
</CardSummary>
</section>
)}

View File

@ -5,6 +5,7 @@ import React, { useMemo } from 'react';
import { Button } from '@polkadot/react-components';
import { useProxies } from '../useProxies';
import BidAdd from './BidAdd';
import Queues from './Queues';
import Summary from './Summary';
@ -15,6 +16,7 @@ interface Props {
}
function Overview ({ className }: Props): React.ReactElement<Props> {
const proxies = useProxies();
const { info } = useInfo();
const isDisabled = useMemo(
@ -29,7 +31,10 @@ function Overview ({ className }: Props): React.ReactElement<Props> {
isDisabled={isDisabled}
/>
<Button.Group>
<BidAdd isDisabled={isDisabled} />
<BidAdd
isDisabled={isDisabled}
proxies={proxies}
/>
</Button.Group>
<Queues queueTotals={info?.queueTotals} />
</div>

View File

@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/app-poll authors & contributors
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { UseTranslationResponse } from 'react-i18next';

View File

@ -0,0 +1,40 @@
// Copyright 2017-2021 @polkadot/app-gilt authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Vec } from '@polkadot/types';
import type { BalanceOf, ProxyDefinition } from '@polkadot/types/interfaces';
import type { ITuple } from '@polkadot/types/types';
import { useEffect, useState } from 'react';
import { useAccounts, useApi, useIsMountedRef } from '@polkadot/react-hooks';
type ProxyResult = ITuple<[Vec<ProxyDefinition>, BalanceOf]>;
export function useProxies (): Record<string, string[]> {
const { api } = useApi();
const { allAccounts } = useAccounts();
const mountedRef = useIsMountedRef();
const [state, setState] = useState<Record<string, string[]>>({});
useEffect((): void => {
if (allAccounts.length) {
api.query.proxy.proxies
.multi<ProxyResult>(allAccounts)
.then((result) =>
mountedRef.current && setState(
result
.map(([p], index): [string, string[]] => [
allAccounts[index],
p.map(({ delegate }) => delegate.toString())
])
.filter(([, p]) => p.length)
.reduce((all, [a, p]) => ({ ...all, [a]: p }), {})
)
)
.catch(console.error);
}
}, [allAccounts, api, mountedRef]);
return state;
}

View File

@ -23,7 +23,7 @@ import createItem from './createItem';
interface Props {
className?: string;
defaultValue?: Uint8Array | string | null;
filter?: string[];
filter?: string[] | null;
help?: React.ReactNode;
hideAddress?: boolean;
isDisabled?: boolean;