balance

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const IndexCreatedKey = "balance_index_created"

IndexCreatedKey is the key used to store the index creation flag.

View Source
const InverseBalanceObjectType = "inverse_balance"

InverseBalanceObjectType is designed for indexing the inverse balance values to retrieve a list of token owners.

Variables

View Source
var (
	ErrAmountMustBeNonNegative = errors.New("amount must be non-negative")
	ErrInsufficientBalance     = errors.New("insufficient balance")
)

Error definitions for balance operations.

View Source
var ErrAddressMustNotBeEmpty = errors.New("address must not be empty")

Functions

func Add

func Add(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
	address string,
	token string,
	amount *big.Int,
) error

Add adds the given amount to the balance for the specified address and token, if the amount is greater than zero.

Parameters:

  • stub: shim.ChaincodeStubInterface - The chaincode stub interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance to update, which determines the state key's prefix.
  • address: string - The address associated with the balance.
  • token: string - The token identifier. If empty, the balance associated with the address alone is updated.
  • amount: *big.Int - The amount to add to the balance associated with the address and token.

Returns:

  • error - An error if the addition fails, otherwise nil.

func CreateIndex

func CreateIndex(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
) error

CreateIndex builds an index for states matching the specified balance type.

Parameters:

  • stub: shim.ChaincodeStubInterface - The chaincode stub interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance for which the index is being created.

Returns:

  • err: error - An error if the index creation fails, otherwise nil.

func Get

func Get(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
	address string,
	token string,
) (*big.Int, error)

Get retrieves the balance value for the given address and token, constructing the appropriate composite key.

Parameters:

  • stub: shim.ChaincodeStubInterface - The chaincode stub interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance to retrieve, which determines the state key's prefix.
  • address: string - The address associated with the balance.
  • token: string - The token identifier. If empty, the balance associated with the address alone is retrieved.

Returns:

  • *big.Int - The balance value associated with the composite key.
  • error - An error if the retrieval fails, otherwise nil.

func HasIndexCreatedFlag

func HasIndexCreatedFlag(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
) (bool, error)

HasIndexCreatedFlag checks if the given balance type has an index.

Parameters:

  • stub: shim.ChaincodeStubInterface
  • balanceType: BalanceType

Returns:

  • bool: true if index exists, false otherwise
  • error: error if any

func Move

func Move(
	stub shim.ChaincodeStubInterface,
	sourceBalanceType BalanceType,
	sourceAddress string,
	destBalanceType BalanceType,
	destAddress string,
	token string,
	amount *big.Int,
) error

Move moves the given amount from the balance of one address and balance type to the balance of another address and balance type.

Parameters:

  • stub: shim.ChaincodeStubInterface - The chaincode stub interface for accessing ledger operations.
  • sourceBalanceType: BalanceType - The type of balance from which the amount will be subtracted.
  • sourceAddress: string - The address from which the amount will be subtracted.
  • destBalanceType: BalanceType - The type of balance to which the amount will be added.
  • destAddress: string - The address to which the amount will be added.
  • token: string - The token identifier. If empty, the operation is performed on the balances associated with the addresses alone.
  • amount: *big.Int - The amount to transfer from the source balance and address to the destination balance and address.

Returns:

  • error - An error if the transfer fails, otherwise nil.

func Put

func Put(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
	address string,
	token string,
	value *big.Int,
) error

Put stores the balance for a given address and token into the ledger.

Parameters:

  • stub: shim.ChaincodeStubInterface - The chaincode stub interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance to store, which determines the state key's prefix.
  • address: string - The address associated with the balance.
  • token: string - The token identifier. If empty, the balance associated with the address alone is stored.
  • value: *big.Int - The balance value to store associated with the address and token.

Returns:

  • error - An error if the storage fails, otherwise nil.

func Sub

func Sub(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
	address string,
	token string,
	amount *big.Int,
) error

Sub subtracts the given amount from the balance for the specified address and token, if the amount is greater than zero.

Parameters:

  • stub: shim.ChaincodeStubInterface - The chaincode stub interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance to update, which determines the state key's prefix.
  • address: string - The address associated with the balance.
  • token: string - The token identifier. If empty, the balance associated with the address alone is updated.
  • amount: *big.Int - The amount to subtract from the balance associated with the address and token.

Returns:

  • error - An error if the subtraction fails, otherwise nil.

Types

type BalanceType

type BalanceType byte

BalanceType represents different types of balance-related state keys in the ledger.

const (
	BalanceTypeToken                 BalanceType = 0x2b
	BalanceTypeTokenLocked           BalanceType = 0x2e
	BalanceTypeTokenExternalLocked   BalanceType = 0x32
	BalanceTypeAllowed               BalanceType = 0x2c
	BalanceTypeAllowedLocked         BalanceType = 0x2f
	BalanceTypeAllowedExternalLocked BalanceType = 0x31
	BalanceTypeGiven                 BalanceType = 0x2d
)

Constants for different BalanceType values representing various balance state keys.

func StringToBalanceType

func StringToBalanceType(s string) (BalanceType, error)

StringToBalanceType converts a string representation of a balance state key to its corresponding BalanceType.

func (BalanceType) String

func (ot BalanceType) String() string

String returns the hexadecimal string representation of the BalanceType.

type TokenBalance

type TokenBalance struct {
	Address string
	Token   string
	Balance *big.Int
}

TokenBalance represents a balance entry with a token identifier and its associated value.

func ListBalancesByAddress

func ListBalancesByAddress(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
	address string,
) ([]TokenBalance, error)

ListBalancesByAddress fetches all balance entries associated with the given address.

Parameters:

  • stub: shim.ChaincodeStubInterface - Interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance to retrieve, determining the state key's prefix.
  • address: string - The address whose balances are to be fetched.

Returns:

  • []TokenBalance - A slice of TokenBalance structs representing all balances associated with the address.
  • error - An error if the retrieval fails, otherwise nil.

func ListOwnersByToken

func ListOwnersByToken(
	stub shim.ChaincodeStubInterface,
	balanceType BalanceType,
	token string,
) ([]TokenBalance, error)

ListOwnersByToken fetches all owners and their balances for a specific token.

Parameters:

  • stub: shim.ChaincodeStubInterface - Interface for accessing ledger operations.
  • balanceType: BalanceType - The type of balance to retrieve, determining the state key's prefix.
  • token: string - The token identifier whose owners are to be fetched.

Returns:

  • []TokenBalance - A slice of TokenBalance structs representing all owners and their balances for the token.
  • error - An error if the retrieval fails, otherwise nil.

Jump to

Keyboard shortcuts

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