| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- //! Connect to a SQLite-backed ledger and create accounts.
- //!
- //! Run with:
- //! ```sh
- //! cargo run -p kuatia --example create_accounts
- //! ```
- use std::collections::BTreeMap;
- use kuatia::prelude::*;
- #[tokio::main]
- async fn main() -> Result<(), Box<dyn std::error::Error>> {
- let ledger = Ledger::connect("sqlite::memory:").await?;
- // The common case is one line: a version-1 account.
- ledger
- .create_account(Account::debit_must_not_exceed_credit(AccountId::new(1)))
- .await?;
- ledger
- .create_account(Account::debit_must_not_exceed_credit(AccountId::new(2)))
- .await?;
- // A system account (fees, settlement, market-making) — no balance floor.
- ledger
- .create_account(Account::new(AccountId::new(50)))
- .await?;
- // The same thing spelled out, so you can see every field of an `Account`.
- // This boundary account is where value enters/leaves the ledger.
- let external = Account {
- id: AccountId::new(99),
- version: 1, // accounts always start at version 1
- flags: AccountFlags::empty(), // not frozen, not closed
- book: DEFAULT_BOOK, // the implicit default book
- metadata: BTreeMap::new(), // free-form key/value metadata
- };
- ledger.create_account(external).await?;
- // Read them back (latest version of each).
- println!("accounts:");
- let mut accounts = ledger.list_accounts().await?;
- accounts.sort_by_key(|a| (a.id.id, a.id.sub));
- for a in &accounts {
- println!(" {:?} flags={:?} v{}", a.id, a.flags, a.version);
- }
- Ok(())
- }
|