ghost-portal/packages/ui-app/src/TxButton.tsx
Jaco Greeff 1a103e9812
Substrate contracts, basics (#928)
* Substrate contracts, basics

* Expand Create & Deploy with placeholder inputs

* Add ABI parsing & validation (re-usable)

* Expand ABI parsing

* WIP, putCode works, deploy in-progress

* Updates for Params & Instantiate (untested)

* Up to call...

* Linting

* Updated

* InputFile & ABI adjustments

* Add ability to attch existing (code & deployed)

* Small cleanups

* Very basic intro

* Hide contracts sidebar entry

* Update README.md
2019-04-10 11:52:34 +02:00

91 lines
2.3 KiB
TypeScript

// Copyright 2017-2019 @polkadot/app-123code 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 { ApiProps } from '@polkadot/ui-api/types';
import { QueueTx$ExtrinsicAdd, TxCallback } from './Status/types';
import React from 'react';
import { withApi } from '@polkadot/ui-api';
import { assert, isFunction, isUndefined } from '@polkadot/util';
import { QueueConsumer } from './Status/Context';
import Button from './Button';
type ConstructFn = () => Array<any>;
type InjectedProps = {
queueExtrinsic: QueueTx$ExtrinsicAdd;
};
type Props = ApiProps & {
accountId?: string,
isPrimary?: boolean,
isDisabled?: boolean,
isNegative?: boolean,
label: React.ReactNode,
onClick?: () => any,
onFailed?: TxCallback,
onSuccess?: TxCallback,
onUpdate?: TxCallback,
params?: Array<any> | ConstructFn,
tx: string
};
class TxButtonInner extends React.PureComponent<Props & InjectedProps> {
render () {
const { accountId, isDisabled, isNegative, isPrimary, label } = this.props;
return (
<Button
isDisabled={isDisabled || !accountId}
isNegative={isNegative}
isPrimary={isUndefined(isPrimary) ? !isNegative : isPrimary}
label={label}
onClick={this.send}
/>
);
}
private send = (): void => {
const { accountId, api, onClick, onFailed, onSuccess, onUpdate, params = [], queueExtrinsic, tx } = this.props;
assert(tx, 'Expected tx param passed to TxButton');
const [section, method] = tx.split('.');
assert(api.tx[section] && api.tx[section][method], `Unable to find api.tx.${section}.${method}`);
const extrinsic: any = api.tx[section][method](
...(isFunction(params) ? params() : params)
);
queueExtrinsic({
accountId,
extrinsic,
txFailedCb: onFailed,
txSuccessCb: onSuccess,
txUpdateCb: onUpdate
});
onClick && onClick();
}
}
class TxButton extends React.PureComponent<Props> {
render () {
return (
<QueueConsumer>
{({ queueExtrinsic }) => (
<TxButtonInner
{...this.props}
queueExtrinsic={queueExtrinsic}
/>
)}
</QueueConsumer>
);
}
}
export default withApi(TxButton);