مستندات دارا
مرجع فنیمالی
این صفحه فقط به انگلیسی موجود است.

Ledger model

The financial ledger is double-entry, append-only and the source of truth for what customers own; the chain is reconciled against it. Implementation: backend/internal/ledger, migration 00003_ledger.sql (ported verbatim from the Ethereum sibling) with the cold Treasury kinds of 00006.

1. Rules

  • Every posting is one ledger transaction with two or more entries; per instrument, debits = credits (checked in code and by a deferred database constraint).
  • Entries are never updated or deleted (database triggers). Corrections are reversals of whole transactions, at most once (Reverse).
  • A transaction is unique per (source_type, source_id, purpose): a retried business step cannot post twice.
  • Accounts marked non-negative can never go below zero (checked on the balance projection in the same database transaction); an attempt fails with a typed error (insufficient_funds, insufficient_tokens, insufficient_treasury_inventory).
  • Which module may touch which account is fixed in code (allowedKinds): only supply operations and clawbacks move TOKEN_SUPPLY; only cash operations touch the bank and adjustment accounts; customer trading never reaches COLD.

2. Chart of accounts

KindInstrumentNormalNon-negativeMeaning
CUST_CASH_AVAILABLEIRRCredityesCustomer cash the customer can spend
CUST_CASH_HELDIRRCredityesCustomer cash reserved by an open BUY or withdrawal
CUSTOMER_FUNDS_BANKIRRDebityesCustomer money held at the bank
TREASURY_TRADINGIRRCreditnoPlatform's trading result in cash
FEE_REVENUEIRRCreditnoFees earned
CASH_ADJUSTMENTSIRRDebitnoApproved manual adjustments
CUST_TOKEN_AVAILABLEassetDebityesCustomer tokens free to sell or transfer
CUST_TOKEN_HELDassetDebityesCustomer tokens held by an open SELL, TRANSFER or clawback
TREASURY_AVAILABLEassetDebityesHot Treasury float free for sales
TREASURY_RESERVEDassetDebityesHot float reserved by accepted BUYs and sweeps
TREASURY_COLD_AVAILABLEassetDebityesCold Treasury inventory
TREASURY_COLD_RESERVEDassetDebityesCold inventory reserved by approved burns and refills
TOKEN_SUPPLYassetCredityesTokens in existence (issued − burned − clawed back)

3. Postings

Every money-moving flow has a hold at acceptance and a settlement when the Stellar transaction is applied in a closed ledger (final).

Cash accounts are credit-normal, so a debit decreases them; token and Treasury accounts are debit-normal.

FlowHold (at acceptance)Settle (transaction applied)
BUYDr CUST_CASH_AVAILABLE / Cr CUST_CASH_HELD (total); Dr TREASURY_RESERVED / Cr TREASURY_AVAILABLE (qty)Dr CUST_TOKEN_AVAILABLE / Cr TREASURY_RESERVED (qty); Dr CUST_CASH_HELD (total) / Cr TREASURY_TRADING (gross) + Cr FEE_REVENUE (fee)
SELLDr CUST_TOKEN_HELD / Cr CUST_TOKEN_AVAILABLE (qty)Dr TREASURY_AVAILABLE / Cr CUST_TOKEN_HELD (qty); Dr TREASURY_TRADING (gross) / Cr CUST_CASH_AVAILABLE (net) + Cr FEE_REVENUE (fee)
TRANSFERDr CUST_TOKEN_HELD / Cr CUST_TOKEN_AVAILABLE (sender)Dr CUST_TOKEN_AVAILABLE (recipient) / Cr CUST_TOKEN_HELD (sender)
ISSUE—Dr TREASURY_COLD_AVAILABLE / Cr TOKEN_SUPPLY
BURNDr TREASURY_COLD_RESERVED / Cr TREASURY_COLD_AVAILABLEDr TOKEN_SUPPLY / Cr TREASURY_COLD_RESERVED
REFILL (COLD → HOT)as BURNDr TREASURY_AVAILABLE / Cr TREASURY_COLD_RESERVED
SWEEP (HOT → COLD)Dr TREASURY_RESERVED / Cr TREASURY_AVAILABLEDr TREASURY_COLD_AVAILABLE / Cr TREASURY_RESERVED
CLAWBACKDr CUST_TOKEN_HELD / Cr CUST_TOKEN_AVAILABLEDr TOKEN_SUPPLY / Cr CUST_TOKEN_HELD
DEPOSIT (second approval)—Dr CUSTOMER_FUNDS_BANK / Cr CUST_CASH_AVAILABLE
WITHDRAWALDr CUST_CASH_AVAILABLE / Cr CUST_CASH_HELDPaid: Dr CUST_CASH_HELD / Cr CUSTOMER_FUNDS_BANK
ADJUSTMENT—Dr CASH_ADJUSTMENTS or FEE_REVENUE / Cr CUST_CASH_AVAILABLE (credit), or the reverse (debit)

A failed transaction reverses its hold exactly once.

The worked examples with exact amounts are TestLedgerModelWorkedExample (ledger) and TestLedgerModelWorkedExamplePrices (money); the demo's BUY/SELL totals and fee revenue are checked to the rial.

4. Relation to the chain

LedgerOn-chain
TREASURY_COLD_AVAILABLE + TREASURY_COLD_RESERVEDCOLD trustline balance
TREASURY_AVAILABLE + TREASURY_RESERVEDHOT trustline balance
CUST_TOKEN_AVAILABLE + CUST_TOKEN_HELD (per customer)That customer's trustline balance
TOKEN_SUPPLYSum of every holding of the asset (all trustlines, claimable balances, pools, contract balances)

Reconciliation checks each line (RECONCILIATION_RUNBOOK.md), skipping a balance while an intent that touches it is between submission and a closed ledger.

در این صفحه