forked from blockcypher/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
93 lines (85 loc) · 3.95 KB
/
models.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from django.db import models
from blockcypher.utils import format_crypto_units
from emails.trigger import send_and_log
class OnChainTransaction(models.Model):
'''
Transaction events generated from an address subscription
'''
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
tx_hash = models.CharField(blank=False, null=False, max_length=128, db_index=True)
address_subscription = models.ForeignKey('addresses.AddressSubscription', blank=False, null=False)
num_confs = models.IntegerField(blank=False, null=False, db_index=True)
double_spend = models.BooleanField(db_index=True, default=False)
satoshis_sent = models.BigIntegerField(blank=False, null=False, db_index=True)
fee_in_satoshis = models.BigIntegerField(blank=False, null=False, db_index=True)
is_deposit = models.BooleanField(db_index=True, default=False)
is_withdrawal = models.BooleanField(db_index=True, default=False)
def __str__(self):
return '%s to %s' % (self.id, self.tx_hash)
def send_double_spend_tx_notification(self):
# FIXME
pass
def send_unconfirmed_tx_email(self):
b58_address = self.address_subscription.b58_address
sent_in_btc = format_crypto_units(
input_quantity=self.satoshis_sent,
input_type='satoshi',
output_type='btc',
coin_symbol=self.address_subscription.coin_symbol,
print_cs=False,
safe_trimming=False,
round_digits=4,
)
fee_in_btc = format_crypto_units(
input_quantity=self.fee_in_satoshis,
input_type='satoshi',
output_type='btc',
coin_symbol=self.address_subscription.coin_symbol,
print_cs=False,
safe_trimming=False,
round_digits=4,
)
context_dict = {
'b58_address': b58_address,
'coin_symbol': self.address_subscription.coin_symbol,
'sent_in_btc': sent_in_btc,
'fee_in_btc': fee_in_btc,
'currency_display_name': self.address_subscription.get_currency_display_name(),
'currency_abbrev': self.address_subscription.get_currency_abbrev(),
'tx_hash': self.tx_hash,
'num_confs': self.num_confs,
}
if self.address_subscription.address_forwarding_obj:
context_dict['destination_address'] = self.address_subscription.address_forwarding_obj.destination_address
context_dict['satoshis_transacted'] = self.satoshis_sent
fkey_objs = {
'transaction_event': self,
'address_subscription': self.address_subscription,
}
return send_and_log(
subject='Unconfirmed Transaction for %s' % b58_address,
body_template='new_tx.html',
to_user=self.address_subscription.auth_user,
body_context=context_dict,
fkey_objs=fkey_objs,
)
def send_confirmed_tx_email(self):
b58_address = self.address_subscription.b58_address
context_dict = {
'b58_address': b58_address,
'coin_symbol': self.address_subscription.coin_symbol,
'tx_hash': self.tx_hash,
'num_confs': self.num_confs,
'currency_display_name': self.address_subscription.get_currency_display_name(),
}
fkey_objs = {
'transaction_event': self,
'address_subscription': self.address_subscription,
}
return send_and_log(
subject='Transaction %s... Confirmed' % self.tx_hash[:10],
body_template='confirmed_tx.html',
to_user=self.address_subscription.auth_user,
body_context=context_dict,
fkey_objs=fkey_objs,
)