Skip to content

Commit

Permalink
Send notifications when tx is saved from post save signal
Browse files Browse the repository at this point in the history
  • Loading branch information
joemarct committed Jun 18, 2021
1 parent a78678d commit 178c602
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __str__(self):
else:
return str(self.name)


class BlockHeight(models.Model):
number = models.IntegerField(default=0, unique=True, db_index=True)
transactions_count = models.IntegerField(default=0)
Expand All @@ -59,6 +60,7 @@ def save(self, *args, **kwargs):
def __str__(self):
return str(self.number)


class Transaction(models.Model):
txid = models.CharField(max_length=200, db_index=True)
address = models.CharField(max_length=500,null=True, db_index=True)
Expand Down Expand Up @@ -90,6 +92,7 @@ class Transaction(models.Model):
def __str__(self):
return self.txid


class Recipient(models.Model):
web_url = models.CharField(max_length=500,null=True, blank=True)
telegram_id = models.CharField(max_length=100,null=True, blank=True)
Expand All @@ -103,6 +106,7 @@ def __str__(self):
else:
return 'N/A'


class SlpAddress(models.Model):
address = models.CharField(max_length=200, unique=True, db_index=True)
transactions = models.ManyToManyField(
Expand All @@ -118,6 +122,7 @@ class Meta:
def __str__(self):
return self.address


class BchAddress(models.Model):
address = models.CharField(max_length=200, unique=True, db_index=True)
transactions = models.ManyToManyField(
Expand All @@ -134,6 +139,7 @@ class Meta:
def __str__(self):
return self.address


class Subscription(models.Model):
recipient = models.ForeignKey(
Recipient,
Expand Down
22 changes: 19 additions & 3 deletions main/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from main.tasks import (
save_record,
slpdbquery_transaction,
bitdbquery_transaction
bitdbquery_transaction,
client_acknowledgement,
send_telegram_message
)
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
Expand Down Expand Up @@ -83,7 +85,14 @@ def transaction_post_save(sender, instance=None, created=False, **kwargs):
blockheight_id,
tx_output['index']
)
save_record(*args)
obj_id, created = save_record(*args)
if created:
third_parties = client_acknowledgement(obj_id)
for platform in third_parties:
if 'telegram' in platform:
message = platform[1]
chat_id = platform[2]
send_telegram_message(message, chat_id)

elif instance.address.startswith('simpleledger:'):
# Make sure that any corresponding BCH transaction is saved
Expand Down Expand Up @@ -118,4 +127,11 @@ def transaction_post_save(sender, instance=None, created=False, **kwargs):
blockheight_id,
tx_output['index']
)
save_record(*args)
obj_id, created = save_record(*args)
if created:
third_parties = client_acknowledgement(obj_id)
for platform in third_parties:
if 'telegram' in platform:
message = platform[1]
chat_id = platform[2]
send_telegram_message(message, chat_id)
5 changes: 3 additions & 2 deletions main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ def bitdbquery_transaction(self, transaction, total, block_number, block_id, ale
index= _in['e']['i']
input_scanner(txid, index, block_id=block_id)




@shared_task(bind=True, queue='bitdbquery', max_retries=30)
def bitdbquery(self, block_id):
try:
Expand Down Expand Up @@ -356,6 +355,7 @@ def bitdbquery(self, block_id):
REDIS_STORAGE.set('PENDING-BLOCKS', json.dumps(pending_blocks))
REDIS_STORAGE.set('READY', 1)


@shared_task(bind=True, queue='slpdbquery_transactions')
def slpdbquery_transaction(self, transaction, tx_count, total, alert=True):
source = 'slpdb-query'
Expand Down Expand Up @@ -474,6 +474,7 @@ def manage_block_transactions(self):
active_block = str(REDIS_STORAGE.get('ACTIVE-BLOCK'))
if active_block: return f'REDIS IS TOO BUSY FOR BLOCK {str(active_block)}.'


@shared_task(bind=True, queue='get_latest_block')
def get_latest_block(self):
# This task is intended to check new blockheight every 5 seconds through BitDB Query
Expand Down

0 comments on commit 178c602

Please sign in to comment.