1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 03:20:23 +01:00
metamask-extension/ui/app/higher-order-components/with-method-data/with-method-data.component.js
Esteban Miño e21dfd1862 Use Parity on-chain registry only when is needed (#6052)
* add and use knownMethodData to avoid infura requests

* dataMethod to methodData and check empty response
2019-01-22 10:22:56 -08:00

66 lines
1.7 KiB
JavaScript

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { getMethodData, getFourBytePrefix } from '../../helpers/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 = {
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 getMethodData(data)
if (!Object.entries(methodData).length === 0) {
addKnownMethodData(fourBytePrefix, methodData)
}
}
this.setState({ methodData, done: true })
} catch (error) {
this.setState({ done: true, error })
}
} else {
this.setState({ done: true })
}
}
render () {
const { methodData, done, error } = this.state
return (
<WrappedComponent
{ ...this.props }
methodData={{ data: methodData, done, error }}
/>
)
}
}
}