| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- //! Fund an account, then withdraw value out of the ledger.
- //!
- //! Run with:
- //! ```sh
- //! cargo run -p kuatia --example withdraw
- //! ```
- use kuatia::prelude::*;
- #[tokio::main]
- async fn main() -> Result<(), Box<dyn std::error::Error>> {
- let ledger = Ledger::connect("sqlite::memory:").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(())
- }
|