aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/helpers/higher-order-components/with-method-data/with-method-data.component.js
blob: efa9ad0a23452e4997bf34e0fa6b85f67873ad8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { getMethodData, 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 = {
      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 }}
        />
      )
    }
  }
}