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.
66 lines
1.7 KiB
66 lines
1.7 KiB
6 years ago
|
import React, { PureComponent } from 'react'
|
||
|
import PropTypes from 'prop-types'
|
||
6 years ago
|
import { getMethodData, getFourBytePrefix } from '../../utils/transactions.util'
|
||
6 years ago
|
|
||
|
export default function withMethodData (WrappedComponent) {
|
||
|
return class MethodDataWrappedComponent extends PureComponent {
|
||
|
static propTypes = {
|
||
|
transaction: PropTypes.object,
|
||
6 years ago
|
knownMethodData: PropTypes.object,
|
||
|
addKnownMethodData: PropTypes.func,
|
||
6 years ago
|
}
|
||
|
|
||
|
static defaultProps = {
|
||
|
transaction: {},
|
||
6 years ago
|
knownMethodData: {},
|
||
6 years ago
|
}
|
||
|
|
||
|
state = {
|
||
6 years ago
|
methodData: {},
|
||
|
done: false,
|
||
6 years ago
|
error: null,
|
||
6 years ago
|
}
|
||
|
|
||
|
componentDidMount () {
|
||
|
this.fetchMethodData()
|
||
|
}
|
||
|
|
||
|
async fetchMethodData () {
|
||
6 years ago
|
const { transaction, knownMethodData, addKnownMethodData } = this.props
|
||
6 years ago
|
const { txParams: { data = '' } = {} } = transaction
|
||
|
|
||
|
if (data) {
|
||
6 years ago
|
try {
|
||
6 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
this.setState({ methodData, done: true })
|
||
6 years ago
|
} catch (error) {
|
||
6 years ago
|
this.setState({ done: true, error })
|
||
6 years ago
|
}
|
||
6 years ago
|
} else {
|
||
|
this.setState({ done: true })
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
render () {
|
||
6 years ago
|
const { methodData, done, error } = this.state
|
||
6 years ago
|
|
||
|
return (
|
||
|
<WrappedComponent
|
||
|
{ ...this.props }
|
||
6 years ago
|
methodData={{ data: methodData, done, error }}
|
||
6 years ago
|
/>
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|