withdraw.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //! Fund an account, then withdraw value out of the ledger.
  2. //!
  3. //! Run with:
  4. //! ```sh
  5. //! cargo run -p kuatia --example withdraw
  6. //! ```
  7. use kuatia::prelude::*;
  8. #[tokio::main]
  9. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  10. let ledger = Ledger::connect("sqlite::memory:").await?;
  11. let alice = AccountId::new(1);
  12. let external = AccountId::new(99);
  13. let usd = ledger.asset("USD", "$", 2).await?;
  14. ledger
  15. .create_account(Account::debit_must_not_exceed_credit(alice))
  16. .await?;
  17. ledger.create_account(Account::new(external)).await?;
  18. // Written form, parsed at the asset's own two decimals.
  19. ledger
  20. .commit(
  21. TransferBuilder::new()
  22. .deposit(alice, ("100.00", &usd), external)
  23. .build()?,
  24. )
  25. .await?;
  26. println!(
  27. "after deposit: alice = {}",
  28. ledger.balance(&alice, &usd).await?
  29. );
  30. ledger
  31. .commit(
  32. TransferBuilder::new()
  33. .withdraw(alice, ("30.00", &usd), external)
  34. .build()?,
  35. )
  36. .await?;
  37. println!(
  38. "after withdraw: alice = {}",
  39. ledger.balance(&alice, &usd).await?
  40. );
  41. // The external account carries the offset (negative) side: the mirror of the
  42. // value that currently sits inside the ledger.
  43. println!(
  44. "external boundary: {}",
  45. ledger.balance(&external, &usd).await?
  46. );
  47. Ok(())
  48. }