Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more NativeTokensBuilder helpers #1353

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion bee-message/src/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn verify_amount<const VERIFY: bool>(amount: &U256) -> Result<(), Error> {
}

/// A builder for [`NativeTokens`].
#[derive(Default)]
#[derive(Clone, Default, Debug)]
#[must_use]
pub struct NativeTokensBuilder(HashMap<TokenId, U256>);

Expand Down Expand Up @@ -85,6 +85,30 @@ impl NativeTokensBuilder {
Ok(())
}

/// Merges another [`NativeTokensBuilder`] into this one.
pub fn merge(&mut self, other: NativeTokensBuilder) -> Result<(), Error> {
for (token_id, amount) in other.0.into_iter() {
self.add_native_token(NativeToken::new(token_id, amount)?)?;
}

Ok(())
}

/// Returns whether the [`NativeTokensBuilder`] is empty or not.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Gets an amount from the [`NativeTokensBuilder`].
pub fn get(&self, token_id: &TokenId) -> Option<&U256> {
self.0.get(token_id)
}

/// Inserts a new entry in the [`NativeTokensBuilder`].
pub fn insert(&mut self, token_id: TokenId, amount: U256) -> Option<U256> {
self.0.insert(token_id, amount)
}

/// Finishes the [`NativeTokensBuilder`] into [`NativeTokens`].
pub fn finish(self) -> Result<NativeTokens, Error> {
NativeTokens::try_from(
Expand Down