|
|
@@ -1,4 +1,4 @@
|
|
|
-# Glossary & Usage Guide
|
|
|
+# Glossary
|
|
|
|
|
|
> Coming from classical accounting? See
|
|
|
> [accounting-mapping.md](accounting-mapping.md) for how journals, entries, and
|
|
|
@@ -132,212 +132,11 @@ assigns IDs.
|
|
|
|
|
|
---
|
|
|
|
|
|
-## Usage Examples
|
|
|
+## Usage examples
|
|
|
|
|
|
-### Example 1: Currency Exchange
|
|
|
-
|
|
|
-An exchange lets users deposit fiat, trade between currencies, and withdraw.
|
|
|
-
|
|
|
-**Setup:**
|
|
|
-
|
|
|
-```rust
|
|
|
-use kuatia::prelude::*;
|
|
|
-
|
|
|
-// Assets
|
|
|
-let usd = AssetId::new(1);
|
|
|
-let eur = AssetId::new(2);
|
|
|
-
|
|
|
-// Books: separate deposit/withdrawal flows from trading
|
|
|
-let deposits_book = BookBuilder::new("deposits")
|
|
|
- .allow_asset(usd)
|
|
|
- .allow_asset(eur)
|
|
|
- .allow_flags(AccountFlags::USER_0 | AccountFlags::USER_1) // wallets + bank
|
|
|
- .build();
|
|
|
-
|
|
|
-let trading_book = BookBuilder::new("trading")
|
|
|
- .allow_asset(usd)
|
|
|
- .allow_asset(eur)
|
|
|
- .allow_flags(AccountFlags::USER_0) // only user wallets
|
|
|
- .allow_account(exchange_pool) // + the exchange pool
|
|
|
- .build();
|
|
|
-
|
|
|
-ledger.create_book(deposits_book).await?;
|
|
|
-ledger.create_book(trading_book).await?;
|
|
|
-
|
|
|
-// Accounts — `Account::new` sets version 1, no flags, and the default book;
|
|
|
-// set the other fields explicitly where the common case is not enough.
|
|
|
-// Boundary account: overdraft allowed by default, so it can hold the offset.
|
|
|
-let mut bank = Account::new(AccountId::default());
|
|
|
-bank.flags = AccountFlags::USER_1; // bank flag
|
|
|
-bank.book = deposits_book.id;
|
|
|
-
|
|
|
-// Wallet: forbids overdraft, so its balance may not go negative.
|
|
|
-let mut alice = Account::debit_must_not_exceed_credit(AccountId::default());
|
|
|
-alice.flags |= AccountFlags::USER_0; // wallet flag
|
|
|
-alice.book = deposits_book.id;
|
|
|
-
|
|
|
-let mut exchange_pool = Account::new(AccountId::default());
|
|
|
-exchange_pool.book = trading_book.id;
|
|
|
-```
|
|
|
-
|
|
|
-**Deposit USD into Alice's wallet:**
|
|
|
-
|
|
|
-```rust
|
|
|
-let deposit = TransferBuilder::new()
|
|
|
- .book(deposits_book.id)
|
|
|
- .deposit(alice.id, usd, Cent::from(10_000), bank.id)?
|
|
|
- .build();
|
|
|
-ledger.commit(deposit).await?;
|
|
|
-// Alice: +10,000 USD
|
|
|
-// Bank: -10,000 USD (offset: value entered the ledger boundary)
|
|
|
-```
|
|
|
-
|
|
|
-**Alice trades 5,000 USD for EUR at 1:0.92:**
|
|
|
-
|
|
|
-```rust
|
|
|
-let trade = TransferBuilder::new()
|
|
|
- .book(trading_book.id)
|
|
|
- .pay(alice.id, exchange_pool, usd, Cent::from(5_000))
|
|
|
- .pay(exchange_pool, alice.id, eur, Cent::from(4_600))
|
|
|
- .build();
|
|
|
-ledger.commit(trade).await?;
|
|
|
-// Alice: 5,000 USD + 4,600 EUR
|
|
|
-// Exchange pool: 5,000 USD - 4,600 EUR
|
|
|
-```
|
|
|
-
|
|
|
-**Withdraw EUR to Alice's bank:**
|
|
|
-
|
|
|
-```rust
|
|
|
-let withdrawal = TransferBuilder::new()
|
|
|
- .book(deposits_book.id)
|
|
|
- .withdraw(alice.id, eur, Cent::from(4_600), bank.id)
|
|
|
- .build();
|
|
|
-ledger.commit(withdrawal).await?;
|
|
|
-// Alice: 5,000 USD, 0 EUR
|
|
|
-// Bank: -10,000 USD + 4,600 EUR
|
|
|
-```
|
|
|
-
|
|
|
-Conservation holds at every step. The exchange pool absorbs the spread.
|
|
|
-
|
|
|
-
|
|
|
-### Example 2: Supermarket / Retail POS
|
|
|
-
|
|
|
-A supermarket tracks inventory as product assets, records sales with COGS, and
|
|
|
-manages cash and bank accounts.
|
|
|
-
|
|
|
-**Setup:**
|
|
|
-
|
|
|
-```rust
|
|
|
-// Assets
|
|
|
-let gs = AssetId::new(1); // Guaranies (currency)
|
|
|
-let product_a = AssetId::new(100); // Product: rice 1kg
|
|
|
-let product_b = AssetId::new(101); // Product: cooking oil 1L
|
|
|
-
|
|
|
-// Account flags
|
|
|
-const WAREHOUSE: AccountFlags = AccountFlags::USER_0;
|
|
|
-const CUSTOMER: AccountFlags = AccountFlags::USER_1;
|
|
|
-const REVENUE: AccountFlags = AccountFlags::USER_2;
|
|
|
-const BANK: AccountFlags = AccountFlags::USER_3;
|
|
|
-
|
|
|
-// Books
|
|
|
-let sales_book = BookBuilder::new("sales")
|
|
|
- .allow_asset(gs)
|
|
|
- .allow_asset(product_a)
|
|
|
- .allow_asset(product_b)
|
|
|
- .allow_flags(WAREHOUSE | CUSTOMER | REVENUE)
|
|
|
- .build();
|
|
|
-
|
|
|
-let inventory_book = BookBuilder::new("inventory")
|
|
|
- .allow_asset(product_a)
|
|
|
- .allow_asset(product_b)
|
|
|
- .allow_flags(WAREHOUSE)
|
|
|
- .allow_account(world) // issuance source
|
|
|
- .build();
|
|
|
-
|
|
|
-let banking_book = BookBuilder::new("banking")
|
|
|
- .allow_asset(gs)
|
|
|
- .allow_flags(WAREHOUSE | BANK)
|
|
|
- .build();
|
|
|
-
|
|
|
-// Accounts — start from a constructor, then set flags where needed.
|
|
|
-// issuance source: mints product tokens on receipt (overdraft allowed)
|
|
|
-let world = Account::new(AccountId::default());
|
|
|
-
|
|
|
-let mut warehouse = Account::debit_must_not_exceed_credit(AccountId::default());
|
|
|
-warehouse.flags |= WAREHOUSE;
|
|
|
-
|
|
|
-let mut cash_register = Account::debit_must_not_exceed_credit(AccountId::default());
|
|
|
-cash_register.flags |= WAREHOUSE;
|
|
|
-
|
|
|
-let mut revenue = Account::new(AccountId::default());
|
|
|
-revenue.flags = REVENUE;
|
|
|
-
|
|
|
-let mut cogs = Account::new(AccountId::default()); // cost of goods sold
|
|
|
-cogs.flags = REVENUE;
|
|
|
-
|
|
|
-let mut bank = Account::debit_must_not_exceed_credit(AccountId::default());
|
|
|
-bank.flags |= BANK;
|
|
|
-```
|
|
|
-
|
|
|
-**Receive inventory from supplier (50 units of rice):**
|
|
|
-
|
|
|
-```rust
|
|
|
-let receipt = TransferBuilder::new()
|
|
|
- .book(inventory_book.id)
|
|
|
- .pay(world, warehouse.id, product_a, Cent::from(50_000)) // 50.000 units (precision 3)
|
|
|
- .build();
|
|
|
-ledger.commit(receipt).await?;
|
|
|
-// Warehouse: +50.000 rice
|
|
|
-// World: -50.000 rice (offset: issued into the ledger)
|
|
|
-```
|
|
|
-
|
|
|
-**Cash sale, customer buys 2 rice at 15,000 Gs each:**
|
|
|
-
|
|
|
-```rust
|
|
|
-let sale = TransferBuilder::new()
|
|
|
- .book(sales_book.id)
|
|
|
- // Move product from warehouse to customer (consumed by sale)
|
|
|
- .pay(warehouse.id, customer.id, product_a, Cent::from(2_000))
|
|
|
- // Customer pays cash
|
|
|
- .pay(customer.id, cash_register.id, gs, Cent::from(30_000))
|
|
|
- // Record revenue
|
|
|
- .pay(world, revenue.id, gs, Cent::from(30_000))
|
|
|
- // Record COGS (cost was 10,000 Gs per unit)
|
|
|
- .pay(world, cogs.id, gs, Cent::from(20_000))
|
|
|
- .build();
|
|
|
-ledger.commit(sale).await?;
|
|
|
-```
|
|
|
-
|
|
|
-**Deposit cash to bank:**
|
|
|
-
|
|
|
-```rust
|
|
|
-let deposit = TransferBuilder::new()
|
|
|
- .book(banking_book.id)
|
|
|
- .pay(cash_register.id, bank.id, gs, Cent::from(30_000))
|
|
|
- .build();
|
|
|
-ledger.commit(deposit).await?;
|
|
|
-```
|
|
|
-
|
|
|
-**Query balances:**
|
|
|
-
|
|
|
-```rust
|
|
|
-let warehouse_rice = ledger.balance(&warehouse.id, &product_a).await?;
|
|
|
-// 48.000 units remaining
|
|
|
-
|
|
|
-let bank_balance = ledger.balance(&bank.id, &gs).await?;
|
|
|
-// 30,000 Gs
|
|
|
-
|
|
|
-let total_revenue = ledger.balance(&revenue.id, &gs).await?;
|
|
|
-// 30,000 Gs
|
|
|
-
|
|
|
-let total_cogs = ledger.balance(&cogs.id, &gs).await?;
|
|
|
-// 20,000 Gs gross profit = revenue - cogs = 10,000 Gs
|
|
|
-```
|
|
|
-
|
|
|
-**Why books matter here:** The `sales` book prevents a bug where a bank
|
|
|
-transfer accidentally credits the revenue account. The `banking` book ensures
|
|
|
-only cash and bank accounts participate in deposits. Each flow is isolated by
|
|
|
-scope while sharing the same global balances.
|
|
|
+The worked examples moved to their own section: [overdraft](examples/overdraft.md),
|
|
|
+[currency exchange](examples/exchange.md), [retail point of sale](examples/retail.md),
|
|
|
+and the [runnable programs](examples/runnable.md).
|
|
|
|
|
|
---
|
|
|
|