{"size": 1745, "ext": "js", "max_stars_repo_path": "src/js/services/tx.js", "max_stars_repo_name": "edwardEvans094/KyberWallet", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/js/services/tx.js", "max_issues_repo_name": "edwardEvans094/KyberWallet", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/js/services/tx.js", "max_forks_repo_name": "edwardEvans094/KyberWallet", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.265625, "max_line_length": 65, "alphanum_fraction": 0.540974212}
import * as ethUtil from 'ethereumjs-util'
import constants from "../services/constants"

export default class Tx {
  constructor(
    hash, from, gas, gasPrice, nonce, status,
    type, data, address) {
    this.hash = hash
    this.from = from
    this.gas = gas
    this.gasPrice = gasPrice
    this.nonce = nonce
    this.status = status
    this.type = type
    this.data = data // data can be used to store wallet name
    this.address = address
    this.threw = false
    this.error = null
    this.errorInfo = null
  }

  shallowClone() {
    return new Tx(
    this.hash, this.from, this.gas, this.gasPrice, this.nonce,
    this.status, this.type, this.data, this.address, this.threw,
    this.error, this.errorInfo)
  }

  sync = (ethereum, callback) => {
    ethereum.txMined(this.hash, (mined, receipt) => {
      var newTx = this.shallowClone()
      if (mined) {
        newTx.address = receipt.contractAddress
        newTx.gas = receipt.gasUsed
        var logs = receipt.logs
        if (newTx.type == "exchange") {
          if (logs.length == 0) {
            newTx.threw = true
            newTx.status = "failed"
          } else {
            var theLog
            for (var i = 0; i < logs.length; i++) {
              if (logs[i].address == constants.NETWORK_ADDRESS &&
                logs[i].topics[0] == constants.TRADE_TOPIC) {
                theLog = logs[i]
                break
              }
            }
            newTx.status = theLog ? "success" : "failed"
          }
        } else if (newTx.type == "send") {
          newTx.status = "success"
        } else {
          newTx.status = "mined"
        }
      }
      else {
        newTx.status = "pending"
      }
      callback(newTx)
    })
  }
}
