aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/pending-tx/index.js
blob: fb409cb92a88a5fee2a7c79b92677e7f8e8c7e15 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const Component = require('react').Component
const connect = require('react-redux').connect
const h = require('react-hyperscript')
const PropTypes = require('prop-types')
const clone = require('clone')
const abi = require('human-standard-token-abi')
const abiDecoder = require('abi-decoder')
abiDecoder.addABI(abi)
const inherits = require('util').inherits
const actions = require('../../actions')
const { getSymbolAndDecimals } = require('../../token-util')
const ConfirmSendEther = require('./confirm-send-ether')
const ConfirmSendToken = require('./confirm-send-token')
const ConfirmDeployContract = require('./confirm-deploy-contract')
const Loading = require('../loading')

const TX_TYPES = {
  DEPLOY_CONTRACT: 'deploy_contract',
  SEND_ETHER: 'send_ether',
  SEND_TOKEN: 'send_token',
}

module.exports = connect(mapStateToProps, mapDispatchToProps)(PendingTx)

function mapStateToProps (state) {
  const {
    conversionRate,
    identities,
    tokens: existingTokens,
  } = state.metamask
  const accounts = state.metamask.accounts
  const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
  return {
    conversionRate,
    identities,
    selectedAddress,
    existingTokens,
  }
}

function mapDispatchToProps (dispatch) {
  return {
    backToAccountDetail: address => dispatch(actions.backToAccountDetail(address)),
    cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })),
  }
}

inherits(PendingTx, Component)
function PendingTx () {
  Component.call(this)
  this.state = {
    isFetching: true,
    transactionType: '',
    tokenAddress: '',
    tokenSymbol: '',
    tokenDecimals: '',
  }
}

PendingTx.prototype.componentDidMount = function () {
  this.setTokenData()
}

PendingTx.prototype.componentDidUpdate = function (prevProps, prevState) {
  if (prevState.isFetching) {
    this.setTokenData()
  }
}

PendingTx.prototype.setTokenData = async function () {
  const { existingTokens } = this.props
  const txMeta = this.gatherTxMeta()
  const txParams = txMeta.txParams || {}

  if (txMeta.loadingDefaults) {
    return
  }

  if (!txParams.to) {
    return this.setState({
      transactionType: TX_TYPES.DEPLOY_CONTRACT,
      isFetching: false,
    })
  }

  // inspect tx data for supported special confirmation screens
  let isTokenTransaction = false
  if (txParams.data) {
    const tokenData = abiDecoder.decodeMethod(txParams.data)
    const { name: tokenMethodName } = tokenData || {}
    isTokenTransaction = (tokenMethodName === 'transfer')
  }

  if (isTokenTransaction) {
    const { symbol, decimals } = await getSymbolAndDecimals(txParams.to, existingTokens)

    this.setState({
      transactionType: TX_TYPES.SEND_TOKEN,
      tokenAddress: txParams.to,
      tokenSymbol: symbol,
      tokenDecimals: decimals,
      isFetching: false,
    })
  } else {
    this.setState({
      transactionType: TX_TYPES.SEND_ETHER,
      isFetching: false,
    })
  }
}

PendingTx.prototype.gatherTxMeta = function () {
  const props = this.props
  const state = this.state
  const txData = clone(state.txData) || clone(props.txData)

  return txData
}

PendingTx.prototype.render = function () {
  const {
    isFetching,
    transactionType,
    tokenAddress,
    tokenSymbol,
    tokenDecimals,
  } = this.state

  const { sendTransaction } = this.props

  if (isFetching) {
    return h(Loading, {
      fullScreen: true,
      loadingMessage: this.context.t('generatingTransaction'),
    })
  }

  switch (transactionType) {
    case TX_TYPES.SEND_ETHER:
      return h(ConfirmSendEther, {
        txData: this.gatherTxMeta(),
        sendTransaction,
      })
    case TX_TYPES.SEND_TOKEN:
      return h(ConfirmSendToken, {
        txData: this.gatherTxMeta(),
        sendTransaction,
        token: {
          address: tokenAddress,
          symbol: tokenSymbol,
          decimals: tokenDecimals,
        },
      })
    case TX_TYPES.DEPLOY_CONTRACT:
      return h(ConfirmDeployContract, {
        txData: this.gatherTxMeta(),
        sendTransaction,
      })
    default:
      return h(Loading, {
        fullScreen: true,
      })
  }
}

PendingTx.contextTypes = {
  t: PropTypes.func,
}