Documentation
¶
Overview ¶
Package ledger tracks agent balances on the platform.
Flow:
- Agent deposits USDC to platform address
- Platform credits agent's balance
- Agent spends via session keys (debits balance)
- Agent withdraws (platform sends USDC)
Index ¶
- Variables
- type Balance
- type DepositRequest
- type Entry
- type Handler
- type Ledger
- func (l *Ledger) CanSpend(ctx context.Context, agentAddr, amount string) (bool, error)
- func (l *Ledger) ConfirmHold(ctx context.Context, agentAddr, amount, reference string) error
- func (l *Ledger) Deposit(ctx context.Context, agentAddr, amount, txHash string) error
- func (l *Ledger) EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
- func (l *Ledger) GetBalance(ctx context.Context, agentAddr string) (*Balance, error)
- func (l *Ledger) GetCreditInfo(ctx context.Context, agentAddr string) (creditLimit, creditUsed string, err error)
- func (l *Ledger) GetHistory(ctx context.Context, agentAddr string, limit int) ([]*Entry, error)
- func (l *Ledger) Hold(ctx context.Context, agentAddr, amount, reference string) error
- func (l *Ledger) Refund(ctx context.Context, agentAddr, amount, reference string) error
- func (l *Ledger) RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
- func (l *Ledger) ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
- func (l *Ledger) ReleaseHold(ctx context.Context, agentAddr, amount, reference string) error
- func (l *Ledger) RepayCredit(ctx context.Context, agentAddr, amount string) error
- func (l *Ledger) SetCreditLimit(ctx context.Context, agentAddr, limit string) error
- func (l *Ledger) Spend(ctx context.Context, agentAddr, amount, sessionKeyID string) error
- func (l *Ledger) Withdraw(ctx context.Context, agentAddr, amount, txHash string) error
- type MemoryStore
- func (m *MemoryStore) ConfirmHold(ctx context.Context, agentAddr, amount, reference string) error
- func (m *MemoryStore) Credit(ctx context.Context, agentAddr, amount, txHash, description string) error
- func (m *MemoryStore) Debit(ctx context.Context, agentAddr, amount, reference, description string) error
- func (m *MemoryStore) EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
- func (m *MemoryStore) GetBalance(ctx context.Context, agentAddr string) (*Balance, error)
- func (m *MemoryStore) GetCreditInfo(ctx context.Context, agentAddr string) (string, string, error)
- func (m *MemoryStore) GetHistory(ctx context.Context, agentAddr string, limit int) ([]*Entry, error)
- func (m *MemoryStore) HasDeposit(ctx context.Context, txHash string) (bool, error)
- func (m *MemoryStore) Hold(ctx context.Context, agentAddr, amount, reference string) error
- func (m *MemoryStore) Refund(ctx context.Context, agentAddr, amount, reference, description string) error
- func (m *MemoryStore) RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
- func (m *MemoryStore) ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
- func (m *MemoryStore) ReleaseHold(ctx context.Context, agentAddr, amount, reference string) error
- func (m *MemoryStore) RepayCredit(ctx context.Context, agentAddr, amount string) error
- func (m *MemoryStore) SetCreditLimit(ctx context.Context, agentAddr, limit string) error
- func (m *MemoryStore) UseCredit(ctx context.Context, agentAddr, amount string) error
- func (m *MemoryStore) Withdraw(ctx context.Context, agentAddr, amount, txHash string) error
- type PostgresStore
- func (p *PostgresStore) ConfirmHold(ctx context.Context, agentAddr, amount, reference string) error
- func (p *PostgresStore) Credit(ctx context.Context, agentAddr, amount, txHash, description string) error
- func (p *PostgresStore) Debit(ctx context.Context, agentAddr, amount, reference, description string) error
- func (p *PostgresStore) EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
- func (p *PostgresStore) GetBalance(ctx context.Context, agentAddr string) (*Balance, error)
- func (p *PostgresStore) GetCreditInfo(ctx context.Context, agentAddr string) (string, string, error)
- func (p *PostgresStore) GetHistory(ctx context.Context, agentAddr string, limit int) ([]*Entry, error)
- func (p *PostgresStore) HasDeposit(ctx context.Context, txHash string) (bool, error)
- func (p *PostgresStore) Hold(ctx context.Context, agentAddr, amount, reference string) error
- func (p *PostgresStore) Migrate(ctx context.Context) error
- func (p *PostgresStore) Refund(ctx context.Context, agentAddr, amount, reference, description string) error
- func (p *PostgresStore) RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
- func (p *PostgresStore) ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
- func (p *PostgresStore) ReleaseHold(ctx context.Context, agentAddr, amount, reference string) error
- func (p *PostgresStore) RepayCredit(ctx context.Context, agentAddr, amount string) error
- func (p *PostgresStore) SetCreditLimit(ctx context.Context, agentAddr, limit string) error
- func (p *PostgresStore) UseCredit(ctx context.Context, agentAddr, amount string) error
- func (p *PostgresStore) Withdraw(ctx context.Context, agentAddr, amount, txHash string) error
- type Store
- type WithdrawRequest
- type WithdrawalExecutor
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Balance ¶
type Balance struct {
AgentAddr string `json:"agentAddr"`
Available string `json:"available"` // Can be spent
Pending string `json:"pending"` // Deposits awaiting confirmation
Escrowed string `json:"escrowed"` // Locked in escrow awaiting service delivery
CreditLimit string `json:"creditLimit"` // Maximum credit available
CreditUsed string `json:"creditUsed"` // Current credit drawn
TotalIn string `json:"totalIn"` // Lifetime deposits
TotalOut string `json:"totalOut"` // Lifetime withdrawals + spending
UpdatedAt time.Time `json:"updatedAt"`
}
Balance represents an agent's balance
type DepositRequest ¶
type DepositRequest struct {
AgentAddress string `json:"agentAddress" binding:"required"`
Amount string `json:"amount" binding:"required"`
TxHash string `json:"txHash" binding:"required"`
}
DepositRequest for manual deposit recording (admin use)
type Entry ¶
type Entry struct {
ID string `json:"id"`
AgentAddr string `json:"agentAddr"`
Type string `json:"type"` // deposit, withdrawal, spend, refund
Amount string `json:"amount"`
TxHash string `json:"txHash,omitempty"`
Reference string `json:"reference,omitempty"` // session key ID, service ID, etc.
Description string `json:"description,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
Entry represents a ledger entry
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides HTTP endpoints for ledger operations
func NewHandlerWithWithdrawals ¶
func NewHandlerWithWithdrawals(ledger *Ledger, executor WithdrawalExecutor) *Handler
NewHandlerWithWithdrawals creates a handler that can execute withdrawals
func (*Handler) GetBalance ¶
GetBalance handles GET /agents/:address/balance
func (*Handler) GetHistory ¶
GetHistory handles GET /agents/:address/ledger
func (*Handler) RecordDeposit ¶
RecordDeposit handles POST /admin/deposits (for manual/webhook deposit recording)
func (*Handler) RegisterRoutes ¶
func (h *Handler) RegisterRoutes(r *gin.RouterGroup)
RegisterRoutes sets up ledger routes
func (*Handler) RequestWithdrawal ¶
RequestWithdrawal handles POST /agents/:address/withdraw
type Ledger ¶
type Ledger struct {
// contains filtered or unexported fields
}
Ledger manages agent balances
func (*Ledger) ConfirmHold ¶
ConfirmHold finalizes a held amount after on-chain confirmation. Moves funds from pending → total_out.
func (*Ledger) EscrowLock ¶
EscrowLock locks funds in escrow before service delivery. Moves funds from available → escrowed.
func (*Ledger) GetBalance ¶
GetBalance returns an agent's current balance
func (*Ledger) GetCreditInfo ¶
func (l *Ledger) GetCreditInfo(ctx context.Context, agentAddr string) (creditLimit, creditUsed string, err error)
GetCreditInfo returns the current credit limit and usage
func (*Ledger) GetHistory ¶
GetHistory returns ledger entries for an agent
func (*Ledger) Hold ¶
Hold places a hold on funds before an on-chain transfer. Moves funds from available → pending so they can't be double-spent.
func (*Ledger) Refund ¶
Refund credits back an agent's balance (used when a transfer fails after debit)
func (*Ledger) RefundEscrow ¶
RefundEscrow returns escrowed funds to the buyer after a dispute. Moves from escrowed → available.
func (*Ledger) ReleaseEscrow ¶
func (l *Ledger) ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
ReleaseEscrow releases escrowed funds to the seller after confirmation. Moves from buyer's escrowed → seller's available.
func (*Ledger) ReleaseHold ¶
ReleaseHold returns held funds to available when a transfer fails. Moves funds from pending → available.
func (*Ledger) RepayCredit ¶
RepayCredit reduces outstanding credit usage
func (*Ledger) SetCreditLimit ¶
SetCreditLimit sets the maximum credit for an agent
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an in-memory ledger store for demo/development mode.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates a new in-memory ledger store.
func (*MemoryStore) ConfirmHold ¶
func (m *MemoryStore) ConfirmHold(ctx context.Context, agentAddr, amount, reference string) error
func (*MemoryStore) Credit ¶
func (m *MemoryStore) Credit(ctx context.Context, agentAddr, amount, txHash, description string) error
func (*MemoryStore) Debit ¶
func (m *MemoryStore) Debit(ctx context.Context, agentAddr, amount, reference, description string) error
func (*MemoryStore) EscrowLock ¶
func (m *MemoryStore) EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
func (*MemoryStore) GetBalance ¶
func (*MemoryStore) GetCreditInfo ¶
func (*MemoryStore) GetHistory ¶
func (*MemoryStore) HasDeposit ¶
func (*MemoryStore) Hold ¶
func (m *MemoryStore) Hold(ctx context.Context, agentAddr, amount, reference string) error
func (*MemoryStore) Refund ¶
func (m *MemoryStore) Refund(ctx context.Context, agentAddr, amount, reference, description string) error
func (*MemoryStore) RefundEscrow ¶
func (m *MemoryStore) RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
func (*MemoryStore) ReleaseEscrow ¶
func (m *MemoryStore) ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
func (*MemoryStore) ReleaseHold ¶
func (m *MemoryStore) ReleaseHold(ctx context.Context, agentAddr, amount, reference string) error
func (*MemoryStore) RepayCredit ¶
func (m *MemoryStore) RepayCredit(ctx context.Context, agentAddr, amount string) error
func (*MemoryStore) SetCreditLimit ¶
func (m *MemoryStore) SetCreditLimit(ctx context.Context, agentAddr, limit string) error
type PostgresStore ¶
type PostgresStore struct {
// contains filtered or unexported fields
}
PostgresStore implements Store with PostgreSQL
func NewPostgresStore ¶
func NewPostgresStore(db *sql.DB) *PostgresStore
NewPostgresStore creates a new PostgreSQL-backed ledger store
func (*PostgresStore) ConfirmHold ¶
func (p *PostgresStore) ConfirmHold(ctx context.Context, agentAddr, amount, reference string) error
ConfirmHold finalizes a held amount (moves from pending to total_out). Called after on-chain transfer is confirmed.
func (*PostgresStore) Credit ¶
func (p *PostgresStore) Credit(ctx context.Context, agentAddr, amount, txHash, description string) error
Credit adds funds to an agent's balance, auto-repaying credit first
func (*PostgresStore) Debit ¶
func (p *PostgresStore) Debit(ctx context.Context, agentAddr, amount, reference, description string) error
Debit removes funds from an agent's balance with credit support. Uses available balance first, then draws from credit for any shortfall.
func (*PostgresStore) EscrowLock ¶
func (p *PostgresStore) EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
EscrowLock moves funds from available to escrowed.
func (*PostgresStore) GetBalance ¶
GetBalance retrieves an agent's balance
func (*PostgresStore) GetCreditInfo ¶
func (p *PostgresStore) GetCreditInfo(ctx context.Context, agentAddr string) (string, string, error)
GetCreditInfo returns the current credit limit and usage
func (*PostgresStore) GetHistory ¶
func (p *PostgresStore) GetHistory(ctx context.Context, agentAddr string, limit int) ([]*Entry, error)
GetHistory retrieves ledger entries for an agent
func (*PostgresStore) HasDeposit ¶
HasDeposit checks if a deposit tx has already been processed
func (*PostgresStore) Hold ¶
func (p *PostgresStore) Hold(ctx context.Context, agentAddr, amount, reference string) error
Hold places a hold on funds (moves from available to pending) with credit support. If available < amount, draws the shortfall from credit line.
func (*PostgresStore) Migrate ¶
func (p *PostgresStore) Migrate(ctx context.Context) error
Migrate creates the ledger tables with NUMERIC columns
func (*PostgresStore) Refund ¶
func (p *PostgresStore) Refund(ctx context.Context, agentAddr, amount, reference, description string) error
Refund credits back funds to an agent's balance (reverses a failed debit)
func (*PostgresStore) RefundEscrow ¶
func (p *PostgresStore) RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
RefundEscrow returns escrowed funds to available (dispute refund).
func (*PostgresStore) ReleaseEscrow ¶
func (p *PostgresStore) ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
ReleaseEscrow moves funds from buyer's escrowed to seller's available.
func (*PostgresStore) ReleaseHold ¶
func (p *PostgresStore) ReleaseHold(ctx context.Context, agentAddr, amount, reference string) error
ReleaseHold returns held funds to available (transfer failed/timed out).
func (*PostgresStore) RepayCredit ¶
func (p *PostgresStore) RepayCredit(ctx context.Context, agentAddr, amount string) error
RepayCredit reduces outstanding credit usage
func (*PostgresStore) SetCreditLimit ¶
func (p *PostgresStore) SetCreditLimit(ctx context.Context, agentAddr, limit string) error
SetCreditLimit sets the maximum credit for an agent
type Store ¶
type Store interface {
GetBalance(ctx context.Context, agentAddr string) (*Balance, error)
Credit(ctx context.Context, agentAddr, amount, txHash, description string) error
Debit(ctx context.Context, agentAddr, amount, reference, description string) error
Refund(ctx context.Context, agentAddr, amount, reference, description string) error
Withdraw(ctx context.Context, agentAddr, amount, txHash string) error
GetHistory(ctx context.Context, agentAddr string, limit int) ([]*Entry, error)
HasDeposit(ctx context.Context, txHash string) (bool, error)
// Two-phase hold operations for safe transaction execution.
// Hold moves funds from available → pending before on-chain transfer.
// ConfirmHold moves from pending → total_out after confirmation.
// ReleaseHold moves from pending → available if transfer fails.
Hold(ctx context.Context, agentAddr, amount, reference string) error
ConfirmHold(ctx context.Context, agentAddr, amount, reference string) error
ReleaseHold(ctx context.Context, agentAddr, amount, reference string) error
// Escrow operations for buyer-protection payments.
// EscrowLock moves funds from available → escrowed.
// ReleaseEscrow moves from buyer's escrowed → seller's available.
// RefundEscrow moves from escrowed → available (dispute refund).
EscrowLock(ctx context.Context, agentAddr, amount, reference string) error
ReleaseEscrow(ctx context.Context, buyerAddr, sellerAddr, amount, reference string) error
RefundEscrow(ctx context.Context, agentAddr, amount, reference string) error
// Credit line operations.
// SetCreditLimit sets the maximum credit for an agent.
// UseCredit draws from the agent's credit line.
// RepayCredit reduces outstanding credit usage.
// GetCreditInfo returns the current credit limit and usage.
SetCreditLimit(ctx context.Context, agentAddr, limit string) error
UseCredit(ctx context.Context, agentAddr, amount string) error
RepayCredit(ctx context.Context, agentAddr, amount string) error
GetCreditInfo(ctx context.Context, agentAddr string) (creditLimit, creditUsed string, err error)
}
Store persists ledger data
type WithdrawRequest ¶
type WithdrawRequest struct {
Amount string `json:"amount" binding:"required"`
}
WithdrawRequest for withdrawal