Permalink
Browse files

added balance notifications possibility

  • Loading branch information...
Jeremias Kangas
Jeremias Kangas committed May 22, 2012
1 parent 6f832c5 commit e22a575639cf3443621768b25b0d85ccaf048853
Showing with 53 additions and 0 deletions.
  1. +53 −0 django_bitcoin/management/commands/CheckTransactions.py
@@ -0,0 +1,53 @@
+from django.core.management.base import NoArgsCommand
+from time import sleep, time
+from django_bitcoin.utils import bitcoind
+from django_bitcoin.models import BitcoinAddress
+from django.conf import settings
+from decimal import Decimal
+
+RUN_TIME_SECONDS = 60
+
+
+class Command(NoArgsCommand):
+ help = """This needs transactions signaling enabled. Polls\
+ incoming transactions via listtransactions -bitcoind call, and checks\
+ the balances accordingly.
+ To enable, add this command to your cron, and set
+ BITCOIN_TRANSACTION_SIGNALING = True
+ After that, you will get signals from the transactions you do.
+ balance_changed = django.dispatch.Signal(providing_args=["balance", "changed"])
+"""
+
+ def handle_noargs(self, **options):
+ start_time = time()
+ last_check_time = None
+ while time() - start_time < float(RUN_TIME_SECONDS):
+ if settings.DEBUG:
+ print "starting..."
+ if not last_check_time:
+ addresses_json = bitcoind.bitcoind_api.listreceivedbyaddress(0)
+ addresses = {}
+ for t in addresses_json:
+ addresses[t['address']] = Decimal(t['amount'])
+ for ba in BitcoinAddress.objects.filter(active=True):
+ if ba.address in addresses.keys() and\
+ ba.least_received < addresses[ba.address]:
+ ba.query_bitcoind()
+ ba.query_bitcoind(0)
+ transactions = bitcoind.bitcoind_api.listtransactions()
+ for t in transactions:
+ if not last_check_time or (int(t['time'])) >= last_check_time:
+ try:
+ ba = BitcoinAddress.objects.get(address=t['address'])
+ if ba:
+ ba.query_bitcoind(0)
+ last_check_time = int(t['time'])
+ except BitcoinAddress.DoesNotExist:
+ pass
+ elif not last_check_time:
+ last_check_time = int(t['time'])
+ for ba in BitcoinAddress.objects.filter(active=True).extra(where=["least_received>least_received_confirmed"]):
+ ba.query_bitcoind()
+ if settings.DEBUG:
+ print "done, sleeping..."
+ sleep(2)

0 comments on commit e22a575

Please sign in to comment.