quickstart.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //! Register an asset, open two accounts, fund one from the boundary and pay the
  2. //! other. Needs no database and no feature, so it is the shortest whole program
  3. //! the ledger has.
  4. //!
  5. //! Run with:
  6. //! ```sh
  7. //! cargo run -p kuatia --example quickstart
  8. //! ```
  9. use kuatia::prelude::*;
  10. #[tokio::main]
  11. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  12. let ledger = Ledger::new(InMemoryStore::new());
  13. let usd = ledger.asset("USD", "$", 2).await?;
  14. let alice = AccountId::new(1);
  15. let bob = AccountId::new(2);
  16. let external = AccountId::new(99);
  17. ledger
  18. .create_account(Account::debit_must_not_exceed_credit(alice))
  19. .await?;
  20. ledger
  21. .create_account(Account::debit_must_not_exceed_credit(bob))
  22. .await?;
  23. ledger.create_account(Account::new(external)).await?;
  24. ledger
  25. .commit(
  26. TransferBuilder::new()
  27. .deposit(alice, ("100.00", &usd), external)
  28. .build()?,
  29. )
  30. .await?;
  31. ledger
  32. .commit(
  33. TransferBuilder::new()
  34. .pay(alice, bob, ("25.00", &usd))
  35. .build()?,
  36. )
  37. .await?;
  38. println!("alice: {}", ledger.balance(&alice, &usd).await?);
  39. println!("bob: {}", ledger.balance(&bob, &usd).await?);
  40. Ok(())
  41. }