1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-25 12:52:33 +02:00
metamask-extension/ui/app/helpers/higher-order-components/with-method-data/with-method-data.component.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

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 = {
2018-08-07 10:57:46 +02:00
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)
}
}
2018-08-07 10:57:46 +02:00
this.setState({ methodData, done: true })
} catch (error) {
2018-08-07 10:57:46 +02:00
this.setState({ done: true, error })
}
2018-08-07 10:57:46 +02:00
} else {
this.setState({ done: true })
}
}
render () {
2018-08-07 10:57:46 +02:00
const { methodData, done, error } = this.state
return (
<WrappedComponent
{ ...this.props }
2018-08-07 10:57:46 +02:00
methodData={{ data: methodData, done, error }}
/>
)
}
}
}