iotaledger / bee

A framework for IOTA nodes, clients and applications in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make it easier to work with NativeTokens

Thoralf-M opened this issue · comments

Description

In iota.rs and wallet.rs there are a few places where things with NativeTokens are done and currently I use a HashMap where I insert the TokenId and the amount, so I can add it up from multiple outputs. I now thought about doing a wrapper which makes it easier to do such things than with the HashMap API directly (but using a HashMap internally).

Motivation

Currently not easy to use and requires more code.
Now I use

                if let Some(native_tokens) = output_data.output.native_tokens() {
                    for native_token in native_tokens.iter() {
                        match total_native_tokens.entry(*native_token.token_id()) {
                            Entry::Vacant(e) => {
                                e.insert(*native_token.amount());
                            }
                            Entry::Occupied(mut e) => {
                                *e.get_mut() += *native_token.amount();
                            }
                        }
                    }
                }

would be great to have something like

                if let Some(native_tokens) = output_data.output.native_tokens() {
                    total_native_tokens.add(native_tokens);
                }

Requirements

  1. It should be easy to merge NativeToken with the same TokenId together (for example if already existing in a HashMap the amount should be added)

Open questions (optional)

Is there a better alternative than using a HashMap and could other operations also be useful?

Are you planning to do it yourself in a pull request?

Maybe, depending on how the implementation should look.

Done in #1356