Documentation ¶
Index ¶
- Constants
- Variables
- func Add(stub shim.ChaincodeStubInterface, balanceType BalanceType, address string, ...) error
- func CreateIndex(stub shim.ChaincodeStubInterface, balanceType BalanceType) error
- func Get(stub shim.ChaincodeStubInterface, balanceType BalanceType, address string, ...) (*big.Int, error)
- func HasIndexCreatedFlag(stub shim.ChaincodeStubInterface, balanceType BalanceType) (bool, error)
- func Move(stub shim.ChaincodeStubInterface, sourceBalanceType BalanceType, ...) error
- func Put(stub shim.ChaincodeStubInterface, balanceType BalanceType, address string, ...) error
- func Sub(stub shim.ChaincodeStubInterface, balanceType BalanceType, address string, ...) error
- type BalanceType
- type TokenBalance
Constants ¶
const IndexCreatedKey = "balance_index_created"
IndexCreatedKey is the key used to store the index creation flag.
const InverseBalanceObjectType = "inverse_balance"
InverseBalanceObjectType is designed for indexing the inverse balance values to retrieve a list of token owners.
Variables ¶
var ( ErrAmountMustBeNonNegative = errors.New("amount must be non-negative") ErrInsufficientBalance = errors.New("insufficient balance") )
Error definitions for balance operations.
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 ¶
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.