def fetch_ogn_transactions():

    etherscan_url = 'http://api.etherscan.io/api?module=account&action=tokentx&contractaddress=%s&startblock=0&endblock=999999999&sort=desc&apikey=%s' % (
        ogn_contract, constants.ETHERSCAN_KEY)
    # print etherscan_url
    results = call_etherscan(etherscan_url)

    # loop through every transaction where Origin tokens were moved
    for result in results['result']:
        tx = db_common.get_or_create(db.session,
                                     db_models.TokenTransaction,
                                     tx_hash=result['hash'])
        tx.from_address = result['from'].lower()
        tx.to_address = result['to'].lower()
        # intentionally using ETH instead of WEI to be more human-friendly, despite being less precise
        tx.amount = float(result['value']) / math.pow(10, 18)
        tx.block_number = result['blockNumber']
        tx.timestamp = time_.fromtimestamp(result['timeStamp'])

        if tx.amount > 0:
            print "%g OGN moved in transaction %s" % (tx.amount,
                                                      result['hash'])

        # send an email alert every time OGN tokens are moved
        # only alert once & ignore marketplace transactions which show up as 0 OGN
        if (tx.amount > 0 and not tx.notification_sent):
            to_details = lookup_details(tx.to_address)
            from_details = lookup_details(tx.from_address)

            if from_details.name and to_details.name:
                subject = "%s moved %g OGN to %s" % (
                    from_details.name, tx.amount, to_details.name)
            elif from_details.name:
                subject = "%s moved %g OGN" % (from_details.name, tx.amount)
            elif to_details.name:
                subject = "%g OGN moved to %s" % (tx.amount, to_details.name)
            else:
                subject = "%g OGN moved" % (tx.amount)

            body = u"""
				{amount} OGN <a href='https://etherscan.io/tx/{tx_hash}'>moved</a>
				from <a href='https://etherscan.io/address/{from_address}'>{from_name}</a>
				to <a href='https://etherscan.io/address/{to_address}'>{to_name}</a>
			""".format(amount='{0:g}'.format(float(tx.amount)),
              tx_hash=tx.tx_hash,
              from_name=from_details.name
              if from_details.name else tx.from_address,
              from_address=tx.from_address,
              to_name=to_details.name if to_details.name else tx.to_address,
              to_address=tx.to_address)

            print subject

            sgw.notify_founders(body, subject)
            tx.notification_sent = True
            db.session.add(tx)
            db.session.commit()
def fill_missing_txs(do_it):
    start_block = 10176690
    new_supply = 58821352

    etherscan_url = (
        "http://api.etherscan.io/api?module=account&action=tokentx&contractaddress=%s&startblock=%s&endblock=999999999&sort=asc&apikey=%s"
        % (ogn_contract, start_block, constants.ETHERSCAN_KEY))

    raw_json = requests.get(etherscan_url)
    results = raw_json.json()
    update = 0

    for result in results["result"]:
        from_address = result["from"].lower()
        to_address = result["to"].lower()
        amount = float(result["value"]) / math.pow(10, 18)
        if from_address in reserved_addresses and to_address not in reserved_addresses:
            new_supply = new_supply + amount
        elif from_address not in reserved_addresses and to_address in reserved_addresses:
            new_supply = new_supply - amount
        else:
            continue

        update = update + 1
        instance = db_common.get_or_create(db.session,
                                           db_models.CirculatingSupply,
                                           snapshot_date=time_.fromtimestamp(
                                               result["timeStamp"]))
        instance.supply_amount = new_supply
        db.session.add(instance)

        print("{} {}".format(time_.fromtimestamp(result["timeStamp"]),
                             new_supply))

    if do_it:
        db.session.commit()

    print("Have parsed {}/{} transactions".format(update,
                                                  len(results["result"])))
    print("Circulating supply at the end of all txs: {}".format(new_supply))
Exemplo n.º 3
0
def fill_missing_txs(do_it):
  # last_tx_db = db_models.TokenTransaction.query.order_by(
  #   db_models.TokenTransaction.block_number.desc()
  # ).first()

  last_entry = db_models.CirculatingSupply.query.order_by(
    db_models.CirculatingSupply.snapshot_date.desc()
  ).first()

  if not last_entry:
    print("Couldn't find any entry on DB")
    return

  start_block = 10178155 # the last one in DB, same as last_tx_db.block_number
  new_supply = last_entry.supply_amount

  etherscan_url = (
    "http://api.etherscan.io/api?module=account&action=tokentx&contractaddress=%s&startblock=%s&endblock=999999999&sort=asc&apikey=%s"
    % (ogn_contract, start_block, constants.ETHERSCAN_KEY)
  )

  raw_json = requests.get(etherscan_url)
  results = raw_json.json()
  update = 0

  for result in results["result"]:
    from_address = result["from"].lower()
    to_address = result["to"].lower()
    amount = float(result["value"]) / math.pow(10, 18)
    if from_address in reserved_addresses and to_address not in reserved_addresses:
      new_supply = new_supply + amount
    elif from_address not in reserved_addresses and to_address in reserved_addresses:
      new_supply = new_supply - amount
    else:
      continue
  
    update = update + 1
    instance = db_common.get_or_create(
      db.session, db_models.CirculatingSupply, snapshot_date=time_.fromtimestamp(result["timeStamp"])
    )
    instance.supply_amount = new_supply
    db.session.add(instance)
  
  if do_it:
    db.session.commit()

  print("Have parsed {} transactions".format(update))
  print("Circulating supply at the end of all txs: {}".format(new_supply))