accounts

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInsufficientFunds is returned when an account does not have enough
	// funds to cover a debit.
	ErrInsufficientFunds = errors.New("ephemeral account balance was insufficient") // note: text is required for compatibility with siad
	// ErrBalanceExceeded is returned when an account's balance exceeds the
	// maximum balance.
	ErrBalanceExceeded = errors.New("ephemeral account maximum balance exceeded") // note: text is required for compatibility with siad
)

Functions

This section is empty.

Types

type AccountManager

type AccountManager struct {
	// contains filtered or unexported fields
}

An AccountManager manages deposits and withdrawals for accounts. It is primarily a synchronization wrapper around a store.

func NewManager

func NewManager(store AccountStore, settings Settings) *AccountManager

NewManager creates a new account manager

func (*AccountManager) Balance

func (am *AccountManager) Balance(accountID rhpv3.Account) (types.Currency, error)

Balance returns the balance of the account with the given ID.

func (*AccountManager) Budget

func (am *AccountManager) Budget(accountID rhpv3.Account, amount types.Currency) (*Budget, error)

Budget creates a new budget for an account limited by amount. The spent amount will not be synced to the underlying store until Commit is called.

func (*AccountManager) Credit

func (am *AccountManager) Credit(accountID rhpv3.Account, amount types.Currency, expiration time.Time, refund bool) (types.Currency, error)

Credit adds the specified amount to the account with the given ID. Credits are synced to the underlying store immediately.

type AccountStore

type AccountStore interface {
	// AccountBalance returns the balance of the account with the given ID.
	AccountBalance(accountID rhpv3.Account) (types.Currency, error)
	// CreditAccount adds the specified amount to the account with the given ID.
	CreditAccount(accountID rhpv3.Account, amount types.Currency, expiration time.Time) (types.Currency, error)
	// DebitAccount subtracts the specified amount from the account with the given
	// ID. Returns the remaining balance of the account.
	DebitAccount(accountID rhpv3.Account, amount types.Currency) (types.Currency, error)
}

An AccountStore stores and updates account balances.

type Budget

type Budget struct {
	// contains filtered or unexported fields
}

A Budget transactionally manages an account's balance. It is not safe for concurrent use.

func (*Budget) Commit

func (b *Budget) Commit() error

Commit commits the budget's spending to the account. If the budget has already been committed, Commit will panic.

func (*Budget) Empty

func (b *Budget) Empty() (spent types.Currency)

Empty spends all of the remaining budget and returns the amount spent

func (*Budget) Refund

func (b *Budget) Refund(amount types.Currency)

Refund returns amount back to the budget. Refund will panic if the budget has already been committed or the refund is greater than the amount spent.

func (*Budget) Remaining

func (b *Budget) Remaining() types.Currency

Remaining returns the amount remaining in the budget

func (*Budget) Rollback

func (b *Budget) Rollback() error

Rollback returns the amount spent back to the account. If the budget has already been committed, Rollback is a no-op.

func (*Budget) Spend

func (b *Budget) Spend(amount types.Currency) error

Spend subtracts amount from the remaining budget. An error is returned if their are insufficient funds.

type Settings

type Settings interface {
	Settings() settings.Settings
}

Settings returns the host's current settings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL