Skip to main content

WebLedger

Struct WebLedger 

Source
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: u64

Implementations§

Source§

impl WebLedger

Source

pub fn new(name: &str) -> Self

A new, empty ledger without a genesis (the pre-teller shape).

Source

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.

Source

pub fn id(&self) -> Option<&str>

The ledger’s id, when it has one.

Source

pub fn hash(&self) -> Option<&str>

The ledger hash recorded in the document, when it has a genesis.

Source

pub fn genesis(&self) -> Option<&LedgerGenesis>

The genesis the ledger was born with, if any.

Source

pub fn ledger_hash(&self) -> Option<String>

The ledger hash recomputed from the genesis (teller ledgerHash).

Source

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.

Source

pub fn entries(&self) -> &[LedgerEntry]

The balance entries, one per account.

Source

pub fn deposits(&self) -> &[DepositReceipt]

Every deposit credited, by outpoint.

Source

pub fn payouts(&self) -> &[PayoutReceipt]

Every payout debited, by payout id.

Source

pub fn has_deposit(&self, txid: &str, vout: u32) -> bool

Whether outpoint (<txid>:<vout>) has already credited an account.

Source

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.

Source

pub fn get_balance(&self, did: &str) -> u64

The satoshi balance of did (exact key match).

Source

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);
Source

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.

Source

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).

Source

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

pub fn refund(&mut self, charge: Charge)

Give back a Charge whose service was not delivered.

Source§

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.

Source

pub fn get_currency_balance(&self, did: &str, currency: &str) -> u64

Get a DID’s balance in a specific currency.

Trait Implementations§

Source§

impl Clone for WebLedger

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WebLedger

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for WebLedger

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for WebLedger

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more