currency_collection.rs

Overview

This file defines the SignedCurrencyCollection struct and its associated methods for managing collections of currency amounts that may include both positive and negative values. It primarily facilitates operations on a signed collection of currencies, with support for a base currency ("grams") and multiple other currencies identified by u32 keys.

The core functionality includes constructing a signed currency collection from an unsigned CurrencyCollection, and performing arithmetic operations such as addition and subtraction on these collections. This is useful in contexts where currency amounts may increase or decrease, requiring signed arithmetic.

This file depends on external crates and modules such as num_bigint for arbitrary precision integer arithmetic, and tvm_block and tvm_types for blockchain-specific data structures and utilities.

SignedCurrencyCollection Struct

pub(crate) struct SignedCurrencyCollection {
    pub grams: BigInt,
    pub other: HashMap<u32, BigInt>,
}

Methods

new() -> Self

Creates a new, empty SignedCurrencyCollection with zero grams and an empty other map.

Usage example:

let collection = SignedCurrencyCollection::new();

from_cc(cc: &CurrencyCollection) -> tvm_types::Result<Self>

Constructs a SignedCurrencyCollection from a reference to an unsigned CurrencyCollection.

Usage example:

let signed_collection = SignedCurrencyCollection::from_cc(&currency_collection)?;

add(&mut self, other: &Self)

Adds another SignedCurrencyCollection to this one, updating this collection in place.

Usage example:

collection1.add(&collection2);

sub(&mut self, other: &Self)

Subtracts another SignedCurrencyCollection from this one, updating this collection in place.

Usage example:

collection1.sub(&collection2);

Implementation Details

Interaction with Other System Components


Structure Diagram

classDiagram
class SignedCurrencyCollection {
+grams: BigInt
+other: HashMap<u32, BigInt>
+new()
+from_cc(cc: &CurrencyCollection) Result<Self>
+add(other: &Self)
+sub(other: &Self)
}

This diagram illustrates the SignedCurrencyCollection struct with its properties and main methods, showing the encapsulation of signed currency data and arithmetic operations.