diff --git a/packages/app-transfer/src/Fees.tsx b/packages/app-transfer/src/Fees.tsx deleted file mode 100644 index 02074a4251..0000000000 --- a/packages/app-transfer/src/Fees.tsx +++ /dev/null @@ -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 { - 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 [ - , -
- { - 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 - } -
, - - ]; - } -} - -export default withMulti( - FeeDisplay, - translate, - withCall('derive.balances.votingBalance', { paramProp: 'accountId', propName: 'balanceFrom' }), - withCall('derive.balances.votingBalance', { paramProp: 'recipientId', propName: 'balanceTo' }) -); diff --git a/packages/app-transfer/src/Transfer.tsx b/packages/app-transfer/src/Transfer.tsx index a641a1732e..58fbb77d91 100644 --- a/packages/app-transfer/src/Transfer.tsx +++ b/packages/app-transfer/src/Transfer.tsx @@ -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 { 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 (
@@ -97,9 +90,7 @@ class Transfer extends React.PureComponent { @@ -143,7 +134,7 @@ class Transfer extends React.PureComponent { private nextState (newState: Partial): 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 { accountId, amount, extrinsic, - recipientId, - txfees + hasAvailable, + recipientId }; }); } @@ -170,13 +161,13 @@ class Transfer extends React.PureComponent { 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 ); diff --git a/packages/app-transfer/src/index.tsx b/packages/app-transfer/src/index.tsx index 247d56e3fa..f2c188070b 100644 --- a/packages/app-transfer/src/index.tsx +++ b/packages/app-transfer/src/index.tsx @@ -19,10 +19,7 @@ type Props = I18nProps & { allAccounts?: SubjectInfo }; -type State = { -}; - -class App extends React.PureComponent { +class App extends React.PureComponent { render () { return (
diff --git a/packages/app-transfer/src/types.ts b/packages/app-transfer/src/types.ts deleted file mode 100644 index c5fd55437d..0000000000 --- a/packages/app-transfer/src/types.ts +++ /dev/null @@ -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 -}; diff --git a/packages/ui-api/src/derive/balances/accountIdAndIndex.tsx b/packages/ui-api/src/derive/balances/accountIdAndIndex.ts similarity index 90% rename from packages/ui-api/src/derive/balances/accountIdAndIndex.tsx rename to packages/ui-api/src/derive/balances/accountIdAndIndex.ts index 97f2264fc4..e02652e746 100644 --- a/packages/ui-api/src/derive/balances/accountIdAndIndex.tsx +++ b/packages/ui-api/src/derive/balances/accountIdAndIndex.ts @@ -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]) diff --git a/packages/ui-api/src/derive/balances/accountIdToIndex.ts b/packages/ui-api/src/derive/balances/accountIdToIndex.ts index 77f438e71d..28713733db 100644 --- a/packages/ui-api/src/derive/balances/accountIdToIndex.ts +++ b/packages/ui-api/src/derive/balances/accountIdToIndex.ts @@ -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()]) ); } diff --git a/packages/ui-api/src/derive/balances/validatingBalance.tsx b/packages/ui-api/src/derive/balances/validatingBalance.ts similarity index 100% rename from packages/ui-api/src/derive/balances/validatingBalance.tsx rename to packages/ui-api/src/derive/balances/validatingBalance.ts diff --git a/packages/ui-api/src/derive/balances/validatingBalances.tsx b/packages/ui-api/src/derive/balances/validatingBalances.ts similarity index 100% rename from packages/ui-api/src/derive/balances/validatingBalances.tsx rename to packages/ui-api/src/derive/balances/validatingBalances.ts diff --git a/packages/ui-api/src/derive/balances/votingBalance.tsx b/packages/ui-api/src/derive/balances/votingBalance.ts similarity index 100% rename from packages/ui-api/src/derive/balances/votingBalance.tsx rename to packages/ui-api/src/derive/balances/votingBalance.ts diff --git a/packages/ui-api/src/derive/balances/votingBalances.tsx b/packages/ui-api/src/derive/balances/votingBalances.ts similarity index 100% rename from packages/ui-api/src/derive/balances/votingBalances.tsx rename to packages/ui-api/src/derive/balances/votingBalances.ts diff --git a/packages/ui-api/src/derive/democracy/referendumInfos.tsx b/packages/ui-api/src/derive/democracy/referendumInfos.ts similarity index 100% rename from packages/ui-api/src/derive/democracy/referendumInfos.tsx rename to packages/ui-api/src/derive/democracy/referendumInfos.ts diff --git a/packages/ui-api/src/util/isEqual.ts b/packages/ui-api/src/util/isEqual.ts index 7dd3a0b32b..a56d4b6ce8 100644 --- a/packages/ui-api/src/util/isEqual.ts +++ b/packages/ui-api/src/util/isEqual.ts @@ -20,6 +20,13 @@ function flatten (key: string | null, value: any): any { return value; } -export default function isEqual (a?: T, b?: T): boolean { - return JSON.stringify({ test: a }, flatten) === JSON.stringify({ test: b }, flatten); +export default function isEqual (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; } diff --git a/packages/ui-api/src/with/call.tsx b/packages/ui-api/src/with/call.tsx index f398cd41e9..fffb008bec 100644 --- a/packages/ui-api/src/with/call.tsx +++ b/packages/ui-api/src/with/call.tsx @@ -55,8 +55,9 @@ export default function withCall (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) diff --git a/packages/ui-app/src/AddressSummary.tsx b/packages/ui-app/src/AddressSummary.tsx index 0fce39eb84..56d0215d33 100644 --- a/packages/ui-app/src/AddressSummary.tsx +++ b/packages/ui-app/src/AddressSummary.tsx @@ -77,7 +77,6 @@ class AddressSummary extends React.PureComponent { : value }
- {this.renderCopy(address)} ); } @@ -106,7 +105,6 @@ class AddressSummary extends React.PureComponent { : address } - {this.renderCopy(address)} ); } @@ -150,20 +148,6 @@ class AddressSummary extends React.PureComponent { ); } - protected renderCopy (address: string) { - return null; - - // const { withCopy = true } = this.props; - - // if (!withCopy || !address) { - // return null; - // } - - // return ( - // - // ); - } - protected renderIcon (className: string = 'ui--AddressSummary-icon', size?: number) { const { derive_balances_accountIdAndIndex = [], identIconSize = 96, query_session_validators, value, withIcon = true } = this.props; diff --git a/packages/ui-app/src/styles/app.css b/packages/ui-app/src/styles/app.css index 2125c3af3c..c027120d5a 100644 --- a/packages/ui-app/src/styles/app.css +++ b/packages/ui-app/src/styles/app.css @@ -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, diff --git a/packages/ui-signer/src/Fees/Proposal.tsx b/packages/ui-signer/src/Fees/Proposal.tsx new file mode 100644 index 0000000000..f209be65b4 --- /dev/null +++ b/packages/ui-signer/src/Fees/Proposal.tsx @@ -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 { + 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 + ?
  • {t('proposal.belowmin', { + defaultValue: 'The deposit is below the {{minimum}} minimum required for the proposal to be evaluated', + replace: { + minimum: balanceFormat(query_democracy_minimumDeposit) + } + })}
  • + : undefined, + extraAmount.isZero() + ? undefined + :
  • {t('proposal.deposit', { + defaultValue: 'The deposit of {{deposit}} will be reserved until the proposal is completed', + replace: { + deposit: balanceFormat(extraAmount) + } + })}
  • + ]; + } +} + +export default withMulti( + Proposal, + translate, + withCall('query.democracy.minimumDeposit') +); diff --git a/packages/ui-signer/src/Fees/Transfer.tsx b/packages/ui-signer/src/Fees/Transfer.tsx new file mode 100644 index 0000000000..44c2a19674 --- /dev/null +++ b/packages/ui-signer/src/Fees/Transfer.tsx @@ -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 { + 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 + ?
  • {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' + })}
  • + : undefined, + isCreation + ?
  • {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)}` + } + })}
  • + : undefined + ]; + } +} + +export default withMulti( + Transfer, + translate, + withCall('derive.balances.votingBalance', { paramProp: 'recipientId' }) +); diff --git a/packages/ui-signer/src/Fees/constants.ts b/packages/ui-signer/src/Fees/constants.ts new file mode 100644 index 0000000000..d58b1309ac --- /dev/null +++ b/packages/ui-signer/src/Fees/constants.ts @@ -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; diff --git a/packages/ui-signer/src/Fees/index.tsx b/packages/ui-signer/src/Fees/index.tsx new file mode 100644 index 0000000000..0cd0d405ca --- /dev/null +++ b/packages/ui-signer/src/Fees/index.tsx @@ -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 { + 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 ( +
    +
      +
    • {t('fees', { + defaultValue: 'Fees totalling {{fees}} will be applied to the submission', + replace: { + fees: balanceFormat(allFees) + } + })}
    • +
    • {t('fees.explain', { + defaultValue: 'Fees includes the transaction fee and the per-byte fee' + })}
    • + {this.renderTransfer()} + {this.renderProposal()} + { + 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 + }{ + 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, not taken into account' + })}
    • + : undefined + } +
    • {t('total', { + defaultValue: '{{total}} total transaction amount (fees + value)', + replace: { + total: balanceFormat(allTotal) + } + })}
    • +
    +
    + ); + } + + 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 ( + + ); + } + + 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 ( + + ); + } + + 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' }) +); diff --git a/packages/ui-signer/src/Fees/types.ts b/packages/ui-signer/src/Fees/types.ts new file mode 100644 index 0000000000..fe858bdb05 --- /dev/null +++ b/packages/ui-signer/src/Fees/types.ts @@ -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 +}; diff --git a/packages/ui-signer/src/Modal.tsx b/packages/ui-signer/src/Modal.tsx index d5191c4b89..9b9820ebdc 100644 --- a/packages/ui-signer/src/Modal.tsx +++ b/packages/ui-signer/src/Modal.tsx @@ -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 { } return ( - + {this.renderUnlock()} - + ); } diff --git a/packages/ui-signer/src/Extrinsic.tsx b/packages/ui-signer/src/Transaction.tsx similarity index 84% rename from packages/ui-signer/src/Extrinsic.tsx rename to packages/ui-signer/src/Transaction.tsx index fc13ade6ca..9f689549ca 100644 --- a/packages/ui-signer/src/Extrinsic.tsx +++ b/packages/ui-signer/src/Transaction.tsx @@ -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 {

    {header}

    + {this.renderFees()}
    {icon} @@ -60,6 +62,23 @@ class Transaction extends React.PureComponent { ]; } + + private renderFees () { + const { value: { accountId, extrinsic, isUnsigned } } = this.props; + + if (isUnsigned) { + return null; + } + + return ( +
    + +
    + ); + } } export default translate(Transaction);