pub struct WebLedger {
pub context: String,
pub ledger_type: String,
pub name: String,
pub description: String,
pub default_currency: String,
pub created: u64,
pub updated: u64,
/* private fields */
}Expand description
The full Web Ledger document at /.well-known/webledgers/webledgers.json.
Balances change only through receipt-keyed operations:
credit_by_outpoint (the outpoint is the
receipt) and debit_by_payout (the payout id is
the receipt), as in solidpayorg/teller. reverse_payout,
charge and refund serve the pod’s
broadcast recovery and access fees; none of them can raise a balance
beyond what a recorded deposit put there.
A ledger built with with_genesis carries teller’s
identity, id = urn:webledgers:<sha256(JCS(genesis))>; documents written
before the genesis existed still load, without one.
The receipt-less mutators are crate-private. Outside this crate none of these compile:
let mut ledger = solid_pod_rs::payments::WebLedger::new("Pod Credits");
ledger.credit("did:nostr:aaaa", 1_000);let mut ledger = solid_pod_rs::payments::WebLedger::new("Pod Credits");
let _ = ledger.debit("did:nostr:aaaa", 1);let mut ledger = solid_pod_rs::payments::WebLedger::new("Pod Credits");
ledger.credit_currency("did:nostr:aaaa", "PODS", 1_000);let mut ledger = solid_pod_rs::payments::WebLedger::new("Pod Credits");
ledger.entries.clear();Fields§
§context: String§ledger_type: String§name: String§description: String§default_currency: String§created: u64§updated: u64Implementations§
Source§impl WebLedger
impl WebLedger
Sourcepub fn with_genesis(genesis: LedgerGenesis) -> Result<Self, PaymentError>
pub fn with_genesis(genesis: LedgerGenesis) -> Result<Self, PaymentError>
A new, empty ledger born with genesis (teller newLedger): its
hash is sha256(JCS(genesis)), its id urn:webledgers:<hash>,
its name and defaultCurrency those of the genesis.
Sourcepub fn hash(&self) -> Option<&str>
pub fn hash(&self) -> Option<&str>
The ledger hash recorded in the document, when it has a genesis.
Sourcepub fn genesis(&self) -> Option<&LedgerGenesis>
pub fn genesis(&self) -> Option<&LedgerGenesis>
The genesis the ledger was born with, if any.
Sourcepub fn ledger_hash(&self) -> Option<String>
pub fn ledger_hash(&self) -> Option<String>
The ledger hash recomputed from the genesis (teller ledgerHash).
Sourcepub fn check_genesis(&self) -> Result<(), PaymentError>
pub fn check_genesis(&self) -> Result<(), PaymentError>
Teller checkLedger: a document with a genesis must record the hash
of that genesis. Documents without a genesis (written before it
existed) pass; a genesis without a matching hash does not.
Sourcepub fn entries(&self) -> &[LedgerEntry]
pub fn entries(&self) -> &[LedgerEntry]
The balance entries, one per account.
Sourcepub fn deposits(&self) -> &[DepositReceipt]
pub fn deposits(&self) -> &[DepositReceipt]
Every deposit credited, by outpoint.
Sourcepub fn payouts(&self) -> &[PayoutReceipt]
pub fn payouts(&self) -> &[PayoutReceipt]
Every payout debited, by payout id.
Sourcepub fn has_deposit(&self, txid: &str, vout: u32) -> bool
pub fn has_deposit(&self, txid: &str, vout: u32) -> bool
Whether outpoint (<txid>:<vout>) has already credited an account.
Sourcepub fn is_sats_unit(&self, currency: &str) -> bool
pub fn is_sats_unit(&self, currency: &str) -> bool
Whether currency names this ledger’s satoshi balance: satoshi,
sat, or the ledger’s own defaultCurrency.
Sourcepub fn get_balance(&self, did: &str) -> u64
pub fn get_balance(&self, did: &str) -> u64
The satoshi balance of did (exact key match).
Sourcepub fn credit_by_outpoint(
&mut self,
account: &str,
currency: &str,
txid: &str,
vout: u32,
value: u64,
) -> Result<ReceiptOutcome, PaymentError>
pub fn credit_by_outpoint( &mut self, account: &str, currency: &str, txid: &str, vout: u32, value: u64, ) -> Result<ReceiptOutcome, PaymentError>
Credit a deposit seen on-chain, once: the outpoint is the receipt
(solidpayorg/teller credit, lib/teller.mjs:40-44).
currency is the unit credited: the ledger’s satoshi balance when
is_sats_unit holds, otherwise that unit’s own
balance (an MRC20 deposit credits its ticker, never sats). account
is normalised as teller reads it (did:nostr:<x>, a bare x, or a
Multikey’s x). The txid must be 64 lowercase hex digits and value a
whole number no greater than 21 million coins in satoshis.
A second credit for an outpoint already recorded changes nothing and
returns ReceiptOutcome::AlreadyApplied, whatever its account or
unit.
use solid_pod_rs::payments::{ReceiptOutcome, WebLedger};
let alice = format!("did:nostr:{}", "a".repeat(64));
let txid = "ab".repeat(32);
let mut ledger = WebLedger::new("Pod Credits");
assert_eq!(
ledger.credit_by_outpoint(&alice, "satoshi", &txid, 0, 50_000).unwrap(),
ReceiptOutcome::Applied
);
assert_eq!(
ledger.credit_by_outpoint(&alice, "satoshi", &txid, 0, 50_000).unwrap(),
ReceiptOutcome::AlreadyApplied
);
assert_eq!(ledger.get_balance(&alice), 50_000);Sourcepub fn debit_by_payout(
&mut self,
account: &str,
amount: u64,
txid: &str,
) -> Result<ReceiptOutcome, PaymentError>
pub fn debit_by_payout( &mut self, account: &str, amount: u64, txid: &str, ) -> Result<ReceiptOutcome, PaymentError>
Debit a withdrawal of amount satoshis, once: the payout’s txid is
the receipt (solidpayorg/teller debit, lib/teller.mjs:53-58).
Fails with PaymentError::InsufficientBalance when the account
holds less than amount. A second debit with a payout id already
applied changes nothing and returns
ReceiptOutcome::AlreadyApplied. Unlike teller, no 546-sat
minimum applies here: the pod’s payouts include token transfers
whose satoshi cost is not itself an on-chain output.
Sourcepub fn reverse_payout(&mut self, txid: &str) -> Option<u64>
pub fn reverse_payout(&mut self, txid: &str) -> Option<u64>
Undo a recorded payout whose transaction never reached the chain:
its amount goes back to its account and its receipt is removed.
Returns the amount restored, or None when no payout with that id is
recorded (nothing changes).
Sourcepub fn charge(&mut self, did: &str, amount: u64) -> Result<Charge, PaymentError>
pub fn charge(&mut self, did: &str, amount: u64) -> Result<Charge, PaymentError>
Spend amount satoshis from did for an access fee (a paid request
or an anchor). Fails closed with
PaymentError::InsufficientBalance; the returned Charge is the
only way to refund it.
Source§impl WebLedger
Extension methods on WebLedger for currency-specific operations.
impl WebLedger
Extension methods on WebLedger for currency-specific operations.
The base WebLedger balance (get_balance) is the default satoshi
balance. Trading requires per-currency balances, so these helpers work
through the LedgerAmount::Multi representation. The credit and debit
helpers are crate-private: outside this crate a balance is raised only
by WebLedger::credit_by_outpoint, which records the deposit’s
outpoint as its receipt.
Sourcepub fn get_currency_balance(&self, did: &str, currency: &str) -> u64
pub fn get_currency_balance(&self, did: &str, currency: &str) -> u64
Get a DID’s balance in a specific currency.