Add checks for contract fees (#1299)

* new checks

* contracts checks

* contract checks

* rv yarn.lock

* fixes
This commit is contained in:
kwingram25 2019-06-17 12:51:09 +02:00 committed by GitHub
parent ab79e55df1
commit 357936eafc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 165 additions and 10 deletions

View File

@ -164,8 +164,7 @@ class Deploy extends ContractModal<Props, State> {
);
}
private getAbiState = (abi: string | null | undefined, contractAbi: Abi | null = null): State => {
private getContractAbiState = (abi: string | null | undefined, contractAbi: Abi | null = null): State => {
if (contractAbi) {
const args = contractAbi.deploy.args.map(({ name, type }) => name + ': ' + type);
const text = `deploy(${args.join(', ')})`;
@ -211,7 +210,7 @@ class Deploy extends ContractModal<Props, State> {
name: `${json.name} (instance)`,
isHashValid: true,
isNameValid: true,
...this.getAbiState(json.abi, contractAbi)
...this.getContractAbiState(json.abi, contractAbi)
} as State;
}
}
@ -231,7 +230,7 @@ class Deploy extends ContractModal<Props, State> {
protected onAddAbi = (abi: string | null | undefined, contractAbi?: Abi | null): void => {
this.setState({
...this.getAbiState(abi, contractAbi)
...this.getContractAbiState(abi, contractAbi)
});
}

View File

@ -0,0 +1,48 @@
// 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 { DerivedContractFees } from '@polkadot/api-derive/types';
import { ExtraFees } from './types';
import BN from 'bn.js';
import React from 'react';
import { Compact } from '@polkadot/types';
type Props = {
endowment: BN | Compact,
fees: DerivedContractFees,
onChange: (fees: ExtraFees) => void
};
type State = ExtraFees & {};
export default class ContractCall extends React.PureComponent<Props, State> {
state: State = {
extraFees: new BN(0),
extraAmount: new BN(0),
extraWarn: false
};
static getDerivedStateFromProps ({ endowment, fees, onChange }: Props, state: State) {
let extraFees = new BN(fees.callBaseFee);
const extraAmount = endowment instanceof Compact ? endowment.toBn() : new BN(endowment || 0);
const update = {
extraAmount,
extraFees,
extraWarn: false
};
if (!update.extraAmount.eq(state.extraAmount) || !update.extraFees.eq(state.extraFees)) {
onChange(update);
}
return update;
}
render () {
return null;
}
}

View File

@ -0,0 +1,49 @@
// 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 { DerivedContractFees } from '@polkadot/api-derive/types';
import { ExtraFees } from './types';
import BN from 'bn.js';
import React from 'react';
import { Compact } from '@polkadot/types';
type Props = {
endowment: BN | Compact,
fees: DerivedContractFees,
onChange: (fees: ExtraFees) => void
};
type State = ExtraFees & {};
export default class ContractDeploy extends React.PureComponent<Props, State> {
state: State = {
extraFees: new BN(0),
extraAmount: new BN(0),
extraWarn: false
};
static getDerivedStateFromProps ({ endowment, fees, onChange }: Props, state: State) {
let extraFees = new BN(fees.createBaseFee)
.add(fees.contractFee);
const extraAmount = endowment instanceof Compact ? endowment.toBn() : new BN(endowment || 0);
const update = {
extraAmount,
extraFees,
extraWarn: false
};
if (!update.extraAmount.eq(state.extraAmount) || !update.extraFees.eq(state.extraFees)) {
onChange(update);
}
return update;
}
render () {
return null;
}
}

View File

@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DerivedFees, DerivedBalances } from '@polkadot/api-derive/types';
import { DerivedFees, DerivedBalances, DerivedContractFees } from '@polkadot/api-derive/types';
import BN from 'bn.js';
@ -15,7 +15,7 @@ export const ZERO_BALANCE = {
votingBalance: new BN(0)
} as DerivedBalances;
export const ZERO_FEES = {
export const ZERO_FEES_BALANCES = {
transactionBaseFee: new BN(0),
transactionByteFee: new BN(0),
creationFee: new BN(0),
@ -23,5 +23,20 @@ export const ZERO_FEES = {
transferFee: new BN(0)
} as DerivedFees;
export const ZERO_FEES = ZERO_FEES_BALANCES;
export const ZERO_FEES_CONTRACT = {
callBaseFee: new BN(0),
contractFee: new BN(0),
createBaseFee: new BN(0),
creationFee: new BN(0),
rentByteFee: new BN(0),
rentDepositOffset: new BN(0),
transactionBaseFee: new BN(0),
transactionByteFee: new BN(0),
transferFee: new BN(0),
tombstoneDeposit: new BN(0)
} as DerivedContractFees;
export const MAX_SIZE_MB = 10;
export const MAX_SIZE_BYTES = MAX_SIZE_MB * 1024 * 1024;

View File

@ -3,21 +3,23 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { I18nProps } from '@polkadot/ui-app/types';
import { DerivedFees, DerivedBalances } from '@polkadot/api-derive/types';
import { DerivedFees, DerivedBalances, DerivedContractFees } from '@polkadot/api-derive/types';
import { IExtrinsic } from '@polkadot/types/types';
import { ExtraFees } from './types';
import BN from 'bn.js';
import React from 'react';
import { Method } from '@polkadot/types';
import { Compact, Method } from '@polkadot/types';
import { withCalls } from '@polkadot/ui-api';
import { Icon } from '@polkadot/ui-app';
import { compactToU8a, formatBalance } from '@polkadot/util';
import translate from '../translate';
import ContractCall from './ContractCall';
import ContractDeploy from './ContractDeploy';
import Proposal from './Proposal';
import Transfer from './Transfer';
import { MAX_SIZE_BYTES, MAX_SIZE_MB, ZERO_BALANCE, ZERO_FEES } from './constants';
import { MAX_SIZE_BYTES, MAX_SIZE_MB, ZERO_BALANCE, ZERO_FEES_BALANCES, ZERO_FEES_CONTRACT } from './constants';
type State = ExtraFees & {
allFees: BN,
@ -34,6 +36,7 @@ type State = ExtraFees & {
type Props = I18nProps & {
balances_fees?: DerivedFees,
balances_all?: DerivedBalances,
contract_fees?: DerivedContractFees,
accountId?: string | null,
extrinsic?: IExtrinsic | null,
isSendable: boolean,
@ -66,7 +69,7 @@ export class FeeDisplay extends React.PureComponent<Props, State> {
overLimit: false
};
static getDerivedStateFromProps ({ accountId, balances_all = ZERO_BALANCE, extrinsic, balances_fees = ZERO_FEES, system_accountNonce = new BN(0) }: Props, prevState: State): State | null {
static getDerivedStateFromProps ({ accountId, balances_all = ZERO_BALANCE, extrinsic, balances_fees = ZERO_FEES_BALANCES, system_accountNonce = new BN(0) }: Props, prevState: State): State | null {
if (!accountId || !extrinsic) {
return null;
}
@ -173,6 +176,8 @@ export class FeeDisplay extends React.PureComponent<Props, State> {
}
{this.renderTransfer()}
{this.renderProposal()}
{this.renderCall()}
{this.renderDeploy()}
{
isReserved
? <div><Icon name='arrow right' />{t('This account does have a reserved/locked balance, not taken into account')}</div>
@ -232,6 +237,44 @@ export class FeeDisplay extends React.PureComponent<Props, State> {
);
}
private renderCall () {
const { extrinsic, contract_fees = ZERO_FEES_CONTRACT } = this.props;
const { extMethod, extSection } = this.state;
if (!contract_fees || !extrinsic || extSection !== 'contract' || extMethod !== 'call') {
return null;
}
const [, endowment] = extrinsic.args;
return (
<ContractCall
endowment={endowment as any as Compact}
fees={contract_fees}
onChange={this.onExtraUpdate}
/>
);
}
private renderDeploy () {
const { extrinsic, contract_fees = ZERO_FEES_CONTRACT } = this.props;
const { extMethod, extSection } = this.state;
if (!contract_fees || !extrinsic || extSection !== 'contract' || extMethod !== 'create') {
return null;
}
const [endowment] = extrinsic.args;
return (
<ContractDeploy
endowment={endowment as any as Compact}
fees={contract_fees}
onChange={this.onExtraUpdate}
/>
);
}
private onExtraUpdate = (extra: ExtraFees) => {
this.setState({ ...extra });
}
@ -241,6 +284,7 @@ export default translate(
withCalls<Props>(
'derive.balances.fees',
['derive.balances.all', { paramName: 'accountId' }],
'derive.contract.fees',
['query.system.accountNonce', { paramName: 'accountId' }]
)(FeeDisplay)
);