| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- //! Fund an account, then withdraw value out of the ledger.
- //!
- //! Run with:
- //! ```sh
- //! cargo run -p kuatia --example withdraw
- //! ```
- use kuatia::ledger::Ledger;
- use kuatia_core::*;
- use kuatia_storage_sql::SqlStore;
- #[tokio::main]
- async fn main() -> Result<(), Box<dyn std::error::Error>> {
- let ledger = connect().await?;
- let alice = AccountId::new(1);
- let external = AccountId::new(99);
- let usd = ledger.asset("USD", "$", 2).await?;
- ledger
- .create_account(Account::debit_must_not_exceed_credit(alice))
- .await?;
- ledger.create_account(Account::new(external)).await?;
- // Written form, parsed at the asset's own two decimals.
- ledger
- .commit(
- TransferBuilder::new()
- .deposit(alice, ("100.00", &usd), external)
- .build()?,
- )
- .await?;
- println!(
- "after deposit: alice = {}",
- ledger.balance(&alice, &usd).await?
- );
- ledger
- .commit(
- TransferBuilder::new()
- .withdraw(alice, ("30.00", &usd), external)
- .build()?,
- )
- .await?;
- println!(
- "after withdraw: alice = {}",
- ledger.balance(&alice, &usd).await?
- );
- // The external account carries the offset (negative) side: the mirror of the
- // value that currently sits inside the ledger.
- println!(
- "external boundary: {}",
- ledger.balance(&external, &usd).await?
- );
- Ok(())
- }
- async fn connect() -> Result<Ledger, Box<dyn std::error::Error>> {
- kuatia_storage_sql::install_drivers()?;
- let pool = sqlx::any::AnyPoolOptions::new()
- .max_connections(1)
- .connect("sqlite::memory:")
- .await?;
- let store = SqlStore::new(pool);
- store.migrate().await?;
- let ledger = Ledger::new(store);
- Ok(ledger)
- }
|