Display transaction fees on signer (#596)
* Move fee display component to ui-signer * Format fee display * Cleanup Fees, adjust calls with no params * WIP * WIP * Cleanup derive file naming) * Fees align with transfer * Calculate fees (minimum) around proposals * Apply nonce length correctly
This commit is contained in:
parent
69b4b79606
commit
19258df2c6
@ -1,199 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/app-transfer authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { I18nProps } from '@polkadot/ui-app/types';
|
||||
import { DerivedBalancesFees, DerivedBalances } from '@polkadot/ui-api/derive/types';
|
||||
import { Fees } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { Balance, Extrinsic } from '@polkadot/types';
|
||||
import { Static } from '@polkadot/ui-app/index';
|
||||
import { withCall, withMulti } from '@polkadot/ui-api/index';
|
||||
import { balanceFormat } from '@polkadot/ui-reactive/util/index';
|
||||
|
||||
import translate from './translate';
|
||||
|
||||
type State = Fees;
|
||||
|
||||
type Props = I18nProps & {
|
||||
accountId?: string | null,
|
||||
amount: BN,
|
||||
balanceFrom?: DerivedBalances,
|
||||
balanceTo?: DerivedBalances,
|
||||
extrinsic: Extrinsic | null,
|
||||
fees?: DerivedBalancesFees,
|
||||
recipientId?: string | null,
|
||||
onChange: (fees: Fees) => void
|
||||
};
|
||||
|
||||
const ZERO_BALANCE = {
|
||||
freeBalance: new Balance(0),
|
||||
reservedBalance: new Balance(0),
|
||||
votingBalance: new Balance(0)
|
||||
} as DerivedBalances;
|
||||
|
||||
const ZERO_FEES = {
|
||||
transactionBaseFee: new BN(0),
|
||||
transactionByteFee: new BN(0),
|
||||
creationFee: new BN(0),
|
||||
existentialDeposit: new BN(0),
|
||||
transferFee: new BN(0)
|
||||
} as DerivedBalancesFees;
|
||||
|
||||
const LENGTH_PUBLICKEY = 32 + 1; // publicKey + prefix
|
||||
const LENGTH_SIGNATURE = 64;
|
||||
const LENGTH_NONCE = 8;
|
||||
const LENGTH_ERA = 1;
|
||||
|
||||
// FIXME Hardcoded signature size. This _should_ be ok for the poc-3 version, however in reality is needs to
|
||||
// come from somewhere else (signer?). The issue is that we don't have the tx signed at this point, so we are
|
||||
// manually adding the signature size. As it stands, this will be problematic with Compact encoding on accountNonce.
|
||||
// Additionally, era assumes immortal transcations...
|
||||
const SIGNATURE_SIZE = LENGTH_PUBLICKEY + LENGTH_SIGNATURE + LENGTH_NONCE + LENGTH_ERA;
|
||||
|
||||
class FeeDisplay extends React.PureComponent<Props, State> {
|
||||
constructor (props: Props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
hasAvailable: false,
|
||||
isCreation: false,
|
||||
isNoEffect: false,
|
||||
isRemovable: false,
|
||||
isReserved: false,
|
||||
txfees: new BN(0),
|
||||
txtotal: new BN(0)
|
||||
};
|
||||
}
|
||||
|
||||
static getDerivedStateFromProps ({ accountId, amount, balanceTo = ZERO_BALANCE, balanceFrom = ZERO_BALANCE, extrinsic, fees = ZERO_FEES, recipientId }: Props): State | null {
|
||||
if (!accountId || !recipientId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const txLength = SIGNATURE_SIZE + (
|
||||
extrinsic
|
||||
? extrinsic.encodedLength
|
||||
: 0
|
||||
);
|
||||
|
||||
let txfees = fees.transactionBaseFee
|
||||
.add(fees.transferFee)
|
||||
.add(fees.transactionByteFee.muln(txLength));
|
||||
|
||||
if (balanceTo.votingBalance.isZero()) {
|
||||
txfees = txfees.add(fees.creationFee);
|
||||
}
|
||||
|
||||
const txtotal = amount.add(txfees);
|
||||
const hasAvailable = balanceFrom.freeBalance.gte(txtotal);
|
||||
const isCreation = balanceTo.votingBalance.isZero();
|
||||
const isNoEffect = amount.add(balanceTo.votingBalance).lte(fees.existentialDeposit);
|
||||
const isRemovable = balanceFrom.votingBalance.sub(txtotal).lte(fees.existentialDeposit);
|
||||
const isReserved = balanceFrom.freeBalance.isZero() && balanceFrom.reservedBalance.gtn(0);
|
||||
|
||||
return {
|
||||
hasAvailable,
|
||||
isCreation,
|
||||
isNoEffect,
|
||||
isRemovable,
|
||||
isReserved,
|
||||
txfees,
|
||||
txtotal
|
||||
};
|
||||
}
|
||||
|
||||
componentDidUpdate () {
|
||||
const { onChange } = this.props;
|
||||
|
||||
onChange(this.state);
|
||||
}
|
||||
|
||||
render () {
|
||||
const { accountId, className, fees = ZERO_FEES, recipientId, t } = this.props;
|
||||
const { hasAvailable, isCreation, isNoEffect, isRemovable, isReserved, txfees, txtotal } = this.state;
|
||||
|
||||
if (!accountId || !recipientId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const feeClass = hasAvailable
|
||||
? (
|
||||
(isRemovable || isNoEffect)
|
||||
? 'warning'
|
||||
: 'normal'
|
||||
)
|
||||
: 'error';
|
||||
|
||||
return [
|
||||
<Static
|
||||
className={className}
|
||||
key='txfees'
|
||||
label={t('fees', {
|
||||
defaultValue: 'with fees totalling'
|
||||
})}
|
||||
value={`${balanceFormat(txfees)}`}
|
||||
/>,
|
||||
<article
|
||||
className={feeClass}
|
||||
key='txinfo'
|
||||
>
|
||||
{
|
||||
isRemovable && hasAvailable
|
||||
? t('fees.remove', {
|
||||
defaultValue: 'Submitting this transaction will drop the account balance to below the existential amount, removing the account from the chain state and burning associated funds. '
|
||||
})
|
||||
: undefined
|
||||
}{
|
||||
isNoEffect && hasAvailable
|
||||
? t('fees.noeffect', {
|
||||
defaultValue: 'The final recipient amount is less than the existential amount, hence the total will be deducted from the sender, however the recipient account will not reflect the amount sent. '
|
||||
})
|
||||
: undefined
|
||||
}{
|
||||
hasAvailable
|
||||
? undefined
|
||||
: t('fees.available', {
|
||||
defaultValue: 'The account does not have the required funds available for this transaction with the current values. '
|
||||
})
|
||||
}{
|
||||
isReserved
|
||||
? t('fees.reserved', {
|
||||
defaultValue: '(This account does have a reserved/locked balance, staking locks up the available funds) '
|
||||
})
|
||||
: undefined
|
||||
}{
|
||||
t('fees.explain', {
|
||||
defaultValue: 'Fees includes the transaction fee and the per-byte fee. '
|
||||
})
|
||||
}{
|
||||
isCreation && fees.creationFee.gtn(0)
|
||||
? t('fees.create', {
|
||||
defaultValue: 'A fee of {{creationFee}} will be deducted from the sender since the destination account does not exist.',
|
||||
replace: {
|
||||
creationFee: `${balanceFormat(fees.creationFee)}`
|
||||
}
|
||||
})
|
||||
: undefined
|
||||
}
|
||||
</article>,
|
||||
<Static
|
||||
className='medium'
|
||||
key='txtotal'
|
||||
label={t('total', {
|
||||
defaultValue: 'total transaction amount (fees + value)'
|
||||
})}
|
||||
value={`${balanceFormat(txtotal)}`}
|
||||
/>
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
FeeDisplay,
|
||||
translate,
|
||||
withCall('derive.balances.votingBalance', { paramProp: 'accountId', propName: 'balanceFrom' }),
|
||||
withCall('derive.balances.votingBalance', { paramProp: 'recipientId', propName: 'balanceTo' })
|
||||
);
|
||||
@ -4,9 +4,7 @@
|
||||
|
||||
import { BitLength, I18nProps } from '@polkadot/ui-app/types';
|
||||
import { QueueProps } from '@polkadot/ui-app/Status/types';
|
||||
import { DerivedBalancesFees } from '@polkadot/ui-api/derive/types';
|
||||
import { ApiProps } from '@polkadot/ui-api/types';
|
||||
import { Fees } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
@ -14,23 +12,21 @@ import { decodeAddress } from '@polkadot/keyring';
|
||||
import { Extrinsic } from '@polkadot/types';
|
||||
import { BitLengthOption } from '@polkadot/ui-app/constants';
|
||||
import { AddressSummary, InputAddress, InputNumber } from '@polkadot/ui-app/index';
|
||||
import { withCall, withMulti } from '@polkadot/ui-api/index';
|
||||
import { withApi, withMulti } from '@polkadot/ui-api/index';
|
||||
import { QueueConsumer } from '@polkadot/ui-app/Status/Context';
|
||||
import FeeDisplay from '@polkadot/ui-signer/Fees';
|
||||
|
||||
import FeeDisplay from './Fees';
|
||||
import Submit from './Submit';
|
||||
import translate from './translate';
|
||||
|
||||
type Props = I18nProps & ApiProps & {
|
||||
derive_balances_fees?: DerivedBalancesFees
|
||||
};
|
||||
type Props = I18nProps & ApiProps & {};
|
||||
|
||||
type State = {
|
||||
accountId: string | null,
|
||||
amount: BN,
|
||||
extrinsic: Extrinsic | null,
|
||||
recipientId: string | null,
|
||||
txfees: Fees
|
||||
hasAvailable: boolean,
|
||||
recipientId: string | null
|
||||
};
|
||||
|
||||
const DEFAULT_BITLENGTH = BitLengthOption.CHAIN_SPEC as BitLength;
|
||||
@ -47,17 +43,14 @@ class Transfer extends React.PureComponent<Props, State> {
|
||||
accountId: null,
|
||||
amount: ZERO,
|
||||
extrinsic: null,
|
||||
recipientId: null,
|
||||
txfees: {
|
||||
hasAvailable: false,
|
||||
txtotal: ZERO
|
||||
} as Fees
|
||||
hasAvailable: true,
|
||||
recipientId: null
|
||||
};
|
||||
}
|
||||
|
||||
render () {
|
||||
const { derive_balances_fees, t } = this.props;
|
||||
const { accountId, amount, extrinsic, recipientId, txfees: { hasAvailable } } = this.state;
|
||||
const { t } = this.props;
|
||||
const { accountId, extrinsic, recipientId, hasAvailable } = this.state;
|
||||
|
||||
return (
|
||||
<div className='transfer--Transfer'>
|
||||
@ -97,9 +90,7 @@ class Transfer extends React.PureComponent<Props, State> {
|
||||
<FeeDisplay
|
||||
className='medium'
|
||||
accountId={accountId}
|
||||
amount={amount}
|
||||
fees={derive_balances_fees}
|
||||
recipientId={recipientId}
|
||||
extrinsic={extrinsic}
|
||||
onChange={this.onChangeFees}
|
||||
/>
|
||||
<QueueConsumer>
|
||||
@ -143,7 +134,7 @@ class Transfer extends React.PureComponent<Props, State> {
|
||||
private nextState (newState: Partial<State>): void {
|
||||
this.setState((prevState: State): State => {
|
||||
const { apiPromise } = this.props;
|
||||
const { accountId = prevState.accountId, amount = prevState.amount, recipientId = prevState.recipientId, txfees = prevState.txfees } = newState;
|
||||
const { accountId = prevState.accountId, amount = prevState.amount, recipientId = prevState.recipientId, hasAvailable = prevState.hasAvailable } = newState;
|
||||
const extrinsic = accountId && recipientId
|
||||
? apiPromise.tx.balances.transfer(recipientId, amount)
|
||||
: null;
|
||||
@ -152,8 +143,8 @@ class Transfer extends React.PureComponent<Props, State> {
|
||||
accountId,
|
||||
amount,
|
||||
extrinsic,
|
||||
recipientId,
|
||||
txfees
|
||||
hasAvailable,
|
||||
recipientId
|
||||
};
|
||||
});
|
||||
}
|
||||
@ -170,13 +161,13 @@ class Transfer extends React.PureComponent<Props, State> {
|
||||
this.nextState({ recipientId });
|
||||
}
|
||||
|
||||
private onChangeFees = (txfees: Fees) => {
|
||||
this.setState({ txfees });
|
||||
private onChangeFees = (hasAvailable: boolean) => {
|
||||
this.setState({ hasAvailable });
|
||||
}
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
Transfer,
|
||||
translate,
|
||||
withCall('derive.balances.fees')
|
||||
withApi
|
||||
);
|
||||
|
||||
@ -19,10 +19,7 @@ type Props = I18nProps & {
|
||||
allAccounts?: SubjectInfo
|
||||
};
|
||||
|
||||
type State = {
|
||||
};
|
||||
|
||||
class App extends React.PureComponent<Props, State> {
|
||||
class App extends React.PureComponent<Props> {
|
||||
render () {
|
||||
return (
|
||||
<main className='transfer--App'>
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/app-toolbox authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
export type Fees = {
|
||||
hasAvailable: boolean,
|
||||
isCreation: boolean,
|
||||
isNoEffect: boolean,
|
||||
isRemovable: boolean,
|
||||
isReserved: boolean,
|
||||
txfees: BN,
|
||||
txtotal: BN
|
||||
};
|
||||
@ -18,10 +18,10 @@ export default function accountIdAndIndex (api: ApiPromise): DeriveSubscription
|
||||
return async (address: AccountId | AccountIndex | string | null | undefined, cb: (idAndIndex: IdAndIndex) => any): PromiseSubscription => {
|
||||
try {
|
||||
// yes, this can fail, don't care too much, catch will catch it
|
||||
const length = decodeAddress((address as any).toString()).length;
|
||||
const decoded = decodeAddress((address as any).toString());
|
||||
|
||||
if (length === 32) {
|
||||
const accountId = new AccountId(address as string);
|
||||
if (decoded.length === 32) {
|
||||
const accountId = new AccountId(decoded);
|
||||
|
||||
return accountIdToIndex(api)(accountId, (accountIndex?: AccountIndex) =>
|
||||
cb([accountId, accountIndex])
|
||||
@ -11,8 +11,8 @@ import { AccountId, AccountIndex } from '@polkadot/types';
|
||||
import accountIndexes, { AccountIndexes } from './accountIndexes';
|
||||
|
||||
export default function accountIdToIndex (api: ApiPromise): DeriveSubscription {
|
||||
return async (accountId: AccountId | string, cb: (index?: AccountIndex) => any): PromiseSubscription =>
|
||||
return async (accountId: AccountId | string | null, cb: (index?: AccountIndex) => any): PromiseSubscription =>
|
||||
accountIndexes(api)((indexes?: AccountIndexes) =>
|
||||
cb((indexes || {})[accountId.toString()])
|
||||
cb((indexes || {})[(accountId || '').toString()])
|
||||
);
|
||||
}
|
||||
|
||||
@ -20,6 +20,13 @@ function flatten (key: string | null, value: any): any {
|
||||
return value;
|
||||
}
|
||||
|
||||
export default function isEqual <T> (a?: T, b?: T): boolean {
|
||||
return JSON.stringify({ test: a }, flatten) === JSON.stringify({ test: b }, flatten);
|
||||
export default function isEqual <T> (a?: T, b?: T, debug: boolean = false): boolean {
|
||||
const jsonA = JSON.stringify({ test: a }, flatten);
|
||||
const jsonB = JSON.stringify({ test: b }, flatten);
|
||||
|
||||
if (debug) {
|
||||
console.log('jsonA', jsonA, 'jsonB', jsonB);
|
||||
}
|
||||
|
||||
return jsonA === jsonB;
|
||||
}
|
||||
|
||||
@ -55,8 +55,9 @@ export default function withCall<T, P> (endpoint: string, { at, atProp, rxChange
|
||||
|
||||
componentDidUpdate (prevProps: any) {
|
||||
const newParams = this.getParams(this.props);
|
||||
const oldParams = this.getParams(prevProps);
|
||||
|
||||
if (!isEqual(newParams, this.getParams(prevProps))) {
|
||||
if (!isEqual(newParams, oldParams)) {
|
||||
this
|
||||
.subscribe(newParams)
|
||||
.then(NOOP)
|
||||
|
||||
@ -77,7 +77,6 @@ class AddressSummary extends React.PureComponent<Props> {
|
||||
: value
|
||||
}
|
||||
</div>
|
||||
{this.renderCopy(address)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -106,7 +105,6 @@ class AddressSummary extends React.PureComponent<Props> {
|
||||
: address
|
||||
}
|
||||
</div>
|
||||
{this.renderCopy(address)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -150,20 +148,6 @@ class AddressSummary extends React.PureComponent<Props> {
|
||||
);
|
||||
}
|
||||
|
||||
protected renderCopy (address: string) {
|
||||
return null;
|
||||
|
||||
// const { withCopy = true } = this.props;
|
||||
|
||||
// if (!withCopy || !address) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// return (
|
||||
// <CopyButton value={address} />
|
||||
// );
|
||||
}
|
||||
|
||||
protected renderIcon (className: string = 'ui--AddressSummary-icon', size?: number) {
|
||||
const { derive_balances_accountIdAndIndex = [], identIconSize = 96, query_session_validators, value, withIcon = true } = this.props;
|
||||
|
||||
|
||||
@ -53,6 +53,11 @@ article {
|
||||
/* border-top-width: 1px; */
|
||||
margin: 0.5rem;
|
||||
padding: 1rem 1.5rem;
|
||||
text-align: left;
|
||||
|
||||
&.padded {
|
||||
padding: 1rem 2.5rem;
|
||||
}
|
||||
|
||||
&.error {
|
||||
background: #fff6f6;
|
||||
@ -64,6 +69,11 @@ article {
|
||||
background: #ffffe0;
|
||||
border-color: #eeeeae;
|
||||
}
|
||||
|
||||
> ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
|
||||
header,
|
||||
|
||||
86
packages/ui-signer/src/Fees/Proposal.tsx
Normal file
86
packages/ui-signer/src/Fees/Proposal.tsx
Normal file
@ -0,0 +1,86 @@
|
||||
// Copyright 2017-2019 @polkadot/ui-signer authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { DerivedBalancesFees } from '@polkadot/ui-api/derive/types';
|
||||
import { I18nProps } from '@polkadot/ui-app/types';
|
||||
import { ExtraFees } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { Compact } from '@polkadot/types/codec';
|
||||
import { withCall, withMulti } from '@polkadot/ui-api/index';
|
||||
import { balanceFormat } from '@polkadot/ui-reactive/util/index';
|
||||
|
||||
import translate from '../translate';
|
||||
|
||||
type Props = I18nProps & {
|
||||
deposit: BN | Compact,
|
||||
fees: DerivedBalancesFees,
|
||||
query_democracy_minimumDeposit?: BN
|
||||
onChange: (fees: ExtraFees) => void
|
||||
};
|
||||
type State = ExtraFees & {
|
||||
isBelowMinimum: boolean
|
||||
};
|
||||
|
||||
class Proposal extends React.PureComponent<Props, State> {
|
||||
constructor (props: Props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
extraFees: new BN(0),
|
||||
extraAmount: new BN(0),
|
||||
extraWarn: false,
|
||||
isBelowMinimum: false
|
||||
};
|
||||
}
|
||||
|
||||
static getDerivedStateFromProps ({ deposit, query_democracy_minimumDeposit = new BN(0), onChange }: Props): State {
|
||||
const extraAmount = new BN(deposit.toNumber());
|
||||
const isBelowMinimum = extraAmount.lt(query_democracy_minimumDeposit);
|
||||
|
||||
const update = {
|
||||
extraAmount,
|
||||
extraFees: new BN(0),
|
||||
extraWarn: isBelowMinimum
|
||||
};
|
||||
|
||||
onChange(update);
|
||||
|
||||
return {
|
||||
...update,
|
||||
isBelowMinimum
|
||||
};
|
||||
}
|
||||
|
||||
render () {
|
||||
const { query_democracy_minimumDeposit = new BN(0), t } = this.props;
|
||||
const { extraAmount, isBelowMinimum } = this.state;
|
||||
|
||||
return [
|
||||
isBelowMinimum
|
||||
? <li key='belowmin'>{t('proposal.belowmin', {
|
||||
defaultValue: 'The deposit is below the {{minimum}} minimum required for the proposal to be evaluated',
|
||||
replace: {
|
||||
minimum: balanceFormat(query_democracy_minimumDeposit)
|
||||
}
|
||||
})}</li>
|
||||
: undefined,
|
||||
extraAmount.isZero()
|
||||
? undefined
|
||||
: <li key='infodeposit'>{t('proposal.deposit', {
|
||||
defaultValue: 'The deposit of {{deposit}} will be reserved until the proposal is completed',
|
||||
replace: {
|
||||
deposit: balanceFormat(extraAmount)
|
||||
}
|
||||
})}</li>
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
Proposal,
|
||||
translate,
|
||||
withCall('query.democracy.minimumDeposit')
|
||||
);
|
||||
96
packages/ui-signer/src/Fees/Transfer.tsx
Normal file
96
packages/ui-signer/src/Fees/Transfer.tsx
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright 2017-2019 @polkadot/ui-signer authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { DerivedBalances, DerivedBalancesFees } from '@polkadot/ui-api/derive/types';
|
||||
import { I18nProps } from '@polkadot/ui-app/types';
|
||||
import { ExtraFees } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { Compact } from '@polkadot/types/codec';
|
||||
import { withCall, withMulti } from '@polkadot/ui-api/index';
|
||||
import { balanceFormat } from '@polkadot/ui-reactive/util/index';
|
||||
|
||||
import translate from '../translate';
|
||||
import { ZERO_BALANCE } from './constants';
|
||||
|
||||
type Props = I18nProps & {
|
||||
amount: BN | Compact,
|
||||
fees: DerivedBalancesFees,
|
||||
derive_balances_votingBalance?: DerivedBalances,
|
||||
recipientId: string,
|
||||
onChange: (fees: ExtraFees) => void
|
||||
};
|
||||
|
||||
type State = ExtraFees & {
|
||||
isCreation: boolean,
|
||||
isNoEffect: boolean
|
||||
};
|
||||
|
||||
class Transfer extends React.PureComponent<Props, State> {
|
||||
constructor (props: Props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
extraFees: new BN(0),
|
||||
extraAmount: new BN(0),
|
||||
extraWarn: false,
|
||||
isCreation: false,
|
||||
isNoEffect: false
|
||||
};
|
||||
}
|
||||
|
||||
static getDerivedStateFromProps ({ amount, recipientId, derive_balances_votingBalance = ZERO_BALANCE, fees, onChange }: Props): State {
|
||||
let extraFees = fees.transferFee;
|
||||
|
||||
if (derive_balances_votingBalance.votingBalance.isZero()) {
|
||||
extraFees = extraFees.add(fees.creationFee);
|
||||
}
|
||||
|
||||
const extraAmount = new BN(amount.toNumber());
|
||||
const isCreation = derive_balances_votingBalance.votingBalance.isZero() && fees.creationFee.gtn(0);
|
||||
const isNoEffect = extraAmount.add(derive_balances_votingBalance.votingBalance).lte(fees.existentialDeposit);
|
||||
const extraWarn = isCreation || isNoEffect;
|
||||
const update = {
|
||||
extraAmount,
|
||||
extraFees,
|
||||
extraWarn
|
||||
};
|
||||
|
||||
onChange(update);
|
||||
|
||||
return {
|
||||
...update,
|
||||
isCreation,
|
||||
isNoEffect
|
||||
};
|
||||
}
|
||||
|
||||
render () {
|
||||
const { fees, t } = this.props;
|
||||
const { isCreation, isNoEffect } = this.state;
|
||||
|
||||
return [
|
||||
isNoEffect
|
||||
? <li key='noeffect'>{t('transfer.noeffect', {
|
||||
defaultValue: 'The final recipient amount is less than the existential amount, hence the total will be deducted from the sender, however the recipient account will not reflect the amount sent'
|
||||
})}</li>
|
||||
: undefined,
|
||||
isCreation
|
||||
? <li key='create'>{t('transfer.create', {
|
||||
defaultValue: 'A fee of {{creationFee}} will be deducted from the sender since the destination account does not exist',
|
||||
replace: {
|
||||
creationFee: `${balanceFormat(fees.creationFee)}`
|
||||
}
|
||||
})}</li>
|
||||
: undefined
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
Transfer,
|
||||
translate,
|
||||
withCall('derive.balances.votingBalance', { paramProp: 'recipientId' })
|
||||
);
|
||||
21
packages/ui-signer/src/Fees/constants.ts
Normal file
21
packages/ui-signer/src/Fees/constants.ts
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2019 @polkadot/ui-signer authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { DerivedBalancesFees, DerivedBalances } from '@polkadot/ui-api/derive/types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
export const ZERO_BALANCE = {
|
||||
freeBalance: new BN(0),
|
||||
reservedBalance: new BN(0),
|
||||
votingBalance: new BN(0)
|
||||
} as DerivedBalances;
|
||||
|
||||
export const ZERO_FEES = {
|
||||
transactionBaseFee: new BN(0),
|
||||
transactionByteFee: new BN(0),
|
||||
creationFee: new BN(0),
|
||||
existentialDeposit: new BN(0),
|
||||
transferFee: new BN(0)
|
||||
} as DerivedBalancesFees;
|
||||
231
packages/ui-signer/src/Fees/index.tsx
Normal file
231
packages/ui-signer/src/Fees/index.tsx
Normal file
@ -0,0 +1,231 @@
|
||||
// Copyright 2017-2019 @polkadot/ui-signer authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { I18nProps } from '@polkadot/ui-app/types';
|
||||
import { DerivedBalancesFees, DerivedBalances } from '@polkadot/ui-api/derive/types';
|
||||
import { ExtraFees } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { Extrinsic, Method } from '@polkadot/types';
|
||||
import { withCall, withMulti } from '@polkadot/ui-api/index';
|
||||
import { balanceFormat } from '@polkadot/ui-reactive/util/index';
|
||||
import { compactToU8a } from '@polkadot/util';
|
||||
|
||||
import translate from '../translate';
|
||||
import Proposal from './Proposal';
|
||||
import Transfer from './Transfer';
|
||||
import { ZERO_BALANCE, ZERO_FEES } from './constants';
|
||||
|
||||
type State = ExtraFees & {
|
||||
allFees: BN,
|
||||
allTotal: BN,
|
||||
allWarn: boolean,
|
||||
extMethod?: string,
|
||||
extSection?: string,
|
||||
hasAvailable: boolean,
|
||||
isRemovable: boolean,
|
||||
isReserved: boolean
|
||||
};
|
||||
|
||||
type Props = I18nProps & {
|
||||
derive_balances_fees?: DerivedBalancesFees,
|
||||
derive_balances_votingBalance?: DerivedBalances,
|
||||
accountId?: string | null,
|
||||
extrinsic: Extrinsic | null,
|
||||
onChange?: (hasAvailble: boolean) => void,
|
||||
query_system_accountNonce?: BN
|
||||
};
|
||||
|
||||
const LENGTH_PUBLICKEY = 32 + 1; // publicKey + prefix
|
||||
const LENGTH_SIGNATURE = 64;
|
||||
const LENGTH_ERA = 1;
|
||||
const SIGNATURE_SIZE = LENGTH_PUBLICKEY + LENGTH_SIGNATURE + LENGTH_ERA;
|
||||
|
||||
class FeeDisplay extends React.PureComponent<Props, State> {
|
||||
constructor (props: Props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
allFees: new BN(0),
|
||||
allTotal: new BN(0),
|
||||
allWarn: false,
|
||||
extraAmount: new BN(0),
|
||||
extraFees: new BN(0),
|
||||
extraWarn: false,
|
||||
hasAvailable: false,
|
||||
isRemovable: false,
|
||||
isReserved: false
|
||||
};
|
||||
}
|
||||
|
||||
static getDerivedStateFromProps ({ accountId, derive_balances_votingBalance = ZERO_BALANCE, extrinsic, derive_balances_fees = ZERO_FEES, query_system_accountNonce = new BN(0) }: Props, prevState: State): State | null {
|
||||
if (!accountId || !extrinsic) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fn = Method.findFunction(extrinsic.callIndex);
|
||||
const extMethod = fn.method;
|
||||
const extSection = fn.section;
|
||||
const txLength = SIGNATURE_SIZE + compactToU8a(query_system_accountNonce).length + (
|
||||
extrinsic
|
||||
? extrinsic.encodedLength
|
||||
: 0
|
||||
);
|
||||
const isSameExtrinsic = prevState.extMethod === extMethod && prevState.extSection === extSection;
|
||||
const extraAmount = isSameExtrinsic
|
||||
? prevState.extraAmount
|
||||
: new BN(0);
|
||||
const extraFees = isSameExtrinsic
|
||||
? prevState.extraFees
|
||||
: new BN(0);
|
||||
const extraWarn = isSameExtrinsic
|
||||
? prevState.extraWarn
|
||||
: false;
|
||||
const allFees = extraFees
|
||||
.add(derive_balances_fees.transactionBaseFee)
|
||||
.add(derive_balances_fees.transactionByteFee.muln(txLength));
|
||||
|
||||
const allTotal = extraAmount.add(allFees);
|
||||
const hasAvailable = derive_balances_votingBalance.freeBalance.gte(allTotal);
|
||||
const isRemovable = derive_balances_votingBalance.votingBalance.sub(allTotal).lte(derive_balances_fees.existentialDeposit);
|
||||
const isReserved = derive_balances_votingBalance.freeBalance.isZero() && derive_balances_votingBalance.reservedBalance.gtn(0);
|
||||
const allWarn = extraWarn;
|
||||
|
||||
return {
|
||||
allFees,
|
||||
allTotal,
|
||||
allWarn,
|
||||
extMethod,
|
||||
extSection,
|
||||
extraAmount,
|
||||
extraFees,
|
||||
extraWarn,
|
||||
hasAvailable,
|
||||
isRemovable,
|
||||
isReserved
|
||||
};
|
||||
}
|
||||
|
||||
componentDidUpdate () {
|
||||
const { onChange } = this.props;
|
||||
const { hasAvailable } = this.state;
|
||||
|
||||
onChange && onChange(hasAvailable);
|
||||
}
|
||||
|
||||
render () {
|
||||
const { accountId, className, t } = this.props;
|
||||
const { allFees, allTotal, allWarn, hasAvailable, isRemovable, isReserved } = this.state;
|
||||
|
||||
if (!accountId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const feeClass = hasAvailable
|
||||
? (
|
||||
allWarn
|
||||
? 'warning'
|
||||
: 'normal'
|
||||
)
|
||||
: 'error';
|
||||
|
||||
return (
|
||||
<article
|
||||
className={[className, feeClass, 'padded'].join(' ')}
|
||||
key='txinfo'
|
||||
>
|
||||
<ul>
|
||||
<li>{t('fees', {
|
||||
defaultValue: 'Fees totalling {{fees}} will be applied to the submission',
|
||||
replace: {
|
||||
fees: balanceFormat(allFees)
|
||||
}
|
||||
})}</li>
|
||||
<li>{t('fees.explain', {
|
||||
defaultValue: 'Fees includes the transaction fee and the per-byte fee'
|
||||
})}</li>
|
||||
{this.renderTransfer()}
|
||||
{this.renderProposal()}
|
||||
{
|
||||
isRemovable && hasAvailable
|
||||
? <li>{t('fees.remove', {
|
||||
defaultValue: 'Submitting this transaction will drop the account balance to below the existential amount, removing the account from the chain state and burning associated funds'
|
||||
})}</li>
|
||||
: undefined
|
||||
}{
|
||||
hasAvailable
|
||||
? undefined
|
||||
: <li>{t('fees.available', {
|
||||
defaultValue: 'The account does not have the required funds available for this transaction with the current values'
|
||||
})}</li>
|
||||
}{
|
||||
isReserved
|
||||
? <li>{t('fees.reserved', {
|
||||
defaultValue: 'This account does have a reserved/locked balance, not taken into account'
|
||||
})}</li>
|
||||
: undefined
|
||||
}
|
||||
<li>{t('total', {
|
||||
defaultValue: '{{total}} total transaction amount (fees + value)',
|
||||
replace: {
|
||||
total: balanceFormat(allTotal)
|
||||
}
|
||||
})}</li>
|
||||
</ul>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
private renderProposal () {
|
||||
const { extrinsic, derive_balances_fees } = this.props;
|
||||
const { extMethod, extSection } = this.state;
|
||||
|
||||
if (!derive_balances_fees || !extrinsic || extSection !== 'democracy' || extMethod !== 'propose') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [, deposit] = extrinsic.args;
|
||||
|
||||
return (
|
||||
<Proposal
|
||||
deposit={deposit}
|
||||
fees={derive_balances_fees}
|
||||
onChange={this.onExtraUpdate}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
private renderTransfer () {
|
||||
const { extrinsic, derive_balances_fees } = this.props;
|
||||
const { extMethod, extSection } = this.state;
|
||||
|
||||
if (!derive_balances_fees || !extrinsic || extSection !== 'balances' || extMethod !== 'transfer') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [recipientId, amount] = extrinsic.args;
|
||||
|
||||
return (
|
||||
<Transfer
|
||||
amount={amount}
|
||||
fees={derive_balances_fees}
|
||||
recipientId={recipientId}
|
||||
onChange={this.onExtraUpdate}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
private onExtraUpdate = (extra: ExtraFees) => {
|
||||
this.setState({ ...extra });
|
||||
}
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
FeeDisplay,
|
||||
translate,
|
||||
withCall('derive.balances.fees'),
|
||||
withCall('derive.balances.votingBalance', { paramProp: 'accountId' }),
|
||||
withCall('query.system.accountNonce', { paramProp: 'accountId' })
|
||||
);
|
||||
11
packages/ui-signer/src/Fees/types.ts
Normal file
11
packages/ui-signer/src/Fees/types.ts
Normal file
@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2019 @polkadot/ui-signer authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
export type ExtraFees = {
|
||||
extraAmount: BN,
|
||||
extraFees: BN,
|
||||
extraWarn: boolean
|
||||
};
|
||||
@ -18,7 +18,7 @@ import { withApi, withMulti } from '@polkadot/ui-api/index';
|
||||
import { assert } from '@polkadot/util';
|
||||
import { format } from '@polkadot/util/logger';
|
||||
|
||||
import ExtrinsicDisplay from './Extrinsic';
|
||||
import Transaction from './Transaction';
|
||||
import Unlock from './Unlock';
|
||||
import translate from './translate';
|
||||
|
||||
@ -148,9 +148,9 @@ class Signer extends React.PureComponent<Props, State> {
|
||||
}
|
||||
|
||||
return (
|
||||
<ExtrinsicDisplay value={currentItem}>
|
||||
<Transaction value={currentItem}>
|
||||
{this.renderUnlock()}
|
||||
</ExtrinsicDisplay>
|
||||
</Transaction>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ import { Trans } from 'react-i18next';
|
||||
import { Method } from '@polkadot/types';
|
||||
import { Call, IdentityIcon, Modal } from '@polkadot/ui-app/index';
|
||||
|
||||
import Fees from './Fees';
|
||||
import translate from './translate';
|
||||
|
||||
type Props = I18nProps & {
|
||||
@ -53,6 +54,7 @@ class Transaction extends React.PureComponent<Props> {
|
||||
<div className='expanded'>
|
||||
<p>{header}</p>
|
||||
<Call value={extrinsic} />
|
||||
{this.renderFees()}
|
||||
</div>
|
||||
{icon}
|
||||
</div>
|
||||
@ -60,6 +62,23 @@ class Transaction extends React.PureComponent<Props> {
|
||||
</Modal.Content>
|
||||
];
|
||||
}
|
||||
|
||||
private renderFees () {
|
||||
const { value: { accountId, extrinsic, isUnsigned } } = this.props;
|
||||
|
||||
if (isUnsigned) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='ui--Params-Content'>
|
||||
<Fees
|
||||
accountId={accountId}
|
||||
extrinsic={extrinsic}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default translate(Transaction);
|
||||
Loading…
Reference in New Issue
Block a user