Skip to main content

SessionBilling

Tracks relay session usage and accumulates fees for GhostCloak sessions.

Source

packages/contracts/SessionBilling.compact

Ledgers

LedgerTypeDescription
session_startsMap<Bytes, Counter>Session start timestamps by user ID
session_endsMap<Bytes, Counter>Session end timestamps by user ID
accumulated_feesMap<Bytes, Counter>Total fees accumulated per user
total_sessionsCounterGlobal session count
total_fees_collectedCounterGlobal fees collected
configMap<Bytes, Counter>Configuration (key 0x01 = fee rate per second)

Circuits

initialize(fee_rate: Counter)

Sets the fee rate per second. Must be called once before any sessions.

startSession(user_id: Bytes, start_time: Counter)

Records a session start for the given user. Increments total_sessions.

endSession(user_id: Bytes, end_time: Counter, fee: Counter)

Records a session end. The fee is computed off-chain as duration × fee_rate and added to the user's accumulated_fees and the global total_fees_collected.

getAccumulatedFees(): Counter

Returns the calling user's accumulated fees.

getTotalSessions(): Counter

Returns the global session count.

getTotalFeesCollected(): Counter

Returns the total fees collected across all users.

Fee Calculation

The fee for a session is calculated off-chain:

fee = (end_time - start_time) × fee_rate_per_second

This value is passed to endSession() as a witness. The contract trusts the caller for fee computation — the ZK proof ensures the caller actually had an active session.

Usage with GhostCloak

GhostCloakClient.startSession()
→ SessionBilling.startSession(userId, now)
→ ... relay session active ...
→ SessionBilling.endSession(userId, now, computedFee)
→ GhostCloakClient.stopSession()