Skip to content

Commit

Permalink
fix crypto amount recalc for the new crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-samoylenko committed Jan 20, 2025
1 parent 7b9988a commit a3c9299
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion shkeeper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,23 @@ def add(cls, crypto, request):
# updating existing invoice
invoice.fiat = request["fiat"]
invoice.amount_fiat = Decimal(request["amount"])

# recalc crypto amount for the new fiat amount
rate = ExchangeRate.get(invoice.fiat, invoice.crypto)
invoice.amount_crypto, invoice.exchange_rate = rate.convert(
invoice.amount_fiat
)

crypto_changed = invoice.crypto != crypto.crypto
if crypto_changed or crypto_is_lightning:
invoice.crypto = crypto.crypto

# recalc crypto amount for the new crypto and fiat amount
rate = ExchangeRate.get(invoice.fiat, invoice.crypto)
invoice.amount_crypto, invoice.exchange_rate = rate.convert(
invoice.amount_fiat
)

# if address for new crypto already exist, use it instead of generating a new one
invoice_address = InvoiceAddress.query.filter_by(
invoice_id=invoice.id, crypto=crypto.crypto
Expand Down Expand Up @@ -540,7 +549,7 @@ def add(cls, crypto, tx):
invoice = Invoice.query.filter_by(id=invoice_address.invoice_id).first()

if not invoice:
raise NotRelatedToAnyInvoice(f'{tx["addr"]} is not related to any invoice')
raise NotRelatedToAnyInvoice(f"{tx['addr']} is not related to any invoice")

t = cls()
t.invoice_id = invoice.id
Expand Down

0 comments on commit a3c9299

Please # to comment.