| 1768 | } |
| 1769 | |
| 1770 | char *maybe_update_onchain_fees(const tal_t *ctx, struct db *db, |
| 1771 | struct bitcoin_txid *txid) |
| 1772 | { |
| 1773 | size_t no_accts = 0, plus_ones; |
| 1774 | u64 last_id = 0, wallet_id, extern_id; |
| 1775 | bool contains_wallet = false, skip_wallet = false; |
| 1776 | struct chain_event **events; |
| 1777 | struct amount_msat deposit_msat = AMOUNT_MSAT(0), |
| 1778 | withdraw_msat = AMOUNT_MSAT(0), |
| 1779 | fees_msat, fee_part_msat; |
| 1780 | char *err = NULL; |
| 1781 | u8 *inner_ctx = tal(NULL, u8); |
| 1782 | |
| 1783 | /* Find all the deposits/withdrawals for this txid */ |
| 1784 | events = find_chain_events_bytxid(inner_ctx, db, txid); |
| 1785 | wallet_id = find_acct_id(db, WALLET_ACCT); |
| 1786 | extern_id = find_acct_id(db, EXTERNAL_ACCT); |
| 1787 | |
| 1788 | /* If we don't even have two events, skip */ |
| 1789 | if (tal_count(events) < 2) |
| 1790 | goto finished; |
| 1791 | |
| 1792 | for (size_t i = 0; i < tal_count(events); i++) { |
| 1793 | bool is_channel_close_tx; |
| 1794 | err = is_closed_channel_txid(ctx, db, |
| 1795 | events[i], txid, |
| 1796 | &is_channel_close_tx); |
| 1797 | |
| 1798 | if (err) |
| 1799 | goto finished; |
| 1800 | |
| 1801 | /* We skip channel close txs here! */ |
| 1802 | if (is_channel_close_tx) |
| 1803 | goto finished; |
| 1804 | |
| 1805 | if (events[i]->spending_txid) { |
| 1806 | if (!amount_msat_add(&withdraw_msat, withdraw_msat, |
| 1807 | events[i]->debit)) { |
| 1808 | err = tal_fmt(ctx, "Overflow adding withdrawal debits for" |
| 1809 | " txid: %s", |
| 1810 | type_to_string(ctx, struct bitcoin_txid, |
| 1811 | txid)); |
| 1812 | goto finished; |
| 1813 | } |
| 1814 | } else { |
| 1815 | if (!amount_msat_add(&deposit_msat, deposit_msat, |
| 1816 | events[i]->credit)) { |
| 1817 | err = tal_fmt(ctx, "Overflow adding deposit credits for" |
| 1818 | " txid: %s", |
| 1819 | type_to_string(ctx, struct bitcoin_txid, |
| 1820 | txid)); |
| 1821 | goto finished; |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* While we're here, also count number of accts |
| 1826 | * that were involved! Two little tricks here. |
| 1827 | * |