overdraft.md 1.7 KB

Overdraft and the shortfall posting

Two accounts hold the same 30.00 USD. Both try to pay 70.00 USD. One succeeds and becomes negative. The ledger refuses the other. The difference is one flag.

An account from Account::new permits overdraft without bound. Its positive postings sometimes do not cover a debit. Resolve then consumes all of them and creates a negative posting for the remainder. The transfer still records, because value conserves per asset.

An account from Account::debit_must_not_exceed_credit carries AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT. It cannot hold a negative posting, so selection fails with InsufficientFunds instead. See accounts for the rule. See transfers for the point in resolve where it applies.

The program below prints both outcomes:

Both accounts hold 30.00 USD. Both now try to pay 70.00 USD.

treasury permits overdraft (Account::new):
       30.00 USD  Live
  commit succeeded, balance -40.00 USD
  the 30.00 posting was consumed and a 40.00 shortfall posting created:
       30.00 USD  Spent
      -40.00 USD  Live

wallet forbids it (Account::debit_must_not_exceed_credit):
       30.00 USD  Live
  commit refused: available 30.00 USD, requested 70.00 USD
  balance unchanged at 30.00 USD

The shortfall posting is the reason the balance reads -40.00, and no field changed. The account now holds two postings: +30.00, which is spent, and -40.00, which is live. The balance is the sum of the live ones. The merchant's +70.00 posting makes the transfer conserve: 30.00 consumed, and 70.00 plus -40.00 created.

{{#include ../../crates/kuatia/examples/overdraft.rs}}