overdraft.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //! Overdraft: a shortfall becomes a negative posting on an account that permits
  2. //! it, and `InsufficientFunds` on an account that does not.
  3. //!
  4. //! Run with:
  5. //! ```sh
  6. //! cargo run -p kuatia --example overdraft
  7. //! ```
  8. use kuatia::error::LedgerError;
  9. use kuatia::prelude::*;
  10. #[tokio::main]
  11. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  12. let ledger = Ledger::connect("sqlite::memory:").await?;
  13. let treasury = AccountId::new(1);
  14. let wallet = AccountId::new(2);
  15. let merchant = AccountId::new(3);
  16. let external = AccountId::new(99);
  17. let usd = ledger.asset("USD", "$", 2).await?;
  18. ledger.create_account(Account::new(treasury)).await?;
  19. ledger
  20. .create_account(Account::debit_must_not_exceed_credit(wallet))
  21. .await?;
  22. ledger
  23. .create_account(Account::debit_must_not_exceed_credit(merchant))
  24. .await?;
  25. ledger.create_account(Account::new(external)).await?;
  26. for account in [treasury, wallet] {
  27. ledger
  28. .commit(
  29. TransferBuilder::new()
  30. .deposit(account, ("30.00", &usd), external)
  31. .build()?,
  32. )
  33. .await?;
  34. }
  35. println!("Both accounts hold 30.00 USD. Both now try to pay 70.00 USD.\n");
  36. println!("treasury permits overdraft (Account::new):");
  37. print_postings(&ledger, treasury, &usd).await?;
  38. ledger
  39. .commit(
  40. TransferBuilder::new()
  41. .pay(treasury, merchant, ("70.00", &usd))
  42. .build()?,
  43. )
  44. .await?;
  45. println!(
  46. " commit succeeded, balance {}",
  47. ledger.balance(&treasury, &usd).await?
  48. );
  49. println!(" the 30.00 posting was consumed and a 40.00 shortfall posting created:");
  50. print_postings(&ledger, treasury, &usd).await?;
  51. println!("\nwallet forbids it (Account::debit_must_not_exceed_credit):");
  52. print_postings(&ledger, wallet, &usd).await?;
  53. let refused = ledger
  54. .commit(
  55. TransferBuilder::new()
  56. .pay(wallet, merchant, ("70.00", &usd))
  57. .build()?,
  58. )
  59. .await;
  60. match refused {
  61. // The shortfall names its own asset, so this reports a quantity without
  62. // the example having to remember which asset it asked about.
  63. Err(LedgerError::Selection(shortfall)) => println!(
  64. " commit refused: available {}, requested {}",
  65. shortfall.available, shortfall.requested,
  66. ),
  67. Err(other) => println!(" commit refused for another reason: {other}"),
  68. Ok(_) => println!(" commit succeeded, which it should not have"),
  69. }
  70. println!(
  71. " balance unchanged at {}",
  72. ledger.balance(&wallet, &usd).await?
  73. );
  74. Ok(())
  75. }
  76. /// Prints each posting with its lifecycle state, so a consumed positive and a
  77. /// created negative are visible side by side rather than inferred from balances.
  78. async fn print_postings(
  79. ledger: &Ledger,
  80. account: AccountId,
  81. asset: &std::sync::Arc<Asset>,
  82. ) -> Result<(), Box<dyn std::error::Error>> {
  83. for (posting, state) in ledger.postings_with_state(&account).await? {
  84. if posting.amount.asset_id() == asset.id {
  85. println!(" {:>12} {:?}", posting.amount.to_string(), state);
  86. }
  87. }
  88. Ok(())
  89. }