aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/transaction-manager.js
blob: d0226a6008230a343bc395f509da8e50d9a9d46b (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const EventEmitter = require('events')
const extend = require('xtend')
const TxProviderUtil = require('./lib/provider-utils')

module.exports = class TransactionManager extends EventEmitter {
  constructor (opts) {
    super()
    this.txList = opts.TxListFromStore || []
    this._persistTxList = opts.setTxList
    this._unconfTxCbs = {}
    this.txLimit = opts.txLimit
    this.provider = opts.provider
  }

//   Returns the tx list
  getTxList () {
    return this.txList
  }

  // Saves the new/updated txList.
  // Function is intended only for internal use
  _saveTxList (txList) {
    this.txList = txList
    this._persistTxList(txList)
  }

  // Adds a tx to the txlist
  addTx (txData, onTxDoneCb) {
    var txList = this.getTxList()
    var txLimit = this.txLimit
    if (txList.length > txLimit - 1) {
      txList.shift()
    }
    txList.push(txData)
    this._saveTxList(txList)
    this.addOnTxDoneCb(txData.id, onTxDoneCb)
    this.emit('unapproved', txData)
    this.emit('update')
  }

  // gets tx by Id and returns it
  getTx (txId, cb) {
    var txList = this.getTxList()
    var tx = txList.find((tx) => tx.id === txId)
    return cb ? cb(tx) : tx
  }

  //
  updateTx (txData) {
    var txId = txData.id
    var txList = this.getTxList()

    var updatedTxList = txList.map((tx) => {
      if (tx.id === txId) {
        tx = txData
      }
      return tx
    })
    this._saveTxList(updatedTxList)
  }

  get unConftxCount () {
    return Object.keys(this.getUnapprovedTxList()).length
  }

  get pendingTxCount () {
    return this.getTxsByMetaData('status', 'signed').length
  }

  getUnapprovedTxList () {
    var txList = this.getTxList()
    return txList.filter((tx) => {
      return tx.status === 'unapproved'
    }).reduce((result, tx) => {
      result[tx.id] = tx
      return result
    }, {})
  }

  /*
  Takes an object of fields to search for eg:
  var thingsToLookFor = {
    to: '0x0..',
    from: '0x0..',
    status: 'signed',
  }
  and returns a list of tx with all
  options matching

  this is for things like filtering a the tx list
  for only tx's from 1 account
  or for filltering for all txs from one account
  and that have been 'confirmed'
  */
  getFilterdTxList (opts) {
    var filteredTxList
    Object.keys(opts).forEach((key) => {
      filteredTxList = this.getTxsByMetaData(key, opts[key], filteredTxList)
    })
    return filteredTxList
  }

  getTxsByMetaData (key, value, txList = this.getTxList()) {
    return txList.filter((tx) => {
      if (key in tx.txParams) {
        return tx.txParams[key] === value
      } else {
        return tx[key] === value
      }
    })
  }

// keeps around the txCbs for later
  addOnTxDoneCb (txId, cb) {
    this._unconfTxCbs[txId] = cb || noop
  }

  execOnTxDoneCb (txId, conf) {
    var approvalCb = this._unconfTxCbs[txId]

    approvalCb(null, conf)
    // clean up
    delete this._unconfTxCbs[txId]
  }

  //   should return the tx

  //   Should find the tx in the tx list and
  //   update it.
  //   should set the status in txData
  //     // - `'unapproved'` the user has not responded
  //     // - `'rejected'` the user has responded no!
  //     // - `'signed'` the tx is signed
  //     // - `'submitted'` the tx is sent to a server
  //     // - `'confirmed'` the tx has been included in a block.
  setTxStatus (txId, status) {
    var txData = this.getTx(txId)
    txData.status = status
    this.emit(status, txId)
    this.updateTx(txData, status)
  }


  //   should return the status of the tx.
  getTxStatus (txId, cb) {
    const txData = this.getTx(txId)
    return cb ? cb(txData.staus) : txData.status
  }


  //   should update the status of the tx to 'signed'.
  setTxStatusSigned (txId) {
    this.setTxStatus(txId, 'signed')
    this.emit('update')
  }

  //     should update the status of the tx to 'rejected'.
  setTxStatusRejected (txId) {
    this.setTxStatus(txId, 'rejected')
    this.emit('update')
  }

  setTxStatusConfirmed (txId) {
    this.setTxStatus(txId, 'confirmed')
  }

  // merges txParams obj onto txData.txParams
  // use extend to ensure that all fields are filled
  updateTxParams (txId, txParams) {
    var txData = this.getTx(txId)
    txData.txParams = extend(txData, txParams)
    this.updateTx(txData)
  }

  //  sets provider for provider utils and event listener
  setProvider (provider) {
    this.provider = provider
    this.txProviderUtils = new TxProviderUtil(provider)
    this.provider.on('block', this.checkForTxInBlock.bind(this))
  }

  //  checks if a signed tx is in a block and
  // if included sets the tx status as 'confirmed'
  checkForTxInBlock () {
    var signedTxList = this.getFilterdTxList({status: 'signed'})
    if (!signedTxList.length) return
    var self = this
    signedTxList.forEach((tx) => {
      var txHash = tx.hash
      var txId = tx.id
      if (!txHash) return
      // var d
      this.txProviderUtils.query.getTransactionByHash(txHash, (err, txData) => {
        if (err) {
          tx

          return console.error(err)
        }
        if (txData.blockNumber !== null) {
          self.setTxStatusConfirmed(txId)
        }
      })
    })
  }
}

function noop () {}