aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/transaction-activity-log/transaction-activity-log.util.js
blob: fe780788a9235761c1b9ac79aeaeb185d20043bc (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
// path constants
const STATUS_PATH = '/status'
const GAS_PRICE_PATH = '/txParams/gasPrice'

// status constants
const STATUS_UNAPPROVED = 'unapproved'
const STATUS_SUBMITTED = 'submitted'
const STATUS_CONFIRMED = 'confirmed'
const STATUS_DROPPED = 'dropped'

// op constants
const REPLACE_OP = 'replace'

const eventPathsHash = {
  [STATUS_PATH]: true,
  [GAS_PRICE_PATH]: true,
}

const statusHash = {
  [STATUS_SUBMITTED]: true,
  [STATUS_CONFIRMED]: true,
  [STATUS_DROPPED]: true,
}

function eventCreator (eventKey, timestamp, value, valueDescriptionKey) {
  return {
    eventKey,
    timestamp,
    value,
    valueDescriptionKey,
  }
}

export function getActivities (transaction) {
  const { history = [] } = transaction

  return history.reduce((acc, base) => {
    // First history item should be transaction creation
    if (!Array.isArray(base) && base.status === STATUS_UNAPPROVED && base.txParams) {
      const { time, txParams: { value } = {} } = base
      return acc.concat(eventCreator('created', time, value, 'value'))
    } else if (Array.isArray(base)) {
      const events = []

      base.forEach(entry => {
        const { op, path, value, timestamp } = entry

        if (path in eventPathsHash && op === REPLACE_OP) {
          switch (path) {
            case STATUS_PATH: {
              if (value in statusHash) {
                events.push(eventCreator(value, timestamp))
              }

              break
            }

            case GAS_PRICE_PATH: {
              events.push(eventCreator('updated', timestamp, value, 'gasPrice'))
              break
            }

            default: {
              events.push(eventCreator(value, timestamp))
            }
          }
        }
      })

      return acc.concat(events)
    }

    return acc
  }, [])
}