A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/ui/app/helpers/higher-order-components/with-method-data/with-method-data.component.js

66 lines
1.7 KiB

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { getMethodDataAsync, getFourBytePrefix } from '../../utils/transactions.util'
export default function withMethodData (WrappedComponent) {
return class MethodDataWrappedComponent extends PureComponent {
static propTypes = {
transaction: PropTypes.object,
knownMethodData: PropTypes.object,
addKnownMethodData: PropTypes.func,
}
static defaultProps = {
transaction: {},
knownMethodData: {},
}
state = {
6 years ago
methodData: {},
done: false,
error: null,
}
componentDidMount () {
this.fetchMethodData()
}
async fetchMethodData () {
const { transaction, knownMethodData, addKnownMethodData } = this.props
const { txParams: { data = '' } = {} } = transaction
if (data) {
try {
let methodData
const fourBytePrefix = getFourBytePrefix(data)
if (fourBytePrefix in knownMethodData) {
methodData = knownMethodData[fourBytePrefix]
} else {
methodData = await getMethodDataAsync(data)
if (!Object.entries(methodData).length === 0) {
addKnownMethodData(fourBytePrefix, methodData)
}
}
6 years ago
this.setState({ methodData, done: true })
} catch (error) {
6 years ago
this.setState({ done: true, error })
}
6 years ago
} else {
this.setState({ done: true })
}
}
render () {
6 years ago
const { methodData, done, error } = this.state
return (
<WrappedComponent
{ ...this.props }
6 years ago
methodData={{ data: methodData, done, error }}
/>
)
}
}
}