create_accounts.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //! Connect to a SQLite-backed ledger and create accounts.
  2. //!
  3. //! Run with:
  4. //! ```sh
  5. //! cargo run -p kuatia --example create_accounts
  6. //! ```
  7. use std::collections::BTreeMap;
  8. use kuatia::prelude::*;
  9. #[tokio::main]
  10. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  11. let ledger = Ledger::connect("sqlite::memory:").await?;
  12. // The common case is one line: a version-1 account.
  13. ledger
  14. .create_account(Account::debit_must_not_exceed_credit(AccountId::new(1)))
  15. .await?;
  16. ledger
  17. .create_account(Account::debit_must_not_exceed_credit(AccountId::new(2)))
  18. .await?;
  19. // A system account (fees, settlement, market-making) — no balance floor.
  20. ledger
  21. .create_account(Account::new(AccountId::new(50)))
  22. .await?;
  23. // The same thing spelled out, so you can see every field of an `Account`.
  24. // This boundary account is where value enters/leaves the ledger.
  25. let external = Account {
  26. id: AccountId::new(99),
  27. version: 1, // accounts always start at version 1
  28. flags: AccountFlags::empty(), // not frozen, not closed
  29. book: DEFAULT_BOOK, // the implicit default book
  30. metadata: BTreeMap::new(), // free-form key/value metadata
  31. };
  32. ledger.create_account(external).await?;
  33. // Read them back (latest version of each).
  34. println!("accounts:");
  35. let mut accounts = ledger.list_accounts().await?;
  36. accounts.sort_by_key(|a| (a.id.id, a.id.sub));
  37. for a in &accounts {
  38. println!(" {:?} flags={:?} v{}", a.id, a.flags, a.version);
  39. }
  40. Ok(())
  41. }