From 957e21004384773da5a71e1a6dce694a5afda14b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 16 Jun 2019 07:10:59 +0200 Subject: [PATCH] Default proposal/sudo for new 2.x substrate (#1296) --- packages/app-democracy/src/Propose.tsx | 12 ++---------- packages/app-extrinsics/src/Selection.tsx | 13 +++---------- packages/app-sudo/src/Sudo.tsx | 12 ++---------- packages/ui-api/src/Api.tsx | 16 ++++++++++++---- packages/ui-api/src/types.ts | 1 + packages/ui-app/src/Params/Proposal.tsx | 11 ++--------- 6 files changed, 22 insertions(+), 43 deletions(-) diff --git a/packages/app-democracy/src/Propose.tsx b/packages/app-democracy/src/Propose.tsx index 8bbfb00df0..d69b48f906 100644 --- a/packages/app-democracy/src/Propose.tsx +++ b/packages/app-democracy/src/Propose.tsx @@ -35,18 +35,10 @@ class Propose extends TxComponent { }; render () { - const { api, apiDefaultTx, t } = this.props; + const { apiDefaultTxSudo, t } = this.props; const { isValid, accountId, method, value } = this.state; const hasValue = !!value && value.gtn(0); - const defaultExtrinsic = (() => { - try { - return api.tx.consensus.setCode; - } catch (error) { - return apiDefaultTx; - } - })(); - return (
{ onChange={this.onChangeAccount} /> { } as State; render () { - const { apiDefaultTx, api, t } = this.props; + const { apiDefaultTxSudo, t } = this.props; const { isValid, isValidUnsigned, accountId } = this.state; - const defaultExtrinsic = (() => { - try { - return api.tx.balances.transfer; - } catch (error) { - return apiDefaultTx; - } - })(); - const extrinsic = this.getExtrinsic() || defaultExtrinsic; + const extrinsic = this.getExtrinsic() || apiDefaultTxSudo; return (
@@ -73,7 +66,7 @@ class Selection extends TxComponent {


{ }; render () { - const { api, apiDefaultTx, isMine, sudoKey, t } = this.props; + const { apiDefaultTxSudo, isMine, sudoKey, t } = this.props; const { method, isValid } = this.state; - const defaultExtrinsic = (() => { - try { - return api.tx.consensus.setCode; - } catch (error) { - return apiDefaultTx; - } - })(); - return isMine ? (
{ api.rpc.system.properties(), api.rpc.system.chain() ]); - const section = Object.keys(api.tx)[0]; - const method = Object.keys(api.tx[section])[0]; const chain = value ? value.toString() : null; @@ -133,22 +131,32 @@ export default class Api extends React.PureComponent { type: 'ed25519' }, injectedAccounts); + const section = Object.keys(api.tx)[0]; + const method = Object.keys(api.tx[section])[0]; + const apiDefaultTx = api.tx[section][method]; + const apiDefaultTxSudo = + (api.tx.system && api.tx.system.setCode) || // 2.x + (api.tx.consensus && api.tx.consensus.setCode) || // 1.x + apiDefaultTx; // other + this.setState({ isApiReady: true, - apiDefaultTx: api.tx[section][method], + apiDefaultTx, + apiDefaultTxSudo, chain, isDevelopment }); } render () { - const { api, apiDefaultTx, chain, isApiConnected, isApiReady, isDevelopment, isWaitingInjected, setApiUrl } = this.state; + const { api, apiDefaultTx, apiDefaultTxSudo, chain, isApiConnected, isApiReady, isDevelopment, isWaitingInjected, setApiUrl } = this.state; return ( ', isApiConnected, isApiReady: isApiReady && !!chain, diff --git a/packages/ui-api/src/types.ts b/packages/ui-api/src/types.ts index 0ec1d88ae7..5940762fa2 100644 --- a/packages/ui-api/src/types.ts +++ b/packages/ui-api/src/types.ts @@ -18,6 +18,7 @@ export type BareProps = { export type ApiProps = { api: ApiPromise, apiDefaultTx: SubmittableExtrinsicFunction, + apiDefaultTxSudo: SubmittableExtrinsicFunction, currentChain: string, isApiConnected: boolean, isApiReady: boolean, diff --git a/packages/ui-app/src/Params/Proposal.tsx b/packages/ui-app/src/Params/Proposal.tsx index 19655be386..e73abb1feb 100644 --- a/packages/ui-app/src/Params/Proposal.tsx +++ b/packages/ui-app/src/Params/Proposal.tsx @@ -15,19 +15,12 @@ type Props = ApiProps & BaseProps; class ProposalDisplay extends React.PureComponent { render () { - const { apiDefaultTx, api, className, isDisabled, isError, label, onEnter, style, withLabel } = this.props; - const defaultValue = (() => { - try { - return api.tx.consensus.setCode; - } catch (error) { - return apiDefaultTx; - } - })(); + const { apiDefaultTxSudo, className, isDisabled, isError, label, onEnter, style, withLabel } = this.props; return (