mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-24 01:49:06 +00:00
8fe664c171
* Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
193 lines
5.1 KiB
TypeScript
193 lines
5.1 KiB
TypeScript
import React, { Component } from 'react';
|
|
import InteractForm from './components/InteractForm';
|
|
import InteractExplorer from './components//InteractExplorer';
|
|
import Contract from 'libs/contracts';
|
|
import { withTx, IWithTx } from '../withTx';
|
|
import {
|
|
TxModal,
|
|
Props as DMProps,
|
|
TTxModal
|
|
} from 'containers/Tabs/Contracts/components/TxModal';
|
|
import { IUserSendParams } from 'libs/contracts/ABIFunction';
|
|
import BN from 'bn.js';
|
|
import {
|
|
TxCompare,
|
|
TTxCompare
|
|
} from 'containers/Tabs/Contracts/components/TxCompare';
|
|
|
|
interface State {
|
|
currentContract: Contract | null;
|
|
showExplorer: boolean;
|
|
address: string | null;
|
|
signedTx: string | null;
|
|
rawTx: any | null;
|
|
gasLimit: string;
|
|
value: string;
|
|
displayModal: boolean;
|
|
}
|
|
|
|
class Interact extends Component<IWithTx, State> {
|
|
public initialState: State = {
|
|
currentContract: null,
|
|
showExplorer: false,
|
|
address: null,
|
|
signedTx: null,
|
|
rawTx: null,
|
|
gasLimit: '30000',
|
|
value: '0',
|
|
displayModal: false
|
|
};
|
|
public state: State = this.initialState;
|
|
|
|
public componentWillReceiveProps(nextProps: IWithTx) {
|
|
if (nextProps.wallet && this.state.currentContract) {
|
|
Contract.setConfigForTx(this.state.currentContract, nextProps);
|
|
}
|
|
}
|
|
|
|
public accessContract = (contractAbi: string, address: string) => () => {
|
|
try {
|
|
const parsedAbi = JSON.parse(contractAbi);
|
|
const contractInstance = new Contract(parsedAbi);
|
|
contractInstance.at(address);
|
|
contractInstance.setNode(this.props.nodeLib);
|
|
this.setState({
|
|
currentContract: contractInstance,
|
|
showExplorer: true,
|
|
address
|
|
});
|
|
} catch (e) {
|
|
this.props.showNotification(
|
|
'danger',
|
|
`Contract Access Error: ${(e as Error).message ||
|
|
'Can not parse contract'}`
|
|
);
|
|
this.resetState();
|
|
}
|
|
};
|
|
|
|
public render() {
|
|
const {
|
|
showExplorer,
|
|
currentContract,
|
|
gasLimit,
|
|
value,
|
|
signedTx,
|
|
displayModal
|
|
} = this.state;
|
|
const { wallet, showNotification } = this.props;
|
|
const txGenerated = !!signedTx;
|
|
|
|
return (
|
|
<div className="Interact">
|
|
<InteractForm
|
|
accessContract={this.accessContract}
|
|
resetState={this.resetState}
|
|
/>
|
|
<hr />
|
|
{showExplorer &&
|
|
currentContract && (
|
|
<InteractExplorer
|
|
{...{
|
|
address: currentContract.address,
|
|
walletDecrypted: !!wallet,
|
|
handleInput: this.handleInput,
|
|
contractFunctions: Contract.getFunctions(currentContract),
|
|
gasLimit,
|
|
value,
|
|
handleFunctionSend: this.handleFunctionSend,
|
|
txGenerated,
|
|
txModal: txGenerated ? this.makeModal() : null,
|
|
txCompare: txGenerated ? this.makeCompareTx() : null,
|
|
toggleModal: this.toggleModal,
|
|
displayModal,
|
|
showNotification
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
private makeCompareTx = (): React.ReactElement<TTxCompare> => {
|
|
const { nonce } = this.state.rawTx;
|
|
const { signedTx } = this.state;
|
|
|
|
if (!nonce || !signedTx) {
|
|
throw Error('Can not display raw tx, nonce empty or no signed tx');
|
|
}
|
|
|
|
return <TxCompare signedTx={signedTx} />;
|
|
};
|
|
|
|
private makeModal = (): React.ReactElement<TTxModal> => {
|
|
const { networkName, node: { network, service } } = this.props;
|
|
const { signedTx } = this.state;
|
|
|
|
if (!signedTx) {
|
|
throw Error('Can not deploy contract, no signed tx');
|
|
}
|
|
|
|
const props: DMProps = {
|
|
action: 'send a contract state modifying transaction',
|
|
networkName,
|
|
network,
|
|
service,
|
|
handleBroadcastTx: this.handleBroadcastTx,
|
|
onClose: this.resetState
|
|
};
|
|
|
|
return <TxModal {...props} />;
|
|
};
|
|
|
|
private toggleModal = () => this.setState({ displayModal: true });
|
|
|
|
private resetState = () => this.setState(this.initialState);
|
|
|
|
private handleBroadcastTx = () => {
|
|
const { signedTx } = this.state;
|
|
if (!signedTx) {
|
|
return null;
|
|
}
|
|
|
|
this.props.broadcastTx(signedTx);
|
|
this.resetState();
|
|
};
|
|
|
|
private handleFunctionSend = (selectedFunction, inputs) => async () => {
|
|
try {
|
|
const { address, gasLimit, value } = this.state;
|
|
if (!address) {
|
|
return null;
|
|
}
|
|
|
|
const parsedInputs = Object.keys(inputs).reduce(
|
|
(accu, key) => ({ ...accu, [key]: inputs[key].parsedData }),
|
|
{}
|
|
);
|
|
|
|
const userInputs: IUserSendParams = {
|
|
input: parsedInputs,
|
|
to: address,
|
|
gasLimit: new BN(gasLimit),
|
|
value
|
|
};
|
|
|
|
const { signedTx, rawTx } = await selectedFunction.send(userInputs);
|
|
this.setState({ signedTx, rawTx });
|
|
} catch (e) {
|
|
this.props.showNotification(
|
|
'danger',
|
|
`Function send error: ${(e as Error).message}` ||
|
|
'Invalid input parameters',
|
|
5000
|
|
);
|
|
}
|
|
};
|
|
|
|
private handleInput = name => (ev: React.FormEvent<any>) =>
|
|
this.setState({ [name]: ev.currentTarget.value });
|
|
}
|
|
|
|
export default withTx(Interact);
|