balance

package
v1.10.21-0...-4fff869 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNegativeBalance indicates that a balance calculation resulted in a
	// negative value.
	ErrNegativeBalance = errors.New("balance calculation resulted in negative value")

	// ErrNotManagedByCode indicates that an account is not owned by Code.
	// It's up to callers to determine how to handle this situation within
	// the context of a balance.
	ErrNotManagedByCode = errors.New("explicitly not handling account not managed by code")

	// ErrUnhandledAccount indicates that the balance calculator does not
	// have strategies to handle the provided account.
	ErrUnhandledAccount = errors.New("unhandled account")
)

Functions

func BatchCalculateFromCacheWithAccountRecords added in v1.3.0

func BatchCalculateFromCacheWithAccountRecords(ctx context.Context, data code_data.Provider, accountRecordsBatch ...*common.AccountRecords) (map[string]uint64, error)

BatchCalculateFromCacheWithAccountRecords is the default and recommended batch strategy or reliably estimating a set of token accounts' balance when common.AccountRecords are available.

Note: Use this method when calculating balances for accounts that are managed by Code (ie. Timelock account) and operate within the L2 system.

func BatchCalculateFromCacheWithTokenAccounts added in v1.3.0

func BatchCalculateFromCacheWithTokenAccounts(ctx context.Context, data code_data.Provider, tokenAccounts ...*common.Account) (map[string]uint64, error)

BatchCalculateFromCacheWithTokenAccounts is the default and recommended batch strategy or reliably estimating a set of token accounts' balance when common.Account are available.

Note: Use this method when calculating balances for accounts that are managed by Code (ie. Timelock account) and operate within the L2 system.

func Calculate

func Calculate(ctx context.Context, tokenAccount *common.Account, initialBalance uint64, strategies ...Strategy) (balance uint64, err error)

Calculate calculates a token account's balance using a starting point and a set of strategies. Each may be incomplete individually, but in total must form a complete balance calculation.

func CalculateBatch

func CalculateBatch(ctx context.Context, tokenAccounts []string, strategies ...BatchStrategy) (balanceByTokenAccount map[string]uint64, err error)

CalculateBatch calculates a set of token accounts' balance using a starting point and a set of strategies. Each may be incomplete individually, but in total must form a complete balance calculation.

func CalculateFromCache added in v1.3.0

func CalculateFromCache(ctx context.Context, data code_data.Provider, tokenAccount *common.Account) (uint64, error)

CalculateFromCache is the default and recommended strategy for reliably estimating a token account's balance using cached values.

Note: Use this method when calculating balances for accounts that are managed by Code (ie. Timelock account) and operate within the L2 system.

Types

type BatchCalculator

type BatchCalculator func(ctx context.Context, data code_data.Provider, accountRecordsBatch []*common.AccountRecords) (map[string]uint64, error)

BatchCalculator is a functiona that calculates a batch of accounts' balances

type BatchState

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

type BatchStrategy

type BatchStrategy func(ctx context.Context, tokenAccounts []string, state *BatchState) (*BatchState, error)

func FundingFromExternalDepositsBatch

func FundingFromExternalDepositsBatch(ctx context.Context, data code_data.Provider) BatchStrategy

FundingFromExternalDepositsBatch is a balance calculation strategy that adds funding from deposits from external accounts.

func NetBalanceFromIntentActionsBatch

func NetBalanceFromIntentActionsBatch(ctx context.Context, data code_data.Provider) BatchStrategy

NetBalanceFromIntentActionsBatch is a balance calculation strategy that incorporates the net balance by applying payment intents to the current balance.

type Calculator

type Calculator func(ctx context.Context, data code_data.Provider, tokenAccount *common.Account) (uint64, error)

Calculator is a function that calculates a token account's balance

type OpenCloseStatusLock

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

OpenCloseStatusLock is a lock on an account's open/close status

func NewOpenCloseStatusLock

func NewOpenCloseStatusLock(vault *common.Account) *OpenCloseStatusLock

func (*OpenCloseStatusLock) OnClose

OnClose is called in the DB transaction closing the account

func (*OpenCloseStatusLock) OnPaymentToAccount

func (l *OpenCloseStatusLock) OnPaymentToAccount(ctx context.Context, data code_data.Provider) error

OnPaymentToAccount is called in the DB transaction making a payment to the account that may be closed

type OptimisticVersionLock

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

OptimisticVersionLock is an optimistic version lock on an account's cached balance, which can be paired with DB updates against balances that need to be protected against race conditions.

func GetOptimisticVersionLock

func GetOptimisticVersionLock(ctx context.Context, data code_data.Provider, vault *common.Account) (*OptimisticVersionLock, error)

GetOptimisticVersionLock gets an optimistic version lock for the vault account's cached balance

func (*OptimisticVersionLock) OnNewBalanceVersion

func (l *OptimisticVersionLock) OnNewBalanceVersion(ctx context.Context, data code_data.Provider) error

OnNewBalanceVersion is called in the DB transaction updating the account's cached balance

func (*OptimisticVersionLock) RequireSameBalanceVerion

func (l *OptimisticVersionLock) RequireSameBalanceVerion(ctx context.Context, data code_data.Provider) error

RequireSameBalanceVerion is called in the DB transaction requireing the account's cached balance not be changed

type Source added in v1.3.2

type Source uint8
const (
	UnknownSource Source = iota
	CacheSource
	BlockchainSource
)

func CalculateFromBlockchain added in v1.3.0

func CalculateFromBlockchain(ctx context.Context, data code_data.Provider, tokenAccount *common.Account) (uint64, Source, error)

CalculateFromBlockchain is the default and recommended strategy for reliably estimating a token account's balance from the blockchain. This strategy is resistant to various RPC failure nodes, and may return a cached value. The source of the balance calculation is returned.

Note: Use this method when calculating token account balances that are external and not managed by Code and outside the L2 system.

todo: add a batching variant

func (Source) String added in v1.3.2

func (s Source) String() string

type State

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

type Strategy

type Strategy func(ctx context.Context, tokenAccount *common.Account, state *State) (*State, error)

func FundingFromExternalDeposits

func FundingFromExternalDeposits(ctx context.Context, data code_data.Provider) Strategy

FundingFromExternalDeposits is a balance calculation strategy that adds funding from deposits from external accounts.

func NetBalanceFromIntentActions

func NetBalanceFromIntentActions(ctx context.Context, data code_data.Provider) Strategy

NetBalanceFromIntentActions is a balance calculation strategy that incorporates the net balance by applying payment intents to the current balance.

Jump to

Keyboard shortcuts

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