Skip to content

Commit

Permalink
Merge pull request #103 from dogecoinfoundation/rf-mark-events-sent
Browse files Browse the repository at this point in the history
Fix MarkInvoiceEventSent SQL, and check for errors
  • Loading branch information
tjstebbing authored Aug 31, 2023
2 parents 52ec3f0 + 00d8d86 commit b80f406
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
16 changes: 12 additions & 4 deletions pkg/services/balancekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ func (b BalanceKeeper) sendInvoiceEvents(tx giga.StoreTransaction, acc *giga.Acc
b.bus.Send(giga.SYS_MSG, fmt.Sprintf("BalanceKeeper: %s: %s in %s\n", event, inv.ID, id))
}
// This updates LastIncomingAmount from IncomingAmount.
tx.MarkInvoiceEventSent(inv.ID, giga.INV_PART_PAYMENT_DETECTED)
err = tx.MarkInvoiceEventSent(inv.ID, giga.INV_PART_PAYMENT_DETECTED)
if err != nil {
log.Printf("BalanceKeeper: MarkInvoiceEventSent: '%s': %v\n", inv.ID, err)
return err
}
}
if inv.PaidHeight != 0 && inv.PaidEvent.IsZero() {
// invoice is fully paid and confirmed.
Expand All @@ -236,7 +240,7 @@ func (b BalanceKeeper) sendInvoiceEvents(tx giga.StoreTransaction, acc *giga.Acc
}
err = tx.MarkInvoiceEventSent(inv.ID, event)
if err != nil {
log.Printf("BalanceKeeper: MarkInvoiceEventSent '%s': %v\n", id, err)
log.Printf("BalanceKeeper: MarkInvoiceEventSent '%s': %v\n", inv.ID, err)
return err
}
b.bus.Send(giga.SYS_MSG, fmt.Sprintf("BalanceKeeper: %s: %s in %s\n", event, inv.ID, id))
Expand All @@ -259,7 +263,7 @@ func (b BalanceKeeper) sendInvoiceEvents(tx giga.StoreTransaction, acc *giga.Acc
}
err = tx.MarkInvoiceEventSent(inv.ID, event)
if err != nil {
log.Printf("BalanceKeeper: MarkInvoiceEventSent '%s': %v\n", id, err)
log.Printf("BalanceKeeper: MarkInvoiceEventSent '%s': %v\n", inv.ID, err)
return err
}
b.bus.Send(giga.SYS_MSG, fmt.Sprintf("BalanceKeeper: %s: %s in %s\n", event, inv.ID, id))
Expand All @@ -284,7 +288,11 @@ func (b BalanceKeeper) sendInvoiceEvents(tx giga.StoreTransaction, acc *giga.Acc
return err
}
// This updates LastPaidAmount from PaidAmount.
tx.MarkInvoiceEventSent(inv.ID, event)
err = tx.MarkInvoiceEventSent(inv.ID, event)
if err != nil {
log.Printf("BalanceKeeper: MarkInvoiceEventSent: '%s': %v\n", inv.ID, err)
return err
}
b.bus.Send(giga.SYS_MSG, fmt.Sprintf("BalanceKeeper: %s: %s in %s\n", event, inv.ID, id))
}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/store/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,18 @@ func (t SQLiteStoreTransaction) ConfirmUTXOs(confirmations int, blockHeight int6
return
}

const incoming_amount_sql = "UPDATE invoice SET last_incoming=COALESCE((SELECT SUM(value) FROM utxo WHERE added_height IS NOT NULL AND script_address=invoice.invoice_address),0) WHERE invoice_address=$1"
const paid_amount_sql = "UPDATE invoice SET last_paid=COALESCE((SELECT SUM(value) FROM utxo WHERE spendable_height IS NOT NULL AND script_address=invoice.invoice_address),0) WHERE invoice_address=$1"

func (t SQLiteStoreTransaction) MarkInvoiceEventSent(invoiceID giga.Address, event giga.EVENT_INV) error {
sql := ""
switch event {
case giga.INV_PART_PAYMENT_DETECTED, giga.INV_TOTAL_PAYMENT_DETECTED, giga.INV_OVER_PAYMENT_DETECTED:
// set LastIncomingAmount = IncomingAmount
sql = "UPDATE invoice SET last_incoming_amount=incoming_amount WHERE invoice_address=$1"
sql = incoming_amount_sql // "UPDATE invoice SET last_incoming=incoming_amount WHERE invoice_address=$1"
case giga.INV_OVER_PAYMENT_CONFIRMED:
// set LastPaidAmount = PaidAmount
sql = "UPDATE invoice SET last_paid_amount=paid_amount WHERE invoice_address=$1"
sql = paid_amount_sql // "UPDATE invoice SET last_paid=paid_amount WHERE invoice_address=$1"
case giga.INV_TOTAL_PAYMENT_CONFIRMED:
// set paid_event = NOW
sql = "UPDATE invoice SET paid_event=CURRENT_TIMESTAMP WHERE invoice_address=$1"
Expand Down

0 comments on commit b80f406

Please sign in to comment.