accounts

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 8 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 Account added in v0.2.0

type Account struct {
	ID         rhp3.Account   `json:"id"`
	Balance    types.Currency `json:"balance"`
	Expiration time.Time      `json:"expiration"`
}

An Account holds the balance and expiration of an ephemeral account.

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) AccountFunding added in v0.2.0

func (am *AccountManager) AccountFunding(account rhp3.Account) (srcs []FundingSource, err error)

AccountFunding returns the remaining funding sources for an account.

func (*AccountManager) Accounts added in v0.2.0

func (am *AccountManager) Accounts(limit, offset int) (acc []Account, err error)

Accounts returns a list of active ephemeral accounts

func (*AccountManager) Balance

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

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

func (*AccountManager) Budget

func (am *AccountManager) Budget(accountID rhp3.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(req FundAccountWithContract, 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 {
	// AccountFunding returns the remaining funding sources for an account.
	AccountFunding(accountID rhp3.Account) ([]FundingSource, error)
	// Accounts returns a list of active ephemeral accounts
	Accounts(limit, offset int) ([]Account, error)
	// AccountBalance returns the balance of the account with the given ID.
	AccountBalance(accountID rhp3.Account) (types.Currency, error)
	// CreditAccountWithContract adds the specified amount to the account with the given ID.
	CreditAccountWithContract(FundAccountWithContract) (types.Currency, error)
	// DebitAccount subtracts the specified amount from the account with the given
	// ID. Returns the remaining balance of the account.
	DebitAccount(accountID rhp3.Account, usage Usage) (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) Refund

func (b *Budget) Refund(usage Usage)

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(usage Usage) error

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

type FundAccountWithContract added in v0.2.0

type FundAccountWithContract struct {
	Account    rhp3.Account
	Cost       types.Currency
	Amount     types.Currency
	Revision   contracts.SignedRevision
	Expiration time.Time
}

FundAccountWithContract is a helper struct for funding an account with a contract.

type FundingSource added in v0.2.0

type FundingSource struct {
	ContractID types.FileContractID `json:"contractID"`
	AccountID  rhp3.Account         `json:"accountID"`
	Amount     types.Currency       `json:"amount"`
}

FundingSource tracks a funding source for an account.

type Settings

type Settings interface {
	Settings() settings.Settings
}

Settings returns the host's current settings.

type Usage added in v0.2.0

type Usage struct {
	RPCRevenue     types.Currency `json:"rpc"`
	StorageRevenue types.Currency `json:"storage"`
	EgressRevenue  types.Currency `json:"egress"`
	IngressRevenue types.Currency `json:"ingress"`
	RegistryRead   types.Currency `json:"registryRead"`
	RegistryWrite  types.Currency `json:"registryWrite"`
}

Usage tracks the usage of an account's funds.

func (Usage) Add added in v0.2.0

func (a Usage) Add(b Usage) Usage

Add returns the sum of two Usages.

func (Usage) Sub added in v0.2.0

func (a Usage) Sub(b Usage) Usage

Sub returns the difference of two Usages.

func (Usage) Total added in v0.2.0

func (u Usage) Total() types.Currency

Total returns the total of all revenue types.

Jump to

Keyboard shortcuts

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