balance

package
v1.10.8 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 12 Imported by: 0

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.

Note: This only supports post-privacy accounts. Use CalculateFromCache instead.

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.

Note: This only supports post-privacy accounts. Use CalculateFromCache instead.

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.

func GetPrivateBalance

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

GetPrivateBalance gets an owner account's total private balance.

Note: Assumes all private accounts have the same mint

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 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 FundingFromExternalDepositsForPrePrivacy2022Accounts

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

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.

func NetBalanceFromPrePrivacy2022Intents

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

Jump to

Keyboard shortcuts

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